summaryrefslogtreecommitdiff
path: root/test.py
blob: 150fa606be4ea76e8a41165392dddc8b9f422a54 (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240


from lru import *

def _selftest():
    
    class simplelrucache:

        def __init__(self, size):
            self.size = size
            self.length = 0
            self.items = []

        def __contains__(self, key):
            for x in self.items:
                if x[0] == key:
                    return True
    
            return False

	
	
	
	
class simplelrucache:
  
    def __init__(self, size):
    
        # Initialize the cache as empty.
        self.cache = []
        self.size = size
    
    def __contains__(self, key):
    
        for x in self.cache:
            if x[0] == key:
                return True
	
        return False
    
    
    def __getitem__(self, key):
    
        for i in range(len(self.cache)):
            x = self.cache[i]
            if x[0] == key:
                del self.cache[i]
                self.cache.append(x)
                return x[1]
      
        assert False
  
  
    def __setitem__(self, key, obj):
    
        for i in range(len(self.cache)):
            x = self.cache[i]
            if x[0] == key:
                x[1] = obj
                del self.cache[i]
                self.cache.append(x)
                return
	
        if len(self.cache) == self.size:
            self.cache = self.cache[1:]
    
        self.cache.append([key, obj])
    
        return
  
    def __delitem__(self, key):
    
        for i in range(len(self.cache)):
            if self.cache[i][0] == key:
                del self.cache[i]
                return
    
        return
      
  
    
    
    
def testa():
  
    a = lrucache(16)
  
    for i in range(len(vect)):
        a[vect[i]] = 0
    
def testb():
  
    a = simplelrucache(16)
  
    for i in range(len(vect)):
        a[vect[i]] = 0


def wraptest():
    import random

    q = dict()
    x = lruwrap(q, 32)
    for i in range(256):
        a = random.randint(0, 256)
        b = random.randint(0, 256)
    
        x[a] = b
    
    for i in range(512):
        a = random.randint(0, 256)
        tmp1 = None
        tmp2 = None
        try:
            tmp1 = x[a]
        except KeyError:
            tmp1 = None
           
        try:
            tmp2 = q[a]
        except KeyError:
            tmp2 = None

        assert tmp1 == tmp2
        
def wraptest2():
    import random

    q = dict()
    x = lruwrap(q, 32, True)
    for i in range(256):
        a = random.randint(0, 256)
        b = random.randint(0, 256)
    
        x[a] = b
        
    x.sync()
    for i in range(512):
        a = random.randint(0, 256)
        tmp1 = None
        tmp2 = None
        try:
            tmp1 = x[a]
        except KeyError:
            tmp1 = None
           
        try:
            tmp2 = q[a]
        except KeyError:
            tmp2 = None

        assert tmp1 == tmp2


def wraptest3():
    import random

    q = dict()
    with lruwrap(q, 32, True) as x:
        for i in range(256):
            a = random.randint(0, 256)
            b = random.randint(0, 256)
        
            x[a] = b
        
    for i in range(512):
        a = random.randint(0, 256)
        tmp1 = None
        tmp2 = None
        try:
            tmp1 = x[a]
        except KeyError:
            tmp1 = None
           
        try:
            tmp2 = q[a]
        except KeyError:
            tmp2 = None

        assert tmp1 == tmp2
        
        
@lrudecorator(14)
def cube(x):
    return x*x*x
    
if __name__ == '__main__':
    import random
    
    wraptest()
    wraptest2()
    wraptest3()
    
    for i in range(300):
        x = random.randint(0, 25)
        assert cube(x) == x**3
    
  

  
    a = lrucache(20)
    b = simplelrucache(20)
  
    for i in range(256):
        x = random.randint(0, 256)
        y = random.randint(0, 256)
    
        a[x] = y
        b[x] = y
    
        q = []
        z = a.head
        for j in range(len(a.table)):
            q.append([z.key, z.obj])
            z = z.next
      
        if q != b.cache[::-1]:
            print i
            print b.cache[::-1]
            print q
            print a.table.keys()
            assert False
  
  

    from timeit import Timer
    import random
  
    global vect
  
    vect = []
    for i in range(1000000):
        vect.append(random.randint(0, 1000))
  
    t = Timer("testa()", "from __main__ import testa")
    print t.timeit(1)

    t = Timer("testb()", "from __main__ import testb")
    print t.timeit(1)