From 4388bc894114ed31ad1f3bf671a02ce7768c82e8 Mon Sep 17 00:00:00 2001 From: Luke Kenneth Casson Leighton Date: Mon, 18 Feb 2019 17:22:07 +0000 Subject: [PATCH] add div unit tests --- src/add/test_div.py | 148 ++++++++++++++++++++++++++---------- src/add/unit_test_double.py | 1 + src/add/unit_test_single.py | 1 + 3 files changed, 109 insertions(+), 41 deletions(-) diff --git a/src/add/test_div.py b/src/add/test_div.py index cc0b2202..80311123 100644 --- a/src/add/test_div.py +++ b/src/add/test_div.py @@ -1,49 +1,16 @@ +import sys +from random import randint +from random import seed + from nmigen import Module, Signal from nmigen.compat.sim import run_simulation from nmigen_div_experiment import FPDIV -class ORGate: - def __init__(self): - self.a = Signal() - self.b = Signal() - self.x = Signal() - - def get_fragment(self, platform=None): - - m = Module() - m.d.comb += self.x.eq(self.a | self.b) - - return m - -def check_case(dut, a, b, z): - yield dut.in_a.v.eq(a) - yield dut.in_a.stb.eq(1) - yield - yield - a_ack = (yield dut.in_a.ack) - assert a_ack == 0 - yield dut.in_b.v.eq(b) - yield dut.in_b.stb.eq(1) - b_ack = (yield dut.in_b.ack) - assert b_ack == 0 - - while True: - yield - out_z_stb = (yield dut.out_z.stb) - if not out_z_stb: - continue - yield dut.in_a.stb.eq(0) - yield dut.in_b.stb.eq(0) - yield dut.out_z.ack.eq(1) - yield - yield dut.out_z.ack.eq(0) - yield - yield - break - - out_z = yield dut.out_z.v - assert out_z == z, "Output z 0x%x not equal to expected 0x%x" % (out_z, z) +from unit_test_single import (get_mantissa, get_exponent, get_sign, is_nan, + is_inf, is_pos_inf, is_neg_inf, + match, get_case, check_case, run_test) + def testbench(dut): yield from check_case(dut, 0x40000000, 0x3F800000, 0x40000000) @@ -56,6 +23,105 @@ def testbench(dut): yield from check_case(dut, 0x7F7FFCFF, 0x70200201, 0x4ECCC7D5) yield from check_case(dut, 0x70200201, 0x7F7FFCFF, 0x302003E2) + count = 0 + + #regression tests + stimulus_a = [0xbf9b1e94, 0x34082401, 0x5e8ef81, 0x5c75da81, 0x2b017] + stimulus_b = [0xc038ed3a, 0xb328cd45, 0x114f3db, 0x2f642a39, 0xff3807ab] + yield from run_test(dut, stimulus_a, stimulus_b) + count += len(stimulus_a) + print (count, "vectors passed") + + #corner cases + from itertools import permutations + stimulus_a = [i[0] for i in permutations([0x80000000, 0x00000000, 0x7f800000, 0xff800000, 0x7fc00000, 0xffc00000], 2)] + stimulus_b = [i[1] for i in permutations([0x80000000, 0x00000000, 0x7f800000, 0xff800000, 0x7fc00000, 0xffc00000], 2)] + yield from run_test(dut, stimulus_a, stimulus_b) + count += len(stimulus_a) + print (count, "vectors passed") + + #edge cases + stimulus_a = [0x80000000 for i in xrange(1000)] + stimulus_b = [randint(0, 1<<32) for i in xrange(1000)] + yield from run_test(dut, stimulus_a, stimulus_b) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_a = [0x00000000 for i in xrange(1000)] + stimulus_b = [randint(0, 1<<32) for i in xrange(1000)] + yield from run_test(dut, stimulus_a, stimulus_b) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_b = [0x80000000 for i in xrange(1000)] + stimulus_a = [randint(0, 1<<32) for i in xrange(1000)] + yield from run_test(dut, stimulus_a, stimulus_b) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_b = [0x00000000 for i in xrange(1000)] + stimulus_a = [randint(0, 1<<32) for i in xrange(1000)] + yield from run_test(dut, stimulus_a, stimulus_b) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_a = [0x7F800000 for i in xrange(1000)] + stimulus_b = [randint(0, 1<<32) for i in xrange(1000)] + yield from run_test(dut, stimulus_a, stimulus_b) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_a = [0xFF800000 for i in xrange(1000)] + stimulus_b = [randint(0, 1<<32) for i in xrange(1000)] + yield from run_test(dut, stimulus_a, stimulus_b) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_b = [0x7F800000 for i in xrange(1000)] + stimulus_a = [randint(0, 1<<32) for i in xrange(1000)] + yield from run_test(dut, stimulus_a, stimulus_b) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_b = [0xFF800000 for i in xrange(1000)] + stimulus_a = [randint(0, 1<<32) for i in xrange(1000)] + yield from run_test(dut, stimulus_a, stimulus_b) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_a = [0x7FC00000 for i in xrange(1000)] + stimulus_b = [randint(0, 1<<32) for i in xrange(1000)] + yield from run_test(dut, stimulus_a, stimulus_b) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_a = [0xFFC00000 for i in xrange(1000)] + stimulus_b = [randint(0, 1<<32) for i in xrange(1000)] + yield from run_test(dut, stimulus_a, stimulus_b) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_b = [0x7FC00000 for i in xrange(1000)] + stimulus_a = [randint(0, 1<<32) for i in xrange(1000)] + yield from run_test(dut, stimulus_a, stimulus_b) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_b = [0xFFC00000 for i in xrange(1000)] + stimulus_a = [randint(0, 1<<32) for i in xrange(1000)] + yield from run_test(dut, stimulus_a, stimulus_b) + count += len(stimulus_a) + print (count, "vectors passed") + + #seed(0) + for i in xrange(100000): + stimulus_a = [randint(0, 1<<32) for i in xrange(1000)] + stimulus_b = [randint(0, 1<<32) for i in xrange(1000)] + yield from run_test(dut, stimulus_a, stimulus_b) + count += 1000 + print (count, "random vectors passed") + + if __name__ == '__main__': dut = FPDIV(width=32) run_simulation(dut, testbench(dut), vcd_name="test_div.vcd") diff --git a/src/add/unit_test_double.py b/src/add/unit_test_double.py index b5a48a20..3d69d8e0 100644 --- a/src/add/unit_test_double.py +++ b/src/add/unit_test_double.py @@ -1,3 +1,4 @@ +import sys from sfpy import Float64 def get_mantissa(x): diff --git a/src/add/unit_test_single.py b/src/add/unit_test_single.py index 328da10c..ca33efb1 100644 --- a/src/add/unit_test_single.py +++ b/src/add/unit_test_single.py @@ -1,3 +1,4 @@ +import sys from sfpy import Float32 def get_mantissa(x): -- 2.30.2