Fix import error in mac_platform_backend.py
[chromium-blink-merge.git] / tools / deep_memory_profiler / tests / range_dict_tests.py
blob3bc2c139ac2425a21442fff323b1d86845e4e485
1 #!/usr/bin/env python
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
4 # found in the LICENSE file.
6 import logging
7 import os
8 import sys
9 import unittest
11 BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
12 sys.path.append(BASE_PATH)
14 from lib.range_dict import ExclusiveRangeDict
17 class ExclusiveRangeDictTest(unittest.TestCase):
18 class TestAttribute(ExclusiveRangeDict.RangeAttribute):
19 def __init__(self):
20 super(ExclusiveRangeDictTest.TestAttribute, self).__init__()
21 self._value = 0
23 def __str__(self):
24 return str(self._value)
26 def __repr__(self):
27 return '<TestAttribute:%d>' % self._value
29 def get(self):
30 return self._value
32 def set(self, new_value):
33 self._value = new_value
35 def copy(self): # pylint: disable=R0201
36 new_attr = ExclusiveRangeDictTest.TestAttribute()
37 new_attr.set(self._value)
38 return new_attr
40 def test_init(self):
41 ranges = ExclusiveRangeDict(self.TestAttribute)
43 result = []
44 for begin, end, attr in ranges.iter_range(20, 40):
45 result.append({'begin': begin, 'end':end, 'attr':attr.get()})
46 expected = [
47 {'begin': 20, 'end': 40, 'attr': 0},
49 self.assertEqual(expected, result)
51 def test_norange(self):
52 ranges = ExclusiveRangeDict(self.TestAttribute)
54 result = []
55 for begin, end, attr in ranges.iter_range(20, 20):
56 result.append({'begin': begin, 'end':end, 'attr':attr.get()})
57 expected = []
58 self.assertEqual(expected, result)
60 def test_set(self):
61 ranges = ExclusiveRangeDict(self.TestAttribute)
62 for begin, end, attr in ranges.iter_range(20, 30):
63 attr.set(12)
64 for begin, end, attr in ranges.iter_range(30, 40):
65 attr.set(52)
67 result = []
68 for begin, end, attr in ranges.iter_range(20, 40):
69 result.append({'begin': begin, 'end':end, 'attr':attr.get()})
70 expected = [
71 {'begin': 20, 'end': 30, 'attr': 12},
72 {'begin': 30, 'end': 40, 'attr': 52},
74 self.assertEqual(expected, result)
76 def test_split(self):
77 ranges = ExclusiveRangeDict(self.TestAttribute)
78 for begin, end, attr in ranges.iter_range(20, 30):
79 attr.set(1000)
80 for begin, end, attr in ranges.iter_range(30, 40):
81 attr.set(2345)
82 for begin, end, attr in ranges.iter_range(40, 50):
83 attr.set(3579)
85 result1 = []
86 for begin, end, attr in ranges.iter_range(25, 45):
87 result1.append({'begin': begin, 'end':end, 'attr':attr.get()})
88 expected1 = [
89 {'begin': 25, 'end': 30, 'attr': 1000},
90 {'begin': 30, 'end': 40, 'attr': 2345},
91 {'begin': 40, 'end': 45, 'attr': 3579},
93 self.assertEqual(expected1, result1)
95 result2 = []
96 for begin, end, attr in ranges.iter_range(20, 50):
97 result2.append({'begin': begin, 'end':end, 'attr':attr.get()})
98 expected2 = [
99 {'begin': 20, 'end': 25, 'attr': 1000},
100 {'begin': 25, 'end': 30, 'attr': 1000},
101 {'begin': 30, 'end': 40, 'attr': 2345},
102 {'begin': 40, 'end': 45, 'attr': 3579},
103 {'begin': 45, 'end': 50, 'attr': 3579},
105 self.assertEqual(expected2, result2)
107 def test_fill(self):
108 ranges = ExclusiveRangeDict(self.TestAttribute)
109 for begin, end, attr in ranges.iter_range(30, 35):
110 attr.set(12345)
111 for begin, end, attr in ranges.iter_range(40, 45):
112 attr.set(97531)
114 result = []
115 for begin, end, attr in ranges.iter_range(25, 50):
116 result.append({'begin': begin, 'end':end, 'attr':attr.get()})
117 expected = [
118 {'begin': 25, 'end': 30, 'attr': 0},
119 {'begin': 30, 'end': 35, 'attr': 12345},
120 {'begin': 35, 'end': 40, 'attr': 0},
121 {'begin': 40, 'end': 45, 'attr': 97531},
122 {'begin': 45, 'end': 50, 'attr': 0},
124 self.assertEqual(expected, result)
127 if __name__ == '__main__':
128 logging.basicConfig(
129 level=logging.DEBUG if '-v' in sys.argv else logging.ERROR,
130 format='%(levelname)5s %(filename)15s(%(lineno)3d): %(message)s')
131 unittest.main()