1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
|
# -*- tcl -*-
# bignum.test --
# Test cases for the ::math::bignum package
#
# -------------------------------------------------------------------------
source [file join \
[file dirname [file dirname [file join [pwd] [info script]]]] \
devtools testutilities.tcl]
testsNeedTcl 8.5
testsNeedTcltest 2.1
support {
useLocal math.tcl math
}
testing {
useLocal bignum.tcl math::bignum
}
# -------------------------------------------------------------------------
proc matchBignums { expected actual } {
set match 1
foreach a $actual e $expected {
if { $a != $b } {
set match 0
break
}
}
return $match
}
#
# Note:
# Some tests use the internal representation directly.
# The variables atombits is assumed to be 16
#
if { $::math::bignum::atombits != 16 } {
puts "Prerequisite: atombits = 16"
#
# The maximum value for the atoms is 2**16-1 = 65535
#
}
# -------------------------------------------------------------------------
#
# Tests: fromstr/tostr (use the internal representation directly)
#
test "Fromstr-1.0" "Convert string representing small number (1)" -body {
::math::bignum::fromstr 1
} -result {bignum 0 1}
test "Fromstr-1.1" "Convert string representing small number (2)" -body {
::math::bignum::fromstr 257
} -result {bignum 0 257}
test "Fromstr-1.2" "Convert string representing big number (1)" -body {
::math::bignum::fromstr "[expr {256*256*256}]"
} -result {bignum 0 0 256}
test "Fromstr-1.3" "Convert string representing big number (2)" -body {
::math::bignum::fromstr "[expr {256*256*256+1}]"
} -result {bignum 0 1 256}
test "Fromstr-1.4" "Convert string representing negative number" -body {
::math::bignum::fromstr "[expr {-256*256*256-1}]"
} -result {bignum 1 1 256}
test "Fromstr-1.5" "Convert string representing binary number (1)" -body {
::math::bignum::fromstr "10000000000000000000000000000000" 2
} -result {bignum 0 0 32768}
test "Fromstr-1.6" "Convert string representing binary number (2)" -body {
::math::bignum::fromstr "10000000000000000000000000000001" 2
} -result {bignum 0 1 32768}
test "Fromstr-1.7" "Convert string representing hex number (1)" -body {
::math::bignum::fromstr "ffffffff" 16
} -result {bignum 0 65535 65535}
test "Fromstr-1.8" "Convert string representing hex number (2)" -body {
::math::bignum::fromstr "-ffffffff" 16
} -result {bignum 1 65535 65535}
test "Fromstr-1.9" "Convert string representing 2*16+1" -body {
::math::bignum::fromstr "65537"
} -result {bignum 0 1 1}
test "Fromstr-1.10" "Convert string representing 2*16" -body {
::math::bignum::fromstr "65536"
} -result {bignum 0 0 1}
test "Tostr-2.0" "Convert small number (1)" -body {
::math::bignum::tostr {bignum 0 1}
} -result 1
test "Tostr-2.1" "Convert small number (2)" -body {
::math::bignum::tostr {bignum 0 257}
} -result 257
test "Tostr-2.2" "Convert big number (1)" -body {
::math::bignum::tostr {bignum 0 0 256}
} -result "[expr {256*256*256}]"
test "Tostr-2.3" "Convert big number (2)" -body {
::math::bignum::tostr {bignum 0 1 256}
} -result "[expr {256*256*256+1}]"
test "Tostr-2.4" "Convert negative number" -body {
::math::bignum::tostr {bignum 1 1 256}
} -result "[expr {-256*256*256-1}]"
test "Tostr-2.5" "Convert binary number (1)" -body {
::math::bignum::tostr {bignum 0 0 32768} 2
} -result "10000000000000000000000000000000"
test "Tostr-2.6" "Convert binary number (2)" -body {
::math::bignum::tostr {bignum 0 1 32768} 2
} -result "10000000000000000000000000000001"
test "Tostr-2.7" "Convert hex number (1)" -body {
::math::bignum::tostr {bignum 0 65535 65535} 16
} -result "ffffffff"
test "Tostr-2.8" "Convert hex number (2)" -body {
::math::bignum::tostr {bignum 1 65535 65535} 16
} -result "-ffffffff"
test "Tostr-2.9" "Convert very big number" -body {
::math::bignum::tostr [::math::bignum::fromstr "10000000000000000000"]
} -result "10000000000000000000"
test "Tostr-2.10" "Convert to ternary number" -body {
::math::bignum::tostr {bignum 0 9} 3
} -result "100"
#
# Arithmetic operations
#
test "Plus-3.0" "Add two smallish numbers" -body {
set a [::math::bignum::fromstr "100000"]
set b [::math::bignum::fromstr "100001"]
set c [::math::bignum::add $a $b]
::math::bignum::tostr $c
} -result "200001"
test "Plus-3.1" "Add two big numbers" -body {
set a [::math::bignum::fromstr "100000000000000"]
set b [::math::bignum::fromstr "100001000000001"]
set c [::math::bignum::add $a $b]
::math::bignum::tostr $c
} -result "200001000000001"
test "Plus-3.2" "Add two very large numbers" -body {
set a [::math::bignum::fromstr "1[string repeat 0 200]1"]
set b [::math::bignum::fromstr "2[string repeat 0 200]2"]
set c [::math::bignum::add $a $b]
::math::bignum::tostr $c
} -result "3[string repeat 0 200]3"
test "Plus-3.3" "Add zero to a large number" -body {
set a [::math::bignum::fromstr "1[string repeat 0 200]1"]
set b 0
set c [::math::bignum::add $a $b]
::math::bignum::tostr $c
} -result "1[string repeat 0 200]1"
test "Plus-3.4" "Add one to a large number" -body {
set a [::math::bignum::fromstr "1[string repeat 9 200]"]
set b 1
set c [::math::bignum::add $a $b]
::math::bignum::tostr $c
} -result "2[string repeat 0 200]"
test "Minus-3.2" "Subtract two smallish numbers" -body {
set a [::math::bignum::fromstr "100000"]
set b [::math::bignum::fromstr "100001"]
set c [::math::bignum::sub $a $b]
::math::bignum::tostr $c
} -result "-1"
test "Minus-3.3" "Subtract two big numbers" -body {
set a [::math::bignum::fromstr "100000000000000"]
set b [::math::bignum::fromstr "100001000000001"]
set c [::math::bignum::sub $a $b]
::math::bignum::tostr $c
} -result "-1000000001"
test "Minus-3.4" "Subtract one from a big number" -body {
set a [::math::bignum::fromstr "1[string repeat 0 50]"]
set b 1
set c [::math::bignum::sub $a $b]
::math::bignum::tostr $c
} -result [string repeat 9 50]
test "Compare-4.0" "Compare a set of two numbers" -body {
set okay 1
foreach {astring bstring op} {
1 -1 gt
1 -1 ge
1 1 ge
1 1 eq
-1 1 lt
-1 1 le
10000000 -10000000 gt
10000000 -10000000 ge
10000000 10000000 eq
-10000000 10000000 lt
-10000000 10000000 le
100000000000 -100000000000 gt
100000000000 -100000000000 ge
100000000000 100000000000 eq
-100000000000 100000000000 lt
-100000000000 100000000000 le
1000000000000000000000 -1000000000000000000000 gt
1000000000000000000000 -1000000000000000000000 ge
1000000000000000000000 1000000000000000000000 eq
-1000000000000000000000 1000000000000000000000 lt
-1000000000000000000000 1000000000000000000000 le
-1000000000000000000000 1000000000000000000000 ne
} {
set a [::math::bignum::fromstr $astring]
set b [::math::bignum::fromstr $bstring]
if { ! [::math::bignum::$op $a $b] } {
set okay "False: $astring $op $bstring"
break
}
}
return $okay
} -result 1
test "Compare-4.1" "Compare a set of two numbers (inverse result)" -body {
set okay 1
foreach {astring bstring op} {
-1 1 gt
-1 1 ge
1 1 ne
1 -1 lt
1 -1 le
-10000000 10000000 gt
-10000000 10000000 ge
10000000 10000000 ne
10000000 -10000000 lt
10000000 -10000000 le
-100000000000 100000000000 gt
-100000000000 100000000000 ge
100000000000 100000000000 ne
100000000000 -100000000000 lt
100000000000 -100000000000 le
-1000000000000000000000 1000000000000000000000 gt
-1000000000000000000000 1000000000000000000000 ge
1000000000000000000000 1000000000000000000000 ne
1000000000000000000000 -1000000000000000000000 lt
1000000000000000000000 -1000000000000000000000 le
1000000000000000000000 -1000000000000000000000 eq
} {
set a [::math::bignum::fromstr $astring]
set b [::math::bignum::fromstr $bstring]
#
# None should be true
#
if { [::math::bignum::$op $a $b] } {
set okay "True: $astring $op $bstring - should be false"
break
}
}
return $okay
} -result 1
test "Compare-4.2" "Compare a set of numbers against 0 and 1" -body {
set okay 1
foreach {astring opzero opone} {
-1 lt lt
1 gt eq
-10000000 lt lt
10000000 gt gt
0 eq lt
2 gt gt
} {
set a [::math::bignum::fromstr $astring]
foreach b {0 1} op [list $opzero $opone] {
#
# None should be true
#
if {! [::math::bignum::$op $a $b] } {
set okay "False: $astring $op $b - should be true"
break
}
}
}
return $okay
} -result 1
test "Mult-5.0" "Multiply two small numbers" -body {
set a [::math::bignum::fromstr 10]
set b [::math::bignum::fromstr 1000]
set c [::math::bignum::mul $a $b]
::math::bignum::tostr $c
} -result "10000"
test "Mult-5.0a" "Multiply small numbers by 0" -body {
set okay 1
foreach a {1 0 -1 100000 -10000 100000000000 -100000000000} {
set n [::math::bignum::fromstr $a]
if {! [::math::bignum::iszero [::math::bignum::mul $n 0]]} {
set okay "Multiplying $a by 0 does not give 0"
return
}
}
set okay
} -result 1
test "Mult-5.0b" "Multiply small numbers by 1" -body {
set okay 1
foreach a {1 0 -1 100000 -10000 100000000000 -100000000000} {
set n [::math::bignum::fromstr $a]
if {! [::math::bignum::eq [::math::bignum::mul $n 1] $n]} {
set okay "Multiplying $a by 1 does not give $a"
return
}
}
set okay
} -result 1
test "Mult-5.1" "Multiply two small negative numbers" -body {
set a [::math::bignum::fromstr -10]
set b [::math::bignum::fromstr -1000]
set c [::math::bignum::mul $a $b]
::math::bignum::tostr $c
} -result "10000"
test "Mult-5.2" "Multiply two very large numbers" -body {
set a [::math::bignum::fromstr "1[string repeat 0 100]"]
set b [::math::bignum::fromstr "2[string repeat 0 200]"]
set c [::math::bignum::mul $a $b]
::math::bignum::tostr $c
} -result "2[string repeat 0 300]"
test "Mult-5.3" "Multiply two very large numbers of opposite sign" -body {
set a [::math::bignum::fromstr "1[string repeat 0 100]"]
set b [::math::bignum::fromstr "-2[string repeat 0 200]"]
set c [::math::bignum::mul $a $b]
::math::bignum::tostr $c
} -result "-2[string repeat 0 300]"
test "Mult-5.4" "Katsabura multiplication with two very large numbers of opposite sign" -body {
set a [::math::bignum::fromstr "1[string repeat 0 1000]"]
set b [::math::bignum::fromstr "-2[string repeat 0 2000]"]
set c [::math::bignum::mul $a $b]
::math::bignum::tostr $c
} -result "-2[string repeat 0 3000]"
# Div
test "Div-6.1" "Divide 0 by any number" -body {
set okay 1
foreach n {1 -1 2 -2 10 -10 1000000000 -100000000} {
set a [::math::bignum::fromstr $n]
if {! [::math::bignum::iszero [::math::bignum::div 0 $a]]} {
set okay "Zero divided by $n does not give zero"
break
}
}
set okay
} -result 1
test "Div-6.2" "Divide small numbers by 1" -body {
set okay 1
foreach n {0 1 -1 2 -2 10 -10 1000000000 -100000000} {
set a [::math::bignum::fromstr $n]
if {! [::math::bignum::eq [::math::bignum::div $a 1] $a]} {
set okay "$n divided by 1 does not give $n"
break
}
}
set okay
} -result 1
test "Div-6.3" "Divide big numbers by 2" -body {
set okay 1
set two [::math::bignum::fromstr 2]
foreach p {2 5 10 50 100} {
set n 1[string repeat 0 $p]
set a [::math::bignum::fromstr $n]
set q 5[string repeat 0 [expr {$p-1}]]
if {! [string equal [::math::bignum::tostr [::math::bignum::div $a $two]] $q]} {
set okay "$n divided by 2 does not give $q"
break
}
}
set okay
} -result 1
test "Pow-7.1" "Exponentiate large numbers" -body {
set a [::math::bignum::fromstr "1[string repeat 0 10]"]
set b [::math::bignum::fromstr 1]
set okay 1
foreach p {1 2 3 4 5 6 7 8 9 10} {
set c [::math::bignum::mul $b $a]
set d [::math::bignum::pow $a $p]
if { [::math::bignum::ne $c $d] } {
set okay "False: $a**$p != $c"
}
}
return $okay
} -result 1
# Left and right shifts
set c 0
foreach {z n} {
1 1
2 1
4 1
-1 1
-2 1
-4 1
1 2
2 2
4 2
-1 2
-2 2
-4 2
1000001 1
2000001 1
4000001 1
-1000001 1
-2000001 1
-4000001 1
10000000001 1
20000000001 1
40000000001 1
-10000000001 1
-20000000001 1
-40000000001 1
10000000001 11
20000000001 11
40000000001 11
-10000000001 11
-20000000001 11
-40000000001 11
10000000001 21
20000000001 21
40000000001 21
-10000000001 21
-20000000001 21
-40000000001 21
} {
incr c
test "Lshift-8.$c" "Lshift large numbers" -body {
set x [::math::bignum::lshift [::math::bignum::fromstr $z] $n]
set y [expr {$z << $n}]
::math::bignum::cmp $x [::math::bignum::fromstr $y]
} -result 0
test "Rshift-8.$c" "Rshift large numbers" -body {
set x [::math::bignum::rshift [::math::bignum::fromstr $z] $n]
set y [expr {$z >> $n}]
::math::bignum::cmp $x [::math::bignum::fromstr $y]
} -result 0
}
# Bit operations (And, Or, Xor)
foreach {n a b zand zor zxor} {
0 0 0 0 0 0
1 1 2 0 3 3
2 1 3 1 3 2
3 2 3 2 3 1
} {
set a [::math::bignum::fromstr $a]
set b [::math::bignum::fromstr $b]
set zand [::math::bignum::fromstr $zand]
set zor [::math::bignum::fromstr $zor]
set zxor [::math::bignum::fromstr $zxor]
test "Bitand-8.$n" "BitAnd large numbers" -body {
::math::bignum::bitand $a $b
} -result $zand
test "Bitor-9.$n" "BitOr large numbers" -body {
::math::bignum::bitor $a $b
} -result $zor
test "Bitxor-10.$n" "BitXor large numbers" -body {
::math::bignum::bitxor $a $b
} -result $zxor
}
test "Mod-11.1" "Modulo and remainder for small numbers" -body {
set okay 1
foreach {n d m r} {
100 -3 -2 1
-100 -3 -1 -1
-100 3 2 -1
100 3 1 1
} {
set a [::math::bignum::fromstr $n]
set b [::math::bignum::fromstr $d]
set modulo [::math::bignum::tostr [::math::bignum::mod $a $b]]
set remainder [::math::bignum::tostr [::math::bignum::rem $a $b]]
if {! [string equal $modulo $m]} {
set okay "$n modulo $d does not give $m"
break
}
if {! [string equal $remainder $r]} {
set okay "the remainder of $n/$d is not given as $r"
break
}
}
return $okay
} -result 1
# Bit operations (Test bit)
test testbit-1.0 {test with bit in range of used bits} -setup {
set z [::math::bignum::fromstr 3220]
::math::bignum::setbit z 24
} -body {
::math::bignum::testbit $z 23
} -cleanup {
unset z
} -result 0
test testbit-1.1 {test with bit beyond range of used bits} -setup {
set z [::math::bignum::fromstr 3220]
} -body {
::math::bignum::testbit $z 23
} -cleanup {
unset z
} -result 0
test testbit-1.2 {test with bit in range of used bits} -setup {
set z [::math::bignum::fromstr 3220]
::math::bignum::setbit z 24
} -body {
::math::bignum::testbit $z 24
} -cleanup {
unset z
} -result 1
# -------------------------------------------------------------------------
#
# TODO: all the other operations and functions
#
# -------------------------------------------------------------------------
# End of test cases
testsuiteCleanup
|