micropython

Change of internal_tests/objectset.py

487:a898fc7c3998
internal_tests/objectset.py
     1.1 --- a/internal_tests/objectset.py	Mon Feb 27 23:17:50 2012 +0100
     1.2 +++ b/internal_tests/objectset.py	Mon Feb 27 23:22:33 2012 +0100
     1.3 @@ -2,12 +2,16 @@
     1.4  
     1.5  from micropython.common import ObjectSet, combine_mapping_dicts
     1.6  
     1.7 +# Test storage.
     1.8 +
     1.9  o1 = ObjectSet()
    1.10  o1.add("a")
    1.11  o1.add("b")
    1.12  o1.add("c")
    1.13  print "o1 =", o1
    1.14  
    1.15 +# Test storage with associated data.
    1.16 +
    1.17  o2 = ObjectSet()
    1.18  o2.add("b")
    1.19  o2["c"] = ["x"]
    1.20 @@ -15,9 +19,11 @@
    1.21  o2["d"] += ["p", "q"]
    1.22  print "o2 =", o2
    1.23  
    1.24 -assert o1 != o2
    1.25  print "o1 == o2", o1 == o2
    1.26  print "o1 != o2", o1 != o2
    1.27 +assert o1 != o2
    1.28 +
    1.29 +# Test merging.
    1.30  
    1.31  o12 = o1.merge(o2)
    1.32  print "o1 =", o1
    1.33 @@ -26,37 +32,47 @@
    1.34  
    1.35  o12_keys = o12.keys()
    1.36  o12_keys.sort()
    1.37 +print "o12_keys =", o12_keys
    1.38  assert o12_keys == ["a", "b", "c", "d"]
    1.39 -print "o12_keys =", o12_keys
    1.40 +
    1.41 +# Test copying.
    1.42  
    1.43  o1_copy = ObjectSet(o1)
    1.44 -assert o1 == o1_copy
    1.45  print "o1 =", o1
    1.46  print "o1_copy =", o1_copy
    1.47  print "o1 == o1_copy", o1 == o1_copy
    1.48 +assert o1 == o1_copy
    1.49 +
    1.50 +# Test updating.
    1.51  
    1.52  o1_copy.update(o2)
    1.53 -assert o1 != o1_copy
    1.54 -assert o12 == o1_copy
    1.55  print "o1_copy =", o1_copy
    1.56  print "o1 != o1_copy", o1 != o1_copy
    1.57  print "o12 == o1_copy", o12 == o1_copy
    1.58 +assert o1 != o1_copy
    1.59 +assert o12 == o1_copy
    1.60  
    1.61  o3 = ObjectSet()
    1.62  o3.add("c")
    1.63  o3["c"].append("y")
    1.64  print "o3 =", o3
    1.65  
    1.66 +# Test combining.
    1.67 +
    1.68  d1 = {'a' : [ObjectSet(['f', 'g']), ObjectSet(['f', 'h'])]}
    1.69  d2 = {'a' : [ObjectSet(['f']), ObjectSet(['e', 'f', 'g'])]}
    1.70  d3 = combine_mapping_dicts(d1, d2)
    1.71  print "d1 =", d1
    1.72  print "d2 =", d2
    1.73  print "d3 =", d3
    1.74 +assert d3["a"] == [ObjectSet(['f', 'g']), ObjectSet(['f', 'h']), ObjectSet(['e', 'f', 'g']), ObjectSet(['e', 'f', 'g', 'h'])]
    1.75 +
    1.76 +# Test combining with empty sets.
    1.77  
    1.78  d4 = {'a' : None}
    1.79  d5 = combine_mapping_dicts(d1, d4)
    1.80  print "d4 =", d4
    1.81  print "d5 =", d5
    1.82 +assert d5 == d1
    1.83  
    1.84  # vim: tabstop=4 expandtab shiftwidth=4