f3aaea89691b85f5925b67d0f93ea0a12e2bcbda
[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.NoSuchElementException;
29 import java.util.Random;
30 import java.util.Set;
31 import org.junit.Test;
32
33 public class TestMapIterator {
34     @Test
35     public void testMapIterator() {
36         final Random random = new Random();
37
38         for (int i = 0; i < 60 * 1000; i += 400 + random.nextInt(400)) {
39             final Map<Integer, Integer> bt = TrieMap.create();
40             for (int j = 0; j < i; j++) {
41                 assertNull(bt.put(Integer.valueOf(j), Integer.valueOf(j)));
42             }
43             int count = 0;
44             final Set<Integer> set = new HashSet<>();
45             for (final Entry<Integer, Integer> e : bt.entrySet()) {
46                 set.add(e.getKey());
47                 count++;
48             }
49             for (final Integer j : set) {
50                 assertTrue(bt.containsKey(j));
51             }
52             for (final Integer j : bt.keySet()) {
53                 assertTrue(set.contains(j));
54             }
55
56             assertEquals(i, count);
57             assertEquals(i, bt.size());
58
59             for (Entry<Integer, Integer> e : bt.entrySet()) {
60                 assertSame(e.getValue(), bt.get(e.getKey()));
61                 e.setValue(e.getValue() + 1);
62                 assertEquals((Object)e.getValue(), e.getKey() + 1);
63                 assertEquals(e.getValue(), bt.get(e.getKey()));
64                 e.setValue(e.getValue() - 1);
65             }
66
67             final Iterator<Integer> it = bt.keySet().iterator();
68             while (it.hasNext()) {
69                 final Integer k = it.next();
70                 assertTrue(bt.containsKey(k));
71                 it.remove();
72                 assertFalse(bt.containsKey(k));
73             }
74
75             assertEquals(0, bt.size());
76             assertTrue(bt.isEmpty());
77         }
78     }
79
80     @Test
81     public void testMapImmutableIterator() {
82         final Random random = new Random();
83
84         for (int i = 0; i < 60 * 1000; i += 400 + random.nextInt(400)) {
85             final Map<Integer, Integer> bt = TrieMap.create();
86             for (int j = 0; j < i; j++) {
87                 assertNull(bt.put(Integer.valueOf(j), Integer.valueOf(j)));
88             }
89             int count = 0;
90             final Set<Integer> set = new HashSet<>();
91             for (final Entry<Integer, Integer> e : bt.entrySet()) {
92                 set.add(e.getKey());
93                 count++;
94             }
95             for (final Integer j : set) {
96                 assertTrue(bt.containsKey(j));
97             }
98             for (final Integer j : bt.keySet()) {
99                 assertTrue(set.contains(j));
100             }
101
102             assertEquals(i, count);
103             assertEquals(i, bt.size());
104         }
105     }
106
107     private static void failAdvance(final Iterator<?> it) {
108         assertFalse(it.hasNext());
109         it.next();
110     }
111
112     @Test(expected = NoSuchElementException.class)
113     public void testEmptyIterator() {
114         failAdvance(TrieMap.create().iterator());
115     }
116
117     @Test(expected = NoSuchElementException.class)
118     public void testEmptyReadOnlyIterator() {
119         failAdvance(TrieMap.create().immutableIterator());
120     }
121
122     @Test(expected = NoSuchElementException.class)
123     public void testEmptyReadOnlySnapshotIterator() {
124         failAdvance(TrieMap.create().immutableSnapshot().iterator());
125     }
126 }