213

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
if __name__ == "__main__":
# Test 1
i = intervalmap()
i[9:] = "!"
assert repr(i) == "{[9, None] => '!'}"
i[:5] = "Hello"
i[6:7] = "World"
assert repr(i) == "{[None, 5] => 'Hello', [6, 7] => 'World', [9, None] => '!'}"
i[8:10] = "(Test)"
assert repr(i) == "{[None, 5] => 'Hello', [6, 7] => 'World', [8, 10] => '(Test)', [10, None] => '!'}"
i[:3] = 'My,'
assert repr(i) == "{[None, 3] => 'My,', [3, 5] => 'Hello', [6, 7] => 'World', [8, 10] => '(Test)', [10, None] => '!'}"
i[5.5:6] = "Cruel"
assert repr(i) == "{[None, 3] => 'My,', [3, 5] => 'Hello', [5.5, 6] => 'Cruel', [6, 7] => 'World', [8, 10] => '(Test)', [10, None] => '!'}"
i[6:6.5] = "And Harsh"
assert repr(i) == "{[None, 3] => 'My,', [3, 5] => 'Hello', [5.5, 6] => 'Cruel', [6, 6.5] => 'And Harsh', [6.5, 7] => 'World', [8, 10] => '(Test)', [10, None] => '!'}"
i[5.9:6.6] = None
assert repr(i) == "{[None, 3] => 'My,', [3, 5] => 'Hello', [5.5, 5.9000000000000004] => 'Cruel', [6.5999999999999996, 7] => 'World', [8, 10] => '(Test)', [10, None] => '!'}"
assert ' '.join(i.values()) == "My, Hello Cruel World (Test) !"
print 'Test 1 OK'
# Test 2
i = intervalmap()
i[:0] = 'A'
i[2:5] = 'B'
i[8:10] = 'C'
i[12:] = 'D'
assert repr(i) == "{[None, 0] => 'A', [2, 5] => 'B', [8, 10] => 'C', [12, None] => 'D'}"
i[:] = 'K'
assert repr(i) == "{[None, None] => 'K'}"
assert i[5] == 'K'
i[0:10] = 'L'
i[6:8] = 'M'
i[20:] = 'J'
assert i[-1] == 'K'
assert i[5] == 'L'
assert i[7] == 'M'
assert i[9] == 'L'
assert i[15] == 'K'
assert i[42] == 'J'
print 'Test 2 OK'
# Test 3
try:
from datetime import datetime
except:
print 'Test 3 skipped'
else:
i = intervalmap()
i[:datetime(2005,10,24)] = 'A'
i[datetime(2005,11,11):datetime(2005,11,17)] = 'B'
i[datetime(2005,11,30):] = 'C'
assert i[datetime(2005,9,25)] == 'A'
assert i[datetime(2005,10,23)] == 'A'
assert i[datetime(2005,10,26)] == None
assert i[datetime(2005,11,9)] == None
assert i[datetime(2005,11,16)] == 'B'
assert i[datetime(2005,11,23)] == None
assert i[datetime(2005,11,29)] == None
assert i[datetime(2005,11,30)] == 'C'
assert i[datetime(2005,12,3)] == 'C'
print 'Test 3 OK'
try:
import doctest
except:
print 'Skipping the doctests'
else:
print 'And now, the doctests'
doctest.testmod(optionflags=doctest.ELLIPSIS)