Skip to content

Commit 3fd99a8

Browse files
C# code generator in Python for generating unit test cases
1 parent 981d6e2 commit 3fd99a8

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import math
2+
import numpy as np
3+
4+
# The asCode2D function generates NDArray declarations in C# for use in unit testing.
5+
# This avoids some of the tedium and errors of hand-generation.
6+
# For example, calling the function like this generates C# static variables named
7+
# 'a53' and 'b53' from numpy's mgrid:
8+
# aa, bb = np.mgrid[0:5, 0:3]
9+
# cSharp.asCode2D("a53", aa)
10+
# cSharp.asCode2D("b53", bb)
11+
12+
13+
class cSharp:
14+
def asCode2D(varName, v):
15+
if v.dtype.name == "int32":
16+
vType = "Int32"
17+
elif v.dtype.name == "float64":
18+
vType = "double"
19+
else:
20+
vType = "unknown"
21+
print(" static NDArray {0} = new NDArray(new {1}[] {{".format(varName, vType))
22+
valstr = ""
23+
commasToPrint = v.shape[0] * v.shape[1] - 1
24+
for i, row in enumerate(v):
25+
rowStr = " "
26+
for j, item in enumerate(row):
27+
rowStr = rowStr + "{}".format(item)
28+
if commasToPrint > 0:
29+
rowStr = rowStr + ", "
30+
commasToPrint -= 1
31+
#if (i < v)
32+
print(rowStr)
33+
print(" }}, new Shape(new int[] {{ {}, {} }}));".format(v.shape[0], v.shape[1]))
34+
print("")
35+
36+
def asCode1D(varName, v):
37+
if v.dtype.name == "int32":
38+
vType = "Int32"
39+
elif v.dtype.name == "float64":
40+
vType = "double"
41+
else:
42+
vType = "unknown"
43+
print(" static NDArray {0} = new NDArray(new {1}[] {{".format(varName, vType))
44+
rowStr = " "
45+
commasToPrint = v.shape[0] - 1
46+
for j, item in enumerate(v):
47+
rowStr = rowStr + "{}".format(item)
48+
if commasToPrint > 0:
49+
rowStr = rowStr + ", "
50+
commasToPrint -= 1
51+
print(rowStr)
52+
print(" }}, new Shape(new int[] {{ {} }}));".format(v.shape[0]))
53+
print("")

0 commit comments

Comments
 (0)