tests/test-lrucachedict.py
author Gregory Szorc <gregory.szorc@gmail.com>
Wed, 05 Sep 2018 23:15:20 -0700
changeset 39566 bd9d3a89f07b
parent 39565 2dcc68c7d25b
child 39567 ee087f0d7db5
permissions -rw-r--r--
util: add a popoldest() method to lrucachedict This allows consumers to prune the oldest item from the cache. This could be useful for e.g. a consumer that wishes for the size of items tracked by the cache to remain under a high water mark. Differential Revision: https://phab.mercurial-scm.org/D4501
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28931
ba0e4789bd2e tests: make test-lrucachedict use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28930
diff changeset
     1
from __future__ import absolute_import, print_function
28930
e3f01188d439 tests: make test-lrucachedict use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27576
diff changeset
     2
39562
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
     3
import unittest
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
     4
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
     5
import silenttestrunner
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
     6
28930
e3f01188d439 tests: make test-lrucachedict use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27576
diff changeset
     7
from mercurial import (
e3f01188d439 tests: make test-lrucachedict use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27576
diff changeset
     8
    util,
e3f01188d439 tests: make test-lrucachedict use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27576
diff changeset
     9
)
18603
2251b3184e6e util: add an LRU cache dict
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    10
39562
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    11
class testlrucachedict(unittest.TestCase):
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    12
    def testsimple(self):
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    13
        d = util.lrucachedict(4)
39564
5d75a3c16193 util: make capacity a public attribute on lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39563
diff changeset
    14
        self.assertEqual(d.capacity, 4)
39562
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    15
        d['a'] = 'va'
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    16
        d['b'] = 'vb'
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    17
        d['c'] = 'vc'
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    18
        d['d'] = 'vd'
18603
2251b3184e6e util: add an LRU cache dict
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    19
39562
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    20
        self.assertEqual(d['a'], 'va')
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    21
        self.assertEqual(d['b'], 'vb')
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    22
        self.assertEqual(d['c'], 'vc')
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    23
        self.assertEqual(d['d'], 'vd')
18603
2251b3184e6e util: add an LRU cache dict
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    24
39562
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    25
        # 'a' should be dropped because it was least recently used.
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    26
        d['e'] = 've'
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    27
        self.assertNotIn('a', d)
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    28
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    29
        self.assertIsNone(d.get('a'))
18603
2251b3184e6e util: add an LRU cache dict
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    30
39562
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    31
        self.assertEqual(d['b'], 'vb')
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    32
        self.assertEqual(d['c'], 'vc')
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    33
        self.assertEqual(d['d'], 'vd')
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    34
        self.assertEqual(d['e'], 've')
18603
2251b3184e6e util: add an LRU cache dict
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    35
39562
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    36
        # Touch entries in some order (both get and set).
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    37
        d['e']
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    38
        d['c'] = 'vc2'
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    39
        d['d']
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    40
        d['b'] = 'vb2'
29828
79add5a4e857 util: properly implement lrucachedict.get()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28931
diff changeset
    41
39562
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    42
        # 'e' should be dropped now
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    43
        d['f'] = 'vf'
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    44
        self.assertNotIn('e', d)
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    45
        self.assertEqual(d['b'], 'vb2')
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    46
        self.assertEqual(d['c'], 'vc2')
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    47
        self.assertEqual(d['d'], 'vd')
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    48
        self.assertEqual(d['f'], 'vf')
18603
2251b3184e6e util: add an LRU cache dict
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    49
39562
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    50
        d.clear()
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    51
        for key in ('a', 'b', 'c', 'd', 'e', 'f'):
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    52
            self.assertNotIn(key, d)
18603
2251b3184e6e util: add an LRU cache dict
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    53
39562
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    54
    def testunfull(self):
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    55
        d = util.lrucachedict(4)
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    56
        d['a'] = 1
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    57
        d['b'] = 2
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    58
        d['a']
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    59
        d['b']
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    60
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    61
        for key in ('a', 'b'):
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    62
            self.assertIn(key, d)
19710
887ffa22fd0d lrucachedict: implement clear()
Siddharth Agarwal <sid0@fb.com>
parents: 18603
diff changeset
    63
39562
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    64
    def testcopypartial(self):
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    65
        d = util.lrucachedict(4)
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    66
        d['a'] = 'va'
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    67
        d['b'] = 'vb'
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    68
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    69
        dc = d.copy()
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    70
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    71
        self.assertEqual(len(dc), 2)
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    72
        for key in ('a', 'b'):
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    73
            self.assertIn(key, dc)
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    74
            self.assertEqual(dc[key], 'v%s' % key)
27371
45d996a566d7 util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 19710
diff changeset
    75
39563
b31b01f93b11 util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39562
diff changeset
    76
        self.assertEqual(len(d), 2)
b31b01f93b11 util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39562
diff changeset
    77
        for key in ('a', 'b'):
b31b01f93b11 util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39562
diff changeset
    78
            self.assertIn(key, d)
b31b01f93b11 util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39562
diff changeset
    79
            self.assertEqual(d[key], 'v%s' % key)
b31b01f93b11 util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39562
diff changeset
    80
b31b01f93b11 util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39562
diff changeset
    81
        d['c'] = 'vc'
b31b01f93b11 util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39562
diff changeset
    82
        del d['b']
b31b01f93b11 util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39562
diff changeset
    83
        dc = d.copy()
b31b01f93b11 util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39562
diff changeset
    84
        self.assertEqual(len(dc), 2)
b31b01f93b11 util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39562
diff changeset
    85
        for key in ('a', 'c'):
b31b01f93b11 util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39562
diff changeset
    86
            self.assertIn(key, dc)
b31b01f93b11 util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39562
diff changeset
    87
            self.assertEqual(dc[key], 'v%s' % key)
b31b01f93b11 util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39562
diff changeset
    88
b31b01f93b11 util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39562
diff changeset
    89
    def testcopyempty(self):
b31b01f93b11 util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39562
diff changeset
    90
        d = util.lrucachedict(4)
b31b01f93b11 util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39562
diff changeset
    91
        dc = d.copy()
b31b01f93b11 util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39562
diff changeset
    92
        self.assertEqual(len(dc), 0)
b31b01f93b11 util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39562
diff changeset
    93
39562
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    94
    def testcopyfull(self):
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    95
        d = util.lrucachedict(4)
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    96
        d['a'] = 'va'
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    97
        d['b'] = 'vb'
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    98
        d['c'] = 'vc'
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
    99
        d['d'] = 'vd'
27576
6cd3044985c2 lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents: 27371
diff changeset
   100
39562
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
   101
        dc = d.copy()
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
   102
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
   103
        for key in ('a', 'b', 'c', 'd'):
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
   104
            self.assertIn(key, dc)
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
   105
            self.assertEqual(dc[key], 'v%s' % key)
27576
6cd3044985c2 lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents: 27371
diff changeset
   106
39562
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
   107
        # 'a' should be dropped because it was least recently used.
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
   108
        dc['e'] = 've'
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
   109
        self.assertNotIn('a', dc)
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
   110
        for key in ('b', 'c', 'd', 'e'):
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
   111
            self.assertIn(key, dc)
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
   112
            self.assertEqual(dc[key], 'v%s' % key)
27576
6cd3044985c2 lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents: 27371
diff changeset
   113
39562
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
   114
        # Contents and order of original dict should remain unchanged.
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
   115
        dc['b'] = 'vb_new'
27576
6cd3044985c2 lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents: 27371
diff changeset
   116
39562
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
   117
        self.assertEqual(list(iter(d)), ['d', 'c', 'b', 'a'])
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
   118
        for key in ('a', 'b', 'c', 'd'):
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
   119
            self.assertEqual(d[key], 'v%s' % key)
27576
6cd3044985c2 lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents: 27371
diff changeset
   120
39565
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   121
    def testcopydecreasecapacity(self):
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   122
        d = util.lrucachedict(5)
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   123
        d['a'] = 'va'
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   124
        d['b'] = 'vb'
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   125
        d['c'] = 'vc'
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   126
        d['d'] = 'vd'
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   127
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   128
        dc = d.copy(2)
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   129
        for key in ('a', 'b'):
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   130
            self.assertNotIn(key, dc)
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   131
        for key in ('c', 'd'):
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   132
            self.assertIn(key, dc)
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   133
            self.assertEqual(dc[key], 'v%s' % key)
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   134
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   135
        dc['e'] = 've'
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   136
        self.assertNotIn('c', dc)
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   137
        for key in ('d', 'e'):
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   138
            self.assertIn(key, dc)
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   139
            self.assertEqual(dc[key], 'v%s' % key)
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   140
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   141
        # Original should remain unchanged.
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   142
        for key in ('a', 'b', 'c', 'd'):
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   143
            self.assertIn(key, d)
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   144
            self.assertEqual(d[key], 'v%s' % key)
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   145
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   146
    def testcopyincreasecapacity(self):
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   147
        d = util.lrucachedict(5)
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   148
        d['a'] = 'va'
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   149
        d['b'] = 'vb'
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   150
        d['c'] = 'vc'
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   151
        d['d'] = 'vd'
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   152
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   153
        dc = d.copy(6)
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   154
        for key in ('a', 'b', 'c', 'd'):
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   155
            self.assertIn(key, dc)
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   156
            self.assertEqual(dc[key], 'v%s' % key)
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   157
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   158
        dc['e'] = 've'
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   159
        dc['f'] = 'vf'
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   160
        for key in ('a', 'b', 'c', 'd', 'e', 'f'):
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   161
            self.assertIn(key, dc)
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   162
            self.assertEqual(dc[key], 'v%s' % key)
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   163
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   164
        dc['g'] = 'vg'
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   165
        self.assertNotIn('a', dc)
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   166
        for key in ('b', 'c', 'd', 'e', 'f', 'g'):
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   167
            self.assertIn(key, dc)
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   168
            self.assertEqual(dc[key], 'v%s' % key)
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   169
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   170
        # Original should remain unchanged.
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   171
        for key in ('a', 'b', 'c', 'd'):
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   172
            self.assertIn(key, d)
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   173
            self.assertEqual(d[key], 'v%s' % key)
2dcc68c7d25b util: ability to change capacity when copying lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39564
diff changeset
   174
39566
bd9d3a89f07b util: add a popoldest() method to lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39565
diff changeset
   175
    def testpopoldest(self):
bd9d3a89f07b util: add a popoldest() method to lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39565
diff changeset
   176
        d = util.lrucachedict(4)
bd9d3a89f07b util: add a popoldest() method to lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39565
diff changeset
   177
        d['a'] = 'va'
bd9d3a89f07b util: add a popoldest() method to lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39565
diff changeset
   178
        d['b'] = 'vb'
bd9d3a89f07b util: add a popoldest() method to lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39565
diff changeset
   179
bd9d3a89f07b util: add a popoldest() method to lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39565
diff changeset
   180
        self.assertEqual(len(d), 2)
bd9d3a89f07b util: add a popoldest() method to lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39565
diff changeset
   181
        self.assertEqual(d.popoldest(), ('a', 'va'))
bd9d3a89f07b util: add a popoldest() method to lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39565
diff changeset
   182
        self.assertEqual(len(d), 1)
bd9d3a89f07b util: add a popoldest() method to lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39565
diff changeset
   183
        self.assertEqual(d.popoldest(), ('b', 'vb'))
bd9d3a89f07b util: add a popoldest() method to lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39565
diff changeset
   184
        self.assertEqual(len(d), 0)
bd9d3a89f07b util: add a popoldest() method to lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39565
diff changeset
   185
        self.assertIsNone(d.popoldest())
bd9d3a89f07b util: add a popoldest() method to lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39565
diff changeset
   186
bd9d3a89f07b util: add a popoldest() method to lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39565
diff changeset
   187
        d['a'] = 'va'
bd9d3a89f07b util: add a popoldest() method to lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39565
diff changeset
   188
        d['b'] = 'vb'
bd9d3a89f07b util: add a popoldest() method to lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39565
diff changeset
   189
        d['c'] = 'vc'
bd9d3a89f07b util: add a popoldest() method to lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39565
diff changeset
   190
        d['d'] = 'vd'
bd9d3a89f07b util: add a popoldest() method to lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39565
diff changeset
   191
bd9d3a89f07b util: add a popoldest() method to lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39565
diff changeset
   192
        self.assertEqual(d.popoldest(), ('a', 'va'))
bd9d3a89f07b util: add a popoldest() method to lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39565
diff changeset
   193
        self.assertEqual(len(d), 3)
bd9d3a89f07b util: add a popoldest() method to lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39565
diff changeset
   194
        for key in ('b', 'c', 'd'):
bd9d3a89f07b util: add a popoldest() method to lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39565
diff changeset
   195
            self.assertEqual(d[key], 'v%s' % key)
bd9d3a89f07b util: add a popoldest() method to lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39565
diff changeset
   196
bd9d3a89f07b util: add a popoldest() method to lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39565
diff changeset
   197
        d['a'] = 'va'
bd9d3a89f07b util: add a popoldest() method to lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39565
diff changeset
   198
        self.assertEqual(d.popoldest(), ('b', 'vb'))
bd9d3a89f07b util: add a popoldest() method to lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39565
diff changeset
   199
18603
2251b3184e6e util: add an LRU cache dict
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   200
if __name__ == '__main__':
39562
067f7d2c7d60 tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29828
diff changeset
   201
    silenttestrunner.main(__name__)