added unit tests for bigint

This commit is contained in:
2023-05-29 08:43:09 +08:00
parent 42dbf1103d
commit b8f704285c
5 changed files with 73 additions and 34 deletions

63
tests/test_bigint.nim Normal file
View File

@@ -0,0 +1,63 @@
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

View File

@@ -78,7 +78,7 @@ suite "Nim linear algebra library":
var mtx = newHMatrix[float64](100, 100, arr)
var lu = mtx.clone()
let pivot = lu.lup()
var b = newHVector[float64](100)
var b = newHVector[float64](100, 0.0f)
for i in 0...len(b):
b[i] = float64(rng.rand(b.len))

View File

@@ -4,7 +4,7 @@ from mmath/smatrix import det, lu, lup, lu_det, lup, det, from_pivot, invert, id
newSMatrix, SMatrix, clone, tril, triu, lu_solve, `$`, `*`, `-`, `+`, `-=`, `+=`, `==`, `*`, norm2, newSMatrixFromArray,
gauss_jordan_high, gauss_jordan_low, transpose, squared_norm2
from mmath/svector import buildSVector, `-`, abs, norm, Svector
from mmath/pivot import SingularMatrixError
from mmath/error import SingularMatrixError
from mmath/rational import newRational, Rational, one, zero, abs, `>`, `<`, `==`, `*`, `+`, `-`, `/`, `/=`, `+=`, `sqrt`
import unittest
@@ -116,3 +116,4 @@ suite "Nim linear algebra library":
let mtx = newSMatrixFromArray[2, 5, int]([1,4,8,2,5,7,3,6,9,0])
let xpose = newSMatrixFromArray[5, 2, int]([1,7,4,3,8,6,2,9,5,0])
check(mtx.transpose() == xpose)