1 /* TestAll.java -- Tests for java.util.UUID |
|
2 Copyright (C) 2006 Sven de Marothy |
|
3 This file is part of Mauve. |
|
4 |
|
5 Mauve is free software; you can redistribute it and/or modify |
|
6 it under the terms of the GNU General Public License as published by |
|
7 the Free Software Foundation; either version 2, or (at your option) |
|
8 any later version. |
|
9 |
|
10 Mauve is distributed in the hope that it will be useful, but |
|
11 WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 General Public License for more details. |
|
14 |
|
15 You should have received a copy of the GNU General Public License |
|
16 along with Mauve; see the file COPYING. If not, write to the |
|
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 02110-1301 USA. |
|
19 |
|
20 */ |
|
21 |
|
22 // Tags: JDK1.5 |
|
23 |
|
24 package gnu.testlet.java.util.UUID; |
|
25 |
|
26 import gnu.testlet.TestHarness; |
|
27 import gnu.testlet.Testlet; |
|
28 import java.util.UUID; |
|
29 |
|
30 public class TestAll implements Testlet |
|
31 { |
|
32 public void test(TestHarness harness) |
|
33 { |
|
34 harness.checkPoint("equals()"); |
|
35 testEquals( harness ); |
|
36 harness.checkPoint("randomUUID()"); |
|
37 testRandom( harness ); |
|
38 harness.checkPoint("time fields"); |
|
39 testTime( harness ); |
|
40 harness.checkPoint("toString()"); |
|
41 testToString( harness ); |
|
42 harness.checkPoint("hashCode()"); |
|
43 testHash( harness ); |
|
44 harness.checkPoint("compareTo()"); |
|
45 testCompare( harness ); |
|
46 harness.checkPoint("nameUUIDFromBytes()"); |
|
47 testNameFromBytes( harness ); |
|
48 harness.checkPoint("fromString()"); |
|
49 testFromString( harness ); |
|
50 } |
|
51 |
|
52 /** |
|
53 * Test data, some valid timestamp UUIDs |
|
54 */ |
|
55 private static final UUID[] ids = new UUID[] |
|
56 { |
|
57 new UUID(819576242563977691L, -6026651929721136538L), |
|
58 new UUID(2832154967796617691L, -6026651929721136538L), |
|
59 new UUID(3408883180598464987L, -6026651929721136538L), |
|
60 new UUID(3802173340188152283L, -6026651929721136538L) |
|
61 }; |
|
62 |
|
63 /** Some random UUIDs */ |
|
64 private static final UUID[] randomIds = new UUID[] |
|
65 { |
|
66 new UUID( -3712700652812154966L, -6598749860495561479L ), |
|
67 new UUID( 664552433621420518L, -6414775468900364460L ), |
|
68 new UUID( -5464341501079829899L, -5598482408525562595L ), |
|
69 new UUID( -6237697930964942150L, -6792975957340980865L ), |
|
70 new UUID( 1115444745961556609L, -8924788308993396799L ), |
|
71 new UUID( 8935737015972545600L, -7709166330108105025L ), |
|
72 new UUID( -1731090450474971506L, -8180066663887629633L ), |
|
73 new UUID( -4352314495419070300L, -6102009369002353257L ), |
|
74 new UUID( -2372952748710147740L, -7309989210815328856L ), |
|
75 new UUID( -7640168945999331050L, -9131205566142177277L ) |
|
76 }; |
|
77 |
|
78 /** correct string representations of the respective ids */ |
|
79 private static String[] strs = new String[] |
|
80 { |
|
81 "0b5fb840-1460-11db-ac5d-0800200c9a66", |
|
82 "274dd5f0-1460-11db-ac5d-0800200c9a66", |
|
83 "2f4ec931-1460-11db-ac5d-0800200c9a66", |
|
84 "34c40882-1460-11db-ac5d-0800200c9a66" |
|
85 }; |
|
86 |
|
87 private static String[] randomStrs = new String[] |
|
88 { |
|
89 "cc79d66d-4fc5-47aa-a46c-87f6ab80ecf9", |
|
90 "0938f6ea-dca0-49e6-a6fa-23b2ae4dcf54", |
|
91 "b42ac25b-28b6-4275-b24e-31e9568f7d1d", |
|
92 "a96f3ec7-d0a5-42ba-a1ba-805b86ebf97f", |
|
93 "0f7adb16-299d-4a81-8424-c8518ae3afc1", |
|
94 "7c021d78-f35a-4440-9503-8a1153812ebf", |
|
95 "e7f9ee68-315e-4e8e-8e7a-90b583f692bf", |
|
96 "c3997906-a5c3-48a4-ab51-4ecf08825d97", |
|
97 "df11940c-2858-4964-9a8d-b38af16a0da8", |
|
98 "95f8aad2-adbd-4116-8147-70eab306a003" |
|
99 }; |
|
100 |
|
101 private void testNameFromBytes(TestHarness harness) |
|
102 { |
|
103 UUID id1, id2; |
|
104 id1 = new UUID(8833946387751055799L, -7161481369492758254L); |
|
105 id2 = UUID.nameUUIDFromBytes( new byte[]{(byte)80, (byte)43 }); |
|
106 harness.check(id1.equals(id2)); |
|
107 |
|
108 id1 = new UUID(5637592221686249917L, -5921171958455577142L); |
|
109 id2 = UUID.nameUUIDFromBytes( new byte[]{ (byte)114, (byte)45 }); |
|
110 harness.check(id1.equals(id2)); |
|
111 |
|
112 id1 = new UUID(-4355869889751467654L, -7258896509850702779L); |
|
113 id2 = UUID.nameUUIDFromBytes( new byte[]{(byte)36, (byte)32, (byte)172, |
|
114 (byte)170, (byte)254, (byte)224}); |
|
115 harness.check(id1.equals(id2)); |
|
116 |
|
117 id1 = new UUID(-5236193865575288109L, -8631150049002629651L); |
|
118 id2 = UUID.nameUUIDFromBytes( new byte[]{(byte)79, (byte)195, (byte)193, |
|
119 (byte)12 }); |
|
120 harness.check(id1.equals(id2)); |
|
121 |
|
122 id1 = new UUID(6892210306406430384L, -8874384750029244307L); |
|
123 id2 = UUID.nameUUIDFromBytes( new byte[]{(byte)159, (byte)194, (byte)145, |
|
124 (byte)7, (byte)79, (byte)81, |
|
125 (byte)95 }); |
|
126 harness.check(id1.equals(id2)); |
|
127 |
|
128 id1 = new UUID(-1760792916171804329L, -7690807811470976644L); |
|
129 id2 = UUID.nameUUIDFromBytes( new byte[]{(byte)44, (byte)67, (byte)23, |
|
130 (byte)186, (byte)139, (byte)59, |
|
131 (byte)191, (byte)77, (byte)20 }); |
|
132 harness.check(id1.equals(id2)); |
|
133 |
|
134 id1 = new UUID(-8248928743552566013L, -8885233673248765009L); |
|
135 id2 = UUID.nameUUIDFromBytes( new byte[]{(byte)132, (byte)150, (byte)203, |
|
136 (byte)54, (byte)68, (byte)31, |
|
137 (byte)48, (byte)208 }); |
|
138 harness.check(id1.equals(id2)); |
|
139 |
|
140 id1 = new UUID(-3367149413545070022L, -6453356609274991779L); |
|
141 id2 = UUID.nameUUIDFromBytes( new byte[]{ |
|
142 (byte)63 }); |
|
143 harness.check(id1.equals(id2)); |
|
144 |
|
145 id1 = new UUID(-1846445036491163506L, -6140770342100802383L); |
|
146 id2 = UUID.nameUUIDFromBytes( new byte[]{(byte)216, (byte)9, (byte)56, |
|
147 (byte)238, (byte)224, (byte)237, |
|
148 (byte)253 }); |
|
149 harness.check(id1.equals(id2)); |
|
150 |
|
151 id1 = new UUID(-7426333540612096407L, -9029142625441791623L); |
|
152 id2 = UUID.nameUUIDFromBytes( new byte[]{(byte)86, (byte)131, |
|
153 (byte)201 }); |
|
154 harness.check(id1.equals(id2)); |
|
155 |
|
156 id1 = new UUID(-3162216497309273596L, -6232971331865394562L); |
|
157 id2 = UUID.nameUUIDFromBytes( new byte[]{ }); |
|
158 harness.check(id1.equals(id2)); |
|
159 } |
|
160 |
|
161 private void testFromString(TestHarness harness) |
|
162 { |
|
163 for(int i = 0; i < ids.length; i++ ) |
|
164 harness.check( ids[i].equals( UUID.fromString( strs[i] ) ) ); |
|
165 for(int i = 0; i < randomIds.length; i++) |
|
166 harness.check( randomIds[i].equals |
|
167 (UUID.fromString( randomStrs[i] ) ) ); |
|
168 } |
|
169 |
|
170 private void testToString(TestHarness harness) |
|
171 { |
|
172 |
|
173 for(int i = 0; i < ids.length; i++) |
|
174 harness.check(ids[i].toString().equals(strs[i])); |
|
175 for(int i = 0; i < randomIds.length; i++) |
|
176 harness.check(randomIds[i].toString().equals(randomStrs[i])); |
|
177 } |
|
178 |
|
179 private void testHash(TestHarness harness) |
|
180 { |
|
181 int[] hashes = new int[] |
|
182 { |
|
183 -1821492227, |
|
184 -1082370483, |
|
185 -1216394612, |
|
186 -1393194177 |
|
187 }; |
|
188 |
|
189 for(int i = 0; i < ids.length; i++) |
|
190 harness.check(ids[i].hashCode(), hashes[i]); |
|
191 } |
|
192 |
|
193 private void testCompare(TestHarness harness) |
|
194 { |
|
195 for(int i = 0; i < ids.length; i++) |
|
196 { |
|
197 UUID id = new UUID(ids[i].getMostSignificantBits(), |
|
198 ids[i].getLeastSignificantBits()); |
|
199 for(int j = 0; j < ids.length; j++) |
|
200 { |
|
201 int c1 = id.compareTo(ids[j]); |
|
202 int c2; |
|
203 if( i < j ) c2 = -1; |
|
204 else if( i > j) c2 = 1; |
|
205 else c2 = 0; |
|
206 harness.check(c1, c2); |
|
207 } |
|
208 } |
|
209 } |
|
210 |
|
211 private void testRandom(TestHarness harness) |
|
212 { |
|
213 UUID id = UUID.randomUUID(); |
|
214 harness.check(id.variant(), 2); |
|
215 harness.check(id.version(), 4); |
|
216 } |
|
217 |
|
218 /** |
|
219 * Test variant, version, timestamp, clocksequence, node |
|
220 */ |
|
221 private void testTime(TestHarness harness) |
|
222 { |
|
223 long[] vals = new long[] |
|
224 { |
|
225 2, 1, 133723016677800000L, 11357, 8796630719078L, |
|
226 2, 1, 133723017146390000L, 11357, 8796630719078L, |
|
227 2, 1, 133723017280670001L, 11357, 8796630719078L, |
|
228 2, 1, 133723017372240002L, 11357, 8796630719078L |
|
229 }; |
|
230 |
|
231 for(int i = 0; i < ids.length; i++) |
|
232 { |
|
233 harness.check(ids[i].variant(), vals[ i * 5 ]); |
|
234 harness.check(ids[i].version(), vals[ i * 5 + 1]); |
|
235 harness.check(ids[i].timestamp(), vals[ i * 5 + 2]); |
|
236 harness.check(ids[i].clockSequence(), vals[ i * 5 + 3]); |
|
237 harness.check(ids[i].node(), vals[ i * 5 + 4]); |
|
238 } |
|
239 } |
|
240 |
|
241 private void testEquals(TestHarness harness) |
|
242 { |
|
243 for(int i = 0; i < ids.length; i++) |
|
244 { |
|
245 UUID id = new UUID(ids[i].getMostSignificantBits(), |
|
246 ids[i].getLeastSignificantBits()); |
|
247 for(int j = 0; j < ids.length; j++) |
|
248 harness.check((id.equals(ids[j]) == (i == j))); |
|
249 } |
|
250 } |
|
251 } |
|
252 |
|
253 |
|