| OLD | NEW |
| 1 # Copyright 2013 the V8 project authors. All rights reserved. | 1 # Copyright 2013 the V8 project authors. All rights reserved. |
| 2 # Redistribution and use in source and binary forms, with or without | 2 # Redistribution and use in source and binary forms, with or without |
| 3 # modification, are permitted provided that the following conditions are | 3 # modification, are permitted provided that the following conditions are |
| 4 # met: | 4 # met: |
| 5 # | 5 # |
| 6 # * Redistributions of source code must retain the above copyright | 6 # * Redistributions of source code must retain the above copyright |
| 7 # notice, this list of conditions and the following disclaimer. | 7 # notice, this list of conditions and the following disclaimer. |
| 8 # * Redistributions in binary form must reproduce the above | 8 # * Redistributions in binary form must reproduce the above |
| 9 # copyright notice, this list of conditions and the following | 9 # copyright notice, this list of conditions and the following |
| 10 # disclaimer in the documentation and/or other materials provided | 10 # disclaimer in the documentation and/or other materials provided |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 ] | 41 ] |
| 42 | 42 |
| 43 def test_eq(self): | 43 def test_eq(self): |
| 44 for (left, right) in self.__equal_pairs: | 44 for (left, right) in self.__equal_pairs: |
| 45 self.assertEqual(left, right) | 45 self.assertEqual(left, right) |
| 46 | 46 |
| 47 def test_hash(self): | 47 def test_hash(self): |
| 48 for (left, right) in self.__equal_pairs: | 48 for (left, right) in self.__equal_pairs: |
| 49 self.assertEqual(hash(left), hash(right)) | 49 self.assertEqual(hash(left), hash(right)) |
| 50 | 50 |
| 51 def test_compare(self): |
| 52 key = lambda x : TransitionKey.range(self.__encoding, x[0], x[1]) |
| 53 t_key = lambda x : TransitionKey.unique(x) |
| 54 def check(a, b, expected): |
| 55 self.assertEqual(expected, TransitionKey.compare(key(a), key(b))) |
| 56 self.assertEqual(-expected, TransitionKey.compare(key(b), key(a))) |
| 57 check([1,1], [1,1], 0) |
| 58 check([1,3], [1,3], 0) |
| 59 check([1,3], [4,5], -2) |
| 60 check([1,4], [2,3], -3) |
| 61 check([1,4], [2,4], -3) |
| 62 check([1,3], [1,4], -4) |
| 63 check([1,3], [2,4], -5) |
| 64 check([1,3], [3,4], -5) |
| 65 check([1,3], [3,4], -5) |
| 66 |
| 67 def test_superset(self): |
| 68 r_key = lambda a, b : TransitionKey.range(self.__encoding, a, b) |
| 69 t_key = lambda x : TransitionKey.unique(x) |
| 70 def merge(*keys): |
| 71 return TransitionKey.merged_key(self.__encoding, keys) |
| 72 def check(a, b, expected1, expected_none = None): |
| 73 if expected1 == None: |
| 74 expected2 = expected_none |
| 75 elif expected1 == True: |
| 76 expected2 = True if a == b else None |
| 77 else: |
| 78 expected2 = False # disjoint sets remain disjoint |
| 79 for expected in [expected1, expected2]: |
| 80 try: |
| 81 result = a.is_superset_of_key(b) |
| 82 except Exception: |
| 83 self.assertTrue(expected == None) |
| 84 return |
| 85 self.assertTrue(expected != None) |
| 86 self.assertEqual(expected, result) |
| 87 (a, b) = (b, a) |
| 88 # subset cases |
| 89 check(merge(r_key(1, 2), r_key(4, 5)), merge(r_key(1, 2)), True) |
| 90 check(merge(r_key(1, 7), r_key(9, 10)), merge(r_key(3, 4)), True) |
| 91 check(merge(r_key(1, 7), r_key(9, 10)), |
| 92 merge(r_key(1, 7), r_key(9, 10)), True) |
| 93 check(merge(r_key(1, 4)), merge(r_key(2, 3)), True) |
| 94 # disjoint cases |
| 95 check(merge(r_key(1, 4)), merge(r_key(5, 6)), False) |
| 96 check(merge(t_key('t1')), merge(t_key('t2')), False) |
| 97 check(merge(r_key(1, 4), t_key('t1')), |
| 98 merge(r_key(5, 6), t_key('t2')), False) |
| 99 check(merge(r_key(5, 6), t_key('t1')), |
| 100 merge(r_key(1, 4), t_key('t2')), False) |
| 101 # exception cases |
| 102 check(merge(t_key('t1')), merge(t_key('t1'), t_key('t2')), None, True) |
| 103 check(merge(t_key('t1'), t_key('t3')), |
| 104 merge(t_key('t1'), t_key('t2')), None) |
| 105 check(merge(t_key('t2'), t_key('t1')), |
| 106 merge(t_key('t2'), t_key('t3')), None) |
| 107 check(merge(r_key(1, 7), r_key(9, 10)), |
| 108 merge(r_key(2, 8)), None) |
| 109 check(merge(r_key(1, 7), r_key(9, 10)), |
| 110 merge(r_key(10, 11)), None) |
| 111 check(merge(r_key(1, 7), r_key(9, 10)), |
| 112 merge(r_key(8, 9)), None) |
| 113 |
| 51 def test_classes(self): | 114 def test_classes(self): |
| 52 # class regex, should match, should not match | 115 # class regex, should match, should not match |
| 53 data = [ | 116 data = [ |
| 54 ("1-2", "12", "ab"), | 117 ("1-2", "12", "ab"), |
| 55 ("a-zA-Z", "abyzABYZ" , "123"), | 118 ("a-zA-Z", "abyzABYZ" , "123"), |
| 56 ("a-zA-Z0g" , "abyzABYZ0" , "123"), | 119 ("a-zA-Z0g" , "abyzABYZ0" , "123"), |
| 57 ("a-z:whitespace::letter:" , "abc" , "123"), | 120 ("a-z:whitespace::letter:" , "abc" , "123"), |
| 58 ] | 121 ] |
| 59 classes = {} | 122 classes = {} |
| 60 encoding = self.__encoding | 123 encoding = self.__encoding |
| (...skipping 17 matching lines...) Expand all Loading... |
| 78 encoding = self.__encoding | 141 encoding = self.__encoding |
| 79 graph = RegexParser.parse("[a-z]") | 142 graph = RegexParser.parse("[a-z]") |
| 80 classes = { | 143 classes = { |
| 81 'self_defined' : TransitionKey.character_class(encoding, graph, {})} | 144 'self_defined' : TransitionKey.character_class(encoding, graph, {})} |
| 82 graph = RegexParser.parse("[^:self_defined:]") | 145 graph = RegexParser.parse("[^:self_defined:]") |
| 83 key = TransitionKey.character_class(encoding, graph, classes) | 146 key = TransitionKey.character_class(encoding, graph, classes) |
| 84 self.assertTrue(key.matches_char('A')) | 147 self.assertTrue(key.matches_char('A')) |
| 85 | 148 |
| 86 def test_disjoint_keys(self): | 149 def test_disjoint_keys(self): |
| 87 encoding = self.__encoding | 150 encoding = self.__encoding |
| 88 key1 = TransitionKey(encoding, [(1, 10)]) | 151 key = lambda x, y: TransitionKey.range(encoding, x , y) |
| 89 key2 = TransitionKey(encoding, [(5, 15)]) | 152 disjoint_set = TransitionKey.disjoint_keys(encoding, |
| 90 disjoint_set = TransitionKey.disjoint_keys(encoding, set([key1, key2])) | 153 [key(1, 10), key(5, 15)]) |
| 91 self.assertTrue(TransitionKey(encoding, [(1, 4)]) in disjoint_set) | 154 self.assertTrue(key(1, 4) in disjoint_set) |
| 92 self.assertTrue(TransitionKey(encoding, [(5, 10)]) in disjoint_set) | 155 self.assertTrue(key(5, 10) in disjoint_set) |
| 93 self.assertTrue(TransitionKey(encoding, [(11, 15)]) in disjoint_set) | 156 self.assertTrue(key(11, 15) in disjoint_set) |
| OLD | NEW |