@@@ cython and python3
[mLib-python] / test.py
diff --git a/test.py b/test.py
new file mode 100644 (file)
index 0000000..33f781e
--- /dev/null
+++ b/test.py
@@ -0,0 +1,70 @@
+#! /usr/bin/python
+### -*- coding: utf-8 -*-
+
+import sys as SYS
+import mLib as M
+
+if SYS.version_info >= (3,):
+  def _bin(text): return text.encode()
+  def _text(bin): return bin.decode()
+else:
+  def _bin(text): return text
+  def _text(bin): return bin
+
+def must_equal(x, y):
+  if x == y: pass
+  else: raise AssertionError("%r != %r" % (x, y))
+
+## Done!
+print(";; test begins (Python %s)" % SYS.version)
+
+## crc32
+must_equal(M.crc32(_bin("abc")), 0x352441c2)
+must_equal(M.CRC32().chunk(_bin("a")).chunk(_bin("bc")).done(), 0x352441c2)
+
+## unihash
+assert M.Unihash().key is None
+must_equal(M.Unihash.hash(_bin("abc")), 0xbf71f6a2)
+must_equal(M.Unihash().chunk(_bin("a")).chunk(_bin("bc")).done(), 0xbf71f6a2)
+key = M.UnihashKey(0x8498a262)
+assert M.Unihash(key).key is key
+must_equal(M.Unihash.hash(_bin("abc"), key), 0xecd1e2a2)
+must_equal(key.hash(_bin("abc")), 0xecd1e2a2)
+must_equal(M.Unihash(key).chunk(_bin("a")).chunk(_bin("bc")).done(), 0xecd1e2a2)
+M.setglobalkey(0x8498a262)
+must_equal(M.Unihash.hash(_bin("abc")), 0xecd1e2a2)
+
+## atom
+foo = M.Atom("foo")
+bar = M.Atom("bär")
+assert foo != bar
+assert foo is M.Atom("foo")
+assert bar is M.Atom("bär")
+assert foo.internedp
+must_equal(foo.name, "foo")
+must_equal(bar.name, "bär")
+gen = M.Atom()
+assert gen is not M.Atom()
+assert not gen.internedp
+all = set(M.atoms())
+assert foo in all
+assert bar in all
+
+## assoc, sym
+def test_mapping(mapcls, keya, keyz):
+  tab = mapcls()
+  tab[keya] = 69; must_equal(tab[keya], 69)
+  tab[keya] = 42; must_equal(tab[keya], 42)
+  tab[keyz] = 'zing'; must_equal(tab[keyz], 'zing')
+  del tab[keyz]
+  try: tab[keyz]
+  except KeyError: pass
+  else: assert False
+  must_equal(list(tab.keys()), [keya])
+  must_equal(list(tab.values()), [42])
+  must_equal(list(tab.items()), [(keya, 42)])
+test_mapping(M.AssocTable, foo, bar)
+test_mapping(M.SymTable, 'foo', 'bar')
+
+## Done!
+print(";; test completed OK")