2020-12-22 17:38:17 +03:00
|
|
|
import math.big
|
2020-03-10 21:31:01 +03:00
|
|
|
|
2020-12-14 12:53:18 +03:00
|
|
|
fn test_new_big() {
|
2020-03-10 21:31:01 +03:00
|
|
|
n := big.new()
|
2020-12-14 12:53:18 +03:00
|
|
|
assert sizeof(big.Number) == 128
|
2020-03-10 21:31:01 +03:00
|
|
|
assert n.hexstr() == '0'
|
|
|
|
}
|
|
|
|
|
2020-12-14 12:53:18 +03:00
|
|
|
fn test_from_int() {
|
2020-03-10 21:31:01 +03:00
|
|
|
assert big.from_int(255).hexstr() == 'ff'
|
|
|
|
assert big.from_int(127).hexstr() == '7f'
|
|
|
|
assert big.from_int(1024).hexstr() == '400'
|
|
|
|
assert big.from_int(2147483647).hexstr() == '7fffffff'
|
|
|
|
assert big.from_int(-1).hexstr() == 'ffffffffffffffff'
|
|
|
|
}
|
|
|
|
|
2020-12-14 12:53:18 +03:00
|
|
|
fn test_from_u64() {
|
2020-03-10 21:31:01 +03:00
|
|
|
assert big.from_u64(255).hexstr() == 'ff'
|
|
|
|
assert big.from_u64(127).hexstr() == '7f'
|
|
|
|
assert big.from_u64(1024).hexstr() == '400'
|
|
|
|
assert big.from_u64(4294967295).hexstr() == 'ffffffff'
|
|
|
|
assert big.from_u64(4398046511104).hexstr() == '40000000000'
|
|
|
|
assert big.from_u64(-1).hexstr() == 'ffffffffffffffff'
|
|
|
|
}
|
|
|
|
|
2020-12-14 12:53:18 +03:00
|
|
|
fn test_plus() {
|
2021-01-15 04:27:19 +03:00
|
|
|
mut a := big.from_u64(2)
|
2020-03-10 21:31:01 +03:00
|
|
|
b := big.from_u64(3)
|
|
|
|
c := a + b
|
|
|
|
assert c.hexstr() == '5'
|
|
|
|
assert (big.from_u64(1024) + big.from_u64(1024)).hexstr() == '800'
|
2021-01-15 04:27:19 +03:00
|
|
|
a += b
|
|
|
|
assert a.hexstr() == '5'
|
2021-07-20 15:08:32 +03:00
|
|
|
a.inc()
|
|
|
|
assert a.hexstr() == '6'
|
|
|
|
a.dec()
|
|
|
|
a.dec()
|
|
|
|
assert a.hexstr() == '4'
|
2020-03-10 21:31:01 +03:00
|
|
|
}
|
|
|
|
|
2020-12-14 12:53:18 +03:00
|
|
|
fn test_minus() {
|
2020-03-10 21:31:01 +03:00
|
|
|
a := big.from_u64(2)
|
2021-01-15 04:27:19 +03:00
|
|
|
mut b := big.from_u64(3)
|
2020-03-10 21:31:01 +03:00
|
|
|
c := b - a
|
|
|
|
assert c.hexstr() == '1'
|
|
|
|
e := big.from_u64(1024)
|
|
|
|
ee := e - e
|
|
|
|
assert ee.hexstr() == '0'
|
2021-01-15 04:27:19 +03:00
|
|
|
b -= a
|
|
|
|
assert b.hexstr() == '1'
|
2020-03-10 21:31:01 +03:00
|
|
|
}
|
|
|
|
|
2020-12-14 12:53:18 +03:00
|
|
|
fn test_divide() {
|
2020-03-10 21:31:01 +03:00
|
|
|
a := big.from_u64(2)
|
2021-01-15 04:27:19 +03:00
|
|
|
mut b := big.from_u64(3)
|
2020-03-10 21:31:01 +03:00
|
|
|
c := b / a
|
|
|
|
assert c.hexstr() == '1'
|
2020-12-14 12:53:18 +03:00
|
|
|
assert (b % a).hexstr() == '1'
|
2020-03-10 21:31:01 +03:00
|
|
|
e := big.from_u64(1024) // dec(1024) == hex(0x400)
|
|
|
|
ee := e / e
|
|
|
|
assert ee.hexstr() == '1'
|
|
|
|
assert (e / a).hexstr() == '200'
|
2020-12-14 12:53:18 +03:00
|
|
|
assert (e / (a * a)).hexstr() == '100'
|
2021-01-15 04:27:19 +03:00
|
|
|
b /= a
|
|
|
|
assert b.hexstr() == '1'
|
2020-03-10 21:31:01 +03:00
|
|
|
}
|
|
|
|
|
2020-12-14 12:53:18 +03:00
|
|
|
fn test_multiply() {
|
2021-01-15 04:27:19 +03:00
|
|
|
mut a := big.from_u64(2)
|
2020-03-10 21:31:01 +03:00
|
|
|
b := big.from_u64(3)
|
|
|
|
c := b * a
|
|
|
|
assert c.hexstr() == '6'
|
|
|
|
e := big.from_u64(1024)
|
|
|
|
e2 := e * e
|
|
|
|
e4 := e2 * e2
|
|
|
|
e8 := e2 * e2 * e2 * e2
|
|
|
|
e9 := e8 + big.from_u64(1)
|
2020-12-14 12:53:18 +03:00
|
|
|
d := ((e9 * e9) + b) * c
|
2020-03-10 21:31:01 +03:00
|
|
|
assert e4.hexstr() == '10000000000'
|
|
|
|
assert e8.hexstr() == '100000000000000000000'
|
|
|
|
assert e9.hexstr() == '100000000000000000001'
|
|
|
|
assert d.hexstr() == '60000000000000000000c00000000000000000018'
|
2021-01-15 04:27:19 +03:00
|
|
|
a *= b
|
|
|
|
assert a.hexstr() == '6'
|
2020-03-10 21:31:01 +03:00
|
|
|
}
|
|
|
|
|
2020-12-14 12:53:18 +03:00
|
|
|
fn test_mod() {
|
|
|
|
assert (big.from_u64(13) % big.from_u64(10)).int() == 3
|
|
|
|
assert (big.from_u64(13) % big.from_u64(9)).int() == 4
|
|
|
|
assert (big.from_u64(7) % big.from_u64(5)).int() == 2
|
2020-03-10 21:31:01 +03:00
|
|
|
}
|
|
|
|
|
2020-12-16 04:23:02 +03:00
|
|
|
fn test_from_str() {
|
2020-12-22 10:44:59 +03:00
|
|
|
assert big.from_string('9870123').str() == '9870123'
|
|
|
|
assert big.from_string('').str() == '0'
|
|
|
|
assert big.from_string('0').str() == '0'
|
|
|
|
assert big.from_string('1').str() == '1'
|
|
|
|
for i := 1; i < 307; i += 61 {
|
|
|
|
input := '9'.repeat(i)
|
|
|
|
out := big.from_string(input).str()
|
|
|
|
// eprintln('>> i: $i input: $input.str()')
|
|
|
|
// eprintln('>> i: $i out: $out.str()')
|
|
|
|
assert input == out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_from_hex_str() {
|
|
|
|
assert big.from_hex_string('0x123').hexstr() == '123'
|
2020-12-16 04:23:02 +03:00
|
|
|
for i in 1 .. 33 {
|
|
|
|
input := 'e'.repeat(i)
|
2020-12-22 10:44:59 +03:00
|
|
|
out := big.from_hex_string(input).hexstr()
|
2020-12-16 04:23:02 +03:00
|
|
|
assert input == out
|
|
|
|
}
|
|
|
|
assert big.from_string('0').hexstr() == '0'
|
|
|
|
}
|
|
|
|
|
2020-12-14 12:53:18 +03:00
|
|
|
fn test_str() {
|
|
|
|
assert big.from_u64(255).str() == '255'
|
|
|
|
assert big.from_u64(127).str() == '127'
|
|
|
|
assert big.from_u64(1024).str() == '1024'
|
|
|
|
assert big.from_u64(4294967295).str() == '4294967295'
|
|
|
|
assert big.from_u64(4398046511104).str() == '4398046511104'
|
2021-04-14 12:47:24 +03:00
|
|
|
assert big.from_int(int(4294967295)).str() == '18446744073709551615'
|
2020-12-14 12:53:18 +03:00
|
|
|
assert big.from_int(-1).str() == '18446744073709551615'
|
2021-01-23 11:33:22 +03:00
|
|
|
assert big.from_hex_string('e'.repeat(80)).str() == '1993587900192849410235353592424915306962524220866209251950572167300738410728597846688097947807470'
|
2020-12-14 12:53:18 +03:00
|
|
|
}
|
2020-03-10 21:31:01 +03:00
|
|
|
|
2020-12-14 12:53:18 +03:00
|
|
|
fn test_factorial() {
|
|
|
|
f5 := big.factorial(big.from_u64(5))
|
2020-03-10 21:31:01 +03:00
|
|
|
assert f5.hexstr() == '78'
|
2020-12-14 12:53:18 +03:00
|
|
|
f100 := big.factorial(big.from_u64(100))
|
2021-01-23 11:33:22 +03:00
|
|
|
assert f100.hexstr() == '1b30964ec395dc24069528d54bbda40d16e966ef9a70eb21b5b2943a321cdf10391745570cca9420c6ecb3b72ed2ee8b02ea2735c61a000000000000000000000000'
|
2020-03-10 21:31:01 +03:00
|
|
|
}
|
2021-07-19 16:42:37 +03:00
|
|
|
|
|
|
|
fn trimbytes(n int, x []byte) []byte {
|
|
|
|
mut res := x.clone()
|
|
|
|
res.trim(n)
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_bytes() {
|
|
|
|
assert big.from_int(0).bytes().len == 128
|
|
|
|
assert big.from_hex_string('e'.repeat(100)).bytes().len == 128
|
|
|
|
assert trimbytes(3, big.from_int(1).bytes()) == [byte(0x01), 0x00, 0x00]
|
|
|
|
assert trimbytes(3, big.from_int(1024).bytes()) == [byte(0x00), 0x04, 0x00]
|
|
|
|
assert trimbytes(3, big.from_int(1048576).bytes()) == [byte(0x00), 0x00, 0x10]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_bytes_trimmed() {
|
|
|
|
assert big.from_int(0).bytes_trimmed().len == 0
|
|
|
|
assert big.from_hex_string('AB'.repeat(50)).bytes_trimmed().len == 50
|
|
|
|
assert big.from_int(1).bytes_trimmed() == [byte(0x01)]
|
|
|
|
assert big.from_int(1024).bytes_trimmed() == [byte(0x00), 0x04]
|
|
|
|
assert big.from_int(1048576).bytes_trimmed() == [byte(0x00), 0x00, 0x10]
|
|
|
|
}
|