1 19 20 package org.netbeans.core.output2; 21 22 import java.util.Arrays ; 23 import junit.framework.TestCase; 24 25 29 public class IntMapTest extends TestCase { 30 31 public IntMapTest(String testName) { 32 super(testName); 33 } 34 35 public void testFirst() { 36 System.out.println("testFirst"); 37 IntMap map = new IntMap(); 38 39 int[] indices = new int [] { 5, 12, 23, 62, 247, 375, 489, 5255}; 40 41 Object [] values = new Object [] { 42 "zeroth", "first", "second", "third", "fourth", "fifth", "sixth", 43 "seventh"}; 44 45 assert indices.length == values.length; 46 47 for (int i=0; i < indices.length; i++) { 48 map.put (indices[i], values[i]); 49 } 50 51 assertTrue ("First entry should be 5", map.first() == 5); 52 } 53 54 public void testNextEntry() { 55 System.out.println("testNextEntry"); 56 IntMap map = new IntMap(); 57 58 int[] indices = new int [] { 5, 12, 23, 62, 247, 375, 489, 5255}; 59 60 Object [] values = new Object [] { 61 "zeroth", "first", "second", "third", "fourth", "fifth", "sixth", 62 "seventh"}; 63 64 assert indices.length == values.length; 65 66 for (int i=0; i < indices.length; i++) { 67 map.put (indices[i], values[i]); 68 } 69 70 for (int i=0; i < indices.length-1; i++) { 71 int val = indices[i+1]; 72 int next = map.nextEntry (indices[i]); 73 assertTrue ("Entry after " + indices[i] + " should be " + val + " not " + next, next == val); 74 } 75 } 76 77 public void testPrevEntry() { 78 System.out.println("testPrevEntry"); 79 IntMap map = new IntMap(); 80 81 int[] indices = new int [] { 5, 12, 23, 62, 247, 375, 489, 5255}; 82 83 Object [] values = new Object [] { 84 "zeroth", "first", "second", "third", "fourth", "fifth", "sixth", 85 "seventh"}; 86 87 assert indices.length == values.length; 88 89 for (int i=0; i < indices.length; i++) { 90 map.put (indices[i], values[i]); 91 } 92 93 for (int i=indices.length-1; i > 0; i--) { 94 int val = indices[i-1]; 95 int next = map.prevEntry (indices[i]); 96 assertTrue ("Entry before " + indices[i] + " should be " + val + " not " + next, next == val); 97 } 98 } 99 100 public void testNearest() { 101 System.out.println("testNearest"); 102 IntMap map = new IntMap(); 103 104 int[] indices = new int [] { 5, 12, 23, 62, 247, 375, 489, 5255}; 105 106 Object [] values = new Object [] { 107 "zeroth", "first", "second", "third", "fourth", "fifth", "sixth", 108 "seventh"}; 109 110 assert indices.length == values.length; 111 112 for (int i=0; i < indices.length; i++) { 113 map.put (indices[i], values[i]); 114 } 115 116 for (int i=0; i < indices.length-1; i++) { 117 int toTest = indices[i] + ((indices[i+1] - indices[i]) / 2); 118 int next = map.nearest (toTest, false); 119 assertTrue ("Nearest value to " + toTest + " should be " + indices[i+1] + ", not " + next, next == indices[i+1]); 120 } 121 122 assertTrue ("Value after last entry should be 0th", map.nearest (indices[indices.length-1] + 1000, false) == indices[0]); 123 124 assertTrue ("Value before first entry should be last", map.nearest (-1, true) == indices[indices.length-1]); 125 126 assertTrue ("Value after < first entry should be 0th", map.nearest (-1, false) == indices[0]); 127 128 for (int i = indices.length-1; i > 0; i--) { 129 int toTest = indices[i-1] + ((indices[i] - indices[i-1]) / 2); 131 int prev = map.nearest (toTest, true); 132 assertTrue ("Nearest value to " + toTest + " should be " + indices[i-1] + ", not " + prev, prev == indices[i-1]); 133 } 134 135 assertTrue ("Entry previous to value lower than first entry should be last entry", 136 map.nearest(indices[0] - 1, true) == indices[indices.length -1]); 137 138 assertTrue ("Value after > last entry should be last 0th", map.nearest(indices[indices.length-1] + 100, false) == indices[0]); 139 140 assertTrue ("Value before > last entry should be last entry", map.nearest(indices[indices.length-1] + 100, true) == indices[indices.length-1]); 141 142 assertTrue ("Value after < first entry should be 0th", map.nearest(-10, false) == indices[0]); 143 144 } 145 146 147 150 public void testGet() { 151 System.out.println("testGet"); 152 153 IntMap map = new IntMap(); 154 155 int[] indices = new int [] { 5, 12, 23, 62, 247, 375, 489, 5255}; 156 157 Object [] values = new Object [] { 158 "zeroth", "first", "second", "third", "fourth", "fifth", "sixth", 159 "seventh"}; 160 161 assert indices.length == values.length; 162 163 for (int i=0; i < indices.length; i++) { 164 map.put (indices[i], values[i]); 165 } 166 167 for (int i=0; i < indices.length; i++) { 168 assertTrue (map.get(indices[i]) == values[i]); 169 } 170 } 171 172 public void testGetKeys() { 173 IntMap map = new IntMap(); 174 175 int[] indices = new int [] { 5, 12, 23, 62, 247, 375, 489, 5255}; 176 177 Object [] values = new Object [] { 178 "zeroth", "first", "second", "third", "fourth", "fifth", "sixth", 179 "seventh"}; 180 181 for (int i=0; i < indices.length; i++) { 182 map.put (indices[i], values[i]); 183 } 184 185 int[] keys = map.getKeys(); 186 assertTrue ("Keys returned should match those written. Expected: " + i2s(indices) + " Got: " + i2s(keys), Arrays.equals(keys, indices)); 187 } 188 189 private static String i2s (int[] a) { 190 StringBuffer result = new StringBuffer (a.length*2); 191 for (int i=0; i < a.length; i++) { 192 result.append (a[i]); 193 if (i != a.length-1) { 194 result.append(','); 195 } 196 } 197 return result.toString(); 198 } 199 } 200 | Popular Tags |