author | Gregory Szorc <gregory.szorc@gmail.com> |
Sun, 27 Mar 2016 13:50:34 -0700 | |
changeset 28649 | 7acab42ef184 |
parent 27576 | 6cd3044985c2 |
child 28930 | e3f01188d439 |
permissions | -rw-r--r-- |
18603 | 1 |
from mercurial import util |
2 |
||
27576
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
3 |
def printifpresent(d, xs, name='d'): |
18603 | 4 |
for x in xs: |
5 |
present = x in d |
|
27576
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
6 |
print "'%s' in %s: %s" % (x, name, present) |
18603 | 7 |
if present: |
27576
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
8 |
print "%s['%s']: %s" % (name, x, d[x]) |
18603 | 9 |
|
10 |
def test_lrucachedict(): |
|
11 |
d = util.lrucachedict(4) |
|
12 |
d['a'] = 'va' |
|
13 |
d['b'] = 'vb' |
|
14 |
d['c'] = 'vc' |
|
15 |
d['d'] = 'vd' |
|
16 |
||
17 |
# all of these should be present |
|
18 |
printifpresent(d, ['a', 'b', 'c', 'd']) |
|
19 |
||
20 |
# 'a' should be dropped because it was least recently used |
|
21 |
d['e'] = 've' |
|
22 |
printifpresent(d, ['a', 'b', 'c', 'd', 'e']) |
|
23 |
||
24 |
# touch entries in some order (get or set). |
|
25 |
d['e'] |
|
26 |
d['c'] = 'vc2' |
|
27 |
d['d'] |
|
28 |
d['b'] = 'vb2' |
|
29 |
||
30 |
# 'e' should be dropped now |
|
31 |
d['f'] = 'vf' |
|
32 |
printifpresent(d, ['b', 'c', 'd', 'e', 'f']) |
|
33 |
||
19710
887ffa22fd0d
lrucachedict: implement clear()
Siddharth Agarwal <sid0@fb.com>
parents:
18603
diff
changeset
|
34 |
d.clear() |
887ffa22fd0d
lrucachedict: implement clear()
Siddharth Agarwal <sid0@fb.com>
parents:
18603
diff
changeset
|
35 |
printifpresent(d, ['b', 'c', 'd', 'e', 'f']) |
887ffa22fd0d
lrucachedict: implement clear()
Siddharth Agarwal <sid0@fb.com>
parents:
18603
diff
changeset
|
36 |
|
27371
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
19710
diff
changeset
|
37 |
# Now test dicts that aren't full. |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
19710
diff
changeset
|
38 |
d = util.lrucachedict(4) |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
19710
diff
changeset
|
39 |
d['a'] = 1 |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
19710
diff
changeset
|
40 |
d['b'] = 2 |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
19710
diff
changeset
|
41 |
d['a'] |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
19710
diff
changeset
|
42 |
d['b'] |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
19710
diff
changeset
|
43 |
printifpresent(d, ['a', 'b']) |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
19710
diff
changeset
|
44 |
|
27576
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
45 |
# test copy method |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
46 |
d = util.lrucachedict(4) |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
47 |
d['a'] = 'va3' |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
48 |
d['b'] = 'vb3' |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
49 |
d['c'] = 'vc3' |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
50 |
d['d'] = 'vd3' |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
51 |
|
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
52 |
dc = d.copy() |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
53 |
|
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
54 |
# all of these should be present |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
55 |
print "\nAll of these should be present:" |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
56 |
printifpresent(dc, ['a', 'b', 'c', 'd'], 'dc') |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
57 |
|
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
58 |
# 'a' should be dropped because it was least recently used |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
59 |
print "\nAll of these except 'a' should be present:" |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
60 |
dc['e'] = 've3' |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
61 |
printifpresent(dc, ['a', 'b', 'c', 'd', 'e'], 'dc') |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
62 |
|
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
63 |
# contents and order of original dict should remain unchanged |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
64 |
print "\nThese should be in reverse alphabetical order and read 'v?3':" |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
65 |
dc['b'] = 'vb3_new' |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
66 |
for k in list(iter(d)): |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
67 |
print "d['%s']: %s" % (k, d[k]) |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
68 |
|
18603 | 69 |
if __name__ == '__main__': |
70 |
test_lrucachedict() |