Skip to content

Commit

Permalink
std::jule::math: minor refactoring for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Mar 8, 2024
1 parent 9d8d354 commit 4f0da8d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 220 deletions.
145 changes: 30 additions & 115 deletions std/math/big/int_test.jule
Original file line number Diff line number Diff line change
Expand Up @@ -65,154 +65,69 @@ static cases_int_mod = [
["1010", "+", "10000000000", "-", "1111110110", "-"],
]

#test
fn test_int_add(mut t: &T) {
for _, c in cases_int_add {
let n1 = Int.from_bits(c[0], c[1] == "-") else {
fn test_common_int_op(mut &t: &T, op: str, &cases: [][]str) {
for _, c in cases {
let mut n1 = Int.from_bits(c[0], c[1] == "-") else {
t.errorf("exception occurs: {}", error)
continue
}
let n2 = Int.from_bits(c[2], c[3] == "-") else {
t.errorf("exception occurs: {}", error)
continue
}
let mut r = n1.add(n2)
match op {
| "+":
n1 += n2
| "-":
n1 -= n2
| "*":
n1 *= n2
| "/":
n1 /= n2
| "%":
n1 %= n2
}
let cr = c[4]
if r.len() != cr.len ||
r.neg && c[5] != "-" ||
!r.neg && c[5] != "+" {
t.errorf("{}{} + {}{} != {}{}", c[1], c[0], c[3], c[2], c[5], c[4])
if n1.len() != cr.len ||
n1.neg && c[5] != "-" ||
!n1.neg && c[5] != "+" {
t.errorf("{}{} {} {}{} != {}{}", c[1], c[0], op, c[3], c[2], c[5], c[4])
continue
}
for i, b in r.nat.bits {
for i, b in n1.nat.bits {
let cb = cr[cr.len - 1 - i]
if b == 0b1 && cb != '1' ||
b == 0b0 && cb != '0' {
t.errorf("{}{} + {}{} != {}{}", c[1], c[0], c[3], c[2], c[5], c[4])
t.errorf("{}{} {} {}{} != {}{}", c[1], c[0], op, c[3], c[2], c[5], c[4])
break
}
}
}
}

#test
fn test_int_add(mut t: &T) {
test_common_int_op(t, "+", cases_int_add)
}

#test
fn test_int_sub(mut t: &T) {
for _, c in cases_int_sub {
let n1 = Int.from_bits(c[0], c[1] == "-") else {
t.errorf("exception occurs: {}", error)
continue
}
let n2 = Int.from_bits(c[2], c[3] == "-") else {
t.errorf("exception occurs: {}", error)
continue
}
let mut r = n1.sub(n2)
let cr = c[4]
if r.len() != cr.len ||
r.neg && c[5] != "-" ||
!r.neg && c[5] != "+" {
t.errorf("{}{} - {}{} != {}{}", c[1], c[0], c[3], c[2], c[5], c[4])
continue
}
for i, b in r.nat.bits {
let cb = cr[cr.len - 1 - i]
if b == 0b1 && cb != '1' ||
b == 0b0 && cb != '0' {
t.errorf("{}{} - {}{} != {}{}", c[1], c[0], c[3], c[2], c[5], c[4])
break
}
}
}
test_common_int_op(t, "-", cases_int_sub)
}

#test
fn test_int_mul(mut t: &T) {
for _, c in cases_int_mul {
let n1 = Int.from_bits(c[0], c[1] == "-") else {
t.errorf("exception occurs: {}", error)
continue
}
let n2 = Int.from_bits(c[2], c[3] == "-") else {
t.errorf("exception occurs: {}", error)
continue
}
let mut r = n1.mul(n2)
let cr = c[4]
if r.len() != cr.len ||
r.neg && c[5] != "-" ||
!r.neg && c[5] != "+" {
t.errorf("{}{} * {}{} != {}{}", c[1], c[0], c[3], c[2], c[5], c[4])
continue
}
for i, b in r.nat.bits {
let cb = cr[cr.len - 1 - i]
if b == 0b1 && cb != '1' ||
b == 0b0 && cb != '0' {
t.errorf("{}{} * {}{} != {}{}", c[1], c[0], c[3], c[2], c[5], c[4])
break
}
}
}
test_common_int_op(t, "*", cases_int_mul)
}

#test
fn test_int_div(mut t: &T) {
for _, c in cases_int_div {
let n1 = Int.from_bits(c[0], c[1] == "-") else {
t.errorf("exception occurs: {}", error)
continue
}
let n2 = Int.from_bits(c[2], c[3] == "-") else {
t.errorf("exception occurs: {}", error)
continue
}
let mut r = n1.div(n2)
let cr = c[4]
if r.len() != cr.len ||
r.neg && c[5] != "-" ||
!r.neg && c[5] != "+" {
t.errorf("{}{} / {}{} != {}{}", c[1], c[0], c[3], c[2], c[5], c[4])
continue
}
for i, b in r.nat.bits {
let cb = cr[cr.len - 1 - i]
if b == 0b1 && cb != '1' ||
b == 0b0 && cb != '0' {
t.errorf("{}{} / {}{} != {}{}", c[1], c[0], c[3], c[2], c[5], c[4])
break
}
}
}
test_common_int_op(t, "/", cases_int_div)
}

#test
fn test_int_mod(mut t: &T) {
for _, c in cases_int_mod {
let n1 = Int.from_bits(c[0], c[1] == "-") else {
t.errorf("exception occurs: {}", error)
continue
}
let n2 = Int.from_bits(c[2], c[3] == "-") else {
t.errorf("exception occurs: {}", error)
continue
}
let mut r = n1.mod(n2)
let cr = c[4]
if r.len() != cr.len ||
r.neg && c[5] != "-" ||
!r.neg && c[5] != "+" {
t.errorf("{}{} % {}{} != {}{}", c[1], c[0], c[3], c[2], c[5], c[4])
continue
}
for i, b in r.nat.bits {
let cb = cr[cr.len - 1 - i]
if b == 0b1 && cb != '1' ||
b == 0b0 && cb != '0' {
t.errorf("{}{} % {}{} != {}{}", c[1], c[0], c[3], c[2], c[5], c[4])
break
}
}
}
test_common_int_op(t, "%", cases_int_mod)
}

#test
Expand Down
133 changes: 28 additions & 105 deletions std/math/big/nat_test.jule
Original file line number Diff line number Diff line change
Expand Up @@ -118,144 +118,67 @@ fn test_nat_from_bits(mut t: &T) {
}
}

#test
fn test_nat_add(mut t: &T) {
for _, c in cases_nat_add {
let n1 = Nat.from_bits(c[0]) else {
fn test_common_nat_op(mut &t: &T, op: str, &cases: [][]str) {
for _, c in cases {
let mut n1 = Nat.from_bits(c[0]) else {
t.errorf("exception occurs: {}", error)
continue
}
let n2 = Nat.from_bits(c[1]) else {
t.errorf("exception occurs: {}", error)
continue
}
let mut r = n1.add(n2)
match op {
| "+":
n1 += n2
| "-":
n1 -= n2
| "*":
n1 *= n2
| "/":
n1 /= n2
| "%":
n1 %= n2
}
let cr = c[2]
if r.len() != cr.len {
t.errorf("{} + {} != {}", c[0], c[1], cr)
if n1.len() != cr.len {
t.errorf("{} {} {} != {}", c[0], op, c[1], cr)
continue
}
for i, b in r.bits {
for i, b in n1.bits {
let cb = cr[cr.len - 1 - i]
if b == 0b1 && cb != '1' ||
b == 0b0 && cb != '0' {
t.errorf("{} + {} != {}", c[0], c[1], cr)
t.errorf("{} {} {} != {}", c[0], op, c[1], cr)
break
}
}
}
}

#test
fn test_nat_add(mut t: &T) {
test_common_nat_op(t, "+", cases_nat_add)
}

#test
fn test_nat_sub(mut t: &T) {
for _, c in cases_nat_sub {
let n1 = Nat.from_bits(c[0]) else {
t.errorf("exception occurs: {}", error)
continue
}
let n2 = Nat.from_bits(c[1]) else {
t.errorf("exception occurs: {}", error)
continue
}
let mut r = n1.sub(n2)
let cr = c[2]
if r.len() != cr.len {
t.errorf("{} - {} != {}", c[0], c[1], cr)
continue
}
for i, b in r.bits {
let cb = cr[cr.len - 1 - i]
if b == 0b1 && cb != '1' ||
b == 0b0 && cb != '0' {
t.errorf("{} - {} != {}", c[0], c[1], cr)
break
}
}
}
test_common_nat_op(t, "-", cases_nat_sub)
}

#test
fn test_nat_mul(mut t: &T) {
for _, c in cases_nat_mul {
let n1 = Nat.from_bits(c[0]) else {
t.errorf("exception occurs: {}", error)
continue
}
let n2 = Nat.from_bits(c[1]) else {
t.errorf("exception occurs: {}", error)
continue
}
let mut r = n1.mul(n2)
let cr = c[2]
if r.len() != cr.len {
t.errorf("{} * {} != {}", c[0], c[1], cr)
continue
}
for i, b in r.bits {
let cb = cr[cr.len - 1 - i]
if b == 0b1 && cb != '1' ||
b == 0b0 && cb != '0' {
t.errorf("{} * {} != {}", c[0], c[1], cr)
break
}
}
}
test_common_nat_op(t, "*", cases_nat_mul)
}

#test
fn test_nat_div(mut t: &T) {
for _, c in cases_nat_div {
let n1 = Nat.from_bits(c[0]) else {
t.errorf("exception occurs: {}", error)
continue
}
let n2 = Nat.from_bits(c[1]) else {
t.errorf("exception occurs: {}", error)
continue
}
let mut r = n1.div(n2)
let cr = c[2]
if r.len() != cr.len {
t.errorf("{} / {} != {}", c[0], c[1], cr)
continue
}
for i, b in r.bits {
let cb = cr[cr.len - 1 - i]
if b == 0b1 && cb != '1' ||
b == 0b0 && cb != '0' {
t.errorf("{} / {} != {}", c[0], c[1], cr)
break
}
}
}
test_common_nat_op(t, "/", cases_nat_div)
}

#test
fn test_nat_mod(mut t: &T) {
for _, c in cases_nat_mod {
let n1 = Nat.from_bits(c[0]) else {
t.errorf("exception occurs: {}", error)
continue
}
let n2 = Nat.from_bits(c[1]) else {
t.errorf("exception occurs: {}", error)
continue
}
let mut r = n1.mod(n2)
let cr = c[2]
if r.len() != cr.len {
t.errorf("{} % {} != {}", c[0], c[1], cr)
continue
}
for i, b in r.bits {
let cb = cr[cr.len - 1 - i]
if b == 0b1 && cb != '1' ||
b == 0b0 && cb != '0' {
t.errorf("{} % {} != {}", c[0], c[1], cr)
break
}
}
}
test_common_nat_op(t, "%", cases_nat_mod)
}

#test
Expand Down

0 comments on commit 4f0da8d

Please sign in to comment.