micropython

internal_tests/objectset.py

530:0f9aa4c4d92c
2012-06-09 Paul Boddie Added an __iter__ method for strings.
     1 #!/usr/bin/env python     2      3 from micropython.common import ObjectSet, combine_mapping_dicts     4      5 # Test storage.     6      7 o1 = ObjectSet()     8 o1.add("a")     9 o1.add("b")    10 o1.add("c")    11 print "o1 =", o1    12     13 # Test storage with associated data.    14     15 o2 = ObjectSet()    16 o2.add("b")    17 o2["c"] = ["x"]    18 o2.add("d")    19 o2["d"] += ["p", "q"]    20 print "o2 =", o2    21     22 print "o1 == o2", o1 == o2    23 print "o1 != o2", o1 != o2    24 assert o1 != o2    25     26 # Test merging.    27     28 o12 = o1.merge(o2)    29 print "o1 =", o1    30 print "o2 =", o2    31 print "o12 =", o12    32     33 o12_keys = o12.keys()    34 o12_keys.sort()    35 print "o12_keys =", o12_keys    36 assert o12_keys == ["a", "b", "c", "d"]    37     38 # Test copying.    39     40 o1_copy = ObjectSet(o1)    41 print "o1 =", o1    42 print "o1_copy =", o1_copy    43 print "o1 == o1_copy", o1 == o1_copy    44 assert o1 == o1_copy    45     46 # Test updating.    47     48 o1_copy.update(o2)    49 print "o1_copy =", o1_copy    50 print "o1 != o1_copy", o1 != o1_copy    51 print "o12 == o1_copy", o12 == o1_copy    52 assert o1 != o1_copy    53 assert o12 == o1_copy    54     55 o3 = ObjectSet()    56 o3.add("c")    57 o3["c"].append("y")    58 print "o3 =", o3    59     60 # Test combining.    61     62 d1 = {'a' : [ObjectSet(['f', 'g']), ObjectSet(['f', 'h'])]}    63 d2 = {'a' : [ObjectSet(['f']), ObjectSet(['e', 'f', 'g'])]}    64 d3 = combine_mapping_dicts(d1, d2)    65 print "d1 =", d1    66 print "d2 =", d2    67 print "d3 =", d3    68 assert d3["a"] == [ObjectSet(['f', 'g']), ObjectSet(['f', 'h']), ObjectSet(['e', 'f', 'g']), ObjectSet(['e', 'f', 'g', 'h'])]    69     70 # Test combining with empty sets.    71     72 d4 = {'a' : None}    73 d5 = combine_mapping_dicts(d1, d4)    74 print "d4 =", d4    75 print "d5 =", d5    76 assert d5 == d1    77     78 # vim: tabstop=4 expandtab shiftwidth=4