Skip to content

Commit bbcfc56

Browse files
committed
Add test cases for tuple utilites (generated by Claude)
1 parent 06d3552 commit bbcfc56

File tree

8 files changed

+398
-0
lines changed

8 files changed

+398
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package AppendTuple where
2+
3+
-- Test the AppendTuple type class
4+
5+
{-# verilog sysAppendTuple #-}
6+
sysAppendTuple :: Module Empty
7+
sysAppendTuple =
8+
module
9+
counter :: Reg (UInt 5) <- mkReg 0
10+
11+
rules
12+
"test0": when counter == 0 ==> action
13+
$display "=== Testing AppendTuple ==="
14+
-- Test appendTuple with two empty tuples
15+
let u1 :: ()
16+
u1 = ()
17+
let u2 :: ()
18+
u2 = ()
19+
let result :: ()
20+
result = appendTuple u1 u2
21+
$display "appendTuple((), ()) = () [unit type]"
22+
counter := counter + 1
23+
24+
"test1": when counter == 1 ==> action
25+
-- Test appendTuple with unit type (left)
26+
let t1 :: (Bool, Int 8)
27+
t1 = (True, 42)
28+
let t2 :: (Bool, Int 8)
29+
t2 = appendTuple () t1
30+
$display "appendTuple((), (True, 42)) = (%b, %0d)" (tpl_1 t2) (tpl_2 t2)
31+
counter := counter + 1
32+
33+
"test2": when counter == 2 ==> action
34+
-- Test appendTuple with unit type (right)
35+
let t1 :: (Bool, Int 8)
36+
t1 = (False, negate 10)
37+
let t2 :: (Bool, Int 8)
38+
t2 = appendTuple t1 ()
39+
$display "appendTuple((False, -10), ()) = (%b, %0d)" (tpl_1 t2) (tpl_2 t2)
40+
counter := counter + 1
41+
42+
"test3": when counter == 3 ==> action
43+
-- Test append two 2-tuples to create 4-tuple
44+
let t1 :: (Bool, Int 8)
45+
t1 = (True, 5)
46+
let t2 :: (UInt 4, Bit 3)
47+
t2 = (12, 0b101)
48+
let t4 :: (Bool, Int 8, UInt 4, Bit 3)
49+
t4 = appendTuple t1 t2
50+
$display "appendTuple((True, 5), (12, 0b101)) = (%b, %0d, %0d, %b)" (tpl_1 t4) (tpl_2 t4) (tpl_3 t4) (tpl_4 t4)
51+
counter := counter + 1
52+
53+
"test4": when counter == 4 ==> action
54+
-- Test append single element and 2-tuple to create 3-tuple
55+
let b :: Bool
56+
b = False
57+
let t2 :: (Int 8, UInt 4)
58+
t2 = (negate 3, 7)
59+
let t3 :: (Bool, Int 8, UInt 4)
60+
t3 = appendTuple b t2
61+
$display "appendTuple(False, (-3, 7)) = (%b, %0d, %0d)" (tpl_1 t3) (tpl_2 t3) (tpl_3 t3)
62+
counter := counter + 1
63+
64+
"test5": when counter == 5 ==> action
65+
-- Test append 3-tuple and single element to create 4-tuple
66+
let t3 :: (Bool, Int 8, UInt 4)
67+
t3 = (True, 20, 8)
68+
let b :: Bit 5
69+
b = 0b11010
70+
let t4 :: (Bool, Int 8, UInt 4, Bit 5)
71+
t4 = appendTuple t3 b
72+
$display "appendTuple((True, 20, 8), 0b11010) = (%b, %0d, %0d, %b)" (tpl_1 t4) (tpl_2 t4) (tpl_3 t4) (tpl_4 t4)
73+
counter := counter + 1
74+
75+
"test6": when counter == 6 ==> action
76+
-- Test append 2-tuple and 3-tuple to create 5-tuple
77+
let t2 :: (Bool, Int 8)
78+
t2 = (False, 15)
79+
let t3 :: (UInt 4, Bit 3, Int 16)
80+
t3 = (6, 0b111, negate 100)
81+
let t5 :: (Bool, Int 8, UInt 4, Bit 3, Int 16)
82+
t5 = appendTuple t2 t3
83+
$display "appendTuple((False, 15), (6, 0b111, -100)) = (%b, %0d, %0d, %b, %0d)" (tpl_1 t5) (tpl_2 t5) (tpl_3 t5) (tpl_4 t5) (tpl_5 t5)
84+
$display ""
85+
$display "=== Testing splitTuple ==="
86+
counter := counter + 1
87+
88+
"test7": when counter == 7 ==> action
89+
-- Test splitTuple: 4-tuple into 2-tuple and 2-tuple
90+
let t4 :: (Bool, Int 8, UInt 4, Bit 3)
91+
t4 = (True, negate 15, 9, 0b110)
92+
let split :: ((Bool, Int 8), (UInt 4, Bit 3))
93+
split = splitTuple t4
94+
(t1, t2) = split
95+
$display "splitTuple((True, -15, 9, 0b110)) = ((%b, %0d), (%0d, %b))" (tpl_1 t1) (tpl_2 t1) (tpl_1 t2) (tpl_2 t2)
96+
counter := counter + 1
97+
98+
"test8": when counter == 8 ==> action
99+
-- Test splitTuple: 3-tuple into single and 2-tuple
100+
let t3 :: (Bool, Int 8, UInt 4)
101+
t3 = (False, 33, 15)
102+
let split :: (Bool, (Int 8, UInt 4))
103+
split = splitTuple t3
104+
(b, t2) = split
105+
$display "splitTuple((False, 33, 15)) = (%b, (%0d, %0d))" b (tpl_1 t2) (tpl_2 t2)
106+
counter := counter + 1
107+
108+
"test9": when counter == 9 ==> action
109+
-- Test splitTuple: 5-tuple into 2-tuple and 3-tuple
110+
let t5 :: (Bool, Int 8, UInt 4, Bit 3, Int 16)
111+
t5 = (True, 50, 11, 0b001, negate 200)
112+
let split :: ((Bool, Int 8), (UInt 4, Bit 3, Int 16))
113+
split = splitTuple t5
114+
(t2, t3) = split
115+
$display "splitTuple((True, 50, 11, 0b001, -200)) = ((%b, %0d), (%0d, %b, %0d))" (tpl_1 t2) (tpl_2 t2) (tpl_1 t3) (tpl_2 t3) (tpl_3 t3)
116+
$display ""
117+
$display "=== Round-trip test ==="
118+
counter := counter + 1
119+
120+
"test10": when counter == 10 ==> action
121+
-- Test appendTuple and splitTuple round-trip
122+
let t1 :: (Bool, Int 8)
123+
t1 = (True, 42)
124+
let t2 :: (UInt 4, Bit 5)
125+
t2 = (13, 0b10101)
126+
let t4 :: (Bool, Int 8, UInt 4, Bit 5)
127+
t4 = appendTuple t1 t2
128+
let split :: ((Bool, Int 8), (UInt 4, Bit 5))
129+
split = splitTuple t4
130+
(r1, r2) = split
131+
$display "appendTuple/splitTuple round-trip: ((%b, %0d), (%0d, %b))" (tpl_1 r1) (tpl_2 r1) (tpl_1 r2) (tpl_2 r2)
132+
$display ""
133+
$display "All AppendTuple tests passed"
134+
$finish 0

testsuite/bsc.lib/Prelude/Prelude.exp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ test_c_veri_bsv Eq2
66
compile_verilog_fail_no_internal_error Eq3
77

88
test_c_veri_bs_modules TuplePack {}
9+
test_c_veri_bs_modules TupleSize {}
10+
test_c_veri_bs_modules AppendTuple {}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package TupleSize where
2+
3+
-- Test the TupleSize type class by using it in proviso constraints
4+
5+
-- Helper function that requires TupleSize constraint
6+
tupleSize :: (TupleSize t n) => t -> Integer
7+
tupleSize _ = valueOf n
8+
9+
{-# verilog sysTupleSize #-}
10+
sysTupleSize :: Module Empty
11+
sysTupleSize = module
12+
rules
13+
"test": when True ==> action
14+
$display "=== Testing TupleSize type class ==="
15+
16+
-- Test TupleSize for unit type
17+
let u :: ()
18+
u = ()
19+
let size0 = tupleSize u
20+
$display "TupleSize of () = %0d (expected 0)" size0
21+
22+
-- Test TupleSize for single element (non-tuple)
23+
let b :: Bool
24+
b = True
25+
let size1 = tupleSize b
26+
$display "TupleSize of Bool = %0d (expected 1)" size1
27+
28+
-- Test TupleSize for 2-tuple
29+
let t2 :: (Bool, Int 8)
30+
t2 = (True, 0)
31+
let size2 = tupleSize t2
32+
$display "TupleSize of Tuple2 = %0d (expected 2)" size2
33+
34+
-- Test TupleSize for 3-tuple
35+
let t3 :: (Bool, Int 8, UInt 4)
36+
t3 = (True, 0, 0)
37+
let size3 = tupleSize t3
38+
$display "TupleSize of Tuple3 = %0d (expected 3)" size3
39+
40+
-- Test TupleSize for 4-tuple
41+
let t4 :: (Bool, Int 8, UInt 4, Bit 5)
42+
t4 = (True, 0, 0, 0)
43+
let size4 = tupleSize t4
44+
$display "TupleSize of Tuple4 = %0d (expected 4)" size4
45+
46+
-- Test TupleSize for 5-tuple
47+
let t5 :: (Bool, Int 8, UInt 4, Bit 5, Int 16)
48+
t5 = (True, 0, 0, 0, 0)
49+
let size5 = tupleSize t5
50+
$display "TupleSize of Tuple5 = %0d (expected 5)" size5
51+
52+
-- Test TupleSize for 6-tuple
53+
let t6 :: (Bool, Int 8, UInt 4, Bit 5, Int 16, UInt 3)
54+
t6 = (True, 0, 0, 0, 0, 0)
55+
let size6 = tupleSize t6
56+
$display "TupleSize of Tuple6 = %0d (expected 6)" size6
57+
58+
-- Test TupleSize for 7-tuple
59+
let t7 :: (Bool, Int 8, UInt 4, Bit 5, Int 16, UInt 3, Bit 2)
60+
t7 = (True, 0, 0, 0, 0, 0, 0)
61+
let size7 = tupleSize t7
62+
$display "TupleSize of Tuple7 = %0d (expected 7)" size7
63+
64+
-- Test TupleSize for 8-tuple
65+
let t8 :: (Bool, Int 8, UInt 4, Bit 5, Int 16, UInt 3, Bit 2, Int 32)
66+
t8 = (True, 0, 0, 0, 0, 0, 0, 0)
67+
let size8 = tupleSize t8
68+
$display "TupleSize of Tuple8 = %0d (expected 8)" size8
69+
70+
$display "All TupleSize type class tests passed"
71+
$finish 0
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== Testing AppendTuple ===
2+
appendTuple((), ()) = () [unit type]
3+
appendTuple((), (True, 42)) = (1, 42)
4+
appendTuple((False, -10), ()) = (0, -10)
5+
appendTuple((True, 5), (12, 0b101)) = (1, 5, 12, 101)
6+
appendTuple(False, (-3, 7)) = (0, -3, 7)
7+
appendTuple((True, 20, 8), 0b11010) = (1, 20, 8, 11010)
8+
appendTuple((False, 15), (6, 0b111, -100)) = (0, 15, 6, 111, -100)
9+
10+
=== Testing splitTuple ===
11+
splitTuple((True, -15, 9, 0b110)) = ((1, -15), (9, 110))
12+
splitTuple((False, 33, 15)) = (0, (33, 15))
13+
splitTuple((True, 50, 11, 0b001, -200)) = ((1, 50), (11, 001, -200))
14+
15+
=== Round-trip test ===
16+
appendTuple/splitTuple round-trip: ((1, 42), (13, 10101))
17+
18+
All AppendTuple tests passed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== Testing TupleSize type class ===
2+
TupleSize of () = 0 (expected 0)
3+
TupleSize of Bool = 1 (expected 1)
4+
TupleSize of Tuple2 = 2 (expected 2)
5+
TupleSize of Tuple3 = 3 (expected 3)
6+
TupleSize of Tuple4 = 4 (expected 4)
7+
TupleSize of Tuple5 = 5 (expected 5)
8+
TupleSize of Tuple6 = 6 (expected 6)
9+
TupleSize of Tuple7 = 7 (expected 7)
10+
TupleSize of Tuple8 = 8 (expected 8)
11+
All TupleSize type class tests passed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package ConcatTuple where
2+
3+
import Vector
4+
5+
-- Test the ConcatTuple type class
6+
7+
{-# verilog sysConcatTuple #-}
8+
sysConcatTuple :: Module Empty
9+
sysConcatTuple =
10+
module
11+
counter :: Reg (UInt 6) <- mkReg 0
12+
13+
rules
14+
"test0": when counter == 0 ==> action
15+
$display "=== Testing ConcatTuple ==="
16+
-- Test empty vector to unit type
17+
let empty :: Vector 0 (Bool, Int 8)
18+
empty = nil
19+
let unit :: ()
20+
unit = concatTuple empty
21+
$display "concatTuple(empty_vector) = () [unit type]"
22+
counter := counter + 1
23+
24+
"test1": when counter == 1 ==> action
25+
-- Test single element vector
26+
let v1 :: Vector 1 (Bool, Int 8)
27+
v1 = (True, 25) :> nil
28+
let result :: (Bool, Int 8)
29+
result = concatTuple v1
30+
$display "concatTuple([(True, 25)]) = (%b, %0d)" (tpl_1 result) (tpl_2 result)
31+
counter := counter + 1
32+
33+
"test2": when counter == 2 ==> action
34+
-- Test vector of 3 single elements to 3-tuple
35+
let v3 :: Vector 3 (Int 8)
36+
v3 = 10 :> 20 :> 30 :> nil
37+
let t3 :: (Int 8, Int 8, Int 8)
38+
t3 = concatTuple v3
39+
$display "concatTuple([10, 20, 30]) = (%0d, %0d, %0d)" (tpl_1 t3) (tpl_2 t3) (tpl_3 t3)
40+
counter := counter + 1
41+
42+
"test3": when counter == 3 ==> action
43+
-- Test vector of 4 single elements to 4-tuple
44+
let v4 :: Vector 4 (UInt 4)
45+
v4 = 1 :> 2 :> 3 :> 4 :> nil
46+
let t4 :: (UInt 4, UInt 4, UInt 4, UInt 4)
47+
t4 = concatTuple v4
48+
$display "concatTuple([1, 2, 3, 4]) = (%0d, %0d, %0d, %0d)" (tpl_1 t4) (tpl_2 t4) (tpl_3 t4) (tpl_4 t4)
49+
counter := counter + 1
50+
51+
"test4": when counter == 4 ==> action
52+
-- Test vector of 2 pairs to 4-tuple
53+
let v2 :: Vector 2 (Bool, Int 8)
54+
v2 = (True, 5) :> (False, negate 12) :> nil
55+
let t4 :: (Bool, Int 8, Bool, Int 8)
56+
t4 = concatTuple v2
57+
$display "concatTuple([(True, 5), (False, -12)]) = (%b, %0d, %b, %0d)" (tpl_1 t4) (tpl_2 t4) (tpl_3 t4) (tpl_4 t4)
58+
counter := counter + 1
59+
60+
"test5": when counter == 5 ==> action
61+
-- Test vector of 3 pairs to 6-tuple
62+
let v3 :: Vector 3 (UInt 4, Bit 3)
63+
v3 = (1, 0b001) :> (2, 0b010) :> (3, 0b011) :> nil
64+
let t6 :: (UInt 4, Bit 3, UInt 4, Bit 3, UInt 4, Bit 3)
65+
t6 = concatTuple v3
66+
$display "concatTuple([(1, 0b001), (2, 0b010), (3, 0b011)]) = (%0d, %b, %0d, %b, %0d, %b)" (tpl_1 t6) (tpl_2 t6) (tpl_3 t6) (tpl_4 t6) (tpl_5 t6) (tpl_6 t6)
67+
counter := counter + 1
68+
69+
"test6": when counter == 6 ==> action
70+
-- Test vector of 2 triples to 6-tuple
71+
let v2 :: Vector 2 (Bool, Int 8, UInt 4)
72+
v2 = (True, 10, 5) :> (False, negate 20, 8) :> nil
73+
let t6 :: (Bool, Int 8, UInt 4, Bool, Int 8, UInt 4)
74+
t6 = concatTuple v2
75+
$display "concatTuple([(True, 10, 5), (False, -20, 8)]) = (%b, %0d, %0d, %b, %0d, %0d)" (tpl_1 t6) (tpl_2 t6) (tpl_3 t6) (tpl_4 t6) (tpl_5 t6) (tpl_6 t6)
76+
$display ""
77+
$display "=== Testing unconcatTuple ==="
78+
counter := counter + 1
79+
80+
"test7": when counter == 7 ==> action
81+
-- Test 4-tuple to vector of 4 singles
82+
let t4 :: (Int 8, Int 8, Int 8, Int 8)
83+
t4 = (7, 8, 9, 10)
84+
let v4 :: Vector 4 (Int 8)
85+
v4 = unconcatTuple t4
86+
$display "unconcatTuple((7, 8, 9, 10)) = [%0d, %0d, %0d, %0d]" (v4 !! 0) (v4 !! 1) (v4 !! 2) (v4 !! 3)
87+
counter := counter + 1
88+
89+
"test8": when counter == 8 ==> action
90+
-- Test 4-tuple to vector of 2 pairs
91+
let t4 :: (Bool, Int 8, Bool, Int 8)
92+
t4 = (False, 100, True, negate 50)
93+
let v2 :: Vector 2 (Bool, Int 8)
94+
v2 = unconcatTuple t4
95+
$display "unconcatTuple((False, 100, True, -50)) = [(%b, %0d), (%b, %0d)]" (tpl_1 (v2 !! 0)) (tpl_2 (v2 !! 0)) (tpl_1 (v2 !! 1)) (tpl_2 (v2 !! 1))
96+
counter := counter + 1
97+
98+
"test9": when counter == 9 ==> action
99+
-- Test 6-tuple to vector of 3 pairs
100+
let t6 :: (UInt 4, Bit 2, UInt 4, Bit 2, UInt 4, Bit 2)
101+
t6 = (5, 0b00, 10, 0b01, 15, 0b10)
102+
let v3 :: Vector 3 (UInt 4, Bit 2)
103+
v3 = unconcatTuple t6
104+
$display "unconcatTuple((5, 0b00, 10, 0b01, 15, 0b10)) = [(%0d, %b), (%0d, %b), (%0d, %b)]" (tpl_1 (v3 !! 0)) (tpl_2 (v3 !! 0)) (tpl_1 (v3 !! 1)) (tpl_2 (v3 !! 1)) (tpl_1 (v3 !! 2)) (tpl_2 (v3 !! 2))
105+
counter := counter + 1
106+
107+
"test10": when counter == 10 ==> action
108+
-- Test 6-tuple to vector of 2 triples
109+
let t6 :: (Bool, Int 8, UInt 4, Bool, Int 8, UInt 4)
110+
t6 = (True, 15, 3, False, negate 25, 7)
111+
let v2 :: Vector 2 (Bool, Int 8, UInt 4)
112+
v2 = unconcatTuple t6
113+
$display "unconcatTuple((True, 15, 3, False, -25, 7)) = [(%b, %0d, %0d), (%b, %0d, %0d)]" (tpl_1 (v2 !! 0)) (tpl_2 (v2 !! 0)) (tpl_3 (v2 !! 0)) (tpl_1 (v2 !! 1)) (tpl_2 (v2 !! 1)) (tpl_3 (v2 !! 1))
114+
$display ""
115+
$display "=== Round-trip test ==="
116+
counter := counter + 1
117+
118+
"test11": when counter == 11 ==> action
119+
-- Test concatTuple and unconcatTuple round-trip with singles
120+
let v_orig :: Vector 5 (Int 8)
121+
v_orig = 1 :> 2 :> 3 :> 4 :> 5 :> nil
122+
let t5 :: (Int 8, Int 8, Int 8, Int 8, Int 8)
123+
t5 = concatTuple v_orig
124+
let v_restored :: Vector 5 (Int 8)
125+
v_restored = unconcatTuple t5
126+
$display "concatTuple/unconcatTuple round-trip (singles): [%0d, %0d, %0d, %0d, %0d]" (v_restored !! 0) (v_restored !! 1) (v_restored !! 2) (v_restored !! 3) (v_restored !! 4)
127+
counter := counter + 1
128+
129+
"test12": when counter == 12 ==> action
130+
-- Test concatTuple and unconcatTuple round-trip with pairs
131+
let v_orig :: Vector 3 (Bool, Int 8)
132+
v_orig = (True, 11) :> (False, 22) :> (True, 33) :> nil
133+
let t6 :: (Bool, Int 8, Bool, Int 8, Bool, Int 8)
134+
t6 = concatTuple v_orig
135+
let v_restored :: Vector 3 (Bool, Int 8)
136+
v_restored = unconcatTuple t6
137+
$display "concatTuple/unconcatTuple round-trip (pairs): [(%b, %0d), (%b, %0d), (%b, %0d)]" (tpl_1 (v_restored !! 0)) (tpl_2 (v_restored !! 0)) (tpl_1 (v_restored !! 1)) (tpl_2 (v_restored !! 1)) (tpl_1 (v_restored !! 2)) (tpl_2 (v_restored !! 2))
138+
$display ""
139+
$display "All ConcatTuple tests passed"
140+
$finish 0

testsuite/bsc.lib/vector/libvector.exp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ test_c_veri_bsv FindElem
1111
test_c_veri_bsv FindIndex
1212
test_c_veri_bsv RotateBy
1313
test_c_veri_bsv ZeroVector
14+
15+
test_c_veri_bs_modules ConcatTuple {}

0 commit comments

Comments
 (0)