1 /*
   2  * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.lir.test;
  24 
  25 import static org.junit.Assert.assertEquals;
  26 import static org.junit.Assert.assertNull;
  27 
  28 import org.junit.Test;
  29 
  30 import org.graalvm.compiler.core.common.LIRKind;
  31 import org.graalvm.compiler.lir.Variable;
  32 import org.graalvm.compiler.lir.util.GenericValueMap;
  33 
  34 import jdk.vm.ci.code.Register;
  35 import jdk.vm.ci.code.Register.RegisterCategory;
  36 import jdk.vm.ci.code.RegisterValue;
  37 import jdk.vm.ci.meta.PlatformKind;
  38 
  39 public class GenericValueMapTest {
  40 
  41     private enum DummyKind implements PlatformKind {
  42         Long;
  43 
  44         private EnumKey<DummyKind> key = new EnumKey<>(this);
  45 
  46         @Override
  47         public Key getKey() {
  48             return key;
  49         }
  50 
  51         @Override
  52         public int getSizeInBytes() {
  53             return 8;
  54         }
  55 
  56         @Override
  57         public int getVectorLength() {
  58             return 1;
  59         }
  60 
  61         @Override
  62         public char getTypeChar() {
  63             return 'l';
  64         }
  65     }
  66 
  67     @Test
  68     public void run0() {
  69         RegisterCategory cat = new RegisterCategory("regs");
  70 
  71         RegisterValue reg = new Register(0, 0, "reg0", cat).asValue();
  72         Variable var = new Variable(LIRKind.value(DummyKind.Long), 0);
  73         Object obj0 = new Object();
  74         Object obj1 = new Object();
  75 
  76         GenericValueMap<Object> map = new GenericValueMap<>();
  77 
  78         assertNull(map.get(reg));
  79         assertNull(map.get(var));
  80 
  81         map.put(reg, obj0);
  82         map.put(var, obj1);
  83 
  84         assertEquals(obj0, map.get(reg));
  85         assertEquals(obj1, map.get(var));
  86 
  87         map.remove(reg);
  88         map.remove(var);
  89 
  90         assertNull(map.get(reg));
  91         assertNull(map.get(var));
  92 
  93         map.put(reg, obj0);
  94         map.put(var, obj1);
  95 
  96         map.put(var, obj0);
  97         map.put(reg, obj1);
  98 
  99         assertEquals(obj1, map.get(reg));
 100         assertEquals(obj0, map.get(var));
 101 
 102         map.put(reg, null);
 103         map.put(var, null);
 104 
 105         assertNull(map.get(reg));
 106         assertNull(map.get(var));
 107     }
 108 }