chiark / gitweb /
@@@ remove debugging print
[mLib-python] / t / t-atom.py
CommitLineData
81f68b64
MW
1### -*-python-*-
2###
3### Test atoms and related functionality
4###
5### (c) 2019 Straylight/Edgeware
6###
7
8###----- Licensing notice ---------------------------------------------------
9###
10### This file is part of the Python interface to mLib.
11###
12### mLib/Python is free software: you can redistribute it and/or modify it
13### under the terms of the GNU General Public License as published by the
14### Free Software Foundation; either version 2 of the License, or (at your
15### option) any later version.
16###
17### mLib/Python is distributed in the hope that it will be useful, but
18### WITHOUT ANY WARRANTY; without even the implied warranty of
19### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20### General Public License for more details.
21###
22### You should have received a copy of the GNU General Public License
23### along with mLib/Python. If not, write to the Free Software
24### Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
25### USA.
26
27import mLib as M
28import testutils as T
29import unittest as U
30
31###--------------------------------------------------------------------------
32class TestAtoms (U.TestCase):
33
34 def test_simple(me):
35 foo = M.Atom("foo")
36 bar = M.Atom("bar")
37 me.assertTrue(foo is M.Atom("foo"))
38 me.assertTrue(foo is not bar)
39 me.assertEqual(foo, foo)
40 me.assertNotEqual(foo, bar)
41 me.assertEqual(set(M.atoms()), set([foo, bar]))
42 me.assertEqual(foo.name, "foo")
43 me.assertTrue(foo.internedp)
44 me.assertEqual(foo.home, M.DEFAULT_ATOMTABLE)
45
46 def test_obarray(me):
47 tab = M.AtomTable()
48 foo = tab.intern("foo")
49 bar = M.Atom("bar", tab)
50 g0 = tab.gensym()
51 g1 = M.Atom(table = tab)
52 me.assertEqual(bar, tab.intern("bar"))
53 me.assertNotEqual(foo, M.Atom("foo"))
54 for sym in [foo, bar, g0, g1]:
55 me.assertEqual(sym.home, tab)
56 me.assertTrue(sym.livep)
57 for sym in [foo, bar]: me.assertTrue(sym.internedp)
58 for sym in [g0, g1]: me.assertFalse(sym.internedp)
59 me.assertEqual(tab["foo"], foo)
60 me.assertEqual(tab.get("spong"), None)
61 me.assertFalse("spong" in tab)
62 spong = tab["spong"]
63 me.assertTrue("spong" in tab)
64 me.assertEqual(set(T.itervalues(tab)), set([foo, bar, spong, g0, g1]))
65 del tab
66 for sym in [foo, bar, spong, g0, g1]: me.assertFalse(sym.livep)
67 me.assertRaises(ValueError, getattr, foo, "name")
68
69 tab = M.AtomTable()
70 a = tab["a"]
71 me.assertRaises(TypeError, M.Atom, None, tab, foo = "extra!")
72 me.assertRaises(TypeError, M.Atom, table = tab, bar = "extra!")
73 me.assertRaises(TypeError, M.Atom, None, tab, "extra!")
74 me.assertTrue(a.livep)
75 del tab
76 me.assertFalse(a.livep)
77
78
79###--------------------------------------------------------------------------
80class TestAssoc (T.MutableMappingTestMixin):
81
82 def setUp(me):
83 me._obarray = M.AtomTable()
84
85 def _mkkey(me, i): return me._obarray["k#%d" % i]
86 def _getkey(me, k): return int(k.name[2:])
87
88 def test_mapping(me):
89 me.check_mapping(lambda: M.AssocTable(None, me._obarray))
90
91 def test_assoc(me):
92 obarray = me._obarray
93 foo = obarray["foo"]
94 bar = obarray["bar"]
95 baz = obarray["baz"]
96 tab = M.AssocTable({ foo: 1 }, obarray, bar = 2)
97 me.assertEqual(tab.table, obarray)
98 me.assertEqual(tab["foo"], 1)
99 me.assertEqual(tab[bar], 2)
100 me.assertRaises(ValueError, tab.get, M.Atom("foo"))
101
102###----- That's all, folks --------------------------------------------------
103
104if __name__ == "__main__": U.main()