|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 /* |
|
7 ** testll.c -- test suite for 64bit integer (longlong) operations |
|
8 ** |
|
9 ** Summary: testll [-d] | [-h] |
|
10 ** |
|
11 ** Where: |
|
12 ** -d set debug mode on; displays individual test failures |
|
13 ** -v verbose mode; displays progress in test, plus -d |
|
14 ** -h gives usage message. |
|
15 ** |
|
16 ** Description: |
|
17 ** lltest.c tests the functions defined in NSPR 2.0's prlong.h. |
|
18 ** |
|
19 ** Successive tests begin to depend on other LL functions working |
|
20 ** correctly. So, ... Do not change the order of the tests as run |
|
21 ** from main(). |
|
22 ** |
|
23 ** Caveats: |
|
24 ** Do not even begin to think that this is an exhaustive test! |
|
25 ** |
|
26 ** These tests try a little of everything, but not all boundary |
|
27 ** conditions and limits are tested. |
|
28 ** You want better coverage? ... Add it. |
|
29 ** |
|
30 ** --- |
|
31 ** Author: Lawrence Hardiman <larryh@netscape.com>. |
|
32 ** --- |
|
33 ** Revision History: |
|
34 ** 01-Oct-1997. Original implementation. |
|
35 ** |
|
36 */ |
|
37 |
|
38 #include "nspr.h" |
|
39 #include "plgetopt.h" |
|
40 |
|
41 /* --- Local Definitions --- */ |
|
42 #define ReportProgress(m) if (verboseMode) PR_fprintf(output, (m)); |
|
43 |
|
44 |
|
45 /* --- Global variables --- */ |
|
46 static PRIntn failedAlready = 0; |
|
47 static PRFileDesc* output = NULL; |
|
48 static PRBool debugMode = PR_FALSE; |
|
49 static PRBool verboseMode = PR_FALSE; |
|
50 |
|
51 /* |
|
52 ** Constants used in tests. |
|
53 */ |
|
54 const PRInt64 bigZero = LL_INIT( 0, 0 ); |
|
55 const PRInt64 bigOne = LL_INIT( 0, 1 ); |
|
56 const PRInt64 bigTwo = LL_INIT( 0, 2 ); |
|
57 const PRInt64 bigSixTeen = LL_INIT( 0, 16 ); |
|
58 const PRInt64 bigThirtyTwo = LL_INIT( 0, 32 ); |
|
59 const PRInt64 bigMinusOne = LL_INIT( 0xffffffff, 0xffffffff ); |
|
60 const PRInt64 bigMinusTwo = LL_INIT( 0xffffffff, 0xfffffffe ); |
|
61 const PRInt64 bigNumber = LL_INIT( 0x7fffffff, 0xffffffff ); |
|
62 const PRInt64 bigMinusNumber = LL_INIT( 0x80000000, 0x00000001 ); |
|
63 const PRInt64 bigMaxInt32 = LL_INIT( 0x00000000, 0x7fffffff ); |
|
64 const PRInt64 big2To31 = LL_INIT( 0x00000000, 0x80000000 ); |
|
65 const PRUint64 bigZeroFox = LL_INIT( 0x00000000, 0xffffffff ); |
|
66 const PRUint64 bigFoxFox = LL_INIT( 0xffffffff, 0xffffffff ); |
|
67 const PRUint64 bigFoxZero = LL_INIT( 0xffffffff, 0x00000000 ); |
|
68 const PRUint64 bigEightZero = LL_INIT( 0x80000000, 0x00000000 ); |
|
69 const PRUint64 big64K = LL_INIT( 0x00000000, 0x00010000 ); |
|
70 const PRInt64 bigInt0 = LL_INIT( 0x01a00000, 0x00001000 ); |
|
71 const PRInt64 bigInt1 = LL_INIT( 0x01a00000, 0x00001100 ); |
|
72 const PRInt64 bigInt2 = LL_INIT( 0x01a00000, 0x00000100 ); |
|
73 const PRInt64 bigInt3 = LL_INIT( 0x01a00001, 0x00001000 ); |
|
74 const PRInt64 bigInt4 = LL_INIT( 0x01a00001, 0x00001100 ); |
|
75 const PRInt64 bigInt5 = LL_INIT( 0x01a00001, 0x00000100 ); |
|
76 const PRInt64 bigInt6 = LL_INIT( 0xb1a00000, 0x00001000 ); |
|
77 const PRInt64 bigInt7 = LL_INIT( 0xb1a00000, 0x00001100 ); |
|
78 const PRInt64 bigInt8 = LL_INIT( 0xb1a00000, 0x00000100 ); |
|
79 const PRInt64 bigInt9 = LL_INIT( 0xb1a00001, 0x00001000 ); |
|
80 const PRInt64 bigInt10 = LL_INIT( 0xb1a00001, 0x00001100 ); |
|
81 const PRInt64 bigInt11 = LL_INIT( 0xb1a00001, 0x00000100 ); |
|
82 const PRInt32 one = 1l; |
|
83 const PRInt32 minusOne = -1l; |
|
84 const PRInt32 sixteen = 16l; |
|
85 const PRInt32 thirtyTwo = 32l; |
|
86 const PRInt32 sixtyThree = 63l; |
|
87 |
|
88 /* |
|
89 ** SetFailed() -- Report individual test failure |
|
90 ** |
|
91 */ |
|
92 static void |
|
93 SetFailed( char *what, char *how ) |
|
94 { |
|
95 failedAlready = 1; |
|
96 if ( debugMode ) |
|
97 PR_fprintf(output, "%s: failed: %s\n", what, how ); |
|
98 return; |
|
99 } |
|
100 |
|
101 static void |
|
102 ResultFailed( char *what, char *how, PRInt64 expected, PRInt64 got) |
|
103 { |
|
104 if ( debugMode) |
|
105 { |
|
106 SetFailed( what, how ); |
|
107 PR_fprintf(output, "Expected: 0x%llx Got: 0x%llx\n", expected, got ); |
|
108 } |
|
109 return; |
|
110 } |
|
111 |
|
112 |
|
113 /* |
|
114 ** TestAssignment() -- Test the assignment |
|
115 */ |
|
116 static void TestAssignment( void ) |
|
117 { |
|
118 PRInt64 zero = LL_Zero(); |
|
119 PRInt64 min = LL_MinInt(); |
|
120 PRInt64 max = LL_MaxInt(); |
|
121 if (!LL_EQ(zero, bigZero)) |
|
122 SetFailed("LL_EQ(zero, bigZero)", "!="); |
|
123 if (!LL_CMP(max, >, min)) |
|
124 SetFailed("LL_CMP(max, >, min)", "!>"); |
|
125 } |
|
126 |
|
127 /* |
|
128 ** TestComparisons() -- Test the longlong comparison operations |
|
129 */ |
|
130 static void |
|
131 TestComparisons( void ) |
|
132 { |
|
133 ReportProgress("Testing Comparisons Operations\n"); |
|
134 |
|
135 /* test for zero */ |
|
136 if ( !LL_IS_ZERO( bigZero )) |
|
137 SetFailed( "LL_IS_ZERO", "Zero is not zero" ); |
|
138 |
|
139 if ( LL_IS_ZERO( bigOne )) |
|
140 SetFailed( "LL_IS_ZERO", "One tests as zero" ); |
|
141 |
|
142 if ( LL_IS_ZERO( bigMinusOne )) |
|
143 SetFailed( "LL_IS_ZERO", "Minus One tests as zero" ); |
|
144 |
|
145 /* test equal */ |
|
146 if ( !LL_EQ( bigZero, bigZero )) |
|
147 SetFailed( "LL_EQ", "zero EQ zero"); |
|
148 |
|
149 if ( !LL_EQ( bigOne, bigOne )) |
|
150 SetFailed( "LL_EQ", "one EQ one" ); |
|
151 |
|
152 if ( !LL_EQ( bigNumber, bigNumber )) |
|
153 SetFailed( "LL_EQ", "bigNumber EQ bigNumber" ); |
|
154 |
|
155 if ( !LL_EQ( bigMinusOne, bigMinusOne )) |
|
156 SetFailed( "LL_EQ", "minus one EQ minus one"); |
|
157 |
|
158 if ( LL_EQ( bigZero, bigOne )) |
|
159 SetFailed( "LL_EQ", "zero EQ one"); |
|
160 |
|
161 if ( LL_EQ( bigOne, bigZero )) |
|
162 SetFailed( "LL_EQ", "one EQ zero" ); |
|
163 |
|
164 if ( LL_EQ( bigMinusOne, bigOne )) |
|
165 SetFailed( "LL_EQ", "minus one EQ one"); |
|
166 |
|
167 if ( LL_EQ( bigNumber, bigOne )) |
|
168 SetFailed( "LL_EQ", "bigNumber EQ one"); |
|
169 |
|
170 /* test not equal */ |
|
171 if ( LL_NE( bigZero, bigZero )) |
|
172 SetFailed( "LL_NE", "0 NE 0"); |
|
173 |
|
174 if ( LL_NE( bigOne, bigOne )) |
|
175 SetFailed( "LL_NE", "1 NE 1"); |
|
176 |
|
177 if ( LL_NE( bigMinusOne, bigMinusOne )) |
|
178 SetFailed( "LL_NE", "-1 NE -1"); |
|
179 |
|
180 if ( LL_NE( bigNumber, bigNumber )) |
|
181 SetFailed( "LL_NE", "n NE n"); |
|
182 |
|
183 if ( LL_NE( bigMinusNumber, bigMinusNumber )) |
|
184 SetFailed( "LL_NE", "-n NE -n"); |
|
185 |
|
186 if ( !LL_NE( bigZero, bigOne)) |
|
187 SetFailed( "LL_NE", "0 NE 1"); |
|
188 |
|
189 if ( !LL_NE( bigOne, bigMinusNumber)) |
|
190 SetFailed( "LL_NE", "1 NE -n"); |
|
191 |
|
192 /* Greater than or equal to zero */ |
|
193 if ( !LL_GE_ZERO( bigZero )) |
|
194 SetFailed( "LL_GE_ZERO", "0"); |
|
195 |
|
196 if ( !LL_GE_ZERO( bigOne )) |
|
197 SetFailed( "LL_GE_ZERO", "1"); |
|
198 |
|
199 if ( !LL_GE_ZERO( bigNumber )) |
|
200 SetFailed( "LL_GE_ZERO", "n"); |
|
201 |
|
202 if ( LL_GE_ZERO( bigMinusOne )) |
|
203 SetFailed( "LL_GE_ZERO", "-1"); |
|
204 |
|
205 if ( LL_GE_ZERO( bigMinusNumber )) |
|
206 SetFailed( "LL_GE_ZERO", "-n"); |
|
207 |
|
208 /* Algebraic Compare two values */ |
|
209 if ( !LL_CMP( bigZero, ==, bigZero )) |
|
210 SetFailed( "LL_CMP", "0 == 0"); |
|
211 |
|
212 if ( LL_CMP( bigZero, >, bigZero )) |
|
213 SetFailed( "LL_CMP", "0 > 0"); |
|
214 |
|
215 if ( LL_CMP( bigZero, <, bigZero )) |
|
216 SetFailed( "LL_CMP", "0 < 0"); |
|
217 |
|
218 if ( LL_CMP( bigNumber, <, bigOne )) |
|
219 SetFailed( "LL_CMP", "n < 1"); |
|
220 |
|
221 if ( !LL_CMP( bigNumber, >, bigOne )) |
|
222 SetFailed( "LL_CMP", "n <= 1"); |
|
223 |
|
224 if ( LL_CMP( bigOne, >, bigNumber )) |
|
225 SetFailed( "LL_CMP", "1 > n"); |
|
226 |
|
227 if ( LL_CMP( bigMinusNumber, >, bigNumber )) |
|
228 SetFailed( "LL_CMP", "-n > n"); |
|
229 |
|
230 if ( LL_CMP( bigNumber, !=, bigNumber)) |
|
231 SetFailed( "LL_CMP", "n != n"); |
|
232 |
|
233 if ( !LL_CMP( bigMinusOne, >, bigMinusTwo )) |
|
234 SetFailed( "LL_CMP", "-1 <= -2"); |
|
235 |
|
236 if ( !LL_CMP( bigMaxInt32, <, big2To31 )) |
|
237 SetFailed( "LL_CMP", "Max 32-bit signed int >= 2^31"); |
|
238 |
|
239 /* Two positive numbers */ |
|
240 if ( !LL_CMP( bigInt0, <=, bigInt0 )) |
|
241 SetFailed( "LL_CMP", "LL_CMP(<=) failed"); |
|
242 |
|
243 if ( !LL_CMP( bigInt0, <=, bigInt1 )) |
|
244 SetFailed( "LL_CMP", "LL_CMP(<=) failed"); |
|
245 |
|
246 if ( LL_CMP( bigInt0, <=, bigInt2 )) |
|
247 SetFailed( "LL_CMP", "LL_CMP(<=) failed"); |
|
248 |
|
249 if ( !LL_CMP( bigInt0, <=, bigInt3 )) |
|
250 SetFailed( "LL_CMP", "LL_CMP(<=) failed"); |
|
251 |
|
252 if ( !LL_CMP( bigInt0, <=, bigInt4 )) |
|
253 SetFailed( "LL_CMP", "LL_CMP(<=) failed"); |
|
254 |
|
255 if ( !LL_CMP( bigInt0, <=, bigInt5 )) |
|
256 SetFailed( "LL_CMP", "LL_CMP(<=) failed"); |
|
257 |
|
258 /* Two negative numbers */ |
|
259 if ( !LL_CMP( bigInt6, <=, bigInt6 )) |
|
260 SetFailed( "LL_CMP", "LL_CMP(<=) failed"); |
|
261 |
|
262 if ( !LL_CMP( bigInt6, <=, bigInt7 )) |
|
263 SetFailed( "LL_CMP", "LL_CMP(<=) failed"); |
|
264 |
|
265 if ( LL_CMP( bigInt6, <=, bigInt8 )) |
|
266 SetFailed( "LL_CMP", "LL_CMP(<=) failed"); |
|
267 |
|
268 if ( !LL_CMP( bigInt6, <=, bigInt9 )) |
|
269 SetFailed( "LL_CMP", "LL_CMP(<=) failed"); |
|
270 |
|
271 if ( !LL_CMP( bigInt6, <=, bigInt10 )) |
|
272 SetFailed( "LL_CMP", "LL_CMP(<=) failed"); |
|
273 |
|
274 if ( !LL_CMP( bigInt6, <=, bigInt11 )) |
|
275 SetFailed( "LL_CMP", "LL_CMP(<=) failed"); |
|
276 |
|
277 /* One positive, one negative */ |
|
278 if ( LL_CMP( bigInt0, <=, bigInt6 )) |
|
279 SetFailed( "LL_CMP", "LL_CMP(<=) failed"); |
|
280 |
|
281 if ( LL_CMP( bigInt0, <=, bigInt7 )) |
|
282 SetFailed( "LL_CMP", "LL_CMP(<=) failed"); |
|
283 |
|
284 if ( LL_CMP( bigInt0, <=, bigInt8 )) |
|
285 SetFailed( "LL_CMP", "LL_CMP(<=) failed"); |
|
286 |
|
287 /* Bitwise Compare two numbers */ |
|
288 if ( !LL_UCMP( bigZero, ==, bigZero )) |
|
289 SetFailed( "LL_UCMP", "0 == 0"); |
|
290 |
|
291 if ( LL_UCMP( bigZero, >, bigZero )) |
|
292 SetFailed( "LL_UCMP", "0 > 0"); |
|
293 |
|
294 if ( LL_UCMP( bigZero, <, bigZero )) |
|
295 SetFailed( "LL_UCMP", "0 < 0"); |
|
296 |
|
297 if ( LL_UCMP( bigNumber, <, bigOne )) |
|
298 SetFailed( "LL_UCMP", "n < 1"); |
|
299 |
|
300 if ( !LL_UCMP( bigNumber, >, bigOne )) |
|
301 SetFailed( "LL_UCMP", "n < 1"); |
|
302 |
|
303 if ( LL_UCMP( bigOne, >, bigNumber )) |
|
304 SetFailed( "LL_UCMP", "1 > n"); |
|
305 |
|
306 if ( LL_UCMP( bigMinusNumber, <, bigNumber )) |
|
307 SetFailed( "LL_UCMP", "-n < n"); |
|
308 |
|
309 /* Two positive numbers */ |
|
310 if ( !LL_UCMP( bigInt0, <=, bigInt0 )) |
|
311 SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); |
|
312 |
|
313 if ( !LL_UCMP( bigInt0, <=, bigInt1 )) |
|
314 SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); |
|
315 |
|
316 if ( LL_UCMP( bigInt0, <=, bigInt2 )) |
|
317 SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); |
|
318 |
|
319 if ( !LL_UCMP( bigInt0, <=, bigInt3 )) |
|
320 SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); |
|
321 |
|
322 if ( !LL_UCMP( bigInt0, <=, bigInt4 )) |
|
323 SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); |
|
324 |
|
325 if ( !LL_UCMP( bigInt0, <=, bigInt5 )) |
|
326 SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); |
|
327 |
|
328 /* Two negative numbers */ |
|
329 if ( !LL_UCMP( bigInt6, <=, bigInt6 )) |
|
330 SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); |
|
331 |
|
332 if ( !LL_UCMP( bigInt6, <=, bigInt7 )) |
|
333 SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); |
|
334 |
|
335 if ( LL_UCMP( bigInt6, <=, bigInt8 )) |
|
336 SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); |
|
337 |
|
338 if ( !LL_UCMP( bigInt6, <=, bigInt9 )) |
|
339 SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); |
|
340 |
|
341 if ( !LL_UCMP( bigInt6, <=, bigInt10 )) |
|
342 SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); |
|
343 |
|
344 if ( !LL_UCMP( bigInt6, <=, bigInt11 )) |
|
345 SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); |
|
346 |
|
347 /* One positive, one negative */ |
|
348 if ( !LL_UCMP( bigInt0, <=, bigInt6 )) |
|
349 SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); |
|
350 |
|
351 if ( !LL_UCMP( bigInt0, <=, bigInt7 )) |
|
352 SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); |
|
353 |
|
354 if ( !LL_UCMP( bigInt0, <=, bigInt8 )) |
|
355 SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); |
|
356 |
|
357 return; |
|
358 } |
|
359 |
|
360 /* |
|
361 ** TestLogicalOperations() -- Tests for AND, OR, ... |
|
362 ** |
|
363 */ |
|
364 static void |
|
365 TestLogicalOperations( void ) |
|
366 { |
|
367 PRUint64 result, result2; |
|
368 |
|
369 ReportProgress("Testing Logical Operations\n"); |
|
370 |
|
371 /* Test AND */ |
|
372 LL_AND( result, bigZero, bigZero ); |
|
373 if ( !LL_IS_ZERO( result )) |
|
374 ResultFailed( "LL_AND", "0 & 0", bigZero, result ); |
|
375 |
|
376 LL_AND( result, bigOne, bigOne ); |
|
377 if ( LL_IS_ZERO( result )) |
|
378 ResultFailed( "LL_AND", "1 & 1", bigOne, result ); |
|
379 |
|
380 LL_AND( result, bigZero, bigOne ); |
|
381 if ( !LL_IS_ZERO( result )) |
|
382 ResultFailed( "LL_AND", "1 & 1", bigZero, result ); |
|
383 |
|
384 LL_AND( result, bigMinusOne, bigMinusOne ); |
|
385 if ( !LL_UCMP( result, ==, bigMinusOne )) |
|
386 ResultFailed( "LL_AND", "-1 & -1", bigMinusOne, result ); |
|
387 |
|
388 /* test OR */ |
|
389 LL_OR( result, bigZero, bigZero ); |
|
390 if ( !LL_IS_ZERO( result )) |
|
391 ResultFailed( "LL_OR", "0 | 1", bigZero, result); |
|
392 |
|
393 LL_OR( result, bigZero, bigOne ); |
|
394 if ( LL_IS_ZERO( result )) |
|
395 ResultFailed( "LL_OR", "0 | 1", bigOne, result ); |
|
396 |
|
397 LL_OR( result, bigZero, bigMinusNumber ); |
|
398 if ( !LL_UCMP( result, ==, bigMinusNumber )) |
|
399 ResultFailed( "LL_OR", "0 | -n", bigMinusNumber, result); |
|
400 |
|
401 LL_OR( result, bigMinusNumber, bigZero ); |
|
402 if ( !LL_UCMP( result, ==, bigMinusNumber )) |
|
403 ResultFailed( "LL_OR", "-n | 0", bigMinusNumber, result ); |
|
404 |
|
405 /* test XOR */ |
|
406 LL_XOR( result, bigZero, bigZero ); |
|
407 if ( LL_UCMP( result, !=, bigZero )) |
|
408 ResultFailed( "LL_XOR", "0 ^ 0", bigZero, result); |
|
409 |
|
410 LL_XOR( result, bigOne, bigZero ); |
|
411 if ( LL_UCMP( result, !=, bigOne )) |
|
412 ResultFailed( "LL_XOR", "1 ^ 0", bigZero, result ); |
|
413 |
|
414 LL_XOR( result, bigMinusNumber, bigZero ); |
|
415 if ( LL_UCMP( result, !=, bigMinusNumber )) |
|
416 ResultFailed( "LL_XOR", "-n ^ 0", bigMinusNumber, result ); |
|
417 |
|
418 LL_XOR( result, bigMinusNumber, bigMinusNumber ); |
|
419 if ( LL_UCMP( result, !=, bigZero )) |
|
420 ResultFailed( "LL_XOR", "-n ^ -n", bigMinusNumber, result); |
|
421 |
|
422 /* test OR2. */ |
|
423 result = bigZero; |
|
424 LL_OR2( result, bigOne ); |
|
425 if ( LL_UCMP( result, !=, bigOne )) |
|
426 ResultFailed( "LL_OR2", "(r=0) |= 1", bigOne, result); |
|
427 |
|
428 result = bigOne; |
|
429 LL_OR2( result, bigNumber ); |
|
430 if ( LL_UCMP( result, !=, bigNumber )) |
|
431 ResultFailed( "LL_OR2", "(r=1) |= n", bigNumber, result); |
|
432 |
|
433 result = bigMinusNumber; |
|
434 LL_OR2( result, bigMinusNumber ); |
|
435 if ( LL_UCMP( result, !=, bigMinusNumber )) |
|
436 ResultFailed( "LL_OR2", "(r=-n) |= -n", bigMinusNumber, result); |
|
437 |
|
438 /* test NOT */ |
|
439 LL_NOT( result, bigMinusNumber); |
|
440 LL_NOT( result2, result); |
|
441 if ( LL_UCMP( result2, !=, bigMinusNumber )) |
|
442 ResultFailed( "LL_NOT", "r != ~(~-n)", bigMinusNumber, result); |
|
443 |
|
444 /* test Negation */ |
|
445 LL_NEG( result, bigMinusNumber ); |
|
446 LL_NEG( result2, result ); |
|
447 if ( LL_CMP( result2, !=, bigMinusNumber )) |
|
448 ResultFailed( "LL_NEG", "r != -(-(-n))", bigMinusNumber, result); |
|
449 |
|
450 return; |
|
451 } |
|
452 |
|
453 |
|
454 |
|
455 /* |
|
456 ** TestConversion() -- Test Conversion Operations |
|
457 ** |
|
458 */ |
|
459 static void |
|
460 TestConversion( void ) |
|
461 { |
|
462 PRInt64 result; |
|
463 PRInt64 resultU; |
|
464 PRInt32 result32; |
|
465 PRUint32 resultU32; |
|
466 float resultF; |
|
467 PRFloat64 resultD; |
|
468 |
|
469 ReportProgress("Testing Conversion Operations\n"); |
|
470 |
|
471 /* LL_L2I -- Convert to signed 32bit */ |
|
472 LL_L2I(result32, bigOne ); |
|
473 if ( result32 != one ) |
|
474 SetFailed( "LL_L2I", "r != 1"); |
|
475 |
|
476 LL_L2I(result32, bigMinusOne ); |
|
477 if ( result32 != minusOne ) |
|
478 SetFailed( "LL_L2I", "r != -1"); |
|
479 |
|
480 /* LL_L2UI -- Convert 64bit to unsigned 32bit */ |
|
481 LL_L2UI( resultU32, bigMinusOne ); |
|
482 if ( resultU32 != (PRUint32) minusOne ) |
|
483 SetFailed( "LL_L2UI", "r != -1"); |
|
484 |
|
485 LL_L2UI( resultU32, bigOne ); |
|
486 if ( resultU32 != (PRUint32) one ) |
|
487 SetFailed( "LL_L2UI", "r != 1"); |
|
488 |
|
489 /* LL_L2F -- Convert to 32bit floating point */ |
|
490 LL_L2F( resultF, bigOne ); |
|
491 if ( resultF != 1.0 ) |
|
492 SetFailed( "LL_L2F", "r != 1.0"); |
|
493 |
|
494 LL_L2F( resultF, bigMinusOne ); |
|
495 if ( resultF != -1.0 ) |
|
496 SetFailed( "LL_L2F", "r != 1.0"); |
|
497 |
|
498 /* LL_L2D -- Convert to 64bit floating point */ |
|
499 LL_L2D( resultD, bigOne ); |
|
500 if ( resultD != 1.0L ) |
|
501 SetFailed( "LL_L2D", "r != 1.0"); |
|
502 |
|
503 LL_L2D( resultD, bigMinusOne ); |
|
504 if ( resultD != -1.0L ) |
|
505 SetFailed( "LL_L2D", "r != -1.0"); |
|
506 |
|
507 /* LL_I2L -- Convert 32bit signed to 64bit signed */ |
|
508 LL_I2L( result, one ); |
|
509 if ( LL_CMP(result, !=, bigOne )) |
|
510 SetFailed( "LL_I2L", "r != 1"); |
|
511 |
|
512 LL_I2L( result, minusOne ); |
|
513 if ( LL_CMP(result, !=, bigMinusOne )) |
|
514 SetFailed( "LL_I2L", "r != -1"); |
|
515 |
|
516 /* LL_UI2L -- Convert 32bit unsigned to 64bit unsigned */ |
|
517 LL_UI2L( resultU, (PRUint32) one ); |
|
518 if ( LL_CMP(resultU, !=, bigOne )) |
|
519 SetFailed( "LL_UI2L", "r != 1"); |
|
520 |
|
521 /* [lth.] This did not behave as expected, but it is correct |
|
522 */ |
|
523 LL_UI2L( resultU, (PRUint32) minusOne ); |
|
524 if ( LL_CMP(resultU, !=, bigZeroFox )) |
|
525 ResultFailed( "LL_UI2L", "r != -1", bigZeroFox, resultU); |
|
526 |
|
527 /* LL_F2L -- Convert 32bit float to 64bit signed */ |
|
528 LL_F2L( result, 1.0 ); |
|
529 if ( LL_CMP(result, !=, bigOne )) |
|
530 SetFailed( "LL_F2L", "r != 1"); |
|
531 |
|
532 LL_F2L( result, -1.0 ); |
|
533 if ( LL_CMP(result, !=, bigMinusOne )) |
|
534 SetFailed( "LL_F2L", "r != -1"); |
|
535 |
|
536 /* LL_D2L -- Convert 64bit Float to 64bit signed */ |
|
537 LL_D2L( result, 1.0L ); |
|
538 if ( LL_CMP(result, !=, bigOne )) |
|
539 SetFailed( "LL_D2L", "r != 1"); |
|
540 |
|
541 LL_D2L( result, -1.0L ); |
|
542 if ( LL_CMP(result, !=, bigMinusOne )) |
|
543 SetFailed( "LL_D2L", "r != -1"); |
|
544 |
|
545 return; |
|
546 } |
|
547 |
|
548 static void ShiftCompileOnly() |
|
549 { |
|
550 /* |
|
551 ** This function is only compiled, never called. |
|
552 ** The real test is to see if it compiles w/o |
|
553 ** warnings. This is no small feat, by the way. |
|
554 */ |
|
555 PRInt64 ia, ib; |
|
556 PRUint64 ua, ub; |
|
557 LL_SHR(ia, ib, 32); |
|
558 LL_SHL(ia, ib, 32); |
|
559 |
|
560 LL_USHR(ua, ub, 32); |
|
561 LL_ISHL(ia, 49, 32); |
|
562 |
|
563 } /* ShiftCompileOnly */ |
|
564 |
|
565 |
|
566 /* |
|
567 ** TestShift() -- Test Shifting Operations |
|
568 ** |
|
569 */ |
|
570 static void |
|
571 TestShift( void ) |
|
572 { |
|
573 static const PRInt64 largeTwoZero = LL_INIT( 0x00000002, 0x00000000 ); |
|
574 PRInt64 result; |
|
575 PRUint64 resultU; |
|
576 |
|
577 ReportProgress("Testing Shifting Operations\n"); |
|
578 |
|
579 /* LL_SHL -- Shift left algebraic */ |
|
580 LL_SHL( result, bigOne, one ); |
|
581 if ( LL_CMP( result, !=, bigTwo )) |
|
582 ResultFailed( "LL_SHL", "r != 2", bigOne, result ); |
|
583 |
|
584 LL_SHL( result, bigTwo, thirtyTwo ); |
|
585 if ( LL_CMP( result, !=, largeTwoZero )) |
|
586 ResultFailed( "LL_SHL", "r != twoZero", largeTwoZero, result); |
|
587 |
|
588 /* LL_SHR -- Shift right algebraic */ |
|
589 LL_SHR( result, bigFoxZero, thirtyTwo ); |
|
590 if ( LL_CMP( result, !=, bigMinusOne )) |
|
591 ResultFailed( "LL_SHR", "r != -1", bigMinusOne, result); |
|
592 |
|
593 LL_SHR( result, bigTwo, one ); |
|
594 if ( LL_CMP( result, !=, bigOne )) |
|
595 ResultFailed( "LL_SHR", "r != 1", bigOne, result); |
|
596 |
|
597 LL_SHR( result, bigFoxFox, thirtyTwo ); |
|
598 if ( LL_CMP( result, !=, bigMinusOne )) |
|
599 ResultFailed( "LL_SHR", "r != -1 (was ff,ff)", bigMinusOne, result); |
|
600 |
|
601 /* LL_USHR -- Logical shift right */ |
|
602 LL_USHR( resultU, bigZeroFox, thirtyTwo ); |
|
603 if ( LL_UCMP( resultU, !=, bigZero )) |
|
604 ResultFailed( "LL_USHR", "r != 0 ", bigZero, result); |
|
605 |
|
606 LL_USHR( resultU, bigFoxFox, thirtyTwo ); |
|
607 if ( LL_UCMP( resultU, !=, bigZeroFox )) |
|
608 ResultFailed( "LL_USHR", "r != 0 ", bigZeroFox, result); |
|
609 |
|
610 /* LL_ISHL -- Shift a 32bit integer into a 64bit result */ |
|
611 LL_ISHL( resultU, minusOne, thirtyTwo ); |
|
612 if ( LL_UCMP( resultU, !=, bigFoxZero )) |
|
613 ResultFailed( "LL_ISHL", "r != ff,00 ", bigFoxZero, result); |
|
614 |
|
615 LL_ISHL( resultU, one, sixtyThree ); |
|
616 if ( LL_UCMP( resultU, !=, bigEightZero )) |
|
617 ResultFailed( "LL_ISHL", "r != 80,00 ", bigEightZero, result); |
|
618 |
|
619 LL_ISHL( resultU, one, sixteen ); |
|
620 if ( LL_UCMP( resultU, !=, big64K )) |
|
621 ResultFailed( "LL_ISHL", "r != 64K ", big64K, resultU); |
|
622 |
|
623 return; |
|
624 } |
|
625 |
|
626 |
|
627 /* |
|
628 ** TestArithmetic() -- Test arithmetic operations. |
|
629 ** |
|
630 */ |
|
631 static void |
|
632 TestArithmetic( void ) |
|
633 { |
|
634 PRInt64 largeVal = LL_INIT( 0x00000001, 0xffffffff ); |
|
635 PRInt64 largeValPlusOne = LL_INIT( 0x00000002, 0x00000000 ); |
|
636 PRInt64 largeValTimesTwo = LL_INIT( 0x00000003, 0xfffffffe ); |
|
637 PRInt64 largeMultCand = LL_INIT( 0x00000000, 0x7fffffff ); |
|
638 PRInt64 largeMinusMultCand = LL_INIT( 0xffffffff, 0x10000001 ); |
|
639 PRInt64 largeMultCandx64K = LL_INIT( 0x00007fff, 0xffff0000 ); |
|
640 PRInt64 largeNumSHL5 = LL_INIT( 0x0000001f, 0xffffffe0 ); |
|
641 PRInt64 result, result2; |
|
642 |
|
643 /* Addition */ |
|
644 LL_ADD( result, bigOne, bigOne ); |
|
645 if ( LL_CMP( result, !=, bigTwo )) |
|
646 ResultFailed( "LL_ADD", "r != 1 + 1", bigTwo, result); |
|
647 |
|
648 LL_ADD( result, bigMinusOne, bigOne ); |
|
649 if ( LL_CMP( result, !=, bigZero )) |
|
650 ResultFailed( "LL_ADD", "r != -1 + 1", bigOne, result); |
|
651 |
|
652 LL_ADD( result, largeVal, bigOne ); |
|
653 if ( LL_CMP( result, !=, largeValPlusOne )) |
|
654 ResultFailed( "LL_ADD", "lVP1 != lV + 1", largeValPlusOne, result); |
|
655 |
|
656 /* Subtraction */ |
|
657 LL_SUB( result, bigOne, bigOne ); |
|
658 if ( LL_CMP( result, !=, bigZero )) |
|
659 ResultFailed( "LL_SUB", "r != 1 - 1", bigZero, result); |
|
660 |
|
661 LL_SUB( result, bigTwo, bigOne ); |
|
662 if ( LL_CMP( result, !=, bigOne )) |
|
663 ResultFailed( "LL_SUB", "r != 2 - 1", bigOne, result); |
|
664 |
|
665 LL_SUB( result, largeValPlusOne, bigOne ); |
|
666 if ( LL_CMP( result, !=, largeVal )) |
|
667 ResultFailed( "LL_SUB", "r != lVP1 - 1", largeVal, result); |
|
668 |
|
669 |
|
670 /* Multiply */ |
|
671 LL_MUL( result, largeVal, bigTwo ); |
|
672 if ( LL_CMP( result, !=, largeValTimesTwo )) |
|
673 ResultFailed( "LL_MUL", "r != lV*2", largeValTimesTwo, result); |
|
674 |
|
675 LL_MUL( result, largeMultCand, big64K ); |
|
676 if ( LL_CMP( result, !=, largeMultCandx64K )) |
|
677 ResultFailed( "LL_MUL", "r != lV*64K", largeMultCandx64K, result); |
|
678 |
|
679 LL_NEG( result2, largeMultCand ); |
|
680 LL_MUL( result, largeMultCand, bigMinusOne ); |
|
681 if ( LL_CMP( result, !=, result2 )) |
|
682 ResultFailed( "LL_MUL", "r != -lMC", result2, result); |
|
683 |
|
684 LL_SHL( result2, bigZeroFox, 5); |
|
685 LL_MUL( result, bigZeroFox, bigThirtyTwo ); |
|
686 if ( LL_CMP( result, !=, largeNumSHL5 )) |
|
687 ResultFailed( "LL_MUL", "r != 0f<<5", largeNumSHL5, result ); |
|
688 |
|
689 |
|
690 |
|
691 /* LL_DIV() Division */ |
|
692 LL_DIV( result, bigOne, bigOne); |
|
693 if ( LL_CMP( result, !=, bigOne )) |
|
694 ResultFailed( "LL_DIV", "1 != 1", bigOne, result); |
|
695 |
|
696 LL_DIV( result, bigNumber, bigOne ); |
|
697 if ( LL_CMP( result, !=, bigNumber )) |
|
698 ResultFailed( "LL_DIV", "r != n / 1", bigNumber, result); |
|
699 |
|
700 LL_DIV( result, bigNumber, bigMinusOne ); |
|
701 if ( LL_CMP( result, !=, bigMinusNumber )) |
|
702 ResultFailed( "LL_DIV", "r != n / -1", bigMinusNumber, result); |
|
703 |
|
704 LL_DIV( result, bigMinusNumber, bigMinusOne ); |
|
705 if ( LL_CMP( result, !=, bigNumber )) |
|
706 ResultFailed( "LL_DIV", "r != -n / -1", bigNumber, result); |
|
707 |
|
708 LL_SHL( result2, bigZeroFox, 5 ); |
|
709 LL_DIV( result, result2, bigOne ); |
|
710 if ( LL_CMP( result, !=, result2 )) |
|
711 ResultFailed( "LL_DIV", "0f<<5 != 0f<<5", result2, result); |
|
712 |
|
713 LL_SHL( result2, bigZeroFox, 5 ); |
|
714 LL_NEG( result2, result2 ); |
|
715 LL_DIV( result, result2, bigOne ); |
|
716 if ( LL_CMP( result, !=, result2 )) |
|
717 ResultFailed( "LL_DIV", "-0f<<5 != -0f<<5", result2, result); |
|
718 |
|
719 LL_SHL( result2, bigZeroFox, 17 ); |
|
720 LL_DIV( result, result2, bigMinusOne ); |
|
721 LL_NEG( result2, result2 ); |
|
722 if ( LL_CMP( result, !=, result2 )) |
|
723 ResultFailed( "LL_DIV", "-0f<<17 != -0f<<17", result2, result); |
|
724 |
|
725 |
|
726 /* LL_MOD() Modulo Division */ |
|
727 LL_ADD( result2, bigThirtyTwo, bigOne ); |
|
728 LL_MOD( result, result2, bigSixTeen ); |
|
729 if ( LL_CMP( result, !=, bigOne )) |
|
730 ResultFailed( "LL_MOD", "r != 1", bigSixTeen, result); |
|
731 |
|
732 |
|
733 LL_MUL( result2, bigZeroFox, bigThirtyTwo ); |
|
734 LL_ADD( result2, result2, bigSixTeen); |
|
735 LL_MOD( result, result2, bigThirtyTwo ); |
|
736 if ( LL_CMP( result, !=, bigSixTeen )) |
|
737 ResultFailed( "LL_MOD", "r != 16", bigSixTeen, result); |
|
738 |
|
739 /* LL_UDIVMOD */ |
|
740 LL_DIV( result, bigOne, bigOne); |
|
741 if ( LL_CMP( result, !=, bigOne )) |
|
742 ResultFailed( "LL_DIV", "r != 16", bigSixTeen, result); |
|
743 |
|
744 |
|
745 return; |
|
746 } |
|
747 |
|
748 static void TestWellknowns(void) |
|
749 { |
|
750 PRInt64 max = LL_MAXINT, min = LL_MININT, zero = LL_ZERO; |
|
751 PRInt64 mmax = LL_MaxInt(), mmin = LL_MinInt(), mzero = LL_Zero(); |
|
752 if (LL_NE(max, mmax)) |
|
753 ResultFailed( "max, mmax", "max != mmax", max, mmax); |
|
754 if (LL_NE(min, mmin)) |
|
755 ResultFailed( "min, mmin", "min != mmin", max, mmin); |
|
756 if (LL_NE(zero, mzero)) |
|
757 ResultFailed( "zero, mzero", "zero != mzero", zero, mzero); |
|
758 } /* TestWellknowns */ |
|
759 |
|
760 /* |
|
761 ** Initialize() -- Initialize the test case |
|
762 ** |
|
763 ** Parse command line options |
|
764 ** |
|
765 */ |
|
766 static PRIntn |
|
767 Initialize( PRIntn argc, char **argv ) |
|
768 { |
|
769 PLOptState *opt = PL_CreateOptState(argc, argv, "dvh"); |
|
770 PLOptStatus os; |
|
771 |
|
772 /* |
|
773 ** Parse command line options |
|
774 */ |
|
775 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) |
|
776 { |
|
777 if (PL_OPT_BAD == os) continue; |
|
778 switch (opt->option) |
|
779 { |
|
780 case 'd': /* set debug mode */ |
|
781 debugMode = PR_TRUE; |
|
782 break; |
|
783 |
|
784 case 'v': /* set verbose mode */ |
|
785 verboseMode = PR_TRUE; |
|
786 debugMode = PR_TRUE; |
|
787 break; |
|
788 |
|
789 case 'h': /* user wants some guidance */ |
|
790 default: |
|
791 PR_fprintf(output, "You get help.\n"); |
|
792 return(1); |
|
793 } |
|
794 } |
|
795 PL_DestroyOptState(opt); |
|
796 return(0); |
|
797 } |
|
798 |
|
799 int main(int argc, char **argv) |
|
800 { |
|
801 PR_STDIO_INIT(); |
|
802 output = PR_GetSpecialFD(PR_StandardError); |
|
803 |
|
804 if ( Initialize( argc, argv )) |
|
805 return(1); |
|
806 |
|
807 TestAssignment(); |
|
808 TestComparisons(); |
|
809 TestLogicalOperations(); |
|
810 TestConversion(); |
|
811 TestShift(); |
|
812 TestArithmetic(); |
|
813 TestWellknowns(); |
|
814 |
|
815 /* |
|
816 ** That's all folks! |
|
817 */ |
|
818 if ( failedAlready ) |
|
819 { |
|
820 PR_fprintf(output, "FAIL\n");\ |
|
821 } |
|
822 else |
|
823 { |
|
824 PR_fprintf(output, "PASS\n");\ |
|
825 } |
|
826 return failedAlready; |
|
827 } /* end main() */ |