示例#1
0
 def test_pack_str_which_is_too_long_should_truncate_to_fit(self):
     data = bytearray(7)
     data_offset = 0
     st = apx.VmPackState(value='Selected')
     data_offset = st.pack_str(data, data_offset, 7)
     self.assertEqual(data_offset, 7)
     self.assertEqual(data, bytearray('Selecte'.encode('utf-8')))
示例#2
0
 def test_pack_str_which_fits_exactly_shall_not_pad_with_zeros(self):
     data = bytearray(7)
     data_offset = 0
     st = apx.VmPackState(value='Selects')
     data_offset = st.pack_str(data, data_offset, 7)
     self.assertEqual(data_offset, 7)
     self.assertEqual(data, bytearray('Selects'.encode('utf-8')))
示例#3
0
 def test_pack_str_with_shorter_string_should_pad_with_zeros(self):
     data = bytearray(7)
     data_offset = 0
     st = apx.VmPackState(value='Select')
     data_offset = st.pack_str(data, data_offset, 7)
     self.assertEqual(data_offset, 7)
     self.assertEqual(data, bytearray('Select\0'.encode('utf-8')))
示例#4
0
 def test_pack_s16_array(self):
     st = apx.VmPackState(value=[-1, -2, -3])
     data = bytearray(6)
     data_offset = 0
     data_offset = st.pack_s16(data, data_offset, len(st.value))
     self.assertEqual(data_offset, apx.UINT16_LEN * len(st.value))
     self.assertEqual(data, bytearray([0xFF, 0xFF, 0xFE, 0xFF, 0xFD, 0xFF]))
示例#5
0
 def test_pack_u32_array(self):
     st = apx.VmPackState(value=[1, 2, 3])
     data = bytearray(12)
     data_offset = 0
     data_offset = st.pack_u32(data, data_offset, len(st.value))
     self.assertEqual(data_offset, apx.UINT32_LEN * len(st.value))
     self.assertEqual(data, bytearray([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0]))
示例#6
0
 def test_pack_u8_array(self):
     st = apx.VmPackState(value=[1, 2, 3])
     data = bytearray(3)
     data_offset = 0
     data_offset = st.pack_u8(data, data_offset, len(st.value))
     self.assertEqual(data_offset, apx.UINT8_LEN * len(st.value))
     self.assertEqual(data, bytearray([1, 2, 3]))
示例#7
0
 def test_pack_u8_to_record(self):
     st = apx.VmPackState(value={'a': 255, 'b': 7})
     data = bytearray([0, 0])
     data_offset = 0
     st.record_enter()
     st.record_select('a')
     data_offset = st.pack_u8(data, data_offset)
     self.assertEqual(data_offset, 1)
     self.assertEqual(data, bytearray([255, 0]))
     st.record_select('b')
     data_offset = st.pack_u8(data, data_offset)
     self.assertEqual(data_offset, 2)
     self.assertEqual(data, bytearray([255, 7]))
示例#8
0
 def test_pack_u32(self):
     st = apx.VmPackState(value=0)
     data = bytearray([0, 0, 0, 0])
     data_offset = st.pack_u32(data, 0)
     self.assertEqual(data_offset, apx.UINT32_LEN)
     self.assertEqual(data, bytearray([0x00, 0x00, 0x00, 0x00]))
     st.value = 0x12345678
     data_offset = st.pack_u32(data, 0)
     self.assertEqual(data_offset, apx.UINT32_LEN)
     self.assertEqual(data, bytearray([0x78, 0x56, 0x34, 0x12]))
     st.value = 0xFFFFFFFF
     data_offset = st.pack_u32(data, 0)
     self.assertEqual(data_offset, apx.UINT32_LEN)
     self.assertEqual(data, bytearray([0xFF, 0xFF, 0xFF, 0xFF]))
示例#9
0
 def test_pack_u16(self):
     st = apx.VmPackState(value=0)
     data = bytearray([0, 0])
     data_offset = st.pack_u16(data, 0)
     self.assertEqual(data_offset, apx.UINT16_LEN)
     self.assertEqual(data, bytearray([0x00, 0x00]))
     st.value = 0x1234
     data_offset = st.pack_u16(data, 0)
     self.assertEqual(data_offset, apx.UINT16_LEN)
     self.assertEqual(data, bytearray([0x34, 0x12]))
     st.value = 65535
     data_offset = st.pack_u16(data, 0)
     self.assertEqual(data_offset, apx.UINT16_LEN)
     self.assertEqual(data, bytearray([0xFF, 0xFF]))
示例#10
0
 def test_pack_u8(self):
     st = apx.VmPackState(value=1)
     data = bytearray([0, 0, 0])
     data_offset = 0
     data_offset = st.pack_u8(data, data_offset)
     self.assertEqual(data_offset, 1)
     self.assertEqual(data, bytearray([1, 0, 0]))
     st.value = 0x12
     data_offset = st.pack_u8(data, data_offset)
     self.assertEqual(data_offset, 2)
     self.assertEqual(data, bytearray([1, 0x12, 0]))
     st.value = 255
     data_offset = st.pack_u8(data, data_offset)
     self.assertEqual(data_offset, 3)
     self.assertEqual(data, bytearray([1, 0x12, 255]))
示例#11
0
 def test_pack_array_of_records_in_record(self):
     st = apx.VmPackState(
         value={
             'customers': [{
                 'id': 1,
                 'amount': 2
             }, {
                 'id': 2,
                 'amount': 10
             }],
             'priority': 4
         })
     data = bytearray(5)
     data_offset = 0
     st.record_enter()
     st.record_select('customers')
     self.assertEqual(st.key, 'customers')
     self.assertEqual(len(st.stack), 0)
     st.array_enter()
     self.assertEqual(len(st.stack), 1)
     self.assertIsNone(st.key)
     st.record_enter()
     self.assertEqual(st.value, {'id': 1, 'amount': 2})
     st.record_select('id')
     data_offset = st.pack_u8(data, data_offset)
     st.record_select('amount')
     data_offset = st.pack_u8(data, data_offset)
     self.assertEqual(data, bytearray([1, 2, 0, 0, 0]))
     st.record_leave()
     self.assertEqual(st.array_index, 1)
     st.record_enter()
     self.assertEqual(st.value, {'id': 2, 'amount': 10})
     st.record_select('id')
     data_offset = st.pack_u8(data, data_offset)
     st.record_select('amount')
     data_offset = st.pack_u8(data, data_offset)
     self.assertEqual(data, bytearray([1, 2, 2, 10, 0]))
     st.record_leave()
     st.array_leave()
     self.assertEqual(len(st.stack), 0)
     st.record_select('priority')
     data_offset = st.pack_u8(data, data_offset)
     self.assertEqual(data_offset, 5)
     self.assertEqual(data, bytearray([1, 2, 2, 10, 4]))
     self.assertEqual(st.key, 'priority')
     st.record_leave()
     self.assertIsNone(st.key)
示例#12
0
 def test_pack_s32(self):
     st = apx.VmPackState(value=0)
     data = bytearray(4)
     data_offset = st.pack_s32(data, 0)
     self.assertEqual(data_offset, apx.SINT32_LEN)
     self.assertEqual(data, bytearray([0, 0, 0, 0]))
     st.value = 2147483647
     data_offset = st.pack_s32(data, 0)
     self.assertEqual(data_offset, apx.SINT32_LEN)
     self.assertEqual(data, bytearray([0xFF, 0xFF, 0xFF, 0x7F]))
     st.value = -1
     data_offset = st.pack_s32(data, 0)
     self.assertEqual(data_offset, apx.SINT32_LEN)
     self.assertEqual(data, bytearray([0xFF, 0xFF, 0xFF, 0xFF]))
     st.value = -2147483648
     data_offset = st.pack_s32(data, 0)
     self.assertEqual(data_offset, apx.SINT32_LEN)
     self.assertEqual(data, bytearray([0x00, 0x00, 0x00, 0x80]))
示例#13
0
 def test_pack_s16(self):
     st = apx.VmPackState(value=0)
     data = bytearray(2)
     data_offset = st.pack_s16(data, 0)
     self.assertEqual(data_offset, apx.SINT16_LEN)
     self.assertEqual(data, bytearray([0, 0]))
     st.value = 32767
     data_offset = st.pack_s16(data, 0)
     self.assertEqual(data_offset, apx.SINT16_LEN)
     self.assertEqual(data, bytearray([0xFF, 0x7F]))
     st.value = -1
     data_offset = st.pack_s16(data, 0)
     self.assertEqual(data_offset, apx.SINT16_LEN)
     self.assertEqual(data, bytearray([0xFF, 0xFF]))
     st.value = -32768
     data_offset = st.pack_s16(data, 0)
     self.assertEqual(data_offset, apx.SINT16_LEN)
     self.assertEqual(data, bytearray([0x00, 0x80]))
示例#14
0
 def test_pack_s8(self):
     st = apx.VmPackState(value=0)
     data = bytearray(1)
     data_offset = st.pack_s8(data, 0)
     self.assertEqual(data_offset, apx.UINT8_LEN)
     self.assertEqual(data, bytearray([0]))
     st.value = 127
     data_offset = st.pack_s8(data, 0)
     self.assertEqual(data_offset, apx.UINT8_LEN)
     self.assertEqual(data, bytearray([127]))
     st.value = -1
     data_offset = st.pack_s8(data, 0)
     self.assertEqual(data_offset, apx.UINT8_LEN)
     self.assertEqual(data, bytearray([0xFF]))
     st.value = -128
     data_offset = st.pack_s8(data, 0)
     self.assertEqual(data_offset, apx.UINT8_LEN)
     self.assertEqual(data, bytearray([0x80]))
示例#15
0
 def test_pack_array_inside_array(self):
     st = apx.VmPackState(value=[[1, 2], [3, 4]])
     data = bytearray(4)
     data_offset = 0
     st.array_enter()
     st.array_enter()
     self.assertEqual(len(st.stack), 1)
     data_offset = st.pack_u8(data, data_offset, 2)
     self.assertEqual(data_offset, 2)
     self.assertEqual(data, bytearray([1, 2, 0, 0]))
     st.array_leave()
     self.assertEqual(len(st.stack), 0)
     self.assertEqual(st.array_index, 1)
     st.array_enter()
     self.assertEqual(len(st.stack), 1)
     data_offset = st.pack_u8(data, data_offset, 2)
     self.assertEqual(data_offset, 4)
     self.assertEqual(data, bytearray([1, 2, 3, 4]))
     st.array_leave()
     self.assertEqual(len(st.stack), 0)
     self.assertEqual(st.array_index, 2)
     st.array_leave()
     self.assertIsNone(st.array_index)
示例#16
0
 def test_pack_array_of_records(self):
     st = apx.VmPackState(value=[
         {
             'a': 255,
             'b': 7
         },
         {
             'a': 29,
             'b': 4
         },
         {
             'a': 0,
             'b': 200
         },
     ])
     data = bytearray(6)
     data_offset = 0
     self.assertIsNone(st.array_index)
     st.array_enter()
     self.assertEqual(st.array_index, 0)
     self.assertEqual(len(st.stack), 0)
     st.record_enter()
     self.assertIsNone(st.array_index)
     self.assertEqual(len(st.stack), 1)
     self.assertEqual(st.value, {'a': 255, 'b': 7})
     st.record_select('a')
     data_offset = st.pack_u8(data, data_offset)
     self.assertEqual(data, bytearray([255, 0, 0, 0, 0, 0]))
     st.record_select('b')
     data_offset = st.pack_u8(data, data_offset)
     self.assertEqual(data_offset, 2)
     self.assertEqual(data, bytearray([255, 7, 0, 0, 0, 0]))
     st.record_leave()
     self.assertEqual(len(st.stack), 0)
     self.assertEqual(st.array_index, 1)
     st.record_enter()
     self.assertIsNone(st.array_index)
     self.assertEqual(len(st.stack), 1)
     self.assertEqual(st.value, {'a': 29, 'b': 4})
     st.record_select('a')
     data_offset = st.pack_u8(data, data_offset)
     self.assertEqual(data, bytearray([255, 7, 29, 0, 0, 0]))
     st.record_select('b')
     data_offset = st.pack_u8(data, data_offset)
     self.assertEqual(data_offset, 4)
     self.assertEqual(data, bytearray([255, 7, 29, 4, 0, 0]))
     st.record_leave()
     self.assertEqual(len(st.stack), 0)
     self.assertEqual(st.array_index, 2)
     st.record_enter()
     self.assertIsNone(st.array_index)
     self.assertEqual(len(st.stack), 1)
     self.assertEqual(st.value, {'a': 0, 'b': 200})
     st.record_select('a')
     data_offset = st.pack_u8(data, data_offset)
     self.assertEqual(data, bytearray([255, 7, 29, 4, 0, 0]))
     st.record_select('b')
     data_offset = st.pack_u8(data, data_offset)
     self.assertEqual(data_offset, 6)
     self.assertEqual(data, bytearray([255, 7, 29, 4, 0, 200]))
     st.record_leave()
     self.assertEqual(len(st.stack), 0)
     self.assertEqual(st.array_index, 3)
     st.array_leave()
     self.assertIsNone(st.array_index)