Пример #1
0
 def test_object_key_error(self):
     with self.assertRaises(KeyError):
         block = {
             "key":
             "object missing key",
             "type":
             "object",
             "blocklist": [
                 {
                     "key": "key1",
                     "type": "integer",
                     "bits": 3
                 },
                 {
                     "key": "key2",
                     "type": "float",
                     "bits": 5
                 },
                 {
                     "key": "key3",
                     "type": "boolean"
                 },
             ],
         }
         t = {"key1": 6, "key2": 0.9}
         spos.encode_block(t, block)
Пример #2
0
 def test_binary_block(self):
     block = {"key": "binary bin", "type": "binary", "bits": 10}
     t = "0b100101"
     a = "0b0000100101"
     self.assertEqual(spos.encode_block(t, block), a)
     self.assertEqual(spos.decode_block(a, block), a)
     self.assertEqual(spos.encode_block(a, block), a)
Пример #3
0
 def test_integer_type_error(self):
     with self.assertRaises(TypeError):
         block = {
             "key": "integer fail",
             "type": "integer",
             "bits": 6,
         }
         spos.encode_block("fail", block)
Пример #4
0
 def test_binary_type_error(self):
     with self.assertRaises(ValueError):
         block = {
             "key": "binary type fail",
             "type": "binary",
             "bits": 10,
         }
         spos.encode_block("fail", block)
Пример #5
0
 def test_float_type_error(self):
     with self.assertRaises(TypeError):
         block = {
             "key": "float fail",
             "type": "float",
             "bits": 4,
         }
         spos.encode_block("fail", block)
Пример #6
0
 def test_categories_error_raise(self):
     block = {
         "key": "categories",
         "type": "categories",
         "categories": ["critical", "low", "charged", "full"],
     }
     t = "falafel"
     with self.assertRaises(ValueError) as ctx:
         spos.encode_block(t, block)
     self.assertEqual("Invalid value for category.", str(ctx.exception))
Пример #7
0
 def test_static_value(self):
     static_value = "0b10101"
     block = {
         "key": "binary bin",
         "type": "binary",
         "value": static_value,
         "bits": 5,
     }
     self.assertEqual(spos.encode_block(None, block), static_value)
     self.assertEqual(spos.encode_block("0b11", block), static_value)
     self.assertEqual(spos.encode_block(static_value, block), static_value)
Пример #8
0
 def test_steps_error_unordered_steps(self):
     block = {
         "key": "steps",
         "type": "steps",
         "steps": [5, 0, 10],
         "steps_names": ["one", "two"],
     }
     t = 1
     a = "0b10"
     with self.assertRaises(ValueError):
         spos.encode_block(t, block)
     with self.assertRaises(ValueError):
         spos.decode_block(a, block)
Пример #9
0
 def test_array_fixed_length_error(self):
     block = {
         "key": "fixed array length error",
         "type": "array",
         "length": 4,
         "fixed": True,
         "blocks": {
             "key": "array val",
             "type": "integer",
             "bits": 3
         },
     }
     t = [0, 1, 2]
     with self.assertRaises(ValueError):
         spos.encode_block(t, block)
Пример #10
0
 def test_integer_underflow(self):
     block = {
         "key": "integer underflow",
         "type": "integer",
         "bits": 6,
     }
     t = -10
     a = "0b000000"
     self.assertEqual(spos.encode_block(t, block), a)
Пример #11
0
 def test_binary_small_hex(self):
     block = {
         "key": "binary small hex",
         "type": "binary",
         "bits": 10,
     }
     t = "0xff"
     a = truncate_bits(bin(int(t, 16))[:12], block["bits"])
     self.assertEqual(spos.encode_block(t, block), a)
Пример #12
0
 def test_float_overflow(self):
     block = {
         "key": "float overflow",
         "type": "float",
         "bits": 6,
     }
     t = 10
     a = "0b111111"
     self.assertEqual(spos.encode_block(t, block), a)
Пример #13
0
 def test_integer_overflow(self):
     block = {
         "key": "integer overflow",
         "type": "integer",
         "bits": 6,
     }
     t = 128
     a = "0b111111"
     self.assertEqual(spos.encode_block(t, block), a)
Пример #14
0
 def test_float_underflow(self):
     block = {
         "key": "float underflow",
         "type": "float",
         "bits": 6,
     }
     t = -10
     a = "0b000000"
     self.assertEqual(spos.encode_block(t, block), a)
Пример #15
0
 def test_categories_error_none(self):
     block = {
         "key": "categories",
         "type": "categories",
         "categories": ["critical", "low", "charged", "full"],
     }
     t = "low"
     a = "0b01"
     self.assertEqual(spos.encode_block(t, block), a)
Пример #16
0
 def test_integer_block(self):
     block = {
         "key": "integer test",
         "type": "integer",
         "bits": 6,
     }
     t = 1
     a = "0b000001"
     self.assertEqual(spos.encode_block(t, block), a)
     self.assertEqual(spos.decode_block(a, block), t)
Пример #17
0
 def test_binary_hex(self):
     block = {
         "key": "binary large hex",
         "type": "binary",
         "bits": 10,
     }
     t = "0xdeadbeef"
     a = bin(int(t, 16))[:12]
     self.assertEqual(spos.encode_block(t, block), a)
     self.assertEqual(spos.decode_block(a, block), a)
Пример #18
0
 def test_categories_block_3(self):
     block = {
         "key": "categories",
         "type": "categories",
         "categories": ["critical", "low", "charged", "full"],
     }
     t = "full"
     a = "0b11"
     self.assertEqual(spos.encode_block(t, block), a)
     self.assertEqual(spos.decode_block(a, block), t)
Пример #19
0
 def test_object_dot_notation_0(self):
     block = {
         "key":
         "object",
         "type":
         "object",
         "blocklist": [
             {
                 "key": "key1",
                 "type": "integer",
                 "bits": 3
             },
             {
                 "key": "key2.surprise",
                 "type": "string",
                 "length": 5
             },
             {
                 "key":
                 "key2",
                 "type":
                 "object",
                 "blocklist": [
                     {
                         "key": "nKey",
                         "type": "boolean"
                     },
                     {
                         "key":
                         "nKey2",
                         "type":
                         "object",
                         "blocklist": [{
                             "key": "nKey3",
                             "type": "float",
                             "bits": 8
                         }],
                     },
                 ],
             },
         ],
     }
     t = {
         "key1": 6,
         "key2": {
             "surprise": "hello",
             "nKey": False,
             "nKey2": {
                 "nKey3": 0.8
             },
         },
     }
     a = "0b110100001011110100101100101101000011001100"
     self.assertEqual(spos.encode_block(t, block), a)
     self.assertDict(spos.decode_block(a, block), t)
Пример #20
0
 def test_integer_offset(self):
     block = {
         "key": "integer offset",
         "type": "integer",
         "bits": 6,
         "offset": 100,
     }
     t = 120
     a = "0b010100"
     self.assertEqual(spos.encode_block(t, block), a)
     self.assertEqual(spos.decode_block(a, block), t)
Пример #21
0
 def test_steps_auto_steps_names(self):
     block = {
         "key": "steps",
         "type": "steps",
         "steps": [0, 5, 10],
     }
     t = 1
     a = "0b01"
     t_dec = "0<=x<5"
     self.assertEqual(spos.encode_block(t, block), a)
     self.assertEqual(spos.decode_block(a, block), t_dec)
Пример #22
0
 def test_float_upper(self):
     block = {
         "key": "float upper",
         "type": "float",
         "bits": 5,
         "upper": 31,
     }
     t = 13
     a = "0b01101"
     self.assertEqual(spos.encode_block(t, block), a)
     self.assertClose(spos.decode_block(a, block), t)
Пример #23
0
 def test_categories_error_new_valid(self):
     block = {
         "key": "categories",
         "type": "categories",
         "categories": ["critical", "low", "charged", "full"],
         "error": "invalid_category",
     }
     t = "full"
     a = "0b011"
     self.assertEqual(spos.encode_block(t, block), a)
     self.assertEqual(spos.decode_block(a, block), t)
Пример #24
0
 def test_float_lower(self):
     block = {
         "key": "float lower",
         "type": "float",
         "bits": 3,
         "lower": -6,
     }
     t = -1
     a = "0b101"
     self.assertEqual(spos.encode_block(t, block), a)
     self.assertClose(spos.decode_block(a, block), t)
Пример #25
0
 def test_string_block_unknown_character(self):
     block = {
         "key": "string",
         "type": "string",
         "length": 6,
     }
     t = "test%"
     a = "0b111110101101011110101100101101111111"
     t_dec = "+test/"
     self.assertEqual(spos.encode_block(t, block), a)
     self.assertEqual(spos.decode_block(a, block), t_dec)
Пример #26
0
 def test_string_block(self):
     block = {
         "key": "string",
         "type": "string",
         "length": 6,
     }
     t = "test"
     a = "0b111110111110101101011110101100101101"
     t_dec = "++test"
     self.assertEqual(spos.encode_block(t, block), a)
     self.assertEqual(spos.decode_block(a, block), t_dec)
Пример #27
0
 def test_steps_last_index(self):
     block = {
         "key": "steps",
         "type": "steps",
         "steps": [0, 5, 10],
         "steps_names": ["critical", "low", "charged", "full"],
     }
     t = 11
     a = "0b11"
     t_dec = "full"
     self.assertEqual(spos.encode_block(t, block), a)
     self.assertEqual(spos.decode_block(a, block), t_dec)
Пример #28
0
 def test_categories_key_error_existent(self):
     block = {
         "key": "categories",
         "type": "categories",
         "categories": ["critical", "low", "charged", "full"],
         "error": "critical",
     }
     t = "invalid"
     a = "0b00"
     t_dec = "critical"
     self.assertEqual(spos.encode_block(t, block), a)
     self.assertEqual(spos.decode_block(a, block), t_dec)
Пример #29
0
 def test_steps_lower_boundary(self):
     block = {
         "key": "steps",
         "type": "steps",
         "steps": [0, 5, 10],
         "steps_names": ["critical", "low", "charged", "full"],
     }
     t = 5
     a = "0b10"
     t_dec = "charged"
     self.assertEqual(spos.encode_block(t, block), a)
     self.assertEqual(spos.decode_block(a, block), t_dec)
Пример #30
0
 def test_float_approximation_ceil(self):
     block = {
         "key": "float approximation ceil",
         "type": "float",
         "bits": 2,
         "approximation": "ceil",
     }
     t = 0.34
     a = "0b10"
     t_dec = 0.66
     self.assertEqual(spos.encode_block(t, block), a)
     self.assertClose(spos.decode_block(a, block), t_dec)