Initial import
[jpf-core.git] / src / tests / gov / nasa / jpf / util / SortedArrayObjectSetTest.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 import org.junit.Test;
23
24
25 /**
26  * regression test for SortedArrayObjectSet
27  */
28 public class SortedArrayObjectSetTest extends TestJPF {
29   
30   static class X implements Comparable<X> {
31     String id;
32     int x;
33     
34     X (String id, int x){
35       this.id = id;
36       this.x = x;
37     }
38     
39     @Override
40         public int compareTo (X other){
41       return (x - other.x);
42     }
43     
44     @Override
45         public String toString(){
46       return id;
47     }
48     
49     @Override
50         public boolean equals(Object o){
51       if (o instanceof X){
52         X other = (X)o;
53         if (x == other.x){
54           if (id.equals(other.id)){
55             return true;
56           }
57         }
58       }
59       
60       return false;
61     }
62   }
63   
64   @Test
65   public void testBasic(){
66     SortedArrayObjectSet<X> s = new SortedArrayObjectSet<X>();
67     
68     X o1 = new X("1",1);
69     X o2 = new X("20",20);
70     X o3 = new X("5",5);
71     X o4 = new X("7",7);
72     
73     s.add(o1);
74     System.out.println(s);
75     s.add(o2);
76     System.out.println(s);
77     s.add(o3);
78     System.out.println(s);
79     s.add(o4);
80     System.out.println(s);
81     s.add(o1);
82     System.out.println(s);
83
84     assertTrue(s.size() == 4);
85     assertTrue(s.contains(o1));
86     assertTrue(s.contains(o2));
87     assertTrue(s.contains(o3));
88     assertTrue(s.contains(o4));
89     
90     X o3a = new X("5a", 5);
91     s.add(o3a);
92     System.out.println(s);
93     assertTrue(s.size() == 5);
94     assertTrue(s.contains(o3a));
95     
96     s.remove(o3a);
97     System.out.println(s);
98     assertTrue(s.size() == 4);
99     assertFalse(s.contains(o3a));
100     assertTrue(s.contains(o3));
101   }
102 }