def test_case_bevbuffer2(self): lines = "" \ "# Big-Endian Variable Buffer 2 Class\n" \ "class testbevbuffer2 {\n" \ " bevbuffer2 bevbuffer01;\n" \ "}\n" pc = ProtoCompiler() fname = os.path.join(properties.TEMP_DIR, 'bevbuffer2.class') f = open(fname, 'w') f.write(lines) f.close() pc.parseFile(fname) assert pc.classnames == ['testbevbuffer2' ], "Classnames != ['testbevbuffer2']" my_class = pc._getClass('testbevbuffer2') assert my_class.isPackCompatible( ) == 0, "Class should be not pack compatible." assert my_class.isFixedWidth() == 0, "Width should not be fixed." assert my_class.getWidth( ) == 0, "Width should be 0, got %d." % my_class.getWidth() c = pc.emitCode() pyfilename = os.path.join(properties.TEMP_DIR, "testbevbuffer2.py") f = open(pyfilename, 'w') f.write(c) f.close() # Now try to import it. import testbevbuffer2 obj = testbevbuffer2.testbevbuffer2() assert obj.isFixedWidth() == 0, "isFixedWidth() should be 0" assert obj.isPackCompatible() == 0, "isPackCompatible() should be 0" assert obj.getWidth( ) == 0, "Width should be 0, got %d" % obj.getWidth() assert obj.getNumItems( ) == 1, "NumItems should be 1, got %d" % obj.getNumItems() # Object has only one item, run some tests against that item item_obj = obj.items[0] width = item_obj.getWidth() assert width == 2, "width should be 2, got %s" % str(width) os.remove(pyfilename)
def test_case_double1(self): lines = "" \ "# Float Class\n" \ "class double1 {\n" \ " double1 double01;\n" \ "}\n" pc = ProtoCompiler() fname = os.path.join(properties.TEMP_DIR, 'double1.class') f = open(fname, 'w') f.write(lines) f.close() pc.parseFile(fname) assert pc.classnames == ['double1'], "Classnames != ['double1']" my_class = pc._getClass('double1') assert my_class.isPackCompatible( ) == 1, "Class should be pack compatible." exp_spec = 'd' assert my_class.packSpec( ) == exp_spec, "Class spec should be '%s', got '%s'." % ( exp_spec, my_class.packSpec()) assert my_class.isFixedWidth() == 1, "Width should be fixed." assert my_class.getWidth( ) == 8, "Width should be 8, got %d." % my_class.getWidth() c = pc.emitCode() pyfilename = os.path.join(properties.TEMP_DIR, "double1.py") f = open(pyfilename, 'w') f.write(c) f.close() # Now try to import it. import double1 obj = double1.double1() #print 'Dir of new object: %s' % str(dir(obj)) assert obj.isFixedWidth() == 1, "isFixedWidth() should be 1" assert obj.isPackCompatible() == 1, "isPackCompatible() should be 1" assert obj.packSpec() == "d", "packSpec() should be d" assert obj.getWidth( ) == 8, "Width should be 8, got %d" % obj.getWidth() assert obj.getNumItems( ) == 1, "NumItems should be 1, got %d" % obj.getNumItems()
def test_case_singlebyte(self): lines = "" \ "# Single byte Class\n" \ "class singlebyte {\n" \ " uint8 preamble = 0xAA;\n" \ " uint8 data;\n" \ " uint8 postamble = 0xFF;\n" \ "}\n" pc = ProtoCompiler() fname = os.path.join(properties.TEMP_DIR, 'single_byte.class') f = open(fname, 'w') f.write(lines) f.close() pc.parseFile(fname) assert pc.classnames == ['singlebyte'], "Classnames != ['singlebyte']" my_class = pc._getClass('singlebyte') assert my_class.isPackCompatible( ) == 1, "Class should be pack compatible." exp_spec = '<B<B<B' assert my_class.packSpec( ) == exp_spec, "Class spec should be '%s', got '%s'." % ( exp_spec, my_class.packSpec()) assert my_class.isFixedWidth() == 1, "Width should be fixed." assert my_class.getWidth( ) == 3, "Width should be 3, got %d." % my_class.getWidth() c = pc.emitCode() pyfilename = os.path.join(properties.TEMP_DIR, "singlebyte.py") f = open(pyfilename, 'w') f.write(c) f.close() # Now try to import it. import singlebyte obj = singlebyte.singlebyte() assert obj.isFixedWidth() == 1, "isFixedWidth() should be 1" assert obj.isPackCompatible() == 1, "isPackCompatible() should be 1" assert obj.packSpec() == "<B<B<B", "packSpec() should be <B<B<B" assert obj.getWidth( ) == 3, "Width should be 3, got %d" % obj.getWidth() assert obj.getNumItems( ) == 3, "NumItems should be 3, got %d" % obj.getNumItems()
def test_case_float1(self): lines = "" \ "# Float Class\n" \ "class float1 {\n" \ " float1 float01;\n" \ "}\n" pc = ProtoCompiler() fname = os.path.join(properties.TEMP_DIR, 'float1.class') f = open(fname, 'w') f.write(lines) f.close() pc.parseFile(fname) assert pc.classnames == ['float1'], "Classnames != ['float1']" my_class = pc._getClass('float1') assert my_class.isPackCompatible( ) == 1, "Class should be pack compatible." exp_spec = 'f' assert my_class.packSpec( ) == exp_spec, "Class spec should be '%s', got '%s'." % ( exp_spec, my_class.packSpec()) assert my_class.isFixedWidth() == 1, "Width should be fixed." assert my_class.getWidth( ) == 4, "Width should be 4, got %d." % my_class.getWidth() c = pc.emitCode() pyfilename = os.path.join(properties.TEMP_DIR, "float1.py") f = open(pyfilename, 'w') f.write(c) f.close() # Now try to import it. import float1 obj = float1.float1() assert obj.isFixedWidth() == 1, "isFixedWidth() should be 1" assert obj.isPackCompatible() == 1, "isPackCompatible() should be 1" assert obj.packSpec() == "f", "packSpec() should be f" assert obj.getWidth( ) == 4, "Width should be 4, got %d" % obj.getWidth() assert obj.getNumItems( ) == 1, "NumItems should be 1, got %d" % obj.getNumItems()
def test_case_fbuffer(self): lines = "" \ "# Fixed Buffer Class\n" \ "class testfbuffer {\n" \ " width=7 fbuffer fbuffer01;\n" \ "}\n" pc = ProtoCompiler() fname = os.path.join(properties.TEMP_DIR, 'fbuffer.class') f = open(fname, 'w') f.write(lines) f.close() pc.parseFile(fname) assert pc.classnames == ['testfbuffer' ], "Classnames != ['testfbuffer']" my_class = pc._getClass('testfbuffer') assert my_class.isPackCompatible( ) == 0, "Class should be not pack compatible." assert my_class.isFixedWidth() == 1, "Width should be fixed." assert my_class.getWidth( ) == 7, "Width should be 7, got %d." % my_class.getWidth() c = pc.emitCode() pyfilename = os.path.join(properties.TEMP_DIR, "testfbuffer.py") f = open(pyfilename, 'w') f.write(c) f.close() # Now try to import it. import testfbuffer obj = testfbuffer.testfbuffer() assert obj.isFixedWidth() == 1, "isFixedWidth() should be 1" assert obj.isPackCompatible() == 0, "isPackCompatible() should be 0" assert obj.getWidth( ) == 7, "Width should be 7, got %d" % obj.getWidth() assert obj.getNumItems( ) == 1, "NumItems should be 1, got %d" % obj.getNumItems()
def test_case_nested_fbuffer_class(self): lines = "" \ "# Single byte Class\n" \ "class singlebyte {\n" \ " uint8 preamble = 0xAA;\n" \ " uint8 data;\n" \ " uint8 postamble = 0xFF;\n" \ "}\n" \ "# Fixed Buffer Class\n" \ "class testnestedfbuffer {\n" \ " width=3 class=singlebyte fbuffer fbuffer01;\n" \ "}\n" pc = ProtoCompiler() fname = os.path.join(properties.TEMP_DIR, 'nestedfbuffer.class') f = open(fname, 'w') f.write(lines) f.close() pc.parseFile(fname) assert pc.classnames == [ 'singlebyte', 'testnestedfbuffer' ], "Classnames != ['singlebyte', 'testnestedfbuffer']" my_class = pc._getClass('testnestedfbuffer') assert my_class.isPackCompatible( ) == 0, "Class should be not pack compatible." assert my_class.isFixedWidth() == 1, "Width should be fixed." assert my_class.getWidth( ) == 3, "Width should be 3, got %d." % my_class.getWidth() c = pc.emitCode() #print "Got code of:" #print c pyfilename = os.path.join(properties.TEMP_DIR, "testnestedfbuffer.py") f = open(pyfilename, 'w') f.write(c) f.close() # Now try to import it. import testnestedfbuffer obj = testnestedfbuffer.testnestedfbuffer() #print 'Got object of: %s' % str(obj) #print 'Dir of new object: %s' % str(dir(obj)) #print obj.getChildren() #print obj.findChildByName('singlebyte') assert obj.isFixedWidth() == 1, "isFixedWidth() should be 1" assert obj.isPackCompatible() == 0, "isPackCompatible() should be 0" assert obj.getWidth( ) == 3, "Width should be 3, got %d" % obj.getWidth() assert obj.getNumItems( ) == 1, "NumItems should be 1, got %d" % obj.getNumItems()
def _test_case_dbuffer(self): lines = "" \ "# Dynamic Buffer Class\n" \ "class testdbuffer {\n" \ " dbuffer dbuffer01;\n" \ "}\n" pc = ProtoCompiler() x = protocompiler.DynamicBufferItem('dbuffer', 'mydbuffer', None) fname = os.path.join(properties.TEMP_DIR, 'dbuffer.class') f = open(fname, 'w') f.write(lines) f.close() #print lines pc.parseFile(fname) assert pc.classnames == ['testdbuffer' ], "Classnames != ['testdbuffer']" my_class = pc._getClass('testdbuffer') #print 'Got class of %s.' % str(my_class) assert my_class.isPackCompatible( ) == 0, "Class should be not pack compatible." assert my_class.isFixedWidth() == 0, "Width should not be fixed." assert my_class.getWidth( ) == 0, "Width should be 0, got %d." % my_class.getWidth() c = pc.emitCode() #print "Got code of:" #print c pyfilename = os.path.join(properties.TEMP_DIR, "testdbuffer.py") f = open(pyfilename, 'w') f.write(c) f.close() # Now try to import it. import testdbuffer obj = testdbuffer.testdbuffer() #print 'Got object of: %s' % str(obj) #print 'Dir of new object: %s' % str(dir(obj)) assert obj.isFixedWidth() == 0, "isFixedWidth() should be 0" assert obj.isPackCompatible() == 0, "isPackCompatible() should be 0" assert obj.getWidth( ) == 0, "Width should be 0, got %d" % obj.getWidth() assert obj.getNumItems( ) == 1, "NumItems should be 1, got %d" % obj.getNumItems() # Object has only one item, run some tests against that item item_obj = obj.items[0] #print 'str', item_obj #print 'dir', dir (item_obj) #width = item_obj.getWidth() #assert width == 2, "width should be 2, got %s" % str(width) os.remove(pyfilename)
def test_case_vbuffer1(self): lines = "" \ "# Variable Buffer 1 Class\n" \ "class testvbuffer1 {\n" \ " vbuffer1 vbuffer01;\n" \ "}\n" pc = ProtoCompiler() fname = os.path.join(properties.TEMP_DIR, 'vbuffer1.class') f = open(fname, 'w') f.write(lines) f.close() pc.parseFile(fname) assert pc.classnames == ['testvbuffer1' ], "Classnames != ['testvbuffer1']" my_class = pc._getClass('testvbuffer1') #print 'Got class of %s.' % str(my_class) assert my_class.isPackCompatible( ) == 0, "Class should be not pack compatible." assert my_class.isFixedWidth() == 0, "Width should not be fixed." assert my_class.getWidth( ) == 0, "Width should be 0, got %d." % my_class.getWidth() c = pc.emitCode() pyfilename = os.path.join(properties.TEMP_DIR, "testvbuffer1.py") f = open(pyfilename, 'w') f.write(c) f.close() pyfilename = os.path.join('/tmp', "testvbuffer1.py") f = open(pyfilename, 'w') f.write(c) f.close() # Now try to import it. import testvbuffer1 obj = testvbuffer1.testvbuffer1() assert obj.isFixedWidth() == 0, "isFixedWidth() should be 0" assert obj.isPackCompatible() == 0, "isPackCompatible() should be 0" assert obj.getWidth( ) == 0, "Width should be 0, got %d" % obj.getWidth() assert obj.getNumItems( ) == 1, "NumItems should be 1, got %d" % obj.getNumItems() # Object has only one item, run some tests against that item item_obj = obj.items[0] width = item_obj.getWidth() assert width == 1, "width should be 1, got %s" % str(width)
def test_case_allints(self): lines = "" \ "# All Ints Class\n" \ "class allints {\n" \ " uint8 data01;\n" \ " beuint8 data02;\n" \ " int8 data03;\n" \ " uint8 data04;\n" \ "" \ " uint16 data05;\n" \ " beuint16 data06;\n" \ " int16 data07;\n" \ " uint16 data08;\n" \ "" \ " uint32 data09;\n" \ " beuint32 data10;\n" \ " int32 data11;\n" \ " uint32 data12;\n" \ "" \ " uint64 data13;\n" \ " beuint64 data14;\n" \ " int64 data15;\n" \ " uint64 data16;\n" \ "}\n" pc = ProtoCompiler() fname = os.path.join(properties.TEMP_DIR, 'all_ints.class') f = open(fname, 'w') f.write(lines) f.close() pc.parseFile(fname) assert pc.classnames == ['allints'], "Classnames != ['allints']" my_class = pc._getClass('allints') #print 'Got class of %s.' % str(my_class) assert my_class.isPackCompatible( ) == 1, "Class should be pack compatible." exp_spec = '<B>B<b<B<H>H<h<H<L>L<l<L<Q>Q<q<Q' assert my_class.packSpec( ) == exp_spec, "Class spec should be '%s', got '%s'." % ( exp_spec, my_class.packSpec()) assert my_class.isFixedWidth() == 1, "Width should be fixed." assert my_class.getWidth( ) == 60, "Width should be 60, got %d." % my_class.getWidth() c = pc.emitCode() pyfilename = os.path.join(properties.TEMP_DIR, "allints.py") f = open(pyfilename, 'w') f.write(c) f.close() # Now try to import it. import allints obj = allints.allints() #print 'Dir of new object: %s' % str(dir(obj)) assert obj.isFixedWidth() == 1, "isFixedWidth() should be 1" assert obj.isPackCompatible() == 1, "isPackCompatible() should be 1" assert obj.packSpec() == exp_spec, "packSpec() should be %s" % exp_spec assert obj.getWidth( ) == 60, "Width should be 60, got %d" % obj.getWidth() assert obj.getNumItems( ) == 16, "NumItems should be 16, got %d" % obj.getNumItems()