def testUnion(self): unionInst = cxx_writer.Union('myUnion') type = cxx_writer.stringType var = cxx_writer.Variable('pippo', type) unionInst.addMember(var) type = cxx_writer.intType var = cxx_writer.Variable('duck', type) unionInst.addMember(var) unionInst.writeDeclaration(self.writer) self.writer.flush() testFile = open('prova.cpp', 'r') lines = testFile.readlines() testFile.close() self.assertEqual(len(lines), 4) self.assertEqual(lines[0], 'union myUnion {\n') self.assertEqual(lines[1], ' std::string pippo;\n') self.assertEqual(lines[2], ' int duck;\n') self.assertEqual(lines[3], '};\n')
def testVariableInit(self): type = cxx_writer.stringType var = cxx_writer.Variable('pippo', type, False, '\"pippa\"') var.writeDeclaration(self.writer) self.writer.flush() testFile = open('prova.cpp', 'r') lines = testFile.readlines() testFile.close() self.assertEqual(len(lines), 0)
def testDumpVariablesHeader(self): tempType = cxx_writer.TemplateType('std::map', [cxx_writer.intType, cxx_writer.stringType], 'map') tempVar = cxx_writer.Variable('pippo', tempType) dumper = cxx_writer.FileDumper('prova.cpp', True, indentSize = 4, lineWidth = 80) dumper.addMember(tempVar) dumper.write() testFile = open('prova.cpp', 'rt') lines = testFile.readlines() testFile.close() os.remove('prova.cpp') self.assertEqual(len(lines), 2 + 18) self.assertEqual(lines[13], '#include <map>\n') self.assertEqual(lines[14], '#include <string>\n')
def testTemplatedVariable(self): innerType1 = cxx_writer.stringType innerType2 = cxx_writer.intType innerType3 = cxx_writer.doubleType templ1 = cxx_writer.TemplateType('std::map', [innerType2, innerType3]) type = cxx_writer.TemplateType('std::map', [innerType1, templ1]) var = cxx_writer.Variable('pippo', type) var.writeDeclaration(self.writer) self.writer.flush() testFile = open('prova.cpp', 'r') lines = testFile.readlines() testFile.close() self.assertEqual(len(lines), 0)
def addInstructionVar(self, variable): """Declares an instruction variable that will be accessed by this operation.""" if isinstance(variable, type(())): variable = cxx_writer.Variable(variable[0], resolveBitType(variable[1])) for instrVar in self.instrvars + self.localvars: if variable.name == instrVar.name: if variable.type.name != instrVar.type.name: raise Exception('Cannot add variable ' + variable.name + ' of type ' + variable.type.name + ' to operation ' + self.name + '. Variable of type ' + instrVar.type.name + ' already exists in operation.') self.instrvars.append(variable)
def addVariable(self, variable): """Adds a variable (cxx_writer.Variable) as an operation class member.""" if isinstance(variable, type(())): variable = cxx_writer.Variable(variable[0], resolveBitType(variable[1])) for instrVar in self.localvars + self.instrvars: if variable.name == instrVar.name: if variable.type.name != instrVar.type.name: raise Exception('Cannot add variable ' + variable.name + ' of type ' + variable.type.name + ' to operation ' + self.name + '. Variable of type ' + instrVar.type.name + ' already exists in operation.') self.localvars.append(variable)
def addVariable(self, variable): """Adds a variable (cxx_writer.Variable) as an instruction class member.""" if isinstance(variable, type(())): variable = cxx_writer.Variable(variable[0], resolveBitType(variable[1])) for instrVar in self.variables: if variable.name == instrVar.name: if variable.varType.name != instrVar.varType.name: raise Exception('Cannot add variable ' + variable.name + ' of type ' + variable.varType.name + ' to instruction ' + self.name + '. Variable of type ' + instrVar.varType.name + 'already exists in the instruction.') else: return self.variables.append(variable)