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
2.1 --- a/micropython/common.py Mon Feb 27 23:17:50 2012 +0100
2.2 +++ b/micropython/common.py Mon Feb 27 23:22:33 2012 +0100
2.3 @@ -3,7 +3,7 @@
2.4 """
2.5 Common classes.
2.6
2.7 -Copyright (C) 2007, 2008, 2009, 2010, 2011 Paul Boddie <paul@boddie.org.uk>
2.8 +Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 Paul Boddie <paul@boddie.org.uk>
2.9
2.10 This program is free software; you can redistribute it and/or modify it under
2.11 the terms of the GNU General Public License as published by the Free Software
2.12 @@ -76,7 +76,10 @@
2.13 return hash(tuple(self.keys()))
2.14
2.15 def __eq__(self, other):
2.16 - return self.objects == other.objects
2.17 + if hasattr(other, "objects"):
2.18 + return self.objects == other.objects
2.19 + else:
2.20 + return set(self.objects.keys()) == set(other)
2.21
2.22 # Set methods.
2.23
2.24 @@ -228,13 +231,16 @@
2.25 members.
2.26 """
2.27
2.28 + # If either list is undefined (indicated by None), return the defined list,
2.29 + # or return None if neither is defined.
2.30 +
2.31 if l1 is None:
2.32 if l2 is None:
2.33 return None
2.34 else:
2.35 - return l2 + [None]
2.36 + return l2 + []
2.37 elif l2 is None:
2.38 - return l1 + [None]
2.39 + return l1 + []
2.40
2.41 combined = ObjectSet()
2.42 for i1 in l1:
3.1 --- a/tests/attributes_instance_assignment.py Mon Feb 27 23:17:50 2012 +0100
3.2 +++ b/tests/attributes_instance_assignment.py Mon Feb 27 23:22:33 2012 +0100
3.3 @@ -15,9 +15,17 @@
3.4 pass
3.5 a.x = 3
3.6
3.7 +def g(a): # {{z}, {x, z}}
3.8 + if a.z:
3.9 + a.x # {x, z}
3.10 + # {z} (else)
3.11 + a.z = 4
3.12 +
3.13 c = C(1)
3.14 -d = D(2)
3.15 +d = D(0)
3.16 f(c)
3.17 +g(d)
3.18 result_3 = c.x
3.19 +result_4 = d.z
3.20
3.21 # vim: tabstop=4 expandtab shiftwidth=4
4.1 --- a/tests/attributes_instance_assignment_on_self.py Mon Feb 27 23:17:50 2012 +0100
4.2 +++ b/tests/attributes_instance_assignment_on_self.py Mon Feb 27 23:22:33 2012 +0100
4.3 @@ -15,9 +15,16 @@
4.4 self.y = y
4.5 self.z = None
4.6
4.7 + def g(self, a):
4.8 + if self.z:
4.9 + self.x
4.10 + self.z = a
4.11 +
4.12 c = C(1)
4.13 d = D(2)
4.14 c.f(3)
4.15 +d.g(4)
4.16 result_3 = c.x
4.17 +result_4 = d.z
4.18
4.19 # vim: tabstop=4 expandtab shiftwidth=4