ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMap8Test.java
(Generate patch)

Comparing jsr166/src/test/tck/ConcurrentHashMap8Test.java (file contents):
Revision 1.11 by jsr166, Sun Jul 14 16:35:48 2013 UTC vs.
Revision 1.16 by jsr166, Thu Aug 8 19:39:48 2013 UTC

# Line 6 | Line 6
6  
7   import junit.framework.*;
8   import java.util.*;
9 + import static java.util.Spliterator.*;
10   import java.util.function.*;
11   import java.util.concurrent.atomic.LongAdder;
12   import java.util.concurrent.ConcurrentHashMap;
13 + import java.util.concurrent.ConcurrentHashMap.KeySetView;
14  
15   public class ConcurrentHashMap8Test extends JSR166TestCase {
16      public static void main(String[] args) {
# Line 53 | Line 55 | public class ConcurrentHashMap8Test exte
55      }
56  
57      /**
58 <     * computeIfAbsent does not replace  if the key is already present
58 >     * computeIfAbsent does not replace if the key is already present
59       */
60      public void testComputeIfAbsent2() {
61          ConcurrentHashMap map = map5();
# Line 70 | Line 72 | public class ConcurrentHashMap8Test exte
72      }
73  
74      /**
75 <     * computeIfPresent does not replace  if the key is already present
75 >     * computeIfPresent does not replace if the key is already present
76       */
77      public void testComputeIfPresent() {
78          ConcurrentHashMap map = map5();
# Line 87 | Line 89 | public class ConcurrentHashMap8Test exte
89      }
90  
91      /**
92 <     * compute does not replace  if the function returns null
92 >     * compute does not replace if the function returns null
93       */
94      public void testCompute() {
95          ConcurrentHashMap map = map5();
# Line 165 | Line 167 | public class ConcurrentHashMap8Test exte
167          return a;
168      }
169  
170 +    /*
171 +     * replaceAll replaces all matching values.
172 +     */
173 +    public void testReplaceAll() {
174 +        ConcurrentHashMap<Integer, String> map = map5();
175 +        map.replaceAll((x, y) -> {return x > 3 ? "Z" : y;});
176 +        assertEquals("A", map.get(one));
177 +        assertEquals("B", map.get(two));
178 +        assertEquals("C", map.get(three));
179 +        assertEquals("Z", map.get(four));
180 +        assertEquals("Z", map.get(five));
181 +    }
182 +
183      /**
184       * Default-constructed set is empty
185       */
# Line 174 | Line 189 | public class ConcurrentHashMap8Test exte
189      }
190  
191      /**
192 +     * keySet.add adds the key with the established value to the map;
193 +     * remove removes it.
194 +     */
195 +    public void testKeySetAddRemove() {
196 +        ConcurrentHashMap map = map5();
197 +        Set set1 = map.keySet();
198 +        Set set2 = map.keySet(true);
199 +        set2.add(six);
200 +        assertTrue(((KeySetView)set2).getMap() == map);
201 +        assertTrue(((KeySetView)set1).getMap() == map);
202 +        assertEquals(set2.size(), map.size());
203 +        assertEquals(set1.size(), map.size());
204 +        assertTrue((Boolean)map.get(six));
205 +        assertTrue(set1.contains(six));
206 +        assertTrue(set2.contains(six));
207 +        set2.remove(six);
208 +        assertNull(map.get(six));
209 +        assertFalse(set1.contains(six));
210 +        assertFalse(set2.contains(six));
211 +    }
212 +
213 +
214 +    /**
215       * keySet.addAll adds each element from the given collection
216       */
217      public void testAddAll() {
# Line 219 | Line 257 | public class ConcurrentHashMap8Test exte
257      }
258  
259      /**
260 +     * keySet.add throws UnsupportedOperationException if no default
261 +     * mapped value
262 +     */
263 +    public void testAdd4() {
264 +        Set full = map5().keySet();
265 +        try {
266 +            full.add(three);
267 +            shouldThrow();
268 +        } catch (UnsupportedOperationException e){}
269 +    }
270 +
271 +    /**
272 +     * keySet.add throws NullPointerException if the specified key is
273 +     * null
274 +     */
275 +    public void testAdd5() {
276 +        Set full = populatedSet(3);
277 +        try {
278 +            full.add(null);
279 +            shouldThrow();
280 +        } catch (NullPointerException e){}
281 +    }
282 +
283 +    /**
284 +     * KeySetView.getMappedValue returns the map's mapped value
285 +     */
286 +    public void testGetMappedValue() {
287 +        ConcurrentHashMap map = map5();
288 +        assertNull(map.keySet().getMappedValue());
289 +        try {
290 +            map.keySet(null);
291 +            shouldThrow();
292 +        } catch (NullPointerException e) {}
293 +        KeySetView set = map.keySet(one);
294 +        set.add(one);
295 +        set.add(six);
296 +        set.add(seven);
297 +        assertTrue(set.getMappedValue() == one);
298 +        assertTrue(map.get(one) != one);
299 +        assertTrue(map.get(six) == one);
300 +        assertTrue(map.get(seven) == one);
301 +    }
302 +
303 +    void checkSpliteratorCharacteristics(Spliterator<?> sp,
304 +                                         int requiredCharacteristics) {
305 +        assertEquals(requiredCharacteristics,
306 +                     requiredCharacteristics & sp.characteristics());
307 +    }
308 +
309 +    /**
310 +     * KeySetView.spliterator returns spliterator over the elements in this set
311 +     */
312 +    public void testKeySetSpliterator() {
313 +        LongAdder adder = new LongAdder();
314 +        ConcurrentHashMap map = map5();
315 +        Set set = map.keySet();
316 +        Spliterator<Integer> sp = set.spliterator();
317 +        checkSpliteratorCharacteristics(sp, CONCURRENT | DISTINCT | NONNULL);
318 +        assertEquals(sp.estimateSize(), map.size());
319 +        Spliterator<Integer> sp2 = sp.trySplit();
320 +        sp.forEachRemaining((Integer x) -> adder.add(x.longValue()));
321 +        long v = adder.sumThenReset();
322 +        sp2.forEachRemaining((Integer x) -> adder.add(x.longValue()));
323 +        long v2 = adder.sum();
324 +        assertEquals(v + v2, 15);
325 +    }
326 +
327 +    /**
328       * keyset.clear removes all elements from the set
329       */
330      public void testClear() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines