Initial import
[jpf-core.git] / src / tests / gov / nasa / jpf / util / IntVectorTest.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 gov.nasa.jpf.util.IntVector
27  */
28 public class IntVectorTest extends TestJPF {
29
30   @Test
31   public void testPackedBoolean() {
32     IntVector v = new IntVector();
33     boolean[] b = { true,true,true,true, false,false,false,false };
34
35     v.appendPacked(b);
36     int x = v.get(0);
37     System.out.println(Integer.toHexString(x));
38     assert x == 0xf0000000;
39
40     v.clear();
41     b = new boolean[32];
42     b[0] = true;
43     b[31] = true;
44     v.appendPacked(b);
45     assert v.size() == 1;
46     x = v.get(0);
47     System.out.println(Integer.toHexString(x));
48     assert x == 0x80000001;
49
50     v.clear();
51     b = new boolean[33];
52     b[0] = true;
53     b[32] = true;
54     v.appendPacked(b);
55     assert v.size() == 2;
56     x = v.get(0);
57     System.out.println(Integer.toHexString(x));
58     assert x == 0x80000000;
59     x = v.get(1);
60     System.out.println(Integer.toHexString(x));
61     assert x == 0x80000000;
62
63     v.clear();
64     b = new boolean[34];
65     b[0] = true;
66     b[33] = true;
67     v.appendPacked(b);
68     assert v.size() == 2;
69     x = v.get(1);
70     System.out.println(Integer.toHexString(x));
71     assert x == 0x40000000;
72   }
73
74   @Test
75   public void testPackedByte() {
76     IntVector v = new IntVector();
77     byte[] b = { (byte)0xf, 0, (byte)0xf, 0 };
78
79     v.appendPacked(b);
80     assert v.size() == 1;
81     int x = v.get(0);
82     System.out.println(Integer.toHexString(x));
83     assert x == 0x0f000f00;
84
85     v.clear();
86     b = new byte[5];
87     b[0] = (byte)0xff;
88     b[4] = (byte)0xff;
89     v.appendPacked(b);
90     assert v.size() == 2;
91     x = v.get(0);
92     System.out.println(Integer.toHexString(x));
93     assert x == 0xff000000;
94     x = v.get(1);
95     System.out.println(Integer.toHexString(x));
96     assert x == 0xff000000;
97
98     v.clear();
99     b = new byte[6];
100     b[0] = (byte)0xff;
101     b[5] = (byte)0xff;
102     v.appendPacked(b);
103     assert v.size() == 2;
104     x = v.get(1);
105     System.out.println(Integer.toHexString(x));
106     assert x == 0x00ff0000;
107
108   }
109
110   @Test
111   public void testPackedChar() {
112     IntVector v = new IntVector();
113     char[] c = { (char)0xffff, 0};
114
115     v.appendPacked(c);
116     int x = v.get(0);
117     System.out.println(Integer.toHexString(x));
118     assert v.size() == 1;
119     assert x == 0xffff0000;
120
121     v.clear();
122     c = new char[3];
123     c[0] = (char)0xffff;
124     c[2] = (char)0xff;
125     v.appendPacked(c);
126     assert v.size() == 2;
127     x = v.get(1);
128     System.out.println(Integer.toHexString(x));
129     assert x == 0x00ff0000;
130   }
131
132   @Test
133   public void testLongBits(){
134     IntVector v = new IntVector();
135
136     long[] a = { 1, 0x00000000ffff0000L};
137     v.appendBits(a);
138     assert v.size() == 4;
139
140     int x = v.get(0);
141     System.out.println(Integer.toHexString(x));
142     assert x == 0;
143     x = v.get(1);
144     System.out.println(Integer.toHexString(x));
145     assert x == 1;
146     x = v.get(2);
147     System.out.println(Integer.toHexString(x));
148     assert x == 0;
149     x = v.get(3);
150     System.out.println(Integer.toHexString(x));
151     assert x == 0xffff0000;
152
153     a = new long[1];
154     a[0] = -1;
155     v.clear();
156     v.appendBits(a);
157     x = v.get(0);
158     System.out.println(Integer.toHexString(x));
159     x = v.get(0);
160     System.out.println(Integer.toHexString(x));
161     assert x == 0xffffffff;
162     x = v.get(1);
163     System.out.println(Integer.toHexString(x));
164     assert x == 0xffffffff;
165
166   }
167
168 }