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

View File

@@ -260,3 +260,6 @@ proc `>`*(n1 : BigInt, n2 : BigInt) : bool = cmp(n1, n2) > 0
proc `==`*(n1 : BigInt, n2 : BigInt) : bool = cmp(n1, n2) == 0 proc `==`*(n1 : BigInt, n2 : BigInt) : bool = cmp(n1, n2) == 0
proc `<=`*(n1 : BigInt, n2 : BigInt) : bool = cmp(n1, n2) <= 0 proc `<=`*(n1 : BigInt, n2 : BigInt) : bool = cmp(n1, n2) <= 0
proc `>=`*(n1 : BigInt, n2 : BigInt) : bool = cmp(n1, n2) >= 0 proc `>=`*(n1 : BigInt, n2 : BigInt) : bool = cmp(n1, n2) >= 0
proc `!=`*(n1 : BigInt, n2 : BigInt) : bool = cmp(n1, n2) != 0
proc `of`*(_ : typedesc[BigInt], n : int64) : BigInt = newBigInt(n)

View File

@@ -1,3 +1,7 @@
import constant
export constant
proc gcd*[T](a : T, b : T) : T = proc gcd*[T](a : T, b : T) : T =
var n1 = a var n1 = a
var n2 = b var n2 = b
@@ -30,38 +34,6 @@ proc simplify*[T](self : var Rational[T]) : void =
proc newRational*[T](num : T) : Rational[T] = Rational[T](num: num, den: T(1)) proc newRational*[T](num : T) : Rational[T] = Rational[T](num: num, den: T(1))
proc newRational*[T](num : T, den: T) : Rational[T] = Rational[T](num: num, den: den) proc newRational*[T](num : T, den: T) : Rational[T] = Rational[T](num: num, den: den)
proc one*[T : int64](_: type[T]) : T = 1.T
proc zero*[T : int64](_: type[T]) : T = 0.T
proc one*[T : uint64](_: type[T]) : T = 1.T
proc zero*[T : uint64](_: type[T]) : T = 0.T
proc one*[T : int](_: type[T]) : T = 1.T
proc zero*[T : int](_: type[T]) : T = 0.T
proc one*[T : uint](_: type[T]) : T = 1.T
proc zero*[T : uint](_: type[T]) : T = 0.T
proc one*[T : int16](_: type[T]) : T = 1.T
proc zero*[T : int16](_: type[T]) : T = 0.T
proc one*[T : uint16](_: type[T]) : T = 1.T
proc zero*[T : uint16](_: type[T]) : T = 0.T
proc one*[T : int8](_: type[T]) : T = 1.T
proc zero*[T : int8](_: type[T]) : T = 0.T
proc one*[T : uint8](_: type[T]) : T = 1.T
proc zero*[T : uint8](_: type[T]) : T = 0.T
proc zero*[T](_: type[Rational[T]]): Rational[T] = newRational(T.zero, T.one) proc zero*[T](_: type[Rational[T]]): Rational[T] = newRational(T.zero, T.one)
proc one*[T](_: type[Rational[T]]): Rational[T] = newRational(T.one, T.one) proc one*[T](_: type[Rational[T]]): Rational[T] = newRational(T.one, T.one)

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 mtx = newHMatrix[float64](100, 100, arr)
var lu = mtx.clone() var lu = mtx.clone()
let pivot = lu.lup() let pivot = lu.lup()
var b = newHVector[float64](100) var b = newHVector[float64](100, 0.0f)
for i in 0...len(b): for i in 0...len(b):
b[i] = float64(rng.rand(b.len)) 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, newSMatrix, SMatrix, clone, tril, triu, lu_solve, `$`, `*`, `-`, `+`, `-=`, `+=`, `==`, `*`, norm2, newSMatrixFromArray,
gauss_jordan_high, gauss_jordan_low, transpose, squared_norm2 gauss_jordan_high, gauss_jordan_low, transpose, squared_norm2
from mmath/svector import buildSVector, `-`, abs, norm, Svector 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` from mmath/rational import newRational, Rational, one, zero, abs, `>`, `<`, `==`, `*`, `+`, `-`, `/`, `/=`, `+=`, `sqrt`
import unittest 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 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]) let xpose = newSMatrixFromArray[5, 2, int]([1,7,4,3,8,6,2,9,5,0])
check(mtx.transpose() == xpose) check(mtx.transpose() == xpose)