Beispiel #1
0
    def testEncodeDeepTree ( self ):
        rawByteStr = ''.join ( chr ( idx % 256 ) for idx in range ( 512 ) )
        rawShortSeq = self.__createIntSeq ( 15, 16 )
        rawIntSeq = [ 0l ] * 15
        rawLongSeq = [ 0l ] * 15
        rawFloatSeq = [ 0.0 ] * 15
        rawDoubleSeq = [ float ( i ) / 10.0 for i in self.__createIntSeq ( 15, 64 ) ]

        # Construct the message
        message1 = Message ( )
        message1.addField ( None,                'Indicator' )
        message1.addField ( True,                'Boolean' )
        message1.addField ( -128,                'Byte' )
        message1.addField ( -32768,              'Short' )
        message1.addField ( 2147483647,          'Int' )
        message1.addField ( 9223372036854775807, 'Long' )
        message1.addFieldF32 ( 1.23456,          'Float' )
        message1.addField ( 1.2345678,           'Double' )

        submessage = Message ( )
        submessage.addField4ByteArray   ( rawByteStr [ :   4 ], ordinal = 4 )
        submessage.addField8ByteArray   ( rawByteStr [ :   8 ], ordinal = 8 )
        submessage.addField16ByteArray  ( rawByteStr [ :  16 ], ordinal = 16 )
        submessage.addField20ByteArray  ( rawByteStr [ :  20 ], ordinal = 20 )
        submessage.addField32ByteArray  ( rawByteStr [ :  32 ], ordinal = 32 )
        submessage.addField64ByteArray  ( rawByteStr [ :  64 ], ordinal = 64 )
        submessage.addField128ByteArray ( rawByteStr [ : 128 ], ordinal = 128 )
        submessage.addField256ByteArray ( rawByteStr [ : 256 ], ordinal = 256 )
        submessage.addField512ByteArray ( rawByteStr [ : 512 ], ordinal = 512 )
        message1.addField ( submessage, 'ByteArrays' )

        message1.addField ( '',                  u'Empty String' )
        message1.addField ( 'This is a string.', u'String' )

        submessage = Message ( )
        subsubmessage = Message ( )
        submessage.addFieldByteArray ( [ ],                 'Byte[0]' )
        submessage.addFieldByteArray ( rawByteStr [ : 15 ], 'Byte[15]' )
        submessage.addField ( subsubmessage, 'FP Arrays' )
        submessage.addFieldI16Array  ( [] ,                 'Short[0]' )
        submessage.addFieldI16Array  ( rawShortSeq,         'Short[15]' )
        submessage.addFieldI32Array  ( [] ,                 'Int[0]' )
        submessage.addFieldI32Array  ( rawIntSeq,           'Int[15]' )
        submessage.addFieldI64Array  ( [] ,                 'Long[0]' )
        submessage.addFieldI64Array  ( rawLongSeq,          'Long[15]' )
        message1.addField ( submessage, 'Arrays' )

        subsubmessage.addFieldF32Array ( [ ],          'Float[0]' )
        subsubmessage.addFieldF32Array ( rawFloatSeq,  'Float[15]' )
        subsubmessage.addFieldF64Array ( [ ],          'Double[0]' )
        subsubmessage.addFieldF64Array ( rawDoubleSeq, 'Double[15]' )

        message1.addField ( Message ( ), 'Null Message' )

        # Encode it and compare against the test file
        encoded = Envelope ( message1 ).encode ( )
        reference = self.__loadFile ( 'DEEPERTREE' )
        self.assertEqual ( encoded, reference )
Beispiel #2
0
    def testEncodeFixedWidths ( self ):
        rawByteSeq = self.__createIntSeq ( 512, 8 )

        # Construct the message
        message1 = Message ( )
        message1.addField4ByteArray   ( rawByteSeq [ :   4 ], 'byte[4]' )
        message1.addField8ByteArray   ( rawByteSeq [ :   8 ], 'byte[8]' )
        message1.addField16ByteArray  ( rawByteSeq [ :  16 ], 'byte[16]' )
        message1.addField20ByteArray  ( rawByteSeq [ :  20 ], 'byte[20]' )
        message1.addField32ByteArray  ( rawByteSeq [ :  32 ], 'byte[32]' )
        message1.addField64ByteArray  ( rawByteSeq [ :  64 ], 'byte[64]' )
        message1.addField128ByteArray ( rawByteSeq [ : 128 ], 'byte[128]' )
        message1.addField256ByteArray ( rawByteSeq [ : 256 ], 'byte[256]' )
        message1.addField512ByteArray ( rawByteSeq [ : 512 ], 'byte[512]' )
        message1.addFieldByteArray    ( rawByteSeq [ :  28 ], 'byte[28]' )

        # Encode it and compare against the test file
        encoded = Envelope ( message1 ).encode ( )
        reference = self.__loadFile ( 'FIXEDWIDTH' )
        self.assertEqual ( encoded, reference )
Beispiel #3
0
    def testEncodeVariableWidths ( self ):
        # Construct the message
        message1 = Message ( )
        message1.addFieldByteArray ( [ 0 ] * 100,    '100' )
        message1.addFieldByteArray ( [ 0 ] * 1000,   '1000' )
        message1.addFieldByteArray ( [ 0 ] * 100000, '10000' )

        # Encode it and compare against the test file
        encoded = Envelope ( message1 ).encode ( )
        reference = self.__loadFile ( 'VARIABLEWIDTH' )
        self.assertEqual ( encoded, reference )
Beispiel #4
0
    def testFieldFunctions ( self ):
        # Set up the source data
        rawBytesSeq = [ -0x00, -0x11, -0x23, -0x34, -0x45, -0x56, -0x67, -0x77, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00 ]
        rawShortsSeq = ( -32767, 32767, 0, 1, -1, 100, -100, 0, 16385 )
        rawIntsSeq = [ 2147483647, 0, -2147483647, 0 ]
        rawLongsSeq = ( -9223372036854775807l, 0, 9223372036854775807l, -1, 2, -3, 5, -8, 13, -21, 34, -55 )
        rawFloatsSeq = [ 0.0, 2147483647.0, 214748364.7, 21474836.47, 2147483.647, 2.147483647, 21.47483647, 214.7483647 ]
        rawDoublesSeq = ( 9223372036854775807.0, 0.0, 0.0000000123456, 1234560000000.0, -9223372036854775807.0 )
        rawDate = datetime.date ( 1962, 2, 20 )
        rawTime = datetime.time ( 11, 41, 19, 123456, TestTimeZone ( ) )
        rawDateTime = datetime.datetime ( 2012, 2, 19, 11, 43, 16 )

        # Will need a variety of large byte strings and arrays - bit of
        # hackery required to support Fudge's signed bytes and Python's
        # unsigned characters
        fixedByteArrays = ( ( 1, 4,   fudgepyc.types.BYTE_ARRAY_4 ),
                            ( 2, 8,   fudgepyc.types.BYTE_ARRAY_8 ),
                            ( 3, 16,  fudgepyc.types.BYTE_ARRAY_16 ),
                            ( 4, 20,  fudgepyc.types.BYTE_ARRAY_20 ),
                            ( 5, 32,  fudgepyc.types.BYTE_ARRAY_32 ),
                            ( 6, 64,  fudgepyc.types.BYTE_ARRAY_64 ),
                            ( 7, 128, fudgepyc.types.BYTE_ARRAY_128 ),
                            ( 8, 256, fudgepyc.types.BYTE_ARRAY_256 ),
                            ( 9, 512, fudgepyc.types.BYTE_ARRAY_512 ) )

        largeByteArrays = dict ( ( sz, self.__generateByteArray ( sz ) ) for _, sz, _ in fixedByteArrays )
        largeByteStrings = dict ( ( sz, ''.join ( chr ( i % 256 ) for i in ar ) ) for sz, ar in largeByteArrays.iteritems ( ) )

        # Construct an empty message
        message1 = Message ( )
        self.assertEqual ( len ( message1 ), 0 )

        # Test some failure cases
        self.assertRaises ( fudgepyc.Exception, message1.getFieldAtIndex, 0 )
        self.assertEqual ( message1.getFieldByOrdinal ( 0 ), None )
        self.assertEqual ( message1.getFieldByName ( 'Field Name' ), None )

        self.assertRaises ( fudgepyc.Exception,
                            message1.addFieldBool,
                            True,
                            "This message field name is too long. The limit for message field names is 255 characters; "
                            "this is because the name's length must be stored in a single leading byte (a bit like a "
                            "length limited version Pascal). An exception should be thrown containing the "
                            "FUDGE_NAME_TOO_LONG code." )

        # Add indicator, boolean, integer and float fields
        message1.addFieldIndicator ( )
        message1.addFieldBool ( True )
        message1.addFieldByte ( 127 )
        message1.addFieldI16 ( 32767 )
        message1.addFieldI32 ( -2147483647 )
        message1.addFieldI64 ( 9223372036854775807l )
        message1.addFieldF32 ( 2147483647.0 )
        message1.addFieldF64 ( -9223372036854775807.0 )

        self.assertEqual ( len ( message1 ), 8 )

        # Add date, time and datetime fields
        message1.addFieldDate ( rawDate )
        message1.addFieldTime ( rawTime )
        message1.addField ( rawDateTime )

        self.assertEqual ( len ( message1 ), 11 )

        # Add fixed width byte arrays in a sub message
        message2 = Message ( )
        for ordinal, size, _ in fixedByteArrays:
            getattr ( message2, 'addField%dByteArray' % size ) ( largeByteStrings [ size ], ordinal = ordinal )

        self.assertEqual ( len ( message2 ), 9 )
        message1.addFieldMsg ( message2, name = 'Byte Array SubMessage' )

        # Add empty and populate and unicode strings
        message1.addFieldString ( '', name = 'Empty string' )
        message1.addFieldString ( 'This is a string', name = 'String' )
        message1.addFieldString ( u'This is unicode', name = 'Unicode' )

        self.assertEqual ( len ( message1 ), 15 )

        # Add empty and populated arrays in a sub message
        message2 = Message ( )
        self.assertEqual ( len ( message2 ), 0 )
        message2.addFieldByteArray ( [ ], name = 'No Bytes' )
        message2.addFieldByteArray ( rawBytesSeq, name = 'Bytes' )
        message2.addFieldI16Array ( rawShortsSeq, name = 'Shorts' )
        message2.addFieldI32Array ( rawIntsSeq, name = 'Ints' )
        message2.addFieldI64Array ( rawLongsSeq, name = 'Longs' )
        message2.addFieldF32Array ( rawFloatsSeq, name = 'Floats' )
        message2.addFieldF64Array ( rawDoublesSeq, name = 'Doubles' )

        self.assertEqual ( len ( message2 ), 7 )
        message1.addFieldMsg ( message2, name = 'Array SubMessage' )

        # Add an empty sub message
        message2 = Message ( )
        message1.addFieldMsg ( message2, name = 'Empty SubMessage' )

        self.assertEqual ( len ( message1 ), 17 )

        # Retrieve the top-level fields and check their contents
        fields = message1.getFields ( )
        self.assertEqual ( len ( fields ), 17 )
        self.assert_ ( all ( isinstance ( fld, Field ) for fld in fields ) )

        self.assertEqual ( fields [ 0 ].type ( ), fudgepyc.types.INDICATOR )
        self.assertEqual ( fields [ 0 ].value ( ), None )
        self.assertEqual ( fields [ 1 ].type ( ), fudgepyc.types.BOOLEAN )
        self.assertEqual ( fields [ 1 ].getBool ( ), True )
        self.assertEqual ( fields [ 1 ].value ( ), True )
        self.assertEqual ( fields [ 2 ].type ( ), fudgepyc.types.BYTE )
        self.assertEqual ( fields [ 2 ].getByte ( ), 127 )
        self.assertEqual ( fields [ 2 ].value ( ), 127 )
        self.assertEqual ( fields [ 3 ].type ( ), fudgepyc.types.SHORT )
        self.assertEqual ( fields [ 3 ].getInt16 ( ), 32767 )
        self.assertEqual ( fields [ 3 ].value ( ), 32767 )
        self.assertEqual ( fields [ 4 ].type ( ), fudgepyc.types.INT )
        self.assertEqual ( fields [ 4 ].getInt32 ( ), -2147483647 )
        self.assertEqual ( fields [ 4 ].value ( ), -2147483647 )
        self.assertEqual ( fields [ 5 ].type ( ), fudgepyc.types.LONG )
        self.assertEqual ( fields [ 5 ].getInt64 ( ), 9223372036854775807l )
        self.assertEqual ( fields [ 5 ].value ( ), 9223372036854775807l )
        self.assertEqual ( fields [ 6 ].type ( ), fudgepyc.types.FLOAT )
        self.assertAlmostEqual ( fields [ 6 ].getFloat32 ( ), 2147483647.0, -1 )
        self.assertAlmostEqual ( fields [ 6 ].value ( ), 2147483647.0, -1 )
        self.assertEqual ( fields [ 7 ].type ( ), fudgepyc.types.DOUBLE )
        self.assertAlmostEqual ( fields [ 7 ].getFloat64 ( ), -9223372036854775807.0, -1 )
        self.assertAlmostEqual ( fields [ 7 ].value ( ), -9223372036854775807.0, -1 )
        self.assertEqual ( fields [ 8 ].getDate ( ), rawDate )
        self.assertEqual ( fields [ 8 ].value ( ), rawDate )
        self.assertEqual ( fields [ 9 ].getTime ( ), rawTime )
        self.assertEqual ( fields [ 9 ].value ( ), rawTime )
        self.assert_ ( isinstance ( fields [ 9 ].value ( ).tzinfo, fudgepyc.timezone.Timezone ) )
        self.assertEqual ( fields [ 10 ].value ( ), rawDateTime )
        self.assertEqual ( fields [ 10 ].getDateTime ( ), rawDateTime )
        self.assertEqual ( fields [ 10 ].value ( ).tzinfo, None )
        self.assertEqual ( fields [ 11 ].type ( ), fudgepyc.types.MESSAGE )
        self.assert_ ( isinstance ( fields [ 11 ].getMessage ( ), Message ) )
        self.assert_ ( isinstance ( fields [ 11 ].value ( ), Message ) )
        self.assertEqual ( fields [ 12 ].type ( ), fudgepyc.types.STRING )
        self.assertEqual ( fields [ 12 ].getString ( ), '' )
        self.assertEqual ( fields [ 12 ].value ( ), '' )
        self.assertEqual ( fields [ 13 ].type ( ), fudgepyc.types.STRING )
        self.assertEqual ( fields [ 13 ].getString ( ), 'This is a string' )
        self.assertEqual ( fields [ 13 ].value ( ), 'This is a string' )
        self.assertEqual ( fields [ 14 ].type ( ), fudgepyc.types.STRING )
        self.assertEqual ( fields [ 14 ].getString ( ), 'This is unicode' )
        self.assertEqual ( fields [ 14 ].value ( ), 'This is unicode' )
        self.assertEqual ( fields [ 15 ].type ( ), fudgepyc.types.MESSAGE )
        self.assert_ ( isinstance ( fields [ 15 ].getMessage ( ), Message ) )
        self.assert_ ( isinstance ( fields [ 15 ].value ( ), Message ) )
        self.assertEqual ( fields [ 16 ].type ( ), fudgepyc.types.MESSAGE )
        self.assert_ ( isinstance ( fields [ 16 ].getMessage ( ), Message ) )
        self.assert_ ( isinstance ( fields [ 16 ].value ( ), Message ) )

        field1 = message1.getFieldByName ( 'Empty string' )
        self.assertEqual ( field1.type ( ), fudgepyc.types.STRING )
        self.assertEqual ( field1.value ( ), u'' )
        field1 = message1 [ 'Empty SubMessage' ]
        self.assertEqual ( field1.type ( ), fudgepyc.types.MESSAGE )
        self.assertEqual ( len ( field1.value ( ) ), 0 )

        self.assertEqual ( message1.getFieldByName ( '' ), None )
        self.assertEqual ( message1.getFieldByName ( 'Bytes' ), None )
        self.assertRaises ( LookupError, message1.__getitem__, 'null string' )

        self.assertEqual ( len ( fields [ 0 ] ), 0 )
        self.assertEqual ( len ( fields [ 2 ] ), 1 )
        self.assertEqual ( len ( fields [ 15 ] ), 7 )

        self.assertEqual ( fields [ 2 ].ordinal ( ), None )
        self.assertEqual ( fields [ 2 ].name ( ), None )
        self.assertEqual ( fields [ 13 ].ordinal ( ), None )
        self.assertEqual ( fields [ 13 ].name ( ), 'String' )

        # Check the fixed array message
        message2 = fields [ 11 ].getMessage ( )
        self.assertEqual ( len ( message2 ), 9 )
        fields = message2.getFields ( )

        for idx in range ( len ( message2 ) ):
            ordinal, size, fudgetype = fixedByteArrays [ idx ]
            self.assertEqual ( fields [ idx ].type ( ), fudgetype )
            self.assertEqual ( fields [ idx ].numbytes ( ), size )
            self.assertEqual ( len ( fields [ idx ] ), size )
            self.assertEqual ( fields [ idx ].bytes ( ), largeByteStrings [ size ] )
            self.assertEqual ( fields [ idx ].value ( ), largeByteStrings [ size ] )
            self.assertEqual ( fields [ idx ].getByteArray ( ), largeByteArrays [ size ] )
            self.assertEqual ( fields [ idx ].name ( ), None )
            self.assertEqual ( fields [ idx ].ordinal ( ), ordinal )

        field1 = message2.getFieldByOrdinal ( 1 )
        self.assertEqual ( field1.type ( ), fudgepyc.types.BYTE_ARRAY_4 )
        field1 = message2.getFieldByOrdinal ( 4 )
        self.assertEqual ( field1.type ( ), fudgepyc.types.BYTE_ARRAY_20 )

        self.assertEqual ( message2.getFieldByOrdinal ( 0 ), None )
        self.assertRaises ( LookupError, message2.__getitem__, 10 )
        self.assertRaises ( OverflowError, message2.__getitem__, -1 )

        # Check the variable array message
        field1 = message1.getFieldAtIndex ( 15 )
        self.assertEqual ( field1.type ( ), fudgepyc.types.MESSAGE )
        self.assertEqual ( len ( field1 ), 7 )
        fields = field1.value ( ).getFields ( )
        self.assertEqual ( len ( fields ), 7 )

        self.assertEqual ( fields [ 0 ].type ( ), fudgepyc.types.BYTE_ARRAY )
        self.assertEqual ( fields [ 0 ].getByteArray ( ), [ ] )
        self.assertEqual ( fields [ 0 ].value ( ), '' )
        self.assertEqual ( fields [ 1 ].type ( ), fudgepyc.types.BYTE_ARRAY )
        self.assertEqual ( fields [ 1 ].getByteArray ( ), rawBytesSeq )
        self.assertEqual ( fields [ 1 ].value ( ), ''.join ( chr ( i % 256 ) for i in rawBytesSeq ) )
        self.assertEqual ( fields [ 2 ].type ( ), fudgepyc.types.SHORT_ARRAY )
        self.assertEqual ( fields [ 2 ].getInt16Array ( ), list ( rawShortsSeq ) )
        self.assertEqual ( fields [ 2 ].value ( ), list ( rawShortsSeq ) )
        self.assertEqual ( fields [ 3 ].type ( ), fudgepyc.types.INT_ARRAY )
        self.assertEqual ( fields [ 3 ].getInt32Array ( ), rawIntsSeq )
        self.assertEqual ( fields [ 3 ].value ( ), rawIntsSeq )
        self.assertEqual ( fields [ 4 ].type ( ), fudgepyc.types.LONG_ARRAY )
        self.assertEqual ( fields [ 4 ].getInt64Array ( ), list ( rawLongsSeq ) )
        self.assertEqual ( fields [ 4 ].value ( ), list ( rawLongsSeq ) )
        self.assertEqual ( fields [ 5 ].type ( ), fudgepyc.types.FLOAT_ARRAY )
        self.__compareFloatArray ( fields [ 5 ].getFloat32Array ( ), rawFloatsSeq, 8  )
        self.__compareFloatArray ( fields [ 5 ].value ( ), rawFloatsSeq, 8  )
        self.assertEqual ( fields [ 6 ].type ( ), fudgepyc.types.DOUBLE_ARRAY )
        self.__compareFloatArray ( fields [ 6 ].getFloat64Array ( ), rawDoublesSeq, 12 )
        self.__compareFloatArray ( fields [ 6 ].value ( ), rawDoublesSeq, 12 )

        message2 = message1.getFieldAtIndex ( 15 ).value ( )
        field1 = message2.getFieldByName ( 'Bytes' )
        self.assertEqual ( field1.type ( ), fudgepyc.types.BYTE_ARRAY )
        self.assertRaises ( fudgepyc.Exception, field1.getInt16Array )
        field1 = message2 [ 'Floats' ]
        self.assertEqual ( field1.type ( ), fudgepyc.types.FLOAT_ARRAY )
        self.assertRaises ( fudgepyc.Exception, field1.getByteArray )

        self.assertEqual ( message2.getFieldByName ( 'Empty SubMessage' ), None )
        self.assertEqual ( message2.getFieldByOrdinal ( 1 ), None )
        self.assertRaises ( LookupError, message2.__getitem__, 'Empty SubMessage' )
        self.assertRaises ( LookupError, message2.__getitem__, 1 )

        # Check the empty message
        field1 = message1.getFieldAtIndex ( 16 )
        self.assertEqual ( field1.type ( ), fudgepyc.types.MESSAGE )
        self.assertEqual ( len ( field1 ), 0 )
        self.assertEqual ( field1.name ( ), u'Empty SubMessage' )
        self.assertEqual ( field1.ordinal ( ), None )
        message2 = field1.value ( )
        self.assertEqual ( len ( message2 ), 0 )