lltest.c 26 KB

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