test.c (28285B)
1 /* See LICENSE file for copyright and license details. */ 2 #include "zahl.h" 3 4 #include <stdio.h> 5 #include <string.h> 6 7 #define assert(expr, expected)\ 8 do {\ 9 int got = (expr);\ 10 if (!(got expected)) {\ 11 fprintf(stderr,\ 12 "Failure at line %i: %s, expected %s, but got %i.\n",\ 13 __LINE__, #expr, #expected, got);\ 14 ret = 1;\ 15 goto done;\ 16 }\ 17 } while (0) 18 19 #define assert_zu(expr, expected)\ 20 do {\ 21 size_t got = (expr);\ 22 if (got != (expected)) {\ 23 fprintf(stderr,\ 24 "Failure at line %i: %s, expected %zu, but got %zu.\n",\ 25 __LINE__, #expr, (size_t)(expected), got);\ 26 ret = 1;\ 27 goto done;\ 28 }\ 29 } while (0) 30 31 #define assert_s(expr, expected)\ 32 do {\ 33 const char *got = (expr);\ 34 if (strcmp(got, expected)) {\ 35 fprintf(stderr,\ 36 "Failure at line %i: %s, expected %s, but got %s.\n",\ 37 __LINE__, #expr, expected, got);\ 38 ret = 1;\ 39 goto done;\ 40 }\ 41 } while (0) 42 43 #define assert_nr(expr)\ 44 do {\ 45 if (setjmp(env2)) {\ 46 ret = 0;\ 47 zsetup(env);\ 48 } else {\ 49 zsetup(env2);\ 50 expr;\ 51 fprintf(stderr,\ 52 "Failure at line %i: %s, should not have returned.\n",\ 53 __LINE__, #expr);\ 54 ret = 1;\ 55 goto done;\ 56 }\ 57 } while (0) 58 59 int 60 main(void) 61 { 62 /* static because otherwise it would have to be volatile yeilding a lot of stupid 63 * warnings. auto variables are not guaranteed to be readable after a long jump. */ 64 static z_t a, b, c, d, _0, _1, _2, _3; 65 static char buf[2000]; 66 static int ret = 0; 67 static jmp_buf env, env2; 68 static size_t n; 69 70 #define BUF_N (sizeof(buf) - 1) 71 72 if (setjmp(env)) { 73 zperror(0); 74 ret = 2; 75 goto done; 76 } 77 78 zsetup(env); 79 zinit(a), zinit(b), zinit(c), zinit(d), zinit(_0), zinit(_1), zinit(_2), zinit(_3); 80 81 zsetu(_0, 0); 82 zsetu(_1, 1); 83 zsetu(_2, 2); 84 zsetu(_3, 3); 85 86 assert(zeven(_0), == 1); 87 assert(zodd(_0), == 0); 88 assert(zzero(_0), == 1); 89 assert(zsignum(_0), == 0); 90 assert(zeven(_1), == 0); 91 assert(zodd(_1), == 1); 92 assert(zzero(_1), == 0); 93 assert(zsignum(_1), == 1); 94 assert(zeven(_2), == 1); 95 assert(zodd(_2), == 0); 96 assert(zzero(_2), == 0); 97 assert(zsignum(_2), == 1); 98 99 zswap(_1, _2); 100 assert(zeven(_2), == 0); 101 assert(zodd(_2), == 1); 102 assert(zzero(_2), == 0); 103 assert(zsignum(_2), == 1); 104 assert(zeven(_1), == 1); 105 assert(zodd(_1), == 0); 106 assert(zzero(_1), == 0); 107 assert(zsignum(_1), == 1); 108 zswap(_2, _1); 109 assert(zeven(_1), == 0); 110 assert(zodd(_1), == 1); 111 assert(zzero(_1), == 0); 112 assert(zsignum(_1), == 1); 113 assert(zeven(_2), == 1); 114 assert(zodd(_2), == 0); 115 assert(zzero(_2), == 0); 116 assert(zsignum(_2), == 1); 117 118 assert((zneg(_2, _2), zsignum(_2)), == -1); zneg(_2, _2); 119 assert(zsignum(_2), == 1); 120 121 assert(zcmp(_0, _0), == 0); 122 assert(zcmp(_1, _1), == 0); 123 assert(zcmp(_0, _1), < 0); 124 assert(zcmp(_1, _0), > 0); 125 assert(zcmp(_1, _2), < 0); 126 assert(zcmp(_2, _1), > 0); 127 assert(zcmp(_0, _2), < 0); 128 assert(zcmp(_2, _0), > 0); 129 130 zbset(a, _0, 0, 1); 131 assert(zcmp(a, _1), == 0); 132 zbset(a, a, 1, 1); 133 assert(zcmp(a, _3), == 0); 134 zbset(a, a, 0, 0); 135 assert(zcmp(a, _2), == 0); 136 zbset(a, a, 0, 0); 137 assert(zcmp(a, _2), == 0); 138 zbset(a, a, 0, -1); 139 assert(zcmp(a, _3), == 0); 140 zbset(a, a, 0, -1); 141 assert(zcmp(a, _2), == 0); 142 143 zadd(a, _0, _1); 144 assert(zsignum(a), == 1); 145 assert(zcmp(a, _1), == 0); 146 assert(zcmpi(a, 1), == 0); 147 assert(zcmpu(a, 1), == 0); 148 zneg(a, a); 149 assert(zsignum(a), == -1); 150 assert(zcmp(a, _1), < 0); 151 assert(zcmpi(a, 1), < 0); 152 assert(zcmpu(a, 1), < 0); 153 zadd(a, _2, _0); 154 assert(zsignum(a), == 1); 155 assert(zcmp(a, _2), == 0); 156 assert(zcmpi(a, 2), == 0); 157 assert(zcmpu(a, 2), == 0); 158 zneg(a, a); 159 assert(zsignum(a), == -1); 160 assert(zcmp(a, _2), < 0); 161 assert(zcmpi(a, 2), < 0); 162 assert(zcmpu(a, 2), < 0); 163 assert(zsignum(_1), == 1); 164 zadd(a, _1, _1); 165 assert(zsignum(a), == 1); 166 assert(zcmp(a, _2), == 0); 167 assert(zcmpi(a, 2), == 0); 168 assert(zcmpu(a, 2), == 0); 169 zset(b, _1); 170 zadd(a, b, _1); 171 assert(zsignum(a), == 1); 172 assert(zcmp(a, _2), == 0); 173 assert(zcmpi(a, 2), == 0); 174 assert(zcmpu(a, 2), == 0); 175 zneg(a, a); 176 zset(b, _2); 177 zneg(b, b); 178 assert(zsignum(a), == -1); 179 assert(zcmp(a, b), == 0); 180 assert(zcmp(a, _2), < 0); 181 assert(zcmpmag(a, b), == 0); 182 assert(zcmpmag(a, _2), == 0); 183 assert(zcmpi(a, 2), < 0); 184 assert(zcmpu(a, 2), < 0); 185 assert(zcmpi(a, -2), == 0); 186 assert((zneg(_2, _2), zcmp(a, _2)), == 0); zneg(_2, _2); 187 zadd(a, _1, _2); 188 assert(zsignum(a), == 1); 189 assert(zcmp(a, _2), > 0); 190 assert(zcmpi(a, 2), > 0); 191 assert(zcmpu(a, 2), > 0); 192 zneg(a, a); 193 zset(b, _2); 194 zneg(b, b); 195 assert(zsignum(a), == -1); 196 assert(zcmpmag(a, _2), > 0); 197 assert(zcmpmag(a, b), > 0); 198 assert(zcmp(a, b), < 0); 199 assert(zcmp(a, _2), < 0); 200 assert(zcmpi(a, 2), < 0); 201 assert(zcmpu(a, 2), < 0); 202 assert(zcmpi(a, -2), < 0); 203 assert((zneg(_2, _2), zcmp(a, _2)), < 0); zneg(_2, _2); 204 zneg(b, _3); 205 assert(zcmp(a, b), == 0); 206 207 zunsetup(); 208 zsetup(env); 209 210 zsub(a, _2, _1); 211 assert(zcmpmag(_2, _1), > 0); 212 assert(zcmpmag(_2, _0), > 0); 213 assert(zcmpmag(_1, _0), > 0); 214 zsub(b, _1, _2); 215 assert(zcmpmag(_2, _0), > 0); 216 assert(zcmpmag(_1, _0), > 0); 217 assert(zcmpmag(_2, _1), > 0); 218 assert(zcmpmag(a, b), == 0); 219 assert(zcmpmag(a, _1), == 0); 220 assert(zcmp(a, b), > 0); 221 assert(zcmp(a, _1), == 0); 222 assert(zcmp(b, _1), < 0); 223 zsub(a, _1, _1); 224 assert(zcmp(a, _0), == 0); 225 zseti(b, 0); 226 zsetu(c, 0); 227 zsub(a, b, c); 228 assert(zcmp(a, _0), == 0); 229 assert(zcmpmag(_2, _1), > 0); 230 assert(zcmp(_2, _1), > 0); 231 zsub(a, _2, _1); 232 assert(zsignum(a), == 1); 233 assert(zcmpmag(a, _1), == 0); 234 assert(zcmp(a, _1), == 0); 235 zsub(a, a, _1); 236 assert(zcmp(a, _0), == 0); 237 zsub(a, a, _0); 238 assert(zcmp(a, _0), == 0); 239 zsub(a, _1, _2); 240 assert(zcmp(a, _1), < 0); 241 assert(zcmpmag(a, _1), == 0); 242 zabs(a, a); 243 assert(zcmp(a, _1), == 0); 244 zabs(a, a); 245 assert(zcmp(a, _1), == 0); 246 zabs(a, _1); 247 assert(zcmp(a, _1), == 0); 248 zabs(a, _0); 249 assert(zcmp(a, _0), == 0); 250 251 zseti(b, -1); 252 zseti(c, -2); 253 zadd(a, _0, b); 254 assert(zcmp(a, _0), < 0); 255 assert(zcmpi(a, -1), == 0); 256 assert(zcmpmag(a, _1), == 0); 257 assert(zcmp(a, _1), < 0); 258 zadd(a, b, _0); 259 assert(zcmp(a, _0), < 0); 260 assert(zcmpi(a, -1), == 0); 261 assert(zcmpmag(a, _1), == 0); 262 assert(zcmp(a, _1), < 0); 263 zadd(a, b, c); 264 assert(zcmp(a, c), < 0); 265 assert(zcmpmag(a, _2), > 0); 266 zadd(a, c, b); 267 assert(zcmp(a, c), < 0); 268 assert(zcmpmag(a, _2), > 0); 269 zadd(a, b, _1); 270 assert(zcmp(a, _0), == 0); 271 assert(zcmpmag(a, _0), == 0); 272 zadd(a, _1, b); 273 assert(zcmp(a, _0), == 0); 274 assert(zcmpmag(a, _0), == 0); 275 276 zneg(b, _1); 277 zneg(c, _2); 278 zsub(a, _0, b); 279 assert(zcmp(a, _1), == 0); 280 zsub(a, b, _0); 281 assert(zcmpmag(a, _1), == 0); 282 assert(zcmp(a, _1), < 0); 283 zsub(a, b, c); 284 assert(zcmpmag(a, _1), == 0); 285 assert(zcmp(a, _1), == 0); 286 zsub(a, c, b); 287 assert(zcmpmag(a, _1), == 0); 288 assert(zcmp(a, _1), < 0); 289 zsub(a, b, _1); 290 assert(zcmpmag(a, _2), == 0); 291 assert(zcmp(a, _2), < 0); 292 assert(zcmp(a, c), == 0); 293 zsub(a, _1, b); 294 assert(zcmp(b, _1), < 0); 295 assert(zcmpmag(b, _1), == 0); 296 assert(zcmp(a, _2), == 0); 297 298 zsetu(a, 1000); 299 zsetu(b, 0); 300 assert(zcmp(a, b), != 0); 301 n = zsave(a, buf); 302 assert(n > 0, > 0); 303 assert_zu(zload(b, buf), n); 304 assert(zcmp(a, b), == 0); 305 306 zneg(b, _1); 307 zneg(c, _2); 308 309 assert((zadd_unsigned(a, _1, _2), zcmp(a, _3)), == 0); 310 assert((zadd_unsigned(a, b, c), zcmp(a, _3)), == 0); 311 assert((zadd_unsigned(a, b, _2), zcmp(a, _3)), == 0); 312 assert((zadd_unsigned(a, _1, c), zcmp(a, _3)), == 0); 313 314 assert((zadd_unsigned(a, _0, _0), zcmp(a, _0)), == 0); 315 assert((zadd_unsigned(a, _0, _1), zcmp(a, _1)), == 0); 316 assert((zadd_unsigned(a, _1, _1), zcmp(a, _2)), == 0); 317 assert((zadd_unsigned(a, _1, _0), zcmp(a, _1)), == 0); 318 zneg(_1, _1); 319 assert((zadd_unsigned(a, _0, _0), zcmp(a, _0)), == 0); 320 assert((zadd_unsigned(a, _0, _1), zcmp(a, _1)), != 0); 321 assert((zadd_unsigned(a, _0, _1), zcmpmag(a, _1)), == 0); 322 assert((zadd_unsigned(a, _1, _1), zcmp(a, _2)), == 0); 323 assert((zadd_unsigned(a, _1, _0), zcmp(a, _1)), != 0); 324 assert((zadd_unsigned(a, _1, _0), zcmpmag(a, _1)), == 0); 325 zneg(_1, _1); 326 327 assert((zsub_unsigned(a, _2, _1), zcmp(a, _1)), == 0); 328 assert((zsub_unsigned(a, _2, b), zcmp(a, _1)), == 0); 329 assert((zsub_unsigned(a, c, _1), zcmp(a, _1)), == 0); 330 assert((zsub_unsigned(a, c, b), zcmp(a, _1)), == 0); 331 332 assert((zsub_unsigned(a, _1, _2), zcmp(a, b)), == 0); 333 assert((zsub_unsigned(a, b, _2), zcmp(a, b)), == 0); 334 assert((zsub_unsigned(a, _1, c), zcmp(a, b)), == 0); 335 assert((zsub_unsigned(a, b, c), zcmp(a, b)), == 0); 336 337 assert_zu(zbits(_0), 1); 338 assert_zu(zbits(_1), 1); 339 assert_zu(zbits(_2), 2); 340 assert_zu(zbits(_3), 2); 341 342 assert_zu(zlsb(_0), SIZE_MAX); 343 assert_zu(zlsb(_1), 0); 344 assert_zu(zlsb(_2), 1); 345 assert_zu(zlsb(_3), 0); 346 347 assert((zand(a, _0, _0), zcmp(a, _0)), == 0); 348 assert(zzero(a), == 1); 349 assert((zand(a, _0, _1), zcmp(a, _0)), == 0); 350 assert(zzero(a), == 1); 351 assert((zand(a, _0, _2), zcmp(a, _0)), == 0); 352 assert(zzero(a), == 1); 353 assert((zand(a, _0, _3), zcmp(a, _0)), == 0); 354 assert(zzero(a), == 1); 355 assert((zand(a, _1, _1), zcmp(a, _1)), == 0); 356 assert((zand(a, _1, _2), zcmp(a, _0)), == 0); 357 assert(zzero(a), == 1); 358 assert((zand(a, _1, _3), zcmp(a, _1)), == 0); 359 assert((zand(a, _2, _2), zcmp(a, _2)), == 0); 360 assert((zand(a, _2, _3), zcmp(a, _2)), == 0); 361 assert((zand(a, _3, _3), zcmp(a, _3)), == 0); 362 363 assert((zor(a, _0, _0), zcmp(a, _0)), == 0); 364 assert(zzero(a), == 1); 365 assert((zor(a, _0, _1), zcmp(a, _1)), == 0); 366 assert((zor(a, _0, _2), zcmp(a, _2)), == 0); 367 assert((zor(a, _0, _3), zcmp(a, _3)), == 0); 368 assert((zor(a, _1, _1), zcmp(a, _1)), == 0); 369 assert((zor(a, _1, _2), zcmp(a, _3)), == 0); 370 assert((zor(a, _1, _3), zcmp(a, _3)), == 0); 371 assert((zor(a, _2, _2), zcmp(a, _2)), == 0); 372 assert((zor(a, _2, _3), zcmp(a, _3)), == 0); 373 assert((zor(a, _3, _3), zcmp(a, _3)), == 0); 374 375 assert((zxor(a, _0, _0), zcmp(a, _0)), == 0); 376 assert(zzero(a), == 1); 377 assert((zxor(a, _0, _1), zcmp(a, _1)), == 0); 378 assert((zxor(a, _0, _2), zcmp(a, _2)), == 0); 379 assert((zxor(a, _0, _3), zcmp(a, _3)), == 0); 380 assert((zxor(a, _1, _1), zcmp(a, _0)), == 0); 381 assert(zzero(a), == 1); 382 assert((zxor(a, _1, _2), zcmp(a, _3)), == 0); 383 assert((zxor(a, _1, _3), zcmp(a, _2)), == 0); 384 assert((zxor(a, _2, _2), zcmp(a, _0)), == 0); 385 assert(zzero(a), == 1); 386 assert((zxor(a, _2, _3), zcmp(a, _1)), == 0); 387 assert((zxor(a, _3, _3), zcmp(a, _0)), == 0); 388 assert(zzero(a), == 1); 389 390 zneg(b, _1); 391 zneg(c, _3); 392 zneg(_1, _1); 393 zand(a, b, c); 394 assert(zcmpmag(a, _1), == 0); 395 assert(zcmp(a, _1), == 0); 396 zneg(_1, _1); 397 assert((zand(a, b, _3), zcmp(a, _1)), == 0); 398 assert((zand(a, _1, c), zcmp(a, _1)), == 0); 399 assert((zand(a, _0, c), zcmp(a, _0)), == 0); 400 assert((zand(a, b, _0), zcmp(a, _0)), == 0); 401 402 zneg(b, _1); 403 zneg(c, _2); 404 zneg(_3, _3); 405 zor(a, b, c); 406 assert(zcmpmag(a, _3), == 0); 407 assert(zcmp(a, _3), == 0); 408 zor(a, b, _2); 409 assert(zcmpmag(a, _3), == 0); 410 assert(zcmp(a, _3), == 0); 411 zor(a, _1, c); 412 assert((zcmpmag(a, _3)), == 0); 413 assert((zcmp(a, _3)), == 0); 414 assert((zor(a, _0, c), zcmp(a, c)), == 0); 415 assert((zor(a, b, _0), zcmp(a, b)), == 0); 416 zneg(_3, _3); 417 418 zneg(b, _1); 419 zneg(c, _2); 420 zxor(a, b, c); 421 assert(zcmpmag(a, _3), == 0); 422 assert(zcmp(a, _3), == 0); 423 zneg(_3, _3); 424 zxor(a, b, _2); 425 assert(zcmpmag(a, _3), == 0); 426 assert(zcmp(a, _3), == 0); 427 zxor(a, _1, c); 428 assert(zcmpmag(a, _3), == 0); 429 assert(zcmp(a, _3), == 0); 430 zxor(a, b, _0); 431 assert(zcmpmag(a, b), == 0); 432 assert(zcmp(a, b), == 0); 433 zxor(a, _0, c); 434 assert(zcmpmag(a, c), == 0); 435 assert(zcmp(a, c), == 0); 436 zneg(_3, _3); 437 438 assert((zlsh(a, _0, 0), zcmp(a, _0)), == 0); 439 assert(zzero(a), == 1); 440 assert((zlsh(a, _0, 1), zcmp(a, _0)), == 0); 441 assert(zzero(a), == 1); 442 assert((zlsh(a, _1, 0), zcmp(a, _1)), == 0); 443 assert((zlsh(a, _1, 1), zcmp(a, _2)), == 0); 444 assert((zlsh(a, _1, 2), zcmp(a, _2)), > 0); 445 assert((zlsh(a, _2, 0), zcmp(a, _2)), == 0); 446 assert((zlsh(a, _2, 1), zcmp(a, _2)), > 0); 447 448 zset(a, _0); 449 assert((zlsh(a, a, 0), zcmp(a, _0)), == 0); 450 assert(zzero(a), == 1); 451 assert((zlsh(a, a, 1), zcmp(a, _0)), == 0); 452 assert(zzero(a), == 1); 453 zset(a, _1); 454 assert((zlsh(a, a, 0), zcmp(a, _1)), == 0); 455 assert((zlsh(a, a, 1), zcmp(a, _2)), == 0); 456 assert((zlsh(a, a, 2), zcmp(a, _2)), > 0); 457 zset(a, _2); 458 assert((zlsh(a, a, 0), zcmp(a, _2)), == 0); 459 assert((zlsh(a, a, 1), zcmp(a, _2)), > 0); 460 461 assert((zrsh(a, _0, 0), zcmp(a, _0)), == 0); 462 assert(zzero(a), == 1); 463 assert((zrsh(a, _0, 1), zcmp(a, _0)), == 0); 464 assert(zzero(a), == 1); 465 assert((zrsh(a, _1, 0), zcmp(a, _1)), == 0); 466 assert((zrsh(a, _1, 1), zcmp(a, _0)), == 0); 467 assert(zzero(a), == 1); 468 assert((zrsh(a, _1, 2), zcmp(a, _0)), == 0); 469 assert(zzero(a), == 1); 470 assert((zrsh(a, _2, 0), zcmp(a, _2)), == 0); 471 assert((zrsh(a, _2, 1), zcmp(a, _1)), == 0); 472 assert((zrsh(a, _2, 2), zcmp(a, _0)), == 0); 473 assert(zzero(a), == 1); 474 475 zset(a, _0); 476 assert((zrsh(a, a, 0), zcmp(a, _0)), == 0); 477 assert(zzero(a), == 1); 478 assert((zrsh(a, a, 1), zcmp(a, _0)), == 0); 479 assert(zzero(a), == 1); 480 zset(a, _1); 481 assert((zrsh(a, a, 0), zcmp(a, _1)), == 0); 482 assert((zrsh(a, a, 1), zcmp(a, _0)), == 0); 483 assert(zzero(a), == 1); 484 assert((zrsh(a, a, 2), zcmp(a, _0)), == 0); 485 assert(zzero(a), == 1); 486 zset(a, _2); 487 assert((zrsh(a, a, 0), zcmp(a, _2)), == 0); 488 assert((zrsh(a, a, 1), zcmp(a, _1)), == 0); 489 assert((zrsh(a, a, 2), zcmp(a, _0)), == 0); 490 assert(zzero(a), == 1); 491 492 assert(zbtest(_0, 0), == 0); 493 assert(zbtest(_1, 0), == 1); 494 assert(zbtest(_2, 0), == 0); 495 assert(zbtest(_3, 0), == 1); 496 assert(zbtest(_0, 1), == 0); 497 assert(zbtest(_1, 1), == 0); 498 assert(zbtest(_2, 1), == 1); 499 assert(zbtest(_3, 1), == 1); 500 assert(zbtest(_0, 2), == 0); 501 assert(zbtest(_1, 2), == 0); 502 assert(zbtest(_2, 2), == 0); 503 assert(zbtest(_3, 2), == 0); 504 505 znot(a, _2); 506 assert(zcmpmag(a, _1), == 0); 507 assert(zcmp(a, _1), != 0); 508 znot(a, a); 509 assert(zcmp(a, _0), == 0); 510 511 zsetu(a, 0x1234); 512 zsetu(c, 0x234); 513 ztrunc(a, a, 12); 514 assert(zcmp(a, c), == 0); 515 516 zsetu(a, 0xEEFF); 517 zsetu(c, 0xEE); 518 zsetu(d, 0xFF); 519 zsplit(a, b, a, 8); 520 assert(zcmpmag(a, c), == 0); 521 assert(zcmpmag(b, d), == 0); 522 zsetu(a, 0xEEFF); 523 zsplit(b, a, a, 8); 524 assert(zcmpmag(b, c), == 0); 525 assert(zcmpmag(a, d), == 0); 526 527 zmul(a, _2, _3); 528 assert(zcmpi(a, 6), == 0); 529 zneg(_3, _3); 530 zmul(a, _2, _3); 531 assert(zcmpi(a, -6), == 0); 532 zneg(_3, _3); 533 zneg(_2, _2); 534 zmul(a, _2, _3); 535 assert(zcmpi(a, -6), == 0); 536 zneg(_3, _3); 537 zmul(a, _2, _3); 538 assert(zcmpi(a, 6), == 0); 539 zneg(_3, _3); 540 zneg(_2, _2); 541 542 zmul(a, _3, _3); 543 assert(zcmpi(a, 9), == 0); 544 zsqr(a, _3); 545 assert(zcmpi(a, 9), == 0); 546 zneg(_3, _3); 547 zmul(a, _3, _3); 548 assert(zcmpi(a, 9), == 0); 549 zsqr(a, _3); 550 assert(zcmpi(a, 9), == 0); 551 zneg(_3, _3); 552 553 zseti(a, 8); 554 zseti(b, 2); 555 zdiv(c, a, b); 556 assert(zcmpi(c, 4), == 0); 557 zseti(b, -2); 558 zdiv(c, a, b); 559 assert(zcmpi(c, -4), == 0); 560 zseti(a, -8); 561 zseti(b, 2); 562 zdiv(c, a, b); 563 assert(zcmpi(c, -4), == 0); 564 zseti(b, -2); 565 zdiv(c, a, b); 566 assert(zcmpi(c, 4), == 0); 567 568 zseti(a, 1000); 569 zseti(b, 10); 570 zdiv(c, a, b); 571 assert(zcmpi(c, 100), == 0); 572 zseti(b, -10); 573 zdiv(c, a, b); 574 assert(zcmpi(c, -100), == 0); 575 zseti(a, -1000); 576 zseti(b, 10); 577 zdiv(c, a, b); 578 assert(zcmpi(c, -100), == 0); 579 zseti(b, -10); 580 zdiv(c, a, b); 581 assert(zcmpi(c, 100), == 0); 582 583 zseti(a, 7); 584 zseti(b, 3); 585 zmod(c, a, b); 586 assert(zcmpi(c, 1), == 0); 587 zseti(b, -3); 588 zmod(c, a, b); 589 assert(zcmpi(c, 1), == 0); 590 zseti(a, -7); 591 zseti(b, 3); 592 zmod(c, a, b); 593 assert(zcmpi(c, -1), == 0); 594 zseti(b, -3); 595 zmod(c, a, b); 596 assert(zcmpi(c, -1), == 0); 597 598 zseti(a, 7); 599 zseti(b, 3); 600 zdivmod(d, c, a, b); 601 assert(zcmpi(d, 2), == 0); 602 assert(zcmpi(c, 1), == 0); 603 zseti(b, -3); 604 zdivmod(d, c, a, b); 605 assert(zcmpi(d, -2), == 0); 606 assert(zcmpi(c, 1), == 0); 607 zseti(a, -7); 608 zseti(b, 3); 609 zdivmod(d, c, a, b); 610 assert(zcmpi(d, -2), == 0); 611 assert(zcmpi(c, -1), == 0); 612 zseti(b, -3); 613 zdivmod(d, c, a, b); 614 assert(zcmpi(d, 2), == 0); 615 assert(zcmpi(c, -1), == 0); 616 617 zseti(a, 10); 618 zseti(b, -1); 619 zpow(a, a, b); 620 assert(zcmp(a, _0), == 0); 621 622 zseti(a, 10); 623 zseti(b, -1); 624 zseti(a, 20); 625 zmodpow(a, a, b, c); 626 assert(zcmp(a, _0), == 0); 627 628 zseti(a, 10); 629 zseti(c, 100000L); 630 zpowu(a, a, 5); 631 assert(zcmpmag(a, c), == 0); 632 assert(zcmp(a, c), == 0); 633 634 zseti(a, -10); 635 zseti(c, -100000L); 636 zpowu(a, a, 5); 637 assert(zcmpmag(a, c), == 0); 638 assert(zcmp(a, c), == 0); 639 640 zseti(a, -10); 641 zseti(c, 10000L); 642 zpowu(a, a, 4); 643 assert(zcmpmag(a, c), == 0); 644 assert(zcmp(a, c), == 0); 645 646 zseti(a, 10); 647 zseti(c, 3); 648 zmodpowu(a, a, 5, c); 649 assert(zcmpmag(a, _1), == 0); 650 assert(zcmp(a, _1), == 0); 651 652 zseti(a, 10); 653 zseti(b, 5); 654 zseti(c, 100000L); 655 zpow(a, a, b); 656 assert(zcmpmag(a, c), == 0); 657 assert(zcmp(a, c), == 0); 658 659 zseti(a, -10); 660 zseti(b, 5); 661 zseti(c, -100000L); 662 zpow(a, a, b); 663 assert(zcmpmag(a, c), == 0); 664 assert(zcmp(a, c), == 0); 665 666 zseti(a, -10); 667 zseti(b, 4); 668 zseti(c, 10000L); 669 zpow(a, a, b); 670 assert(zcmpmag(a, c), == 0); 671 assert(zcmp(a, c), == 0); 672 673 zseti(a, 10); 674 zseti(b, 5); 675 zseti(c, 3); 676 zmodpow(a, a, b, c); 677 assert(zcmpmag(a, _1), == 0); 678 assert(zcmp(a, _1), == 0); 679 680 zseti(a, 102); 681 zseti(b, 501); 682 zseti(c, 5); 683 zmodmul(a, a, b, c); 684 assert(zcmp(a, _2), == 0); 685 686 zseti(b, 2 * 3 * 3 * 7); 687 zseti(c, 3 * 7 * 11); 688 zseti(d, 3 * 7); 689 assert((zgcd(a, _0, _0), zcmp(a, _0)), == 0); 690 assert((zgcd(a, b, _0), zcmp(a, b)), == 0); 691 assert((zgcd(a, _0, c), zcmp(a, c)), == 0); 692 assert((zgcd(a, b, b), zcmp(a, b)), == 0); 693 assert((zgcd(a, b, _2), zcmp(a, _2)), == 0); 694 assert((zgcd(a, _2, b), zcmp(a, _2)), == 0); 695 assert((zgcd(a, _2, _2), zcmp(a, _2)), == 0); 696 assert((zgcd(a, c, _2), zcmp(a, _1)), == 0); 697 assert((zgcd(a, _2, c), zcmp(a, _1)), == 0); 698 assert((zgcd(a, b, _1), zcmp(a, _1)), == 0); 699 assert((zgcd(a, _1, c), zcmp(a, _1)), == 0); 700 assert((zgcd(a, _1, _1), zcmp(a, _1)), == 0); 701 assert((zgcd(a, b, c), zcmp(a, d)), == 0); 702 assert((zgcd(a, c, b), zcmp(a, d)), == 0); 703 704 zsets(a, "1234"); 705 assert(zcmpi(a, 1234), == 0); 706 zsets(b, "+1234"); 707 assert(zcmp(a, b), == 0); 708 assert_zu(zstr_length(_0, 10), 1); 709 assert_zu(zstr_length(_1, 10), 1); 710 assert_zu(zstr_length(_2, 10), 1); 711 assert_zu(zstr_length(_3, 10), 1); 712 zneg(_2, _2); 713 assert_zu(zstr_length(_2, 10), 2); 714 zneg(_2, _2); 715 assert_zu(zstr_length(a, 10), 4); 716 zstr(a, buf, 0); 717 assert_s(buf, "1234"); 718 zsets(a, "-1234"); 719 zseti(b, -1234); 720 zseti(c, 1234); 721 assert(zcmp(a, _0), < 0); 722 assert(zcmp(a, b), == 0); 723 assert(zcmpmag(a, c), == 0); 724 assert(zcmp(a, c), < 0); 725 zstr(a, buf, 0); 726 assert_s(buf, "-1234"); 727 assert_s(zstr(a, buf, 0), "-1234"); 728 729 zsetu(d, 100000UL); 730 zrand(a, FAST_RANDOM, UNIFORM, d); 731 assert(zcmp(a, _0), >= 0); 732 assert(zcmp(a, d), <= 0); 733 zrand(b, SECURE_RANDOM, UNIFORM, d); 734 assert(zcmp(b, _0), >= 0); 735 assert(zcmp(b, d), <= 0); 736 zrand(c, FASTEST_RANDOM, UNIFORM, d); 737 assert(zcmp(c, _0), >= 0); 738 assert(zcmp(c, d), <= 0); 739 assert(zcmp(a, b), != 0); 740 assert(zcmp(a, c), != 0); 741 assert(zcmp(b, c), != 0); 742 743 zsetu(d, 100000UL); 744 zrand(a, DEFAULT_RANDOM, QUASIUNIFORM, d); 745 assert(zcmp(a, _0), >= 0); 746 assert(zcmp(a, d), <= 0); 747 zrand(b, DEFAULT_RANDOM, QUASIUNIFORM, d); 748 assert(zcmp(b, _0), >= 0); 749 assert(zcmp(b, d), <= 0); 750 zrand(c, DEFAULT_RANDOM, QUASIUNIFORM, d); 751 assert(zcmp(c, _0), >= 0); 752 assert(zcmp(c, d), <= 0); 753 assert(zcmp(a, b), != 0); 754 assert(zcmp(a, c), != 0); 755 assert(zcmp(b, c), != 0); 756 757 zsetu(d, 100000UL); 758 zrand(a, DEFAULT_RANDOM, MODUNIFORM, d); 759 assert(zcmp(a, _0), >= 0); 760 assert(zcmp(a, d), <= 0); 761 zrand(b, DEFAULT_RANDOM, MODUNIFORM, d); 762 assert(zcmp(b, _0), >= 0); 763 assert(zcmp(b, d), <= 0); 764 zrand(c, DEFAULT_RANDOM, MODUNIFORM, d); 765 assert(zcmp(c, _0), >= 0); 766 assert(zcmp(c, d), <= 0); 767 assert(zcmp(a, b), != 0); 768 assert(zcmp(a, c), != 0); 769 assert(zcmp(b, c), != 0); 770 771 assert((zseti(a, -5), zptest(0, a, 100)), == NONPRIME); 772 assert((zseti(a, -4), zptest(0, a, 100)), == NONPRIME); 773 assert((zseti(a, -3), zptest(0, a, 100)), == NONPRIME); 774 assert((zseti(a, -2), zptest(0, a, 100)), == NONPRIME); 775 assert((zseti(a, -1), zptest(0, a, 100)), == NONPRIME); 776 assert((zseti(a, 0), zptest(0, a, 100)), == NONPRIME); 777 assert((zseti(a, 1), zptest(0, a, 100)), == NONPRIME); 778 assert((zseti(a, 2), zptest(0, a, 100)), == PRIME); 779 assert((zseti(a, 3), zptest(0, a, 100)), == PRIME); 780 assert((zseti(a, 4), zptest(0, a, 100)), == NONPRIME); 781 assert((zseti(a, 5), zptest(0, a, 100)), != NONPRIME); 782 assert((zseti(a, 6), zptest(0, a, 100)), == NONPRIME); 783 assert((zseti(a, 7), zptest(0, a, 100)), != NONPRIME); 784 assert((zseti(a, 8), zptest(0, a, 100)), == NONPRIME); 785 assert((zseti(a, 9), zptest(0, a, 100)), == NONPRIME); 786 assert((zseti(a, 10), zptest(0, a, 100)), == NONPRIME); 787 assert((zseti(a, 11), zptest(0, a, 100)), != NONPRIME); 788 assert((zseti(a, 101), zptest(0, a, 100)), != NONPRIME); 789 790 #if defined(ZAHL_UNSAFE) 791 (void) env2; 792 #else 793 assert_nr(zdivmod(a, b, _0, _0)); 794 assert_nr(zdivmod(a, b, _1, _0)); 795 zdivmod(a, b, _0, _1); 796 zdivmod(a, b, _1, _1); 797 assert_nr(zdiv(a, _0, _0)); 798 assert_nr(zdiv(a, _1, _0)); 799 zdiv(a, _0, _1); 800 zdiv(a, _1, _1); 801 assert_nr(zmod(a, _0, _0)); 802 assert_nr(zmod(a, _1, _0)); 803 zmod(a, _0, _1); 804 zmod(a, _1, _1); 805 assert_nr(zpow(a, _0, _0)); 806 assert_nr((zneg(_1, _1), zpow(a, _0, _1))); zneg(_1, _1); 807 zpow(a, _0, _1); 808 zpow(a, _1, _0); 809 zneg(_1, _1), zpow(a, _1, _0), zneg(_1, _1); 810 assert_nr(zmodmul(a, _1, _1, _0)); 811 assert_nr(zmodpow(a, _0, _0, _1)); 812 assert_nr((zneg(_1, _1), zmodpow(a, _0, _1, _1))); zneg(_1, _1); 813 zmodpow(a, _0, _1, _1); 814 zmodpow(a, _1, _0, _1); 815 zneg(_1, _1), zmodpow(a, _1, _0, _1), zneg(_1, _1); 816 assert_nr(zmodpow(a, _0, _0, _0)); 817 assert_nr((zneg(_1, _1), zmodpow(a, _0, _1, _0))); zneg(_1, _1); 818 assert_nr(zmodpow(a, _0, _1, _0)); 819 assert_nr(zmodpow(a, _1, _0, _0)); 820 assert_nr((zneg(_1, _1), zmodpow(a, _1, _0, _0))); zneg(_1, _1); 821 assert_nr(zpowu(a, _0, 0)); 822 zpowu(a, _0, 1); 823 zpowu(a, _1, 0); 824 zneg(_1, _1), zpowu(a, _1, 0), zneg(_1, _1); 825 assert_nr(zmodpowu(a, _0, 0, _1)); 826 zmodpowu(a, _0, 1, _1); 827 zmodpowu(a, _1, 0, _1); 828 zneg(_1, _1), zmodpowu(a, _1, 0, _1), zneg(_1, _1); 829 assert_nr(zmodpowu(a, _0, 0, _0)); 830 assert_nr((zneg(_1, _1), zmodpowu(a, _0, 1, _0))); zneg(_1, _1); 831 assert_nr(zmodpowu(a, _0, 1, _0)); 832 assert_nr(zmodpowu(a, _1, 0, _0)); 833 assert_nr((zneg(_1, _1), zmodpowu(a, _1, 0, _0))); zneg(_1, _1); 834 assert_nr(zstr_length(a, 0)); 835 assert_nr(zstr_length(a, 1)); 836 zstr_length(a, 2); 837 zstr_length(a, 3); 838 #endif 839 840 zsetu(a, 1LL); 841 assert_s(zstr(a, buf, 1), "1"); 842 zsetu(a, 10LL); 843 assert_s(zstr(a, buf, 2), "10"); 844 zsetu(a, 100LL); 845 assert_s(zstr(a, buf, 3), "100"); 846 zsetu(a, 1000LL); 847 assert_s(zstr(a, buf, 4), "1000"); 848 zsetu(a, 10000LL); 849 assert_s(zstr(a, buf, BUF_N), "10000"); 850 zsetu(a, 100000LL); 851 assert_s(zstr(a, buf, BUF_N), "100000"); 852 zsetu(a, 1000000LL); 853 assert_s(zstr(a, buf, BUF_N), "1000000"); 854 zsetu(a, 10000000LL); 855 assert_s(zstr(a, buf, BUF_N), "10000000"); 856 zsetu(a, 100000000LL); 857 assert_s(zstr(a, buf, BUF_N), "100000000"); 858 zsetu(a, 999999999LL); 859 assert_s(zstr(a, buf, BUF_N), "999999999"); 860 zsetu(a, 1000000000LL); 861 assert_s(zstr(a, buf, BUF_N), "1000000000"); 862 zsetu(a, 1000000001LL); 863 assert_s(zstr(a, buf, BUF_N), "1000000001"); 864 zsetu(a, 2000000000LL); 865 assert_s(zstr(a, buf, BUF_N), "2000000000"); 866 zsetu(a, 2050000000LL); 867 assert_s(zstr(a, buf, BUF_N), "2050000000"); 868 zsetu(a, 2100000000LL); 869 assert_s(zstr(a, buf, BUF_N), "2100000000"); 870 zsetu(a, 2140000000LL); 871 assert_s(zstr(a, buf, BUF_N), "2140000000"); 872 zsetu(a, 2147000000LL); 873 assert_s(zstr(a, buf, BUF_N), "2147000000"); 874 zsetu(a, 2147483000LL); 875 assert_s(zstr(a, buf, BUF_N), "2147483000"); 876 zsetu(a, 2147483640LL); 877 assert_s(zstr(a, buf, BUF_N), "2147483640"); 878 zsetu(a, 2147483646LL); 879 assert_s(zstr(a, buf, BUF_N), "2147483646"); 880 881 zseti(a, 2147483647LL); 882 assert_s(zstr(a, buf, BUF_N), "2147483647"); 883 zseti(a, -2147483647LL); 884 assert_s(zstr(a, buf, BUF_N), "-2147483647"); 885 zseti(a, -2147483647LL - 1LL); 886 assert_s(zstr(a, buf, BUF_N), "-2147483648"); 887 888 zsetu(a, 2147483647ULL); 889 assert_s(zstr(a, buf, BUF_N), "2147483647"); 890 zsetu(a, 2147483648ULL); 891 assert_s(zstr(a, buf, BUF_N), "2147483648"); 892 zsetu(a, 2147483649ULL); 893 assert_s(zstr(a, buf, BUF_N), "2147483649"); 894 895 zsetu(a, 3000000000ULL); 896 assert_s(zstr(a, buf, BUF_N), "3000000000"); 897 zsetu(a, 3100000000ULL); 898 assert_s(zstr(a, buf, BUF_N), "3100000000"); 899 zsetu(a, 3200000000ULL); 900 assert_s(zstr(a, buf, BUF_N), "3200000000"); 901 zsetu(a, 3300000000ULL); 902 assert_s(zstr(a, buf, BUF_N), "3300000000"); 903 zsetu(a, 3400000000ULL); 904 assert_s(zstr(a, buf, BUF_N), "3400000000"); 905 zsetu(a, 3500000000ULL); 906 assert_s(zstr(a, buf, BUF_N), "3500000000"); 907 zsetu(a, 3600000000ULL); 908 assert_s(zstr(a, buf, BUF_N), "3600000000"); 909 zsetu(a, 3700000000ULL); 910 assert_s(zstr(a, buf, BUF_N), "3700000000"); 911 zsetu(a, 3800000000ULL); 912 assert_s(zstr(a, buf, BUF_N), "3800000000"); 913 zsetu(a, 3900000000ULL); 914 assert_s(zstr(a, buf, BUF_N), "3900000000"); 915 zsetu(a, 3999999999ULL); 916 assert_s(zstr(a, buf, BUF_N), "3999999999"); 917 zsetu(a, 4000000000ULL); 918 assert_s(zstr(a, buf, BUF_N), "4000000000"); 919 zsetu(a, 4000000001ULL); 920 assert_zu(zstr_length(a, 10), 10); 921 assert_s(zstr(a, buf, BUF_N), "4000000001"); 922 923 zsetu(a, 4000000000ULL); 924 zsetu(b, 4000000000ULL); 925 zadd(c, a, a); 926 zsets(d, "8000000000"); 927 assert(zcmp(c, d), == 0); 928 zadd(c, a, b); 929 assert(zcmp(c, d), == 0); 930 zadd(c, c, a); 931 zsets(d, "12000000000"); 932 assert(zcmp(c, d), == 0); 933 zsub(c, c, a); 934 zsets(d, "8000000000"); 935 assert(zcmp(c, d), == 0); 936 zsub(c, c, a); 937 zsets(d, "4000000000"); 938 assert(zcmp(c, d), == 0); 939 zsets(d, "8000000000"); 940 zrsh(d, d, 1); 941 assert(zcmp(c, d), == 0); 942 zsets(a, "6234216714"); 943 zsets(b, "9424614147"); 944 zsets(d, "830476546"); 945 zand(c, a, b); 946 assert(zcmp(c, d), == 0); 947 zsets(a, "234216714"); 948 zsets(b, "9424614147"); 949 zsets(d, "9629466379"); 950 zor(c, a, b); 951 assert(zcmp(c, d), == 0); 952 zsets(a, "6234216714"); 953 zsets(b, "9424614147"); 954 zsets(d, "13997877769"); 955 zxor(c, a, b); 956 assert(zcmp(c, d), == 0); 957 zsets(a, "34216714"); 958 zsets(b, "9424614147"); 959 zsets(d, "9458821129"); 960 zxor(c, a, b); 961 assert(zcmp(c, d), == 0); 962 zsetu(a, 1000000000ULL); 963 zsets(d, "1000000000000000000"); 964 zmul(c, a, a); 965 assert(zcmp(c, d), == 0); 966 zdiv(c, c, a); 967 assert(zcmp(c, a), == 0); 968 zsetu(a, 1000000000ULL); 969 zsets(d, "1000000000000000000"); 970 zsqr(c, a); 971 assert(zcmp(c, d), == 0); 972 zsetu(a, 1000000000ULL); 973 zmodpowu(c, a, 5, _3); 974 assert(zcmpu(c, 1), == 0); 975 zsetu(a, 1000000000ULL); 976 zsets(d, "1"); 977 zpowu(c, a, 0); 978 assert(zcmp(c, d), == 0); 979 zsetu(a, 1000000000ULL); 980 zsets(d, "1000000000"); 981 zpowu(c, a, 1); 982 assert(zcmp(c, d), == 0); 983 zsetu(a, 1000000000ULL); 984 zsets(d, "1000000000000000000"); 985 zpowu(c, a, 2); 986 assert(zcmp(c, d), == 0); 987 zsetu(a, 1000000000ULL); 988 zsets(b, "1000000000000000000"); 989 zsets(d, "1000000000000000000000000000"); 990 zmul(c, a, b); 991 assert(zcmp(c, d), == 0); 992 zsetu(a, 1000000000ULL); 993 zsets(d, "1000000000000000000000000000"); 994 zmul(b, a, a); 995 zmul(b, b, a); 996 assert(zcmp(c, d), == 0); 997 zsetu(a, 1000000000ULL); 998 zsets(d, "1000000000000000000000000000"); 999 zpowu(c, a, 3); 1000 assert(zcmp(c, d), == 0); 1001 zsetu(a, 1000000000ULL); 1002 zsets(d, "1000000000000000000000000000000000000"); 1003 zpowu(c, a, 4); 1004 assert(zcmp(c, d), == 0); 1005 zsetu(a, 1000000000ULL); 1006 zsets(d, "1000000000000000000000000000000000000000000000"); 1007 zpowu(c, a, 5); 1008 assert(zcmp(c, d), == 0); 1009 1010 zsetu(a, 4294967294ULL); 1011 assert_s(zstr(a, buf, BUF_N), "4294967294"); 1012 zsetu(a, 4294967295ULL); 1013 assert_s(zstr(a, buf, BUF_N), "4294967295"); 1014 zsetu(a, 4294967296ULL); 1015 assert_s(zstr(a, buf, BUF_N), "4294967296"); 1016 zsetu(a, 4294967297ULL); 1017 assert_s(zstr(a, buf, BUF_N), "4294967297"); 1018 1019 zseti(a, 9223372036854775807LL); 1020 assert_s(zstr(a, buf, BUF_N), "9223372036854775807"); 1021 zseti(a, -9223372036854775807LL); 1022 assert_s(zstr(a, buf, BUF_N), "-9223372036854775807"); 1023 zseti(a, -9223372036854775807LL - 1LL); 1024 assert_s(zstr(a, buf, BUF_N), "-9223372036854775808"); 1025 1026 zsetu(a, 18446744073709551614ULL); 1027 assert_s(zstr(a, buf, BUF_N), "18446744073709551614"); 1028 zsetu(a, 18446744073709551615ULL); 1029 assert_s(zstr(a, buf, BUF_N), "18446744073709551615"); 1030 zadd(a, a, _1); 1031 assert_s(zstr(a, buf, BUF_N), "18446744073709551616"); 1032 zadd(a, a, _1); 1033 assert_s(zstr(a, buf, BUF_N), "18446744073709551617"); 1034 1035 zsets(a, "1000000000000000000000000000000"); 1036 assert_s(zstr(a, buf, BUF_N), "1000000000000000000000000000000"); 1037 zsets(a, "+1000000000000000000000000000000"); 1038 assert_s(zstr(a, buf, BUF_N), "1000000000000000000000000000000"); 1039 zsets(a, "-1000000000000000000000000000000"); 1040 assert_s(zstr(a, buf, BUF_N), "-1000000000000000000000000000000"); 1041 1042 zsetu(a, 1000000000000000ULL); 1043 zsqr(a, a); 1044 assert_s(zstr(a, buf, BUF_N), "1000000000000000000000000000000"); 1045 1046 #include "test-random.c" 1047 1048 done: 1049 zfree(a), zfree(b), zfree(c), zfree(d), zfree(_0), zfree(_1), zfree(_2), zfree(_3); 1050 zunsetup(); 1051 return ret; 1052 } 1053