Add horrible tests

This commit is contained in:
Mikko Ahlroth 2024-02-13 22:07:38 +02:00
parent 559ade40bd
commit a289a9587d
3 changed files with 130 additions and 2 deletions

View file

@ -76,7 +76,11 @@ pub fn eject(op: interface.OpResult(Int128)) {
}
fn limits() {
let min = bigi.power(bigi.from_int(-2), bigi.from_int(127))
let min =
bigi.multiply(
bigi.power(bigi.from_int(2), bigi.from_int(127)),
bigi.from_int(-1),
)
let max = bigi.subtract(bigi.absolute(min), bigi.from_int(-1))
interface.overflowable_limits(min, max)

View file

@ -76,7 +76,11 @@ pub fn eject(op: interface.OpResult(Int64)) {
}
fn limits() {
let min = bigi.power(bigi.from_int(-2), bigi.from_int(64))
let min =
bigi.multiply(
bigi.power(bigi.from_int(2), bigi.from_int(63)),
bigi.from_int(-1),
)
let max = bigi.subtract(bigi.absolute(min), bigi.from_int(-1))
interface.overflowable_limits(min, max)

View file

@ -0,0 +1,120 @@
import gleeunit/should
import bigi.{type BigInt}
import ranged_int/interface
import ranged_int/builtin/uint8
import ranged_int/builtin/uint16
import ranged_int/builtin/uint32
import ranged_int/builtin/uint64
import ranged_int/builtin/uint128
import ranged_int/builtin/int8
import ranged_int/builtin/int16
import ranged_int/builtin/int32
import ranged_int/builtin/int64
import ranged_int/builtin/int128
pub fn absolute_test() {
let input = bigi.from_int(-19)
let expected = bigi.from_int(19)
unary(int8.from_bigint, int8.absolute)(input, expected)
unary(int16.from_bigint, int16.absolute)(input, expected)
unary(int32.from_bigint, int32.absolute)(input, expected)
unary(int64.from_bigint, int64.absolute)(input, expected)
unary(int128.from_bigint, int128.absolute)(input, expected)
}
pub fn add_test() {
let input1 = bigi.from_int(1)
let input2 = bigi.from_int(12)
let expected = bigi.from_int(13)
binary(int8.from_bigint, int8.add)(
input1,
input2,
should.equal(_, int8.from_bigint(expected)),
)
binary(int16.from_bigint, int16.add)(
input1,
input2,
should.equal(_, int16.from_bigint(expected)),
)
binary(int32.from_bigint, int32.add)(
input1,
input2,
should.equal(_, int32.from_bigint(expected)),
)
binary(int64.from_bigint, int64.add)(
input1,
input2,
should.equal(_, int64.from_bigint(expected)),
)
binary(int128.from_bigint, int128.add)(
input1,
input2,
should.equal(_, int128.from_bigint(expected)),
)
binary(uint8.from_bigint, uint8.add)(
input1,
input2,
should.equal(_, uint8.from_bigint(expected)),
)
binary(uint16.from_bigint, uint16.add)(
input1,
input2,
should.equal(_, uint16.from_bigint(expected)),
)
binary(uint32.from_bigint, uint32.add)(
input1,
input2,
should.equal(_, uint32.from_bigint(expected)),
)
binary(uint64.from_bigint, uint64.add)(
input1,
input2,
should.equal(_, uint64.from_bigint(expected)),
)
binary(uint128.from_bigint, uint128.add)(
input1,
input2,
should.equal(_, uint128.from_bigint(expected)),
)
}
pub fn add_overflow_test() {
let input1 = bigi.zero()
let input2 = bigi.power(bigi.from_int(2), bigi.from_int(1024))
binary(int8.from_bigint, int8.add)(input1, input2, should.be_error)
binary(int16.from_bigint, int16.add)(input1, input2, should.be_error)
binary(int32.from_bigint, int32.add)(input1, input2, should.be_error)
binary(int64.from_bigint, int64.add)(input1, input2, should.be_error)
binary(int128.from_bigint, int128.add)(input1, input2, should.be_error)
binary(uint8.from_bigint, uint8.add)(input1, input2, should.be_error)
binary(uint16.from_bigint, uint16.add)(input1, input2, should.be_error)
binary(uint32.from_bigint, uint32.add)(input1, input2, should.be_error)
binary(uint64.from_bigint, uint64.add)(input1, input2, should.be_error)
binary(uint128.from_bigint, uint128.add)(input1, input2, should.be_error)
}
fn unary(
from_bigint: fn(BigInt) -> Result(a, b),
fun: fn(a) -> interface.OpResult(a),
) {
fn(val: BigInt, expected: BigInt) {
let assert Ok(val) = from_bigint(val)
let assert Ok(expected) = from_bigint(expected)
let res = fun(val)
should.equal(res, Ok(expected))
}
}
fn binary(
from_bigint: fn(BigInt) -> Result(a, b),
fun: fn(a, BigInt) -> interface.OpResult(a),
) {
fn(val1: BigInt, val2: BigInt, check: fn(interface.OpResult(a)) -> any) {
let assert Ok(val1) = from_bigint(val1)
let res = fun(val1, val2)
check(res)
}
}