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.
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
):
20 super(ExclusiveRangeDictTest
.TestAttribute
, self
).__init
__()
24 return str(self
._value
)
27 return '<TestAttribute:%d>' % 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
)
41 ranges
= ExclusiveRangeDict(self
.TestAttribute
)
44 for begin
, end
, attr
in ranges
.iter_range(20, 40):
45 result
.append({'begin': begin
, 'end':end
, 'attr':attr
.get()})
47 {'begin': 20, 'end': 40, 'attr': 0},
49 self
.assertEqual(expected
, result
)
51 def test_norange(self
):
52 ranges
= ExclusiveRangeDict(self
.TestAttribute
)
55 for begin
, end
, attr
in ranges
.iter_range(20, 20):
56 result
.append({'begin': begin
, 'end':end
, 'attr':attr
.get()})
58 self
.assertEqual(expected
, result
)
61 ranges
= ExclusiveRangeDict(self
.TestAttribute
)
62 for begin
, end
, attr
in ranges
.iter_range(20, 30):
64 for begin
, end
, attr
in ranges
.iter_range(30, 40):
68 for begin
, end
, attr
in ranges
.iter_range(20, 40):
69 result
.append({'begin': begin
, 'end':end
, 'attr':attr
.get()})
71 {'begin': 20, 'end': 30, 'attr': 12},
72 {'begin': 30, 'end': 40, 'attr': 52},
74 self
.assertEqual(expected
, result
)
77 ranges
= ExclusiveRangeDict(self
.TestAttribute
)
78 for begin
, end
, attr
in ranges
.iter_range(20, 30):
80 for begin
, end
, attr
in ranges
.iter_range(30, 40):
82 for begin
, end
, attr
in ranges
.iter_range(40, 50):
86 for begin
, end
, attr
in ranges
.iter_range(25, 45):
87 result1
.append({'begin': begin
, 'end':end
, 'attr':attr
.get()})
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
)
96 for begin
, end
, attr
in ranges
.iter_range(20, 50):
97 result2
.append({'begin': begin
, 'end':end
, 'attr':attr
.get()})
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
)
108 ranges
= ExclusiveRangeDict(self
.TestAttribute
)
109 for begin
, end
, attr
in ranges
.iter_range(30, 35):
111 for begin
, end
, attr
in ranges
.iter_range(40, 45):
115 for begin
, end
, attr
in ranges
.iter_range(25, 50):
116 result
.append({'begin': begin
, 'end':end
, 'attr':attr
.get()})
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__':
129 level
=logging
.DEBUG
if '-v' in sys
.argv
else logging
.ERROR
,
130 format
='%(levelname)5s %(filename)15s(%(lineno)3d): %(message)s')