bed17f245edce1861e06778acf1f87c9c970ce6d
[yangtools.git] / third-party / triemap / src / test / java / org / opendaylight / yangtools / triemap / SnapshotTest.java
1 /*
2  * (C) Copyright 2017 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.assertTrue;
21
22 import java.util.Map;
23 import org.junit.Before;
24 import org.junit.Test;
25
26 public class SnapshotTest {
27     private TrieMap<String, String> map;
28
29     @Before
30     public void setUp() {
31         map = TrieMap.create();
32         map.put("k1", "v1");
33         map.put("k2", "v2");
34     }
35
36     private static void assertMutableIsolation(final Map<String, String> m1, final Map<String, String> m2) {
37         assertTrue(m2.containsKey("k1"));
38         assertTrue(m2.containsKey("k2"));
39
40         m1.remove("k1");
41         assertFalse(m1.containsKey("k1"));
42         assertTrue(m2.containsKey("k1"));
43
44         m2.remove("k2");
45         assertFalse(m1.containsKey("k1"));
46         assertTrue(m2.containsKey("k1"));
47
48         assertEquals(1, m1.size());
49         assertEquals(1, m2.size());
50     }
51
52     @Test
53     public void testMutableSnapshotIsolation() {
54         assertMutableIsolation(map, map.mutableSnapshot());
55     }
56
57     @Test
58     public void testMutableSnapshotIsolationAcrossImmutable() {
59         final TrieMap<String, String> snap = map.immutableSnapshot();
60         assertTrue(snap.containsKey("k1"));
61         assertTrue(snap.containsKey("k2"));
62
63         assertMutableIsolation(map, snap.mutableSnapshot());
64
65         assertTrue(snap.containsKey("k1"));
66         assertTrue(snap.containsKey("k2"));
67
68     }
69 }