about summary refs log tree commit diff stats
path: root/test/core/utils.py
diff options
context:
space:
mode:
authorCamille Mougey <camille.mougey@cea.fr>2015-02-23 17:21:05 +0100
committerCamille Mougey <camille.mougey@cea.fr>2015-02-23 17:54:59 +0100
commit9303454bbfb934017c1054f668f3083c75b2d61c (patch)
tree2d1cbeaf42338603556e00241b36afa2842283e8 /test/core/utils.py
parent4713279e25625b33b8867c35d7da686781b8b9a7 (diff)
downloadmiasm-9303454bbfb934017c1054f668f3083c75b2d61c.tar.gz
miasm-9303454bbfb934017c1054f668f3083c75b2d61c.zip
Core: Introduce BoundedDict and its regression test
Diffstat (limited to 'test/core/utils.py')
-rw-r--r--test/core/utils.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/test/core/utils.py b/test/core/utils.py
new file mode 100644
index 00000000..bf14df68
--- /dev/null
+++ b/test/core/utils.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python
+#-*- coding:utf-8 -*-
+
+import unittest
+
+
+class TestUtils(unittest.TestCase):
+
+    def test_boundedDict(self):
+        from miasm2.core.utils import BoundedDict
+
+        # Use a callback
+        def logger(key):
+            print "DELETE", key
+
+        # Create a 5/2 dictionnary
+        bd = BoundedDict(5, 2, initialdata={"element": "value"},
+                         delete_cb=logger)
+        bd["element2"] = "value2"
+        assert("element" in bd)
+        assert("element2" in bd)
+        self.assertEqual(bd["element"], "value")
+        self.assertEqual(bd["element2"], "value2")
+
+        # Increase 'element2' use
+        _ = bd["element2"]
+
+        for i in xrange(6):
+            bd[i] = i
+            print "Insert %d -> %s" % (i, bd)
+
+        assert(len(bd) == 2)
+
+        assert("element2" in bd)
+        self.assertEqual(bd["element2"], "value2")
+
+
+if __name__ == '__main__':
+    testsuite = unittest.TestLoader().loadTestsFromTestCase(TestUtils)
+    report = unittest.TextTestRunner(verbosity=2).run(testsuite)
+    exit(len(report.errors + report.failures))