-
Notifications
You must be signed in to change notification settings - Fork 1
/
hello-range.py
115 lines (95 loc) · 2.29 KB
/
hello-range.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from range_list import *
from random import randrange
class range_item:
# "range" is reserved
def __init__(self,address,size):
self.address = address
self.size = size
def __str__(self):
return "%x:%x"%(self.address,self.size)
def __eq__(self,o):
return self.address==o.address and self.size==o.size
a=range_item(2,3)
assert a!=None
l=range_list()
l.insert(a)
assert len(l)==1
b1=l.search(2)
assert a==b1
b2=l.search(10)
assert b2==None
for i in xrange(1,10):
a=range_item(i*10,5)
l.insert(a)
print "all:"
for i in l:
print i
x,y=l.detailed_search(100)
print "\ndetailed search(100)",l[x],y
assert y==None
assert l[x]==range_item(90,5)
x,y=l.detailed_search(50)
assert y==x
assert l[x]==range_item(50,5)
x,y=l.detailed_search(49)
assert l[y]==range_item(50,5)
assert l[x]==range_item(40,5)
assert x+1==y
x,y=l.detailed_search(1)
assert x==None
assert l[y]==range_item(2,3)
print "\nslice to end:"
i1=l.index_address(50)
caught=False
try:
assert i1==l.index(range_item(50,5))
except ValueError:
caught=True;
assert caught
for x in l.slice(i1):
print x
print "\nslice:"
i2=l.index_address(80)
for x in l.slice(i1,i2):
print x
l.remove_address(80)
print "after remove 80, now len is", len(l)
del l[l.begin()]
print "after remove the first item, now len is", len(l)
l.remove(l[l.end()-1])
print "after remove the last item, now len is", len(l)
i3=i1+1
print "i1+1:", l[i3]
l1=range_list()
assert l1.insert(range_item(200,5))
assert l1.insert(range_item(220,5))
assert l1.insert(range_item(210,5))
a=range_item(190,5)
assert l1.insert(a)
i4=l1.index(a)
assert l1.insert(range_item(205,5))
assert not l1.insert(range_item(190,5))
assert not l1.insert(range_item(189,5))
assert not l1.insert(range_item(199,2))
assert not l1.insert(range_item(194,2))
assert not l1.insert(range_item(189,10))
assert not l1.insert(range_item(224,10))
l.merge(l1)
print "\nmerge:"
for x in l:
print x
i4=l.index(a)
class range_item_s(range_item):
# "range" is reserved
def __init__(self,address,size,s):
self.address = address
self.size = size
self.s=s
def __str__(self):
return "%x:%x - %s"%(self.address,self.size,self.s)
s="str"
a=range_item_s(256,5,s)
l.insert(a)
print "\ninsert a range_item with extra attr:"
b=l.search(257)
print "the item is",b