Skip to content

Instantly share code, notes, and snippets.

@akbiggs
Created July 18, 2013 18:40
Show Gist options
  • Save akbiggs/6031808 to your computer and use it in GitHub Desktop.
Save akbiggs/6031808 to your computer and use it in GitHub Desktop.
Simple HDL test script generator
import sys
import math
class InvalidTestFileException(Exception):
pass
ERRORS = {
"novars": "Test file is missing either input or output variables"
}
def generateTestsFor(inputFile):
testLines = []
with open(inputFile) as f:
inputVars, outputVars = [], []
for line in f:
if (isInputLine(line)):
inputVars = getInputVars(line)
elif (isOutputLine(line)):
outputVars = getOutputVars(line)
if inputVars and outputVars:
testLines += ["load " + inputFile + ",",
"output-list " + ", ".join(inputVars) + ", " +
", ".join(outputVars) + ";"]
tests = generateTestValuesFor(inputVars)
for i in range(0, tests["count"]):
setLine = ", ".join(["set " + str(var) + " " + str(val) for var, val in
{var: tests[var][i] for var in inputVars}.items()])
setLine += ","
testLines += [setLine, "eval, output;"]
return testLines
else:
raise InvalidTestFileException(ERRORS["novars"])
def isInputLine(line):
return "IN" in line
def getInputVars(line):
return getVars(line, "IN")
def isOutputLine(line):
return "OUT" in line
def getOutputVars(line):
return getVars(line, "OUT")
def getVars(line, spec):
varString = line.split(spec, 1)[1].strip().replace(" ", "").strip(";")
return varString.split(",")
def generateTestValuesFor(varList):
result = {}
combos = getCombos(varList)
for i in range(0, len(varList)):
var = varList[i]
result[var] = [combo[i] for combo in combos]
result["count"] = len(combos)
return result
def getCombos(varNames, accumulator=None):
if not accumulator:
accumulator = []
if not varNames:
return accumulator
else:
combos = []
if not len(accumulator):
combos = [[0], [1]]
else:
for accumulated in accumulator:
combos.append(accumulated + [0])
combos.append(accumulated + [1])
return getCombos(varNames[1:], combos)
if __name__ == "__main__":
if (len(sys.argv) <= 1):
sys.exit("Usage: script.py file1.hdl [file2.hdl ...]")
else:
for filename in sys.argv[1:]:
for line in generateTestsFor(filename):
print(line)
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment