Remove the object cache
[yangtools.git] / third-party / triemap / src / test / java / org / opendaylight / yangtools / triemap / TestDelete.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.assertNull;
20 import static org.junit.Assert.assertTrue;
21
22 import org.junit.Test;
23
24 public class TestDelete {
25     @Test(expected = NullPointerException.class)
26     public void testNullSimple() {
27         TrieMap.create().remove(null);
28     }
29
30     @Test(expected = NullPointerException.class)
31     public void testNullKey() {
32         TrieMap.create().remove(null, "");
33     }
34
35     @Test(expected = NullPointerException.class)
36     public void testNullValue() {
37         TrieMap.create().remove("", null);
38     }
39
40     @Test(expected = NullPointerException.class)
41     public void testNullBoth() {
42         TrieMap.create().remove(null, null);
43     }
44
45     @Test
46     public void testClear() {
47         final TrieMap<Integer, Integer> bt = TrieMap.create();
48         bt.put(1, 1);
49         bt.clear();
50         assertTrue(bt.isEmpty());
51         assertEquals(0, bt.size());
52     }
53
54     @Test
55     public void testDelete() {
56         final TrieMap<Integer, Integer> bt = TrieMap.create();
57
58         for (int i = 0; i < 10000; i++) {
59             assertNull(bt.put(Integer.valueOf(i), Integer.valueOf(i)));
60             assertEquals(Integer.valueOf(i), bt.get(Integer.valueOf(i)));
61         }
62
63         checkAddInsert(bt, 536);
64         checkAddInsert(bt, 4341);
65         checkAddInsert(bt, 8437);
66
67         for (int i = 0; i < 10000; i++) {
68             boolean removed = null != bt.remove(Integer.valueOf(i));
69             assertTrue(removed);
70             final Object lookup = bt.get(Integer.valueOf(i));
71             assertNull(lookup);
72         }
73
74         bt.toString();
75     }
76
77     /**
78      * Test if the Map.remove(Object, Object) method works correctly for hash collisions, which are handled by LNode.
79      */
80     @Test
81     public void testRemoveObjectLNode() {
82         final TrieMap<ZeroHashInt, ZeroHashInt> bt = TrieMap.create();
83
84         for (int i = 0; i < 100; i++) {
85             final ZeroHashInt v = new ZeroHashInt(i);
86             assertNull(bt.put(v, v));
87         }
88
89         for (int i = 0; i < 100; i++) {
90             final ZeroHashInt v = new ZeroHashInt(i);
91             assertTrue(bt.remove(v, v));
92         }
93     }
94
95     private static void checkAddInsert(final TrieMap<Integer, Integer> bt, final int k) {
96         final Integer v = Integer.valueOf(k);
97         bt.remove(v);
98         Integer foundV = bt.get(v);
99         assertNull(foundV);
100         assertNull(bt.put(v, v));
101         foundV = bt.get(v);
102         assertEquals(v, foundV);
103
104         assertEquals(v, bt.put(v, Integer.valueOf(-1)));
105         assertEquals(Integer.valueOf(-1), bt.put(v, v));
106     }
107 }