Initial import
[jpf-core.git] / src / tests / gov / nasa / jpf / util / IntTableTest.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18
19 package gov.nasa.jpf.util;
20
21 import gov.nasa.jpf.util.test.TestJPF;
22
23 import org.junit.Test;
24
25 /**
26  * unit test for IntTable
27  */
28 public class IntTableTest extends TestJPF {
29
30   @Test
31   public void testBasicPut(){
32     IntTable<Integer> tbl = new IntTable<Integer>();
33
34     assert tbl.size() == 0;
35     final int N = 5000;
36
37     for (int i=0; i<N; i++){
38       tbl.put(i, i);
39
40       IntTable.Entry<Integer> e = tbl.get(i);
41       assert e.val == i;
42     }
43
44     assert tbl.size() == N;
45   }
46
47   
48   @Test
49   public void testStringKeyAdd(){
50     IntTable<String> tbl = new IntTable<String>();
51
52     assert tbl.size() == 0;
53     final int N = 5000;
54
55     for (int i=0; i<N; i++){
56       String key = "averylonginttablekey-" + i;
57       tbl.add(key, i);
58
59       IntTable.Entry<String> e = tbl.get(key);
60       assert e.val == i;
61     }
62
63     assert tbl.size() == N;
64   }
65
66   @Test
67   public void testClone(){
68     IntTable<String> tbl = new IntTable<String>(3); // make it small so that we rehash
69
70     tbl.add("1", 1);
71     tbl.add("2", 2);
72     tbl.add("3", 3);
73
74     IntTable<String> t1 = tbl.clone();
75
76     for (int i=10; i<20; i++){
77       t1.add(Integer.toString(i), i);
78     }
79
80     assert tbl.size() == 3;
81     System.out.println("-- original table");
82     for (IntTable.Entry<String> e : tbl){
83       assert Integer.parseInt(e.key) == e.val;
84       System.out.println(e);
85     }
86
87     assert t1.size() == 13;
88     System.out.println("-- cloned+modified table");
89     for (IntTable.Entry<String> e : t1){
90       assert Integer.parseInt(e.key) == e.val;
91       System.out.println(e);
92     }
93   }
94   
95   @Test
96   public void testSnapshot (){
97     IntTable<String> tbl = new IntTable<String>();
98     
99     tbl.add("1", 1);
100     tbl.add("2", 2);
101     tbl.add("3", 3);
102     tbl.add("12345", 12345);
103     tbl.dump();
104     
105     IntTable.Snapshot<String> snap = tbl.getSnapshot();
106     
107     tbl.remove("3");
108     tbl.remove("1");
109     tbl.add("42", 42);
110     tbl.dump();
111     
112     assertTrue(tbl.size() == 3);
113     
114     tbl.restore(snap);
115     tbl.dump();
116     
117     assertTrue(tbl.size() == 4);
118     assertTrue(tbl.hasEntry("1"));
119     assertTrue(tbl.hasEntry("2"));
120     assertTrue(tbl.hasEntry("3"));
121     assertTrue(tbl.hasEntry("12345"));
122   }
123 }