Simplify template logic.

* Add function to generate test inputs.
* Refactor template structure.

Change-Id: I1913a75b6f7ac478d77722740970af26400585e4
diff --git a/softrvv/tests/templates/base.tpl.cpp b/softrvv/tests/templates/base.tpl.cpp
index a9887cb..bf3e1db 100644
--- a/softrvv/tests/templates/base.tpl.cpp
+++ b/softrvv/tests/templates/base.tpl.cpp
@@ -1,9 +1,15 @@
 <%!
 import numpy  as np
 
-def to_carr_str(arr):
-    return ", ".join(("%s" % x for x in arr))
-
+def get_test_inputs(dtype, N, allow_zero=True):
+    ii32 = np.iinfo(dtype)
+    src1_data =  np.random.randint(ii32.min, ii32.max, N).astype(dtype)
+    src2_data = np.random.randint(ii32.min, ii32.max, N).astype(dtype)
+    rs1 = dtype(np.random.randint(ii32.min, ii32.max))
+    if not allow_zero:
+        src2_data[src2_data==0] = 1
+        rs1 = 1 if rs1 == 0 else rs1
+    return src1_data, src2_data, rs1
 %>
 /* File is auto-generated. */
 #include <riscv_vector.h>
diff --git a/softrvv/tests/templates/opivv_opivx_test.tpl.cpp b/softrvv/tests/templates/opivv_opivx_test.tpl.cpp
index 7134d91..c91eb95 100644
--- a/softrvv/tests/templates/opivv_opivx_test.tpl.cpp
+++ b/softrvv/tests/templates/opivv_opivx_test.tpl.cpp
@@ -1,4 +1,14 @@
 <%def name="test_opivv_opivx(dtype, op, src1, src2, rs1, ref_vv, ref_vx)">
+<%
+
+def to_carr_str(arr):
+    return ", ".join(("%s" % x for x in arr))
+
+src1 = to_carr_str(src1)
+src2 = to_carr_str(src2)
+ref_vv = to_carr_str(ref_vv)
+ref_vx = to_carr_str(ref_vx)
+%>
 namespace softrvv_${op}_test {
 namespace {
 
diff --git a/softrvv/tests/templates/softrvv_vdiv_test.tpl.cpp b/softrvv/tests/templates/softrvv_vdiv_test.tpl.cpp
index d574f5a..0c2bdd3 100644
--- a/softrvv/tests/templates/softrvv_vdiv_test.tpl.cpp
+++ b/softrvv/tests/templates/softrvv_vdiv_test.tpl.cpp
@@ -2,24 +2,8 @@
 <%namespace name="tests" file="opivv_opivx_test.tpl.cpp"/>
 <%
 import numpy as np
-# Generate test vectors using python
-N = 5
-ii32 = np.iinfo(np.int32)
-src1_data =  np.random.random_integers(ii32.min, ii32.max, N).astype(np.int32)
-src2_data = np.random.random_integers(ii32.min, ii32.max, N).astype(np.int32)
-# Don't allow divide by zero
-src2_data[src2_data==0] = 1
-ref_vv_data = np.divide(src1_data,src2_data).astype(np.int32)
-rs1 = np.int32(np.random.randint(ii32.min, ii32.max))
-# Don't allow divide by zero
-if rs1 == 0:
-  rs1 = 1
-ref_vx_data = np.divide(src1_data, rs1).astype(np.int32)
-
-# Convert test vectors to strings for array initialization
-src1 = parent.module.to_carr_str(src1_data)
-src2 = parent.module.to_carr_str(src2_data)
-ref_vv = parent.module.to_carr_str(ref_vv_data)
-ref_vx = parent.module.to_carr_str(ref_vx_data)
+src1, src2, rs1 = parent.module.get_test_inputs(np.int32, N=5, allow_zero=False)
+ref_vv = np.divide(src1, src2).astype(np.int32)
+ref_vx = np.divide(src1, rs1).astype(np.int32)
 %>\
 ${tests.test_opivv_opivx("int32_t", op, src1, src2, rs1, ref_vv, ref_vx)}
diff --git a/softrvv/tests/templates/softrvv_vmul_test.tpl.cpp b/softrvv/tests/templates/softrvv_vmul_test.tpl.cpp
index e9a1832..8b47598 100644
--- a/softrvv/tests/templates/softrvv_vmul_test.tpl.cpp
+++ b/softrvv/tests/templates/softrvv_vmul_test.tpl.cpp
@@ -2,19 +2,8 @@
 <%namespace name="tests" file="opivv_opivx_test.tpl.cpp"/>
 <%
 import numpy as np
-# Generate test vectors using python
-N = 5
-ii32 = np.iinfo(np.int32)
-src1_data =  np.random.randint(ii32.min, ii32.max, N).astype(np.int32)
-src2_data = np.random.randint(ii32.min, ii32.max, N).astype(np.int32)
-rs1 = np.int32(np.random.randint(ii32.min, ii32.max))
-ref_vv_data = src1_data * src2_data
-ref_vx_data = src1_data * rs1
-
-# Convert test vectors to strings for array initialization
-src1 = parent.module.to_carr_str(src1_data)
-src2 = parent.module.to_carr_str(src2_data)
-ref_vv = parent.module.to_carr_str(ref_vv_data)
-ref_vx = parent.module.to_carr_str(ref_vx_data)
+src1, src2, rs1 = parent.module.get_test_inputs(np.int32, N=5)
+ref_vv = src1 * src2
+ref_vx = src1 * rs1
 %>\
 ${tests.test_opivv_opivx("int32_t", op, src1, src2, rs1, ref_vv, ref_vx)}
diff --git a/softrvv/tests/templates/softrvv_vmulh_test.tpl.cpp b/softrvv/tests/templates/softrvv_vmulh_test.tpl.cpp
index c4d82a8..5a77585 100644
--- a/softrvv/tests/templates/softrvv_vmulh_test.tpl.cpp
+++ b/softrvv/tests/templates/softrvv_vmulh_test.tpl.cpp
@@ -2,21 +2,10 @@
 <%namespace name="tests" file="opivv_opivx_test.tpl.cpp"/>
 <%
 import numpy as np
-# Generate test vectors using python
-N = 5
-ii32 = np.iinfo(np.int32)
-src1_data =  np.random.randint(ii32.min, ii32.max, N).astype(np.int32)
-src2_data = np.random.randint(ii32.min, ii32.max, N).astype(np.int32)
-rs1 = np.int32(np.random.randint(ii32.min, ii32.max))
-ref_vv_data = np.right_shift(
-    src1_data.astype(np.int64) * src2_data.astype(np.int64), 32).astype(np.int32)
-ref_vx_data = np.right_shift(
-    src1_data.astype(np.int64) * rs1, 32).astype(np.int32)
-
-# Convert test vectors to strings for array initialization
-src1 = parent.module.to_carr_str(src1_data)
-src2 = parent.module.to_carr_str(src2_data)
-ref_vv = parent.module.to_carr_str(ref_vv_data)
-ref_vx = parent.module.to_carr_str(ref_vx_data)
+src1, src2, rs1 = parent.module.get_test_inputs(np.int32, N=5)
+ref_vv = np.right_shift(
+    src1.astype(np.int64) * src2.astype(np.int64), 32).astype(np.int32)
+ref_vx = np.right_shift(
+    src1.astype(np.int64) * rs1, 32).astype(np.int32)
 %>
 ${tests.test_opivv_opivx("int32_t", op, src1, src2, rs1, ref_vv, ref_vx)}
diff --git a/softrvv/tests/templates/softrvv_vrem_test.tpl.cpp b/softrvv/tests/templates/softrvv_vrem_test.tpl.cpp
index e4eefd7..33c42b4 100644
--- a/softrvv/tests/templates/softrvv_vrem_test.tpl.cpp
+++ b/softrvv/tests/templates/softrvv_vrem_test.tpl.cpp
@@ -2,24 +2,8 @@
 <%namespace name="tests" file="opivv_opivx_test.tpl.cpp"/>
 <%
 import numpy as np
-# Generate test vectors using python
-N = 5
-ii32 = np.iinfo(np.int32)
-src1_data = np.random.random_integers(0, ii32.max, N).astype(np.int32)
-src2_data = np.random.random_integers(0, ii32.max, N).astype(np.int32)
-# Don't allow zero
-src2_data[src2_data==0] = 1
-ref_vv_data = np.mod(src1_data,src2_data).astype(np.int32)
-rs1 = np.int32(np.random.randint(0, ii32.max))
-# Don't allow divide by zero
-if rs1 == 0:
-  rs1 = 1
-ref_vx_data = np.mod(src1_data, rs1).astype(np.int32)
-
-# Convert test vectors to strings for array initialization
-src1 = parent.module.to_carr_str(src1_data)
-src2 = parent.module.to_carr_str(src2_data)
-ref_vv = parent.module.to_carr_str(ref_vv_data)
-ref_vx = parent.module.to_carr_str(ref_vx_data)
+src1, src2, rs1 = parent.module.get_test_inputs(np.uint32, N=5, allow_zero=False)
+ref_vv = np.mod(src1,src2).astype(np.uint32)
+ref_vx = np.mod(src1, rs1).astype(np.uint32)
 %>\
-${tests.test_opivv_opivx("int32_t", op, src1, src2, rs1, ref_vv, ref_vx)}
+${tests.test_opivv_opivx("uint32_t", op, src1, src2, rs1, ref_vv, ref_vx)}