| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import logging | 6 import logging |
| 7 import os | 7 import os |
| 8 import sys | 8 import sys |
| 9 import unittest | 9 import unittest |
| 10 | 10 |
| 11 ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | 11 BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 12 sys.path.insert(0, ROOT_DIR) | 12 sys.path.append(BASE_PATH) |
| 13 | 13 |
| 14 import range_dict | 14 from lib.range_dict import ExclusiveRangeDict |
| 15 | 15 |
| 16 | 16 |
| 17 class ExclusiveRangeDictTest(unittest.TestCase): | 17 class ExclusiveRangeDictTest(unittest.TestCase): |
| 18 class TestAttribute(range_dict.ExclusiveRangeDict.RangeAttribute): | 18 class TestAttribute(ExclusiveRangeDict.RangeAttribute): |
| 19 def __init__(self): | 19 def __init__(self): |
| 20 super(ExclusiveRangeDictTest.TestAttribute, self).__init__() | 20 super(ExclusiveRangeDictTest.TestAttribute, self).__init__() |
| 21 self._value = 0 | 21 self._value = 0 |
| 22 | 22 |
| 23 def __str__(self): | 23 def __str__(self): |
| 24 return str(self._value) | 24 return str(self._value) |
| 25 | 25 |
| 26 def __repr__(self): | 26 def __repr__(self): |
| 27 return '<TestAttribute:%d>' % self._value | 27 return '<TestAttribute:%d>' % self._value |
| 28 | 28 |
| 29 def get(self): | 29 def get(self): |
| 30 return self._value | 30 return self._value |
| 31 | 31 |
| 32 def set(self, new_value): | 32 def set(self, new_value): |
| 33 self._value = new_value | 33 self._value = new_value |
| 34 | 34 |
| 35 def copy(self): # pylint: disable=R0201 | 35 def copy(self): # pylint: disable=R0201 |
| 36 new_attr = ExclusiveRangeDictTest.TestAttribute() | 36 new_attr = ExclusiveRangeDictTest.TestAttribute() |
| 37 new_attr.set(self._value) | 37 new_attr.set(self._value) |
| 38 return new_attr | 38 return new_attr |
| 39 | 39 |
| 40 def test_init(self): | 40 def test_init(self): |
| 41 ranges = range_dict.ExclusiveRangeDict(self.TestAttribute) | 41 ranges = ExclusiveRangeDict(self.TestAttribute) |
| 42 | 42 |
| 43 result = [] | 43 result = [] |
| 44 for begin, end, attr in ranges.iter_range(20, 40): | 44 for begin, end, attr in ranges.iter_range(20, 40): |
| 45 result.append({'begin': begin, 'end':end, 'attr':attr.get()}) | 45 result.append({'begin': begin, 'end':end, 'attr':attr.get()}) |
| 46 expected = [ | 46 expected = [ |
| 47 {'begin': 20, 'end': 40, 'attr': 0}, | 47 {'begin': 20, 'end': 40, 'attr': 0}, |
| 48 ] | 48 ] |
| 49 self.assertEqual(expected, result) | 49 self.assertEqual(expected, result) |
| 50 | 50 |
| 51 def test_norange(self): | 51 def test_norange(self): |
| 52 ranges = range_dict.ExclusiveRangeDict(self.TestAttribute) | 52 ranges = ExclusiveRangeDict(self.TestAttribute) |
| 53 | 53 |
| 54 result = [] | 54 result = [] |
| 55 for begin, end, attr in ranges.iter_range(20, 20): | 55 for begin, end, attr in ranges.iter_range(20, 20): |
| 56 result.append({'begin': begin, 'end':end, 'attr':attr.get()}) | 56 result.append({'begin': begin, 'end':end, 'attr':attr.get()}) |
| 57 expected = [] | 57 expected = [] |
| 58 self.assertEqual(expected, result) | 58 self.assertEqual(expected, result) |
| 59 | 59 |
| 60 def test_set(self): | 60 def test_set(self): |
| 61 ranges = range_dict.ExclusiveRangeDict(self.TestAttribute) | 61 ranges = ExclusiveRangeDict(self.TestAttribute) |
| 62 for begin, end, attr in ranges.iter_range(20, 30): | 62 for begin, end, attr in ranges.iter_range(20, 30): |
| 63 attr.set(12) | 63 attr.set(12) |
| 64 for begin, end, attr in ranges.iter_range(30, 40): | 64 for begin, end, attr in ranges.iter_range(30, 40): |
| 65 attr.set(52) | 65 attr.set(52) |
| 66 | 66 |
| 67 result = [] | 67 result = [] |
| 68 for begin, end, attr in ranges.iter_range(20, 40): | 68 for begin, end, attr in ranges.iter_range(20, 40): |
| 69 result.append({'begin': begin, 'end':end, 'attr':attr.get()}) | 69 result.append({'begin': begin, 'end':end, 'attr':attr.get()}) |
| 70 expected = [ | 70 expected = [ |
| 71 {'begin': 20, 'end': 30, 'attr': 12}, | 71 {'begin': 20, 'end': 30, 'attr': 12}, |
| 72 {'begin': 30, 'end': 40, 'attr': 52}, | 72 {'begin': 30, 'end': 40, 'attr': 52}, |
| 73 ] | 73 ] |
| 74 self.assertEqual(expected, result) | 74 self.assertEqual(expected, result) |
| 75 | 75 |
| 76 def test_split(self): | 76 def test_split(self): |
| 77 ranges = range_dict.ExclusiveRangeDict(self.TestAttribute) | 77 ranges = ExclusiveRangeDict(self.TestAttribute) |
| 78 for begin, end, attr in ranges.iter_range(20, 30): | 78 for begin, end, attr in ranges.iter_range(20, 30): |
| 79 attr.set(1000) | 79 attr.set(1000) |
| 80 for begin, end, attr in ranges.iter_range(30, 40): | 80 for begin, end, attr in ranges.iter_range(30, 40): |
| 81 attr.set(2345) | 81 attr.set(2345) |
| 82 for begin, end, attr in ranges.iter_range(40, 50): | 82 for begin, end, attr in ranges.iter_range(40, 50): |
| 83 attr.set(3579) | 83 attr.set(3579) |
| 84 | 84 |
| 85 result1 = [] | 85 result1 = [] |
| 86 for begin, end, attr in ranges.iter_range(25, 45): | 86 for begin, end, attr in ranges.iter_range(25, 45): |
| 87 result1.append({'begin': begin, 'end':end, 'attr':attr.get()}) | 87 result1.append({'begin': begin, 'end':end, 'attr':attr.get()}) |
| (...skipping 10 matching lines...) Expand all Loading... |
| 98 expected2 = [ | 98 expected2 = [ |
| 99 {'begin': 20, 'end': 25, 'attr': 1000}, | 99 {'begin': 20, 'end': 25, 'attr': 1000}, |
| 100 {'begin': 25, 'end': 30, 'attr': 1000}, | 100 {'begin': 25, 'end': 30, 'attr': 1000}, |
| 101 {'begin': 30, 'end': 40, 'attr': 2345}, | 101 {'begin': 30, 'end': 40, 'attr': 2345}, |
| 102 {'begin': 40, 'end': 45, 'attr': 3579}, | 102 {'begin': 40, 'end': 45, 'attr': 3579}, |
| 103 {'begin': 45, 'end': 50, 'attr': 3579}, | 103 {'begin': 45, 'end': 50, 'attr': 3579}, |
| 104 ] | 104 ] |
| 105 self.assertEqual(expected2, result2) | 105 self.assertEqual(expected2, result2) |
| 106 | 106 |
| 107 def test_fill(self): | 107 def test_fill(self): |
| 108 ranges = range_dict.ExclusiveRangeDict(self.TestAttribute) | 108 ranges = ExclusiveRangeDict(self.TestAttribute) |
| 109 for begin, end, attr in ranges.iter_range(30, 35): | 109 for begin, end, attr in ranges.iter_range(30, 35): |
| 110 attr.set(12345) | 110 attr.set(12345) |
| 111 for begin, end, attr in ranges.iter_range(40, 45): | 111 for begin, end, attr in ranges.iter_range(40, 45): |
| 112 attr.set(97531) | 112 attr.set(97531) |
| 113 | 113 |
| 114 result = [] | 114 result = [] |
| 115 for begin, end, attr in ranges.iter_range(25, 50): | 115 for begin, end, attr in ranges.iter_range(25, 50): |
| 116 result.append({'begin': begin, 'end':end, 'attr':attr.get()}) | 116 result.append({'begin': begin, 'end':end, 'attr':attr.get()}) |
| 117 expected = [ | 117 expected = [ |
| 118 {'begin': 25, 'end': 30, 'attr': 0}, | 118 {'begin': 25, 'end': 30, 'attr': 0}, |
| 119 {'begin': 30, 'end': 35, 'attr': 12345}, | 119 {'begin': 30, 'end': 35, 'attr': 12345}, |
| 120 {'begin': 35, 'end': 40, 'attr': 0}, | 120 {'begin': 35, 'end': 40, 'attr': 0}, |
| 121 {'begin': 40, 'end': 45, 'attr': 97531}, | 121 {'begin': 40, 'end': 45, 'attr': 97531}, |
| 122 {'begin': 45, 'end': 50, 'attr': 0}, | 122 {'begin': 45, 'end': 50, 'attr': 0}, |
| 123 ] | 123 ] |
| 124 self.assertEqual(expected, result) | 124 self.assertEqual(expected, result) |
| 125 | 125 |
| 126 | 126 |
| 127 if __name__ == '__main__': | 127 if __name__ == '__main__': |
| 128 logging.basicConfig( | 128 logging.basicConfig( |
| 129 level=logging.DEBUG if '-v' in sys.argv else logging.ERROR, | 129 level=logging.DEBUG if '-v' in sys.argv else logging.ERROR, |
| 130 format='%(levelname)5s %(filename)15s(%(lineno)3d): %(message)s') | 130 format='%(levelname)5s %(filename)15s(%(lineno)3d): %(message)s') |
| 131 unittest.main() | 131 unittest.main() |
| OLD | NEW |