BUG-7464: remove TestHelper
[yangtools.git] / third-party / triemap / src / test / java / org / opendaylight / yangtools / triemap / TestMapIterator.java
1 /*
2  * (C) Copyright 2016 Pantheon Technologies, s.r.o. and others.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.opendaylight.yangtools.triemap;
17
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertNull;
21 import static org.junit.Assert.assertSame;
22 import static org.junit.Assert.assertTrue;
23
24 import java.util.HashSet;
25 import java.util.Iterator;
26 import java.util.Map;
27 import java.util.Map.Entry;
28 import java.util.Random;
29 import java.util.Set;
30 import org.junit.Test;
31
32 public class TestMapIterator {
33     @Test
34     public void testMapIterator () {
35         final Random random = new Random();
36
37         for (int i = 0; i < 60 * 1000; i+= 400 + random.nextInt(400)) {
38             final Map<Integer, Integer> bt = new TrieMap <>();
39             for (int j = 0; j < i; j++) {
40                 assertNull(bt.put(Integer.valueOf(j), Integer.valueOf(j)));
41             }
42             int count = 0;
43             final Set<Integer> set = new HashSet<>();
44             for (final Entry<Integer, Integer> e : bt.entrySet()) {
45                 set.add(e.getKey());
46                 count++;
47             }
48             for (final Integer j : set) {
49                 assertTrue(bt.containsKey(j));
50             }
51             for (final Integer j : bt.keySet()) {
52                 assertTrue(set.contains(j));
53             }
54
55             assertEquals(i, count);
56             assertEquals(i, bt.size());
57
58             for (Entry<Integer, Integer> e : bt.entrySet()) {
59                 assertSame(e.getValue(), bt.get(e.getKey()));
60                 e.setValue(e.getValue() + 1);
61                 assertEquals((Object)e.getValue(), e.getKey () + 1);
62                 assertEquals(e.getValue(), bt.get(e.getKey()));
63                 e.setValue(e.getValue() - 1);
64             }
65
66             final Iterator<Integer> it = bt.keySet().iterator();
67             while(it.hasNext()) {
68                 final Integer k = it.next ();
69                 assertTrue(bt.containsKey(k));
70                 it.remove();
71                 assertFalse(bt.containsKey(k));
72             }
73
74             assertEquals(0, bt.size ());
75             assertTrue(bt.isEmpty ());
76         }
77     }
78 }