Files
mmath/tests/test_bigint.nim

63 lines
1.7 KiB
Nim

import mmath/bigint
import unittest
import random
from strutils import parseBiggestUInt
suite "Nim arbitrary precision integers (powered by GMP)":
let str = "2347822319"
test "parse " & str:
let str = "2347822319"
let bi = BigInt.fromString(str, 10)
let ul : BiggestUInt = parseBiggestUInt(str)
check newBigInt(ul.int64) == bi
var rng = initRand(101325)
for i in 0..5:
let n1 = rng.rand(-1000..1000).int64
let n2 = rng.rand(-1000..1000).int64
let bi1 = BigInt.of(n1)
let bi2 = BigInt.of(n2)
test $n1 & " + " & $n2:
check BigInt.of(n1 + n2) == bi1 + bi2
test $n1 & " - " & $n2:
check BigInt.of(n1 - n2) == bi1 - bi2
test $n1 & " * " & $n2:
check BigInt.of(n1 * n2) == bi1 * bi2
test $n1 & " div " & $n2:
check BigInt.of(n1 div n2) == bi1 div bi2
test $n1 & " mod " & $n2:
check BigInt.of(n1 mod n2) == bi1 mod bi2
test $n1 & " pow " & $3:
check BigInt.of(n1 * n1 * n1) == pow(bi1, 3)
test "abs(" & $n1 & ")":
check BigInt.of(n1.abs()) == bi1.abs()
test "abs(" & $n2 & ")":
check BigInt.of(n2.abs()) == bi2.abs()
test $n1 & " cmp " & $n2:
check cmp(n1, n2) == cmp(bi1, bi2)
test $n1 & " < " & $n2:
check (n1 < n2) == (bi1 < bi2)
test $n1 & " > " & $n2:
check (n1 > n2) == (bi1 > bi2)
test $n1 & " == " & $n1:
check bi1 == bi1
test $n2 & " == " & $n2:
check bi2 == bi2
test $n1 & " != " & $n2:
check bi1 != bi2