-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
164 lines (162 loc) · 7.22 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import udbm
import unittest
class UDBMTest(unittest.TestCase):
def setUp(self):
self.c = udbm.Context(["x", "y", "z"], name = "c")
def test_int_valuation(self):
c = self.c
v = udbm.IntValuation(c)
self.assertRaises(KeyError, lambda :(v["not_in_federation"]))
self.assertRaises(TypeError, v.__setitem__, ("x", 0.1)) # too bad we aren't using python 2.7 where it's possible to use "with self.assertRaises"
v["x"] = 1
v["y"] = 1
v["z"] = 1
self.assertTrue(( (c.x == 1) & (c.y == 1) & (c.z == 1)).contains(v))
self.assertFalse(((c.x == 2) & (c.y == 1) & (c.z == 1)).contains(v))
def test_float_valuation(self):
c = self.c
v = udbm.FloatValuation(c)
self.assertRaises(KeyError, lambda :(v["not_in_federation"]))
v["x"] = 1.0
v["y"] = 1.01
v["z"] = 1
self.assertFalse(( (c.x == 1) & (c.y == 1) & (c.z == 1)).contains(v))
self.assertTrue(((c.x == 1) & (c.y < 2) & (c.y > 1) & (c.z == 1)).contains(v))
def test_set_operations(self):
c = self.c
self.assertTrue( (c.x == 1) == (c.x >= 1) & (c.x <= 1))
self.assertFalse( (c.x == 1) == (c.x >= 1) & (c.x < 1))
self.assertTrue( (c.x != 1) == ((c.x > 1) | (c.x < 1)))
self.assertFalse( (c.x != 1) == ((c.x > 1) | (c.x <= 1)))
self.assertTrue( (c.x == 1) & (c.y == 1) == (c.y == 1) & (c.x == 1))
self.assertTrue( (c.x == 1) | (c.y == 1) == (c.y == 1) | (c.x == 1))
self.assertFalse( (c.x == 1) | (c.y == 1) != (c.y == 1) | (c.x == 1))
self.assertFalse( (c.x == 1) & (c.y == 1) != (c.y == 1) & (c.x == 1))
self.assertTrue( (c.x == 1) & (c.y == 1) != (c.y == 1) | (c.x == 1))
self.assertTrue( (c.x == 1) & ((c.y == 1) | (c.z ==1)) == (c.x == 1) & (c.y == 1) |(c.x == 1) & (c.z ==1) )
self.assertFalse( (c.x == 1) & ((c.y == 1) | (c.z ==1)) == (c.x == 1) & (c.y == 1) |(c.x == 1) )
self.assertTrue( (c.x - c.y <= 1) == (c.y - c.x >= -1))
self.assertFalse( (c.x - c.y <= 1) == (c.y - c.x > -1))
self.assertTrue( ((c.x - c.y == 1) & (c.x == 1) ) == ((c.x == 1) & (c.y == 0)) )
self.assertTrue( (c.x - c.y != 1) == ((c.x - c.y > 1) | (c.x - c.y < 1)) )
def test_zero(self):
c = self.c
self.assertFalse( (c.x == 1).hasZero())
self.assertFalse( (c.x > 1).hasZero())
self.assertTrue( (c.x < 1).hasZero())
self.assertTrue( ((c.x == 1) & (c.z == 2)).setZero() == ((c.x == 0 ) & (c.z == 0) & (c.y ==0) ))
self.assertTrue( ((c.x == 1) & (c.z == 2)).setZero().hasZero())
def test_update_clocks(self):
c = self.c
self.assertTrue( ((c.x == 1) | (c.z == 2)).updateValue(c.x, 2) == (c.x == 2 ) )
self.assertTrue( ((c.x == 1) & (c.z == 2)).resetValue(c.x) == ((c.x == 0 ) & (c.z == 2) ) )
def test_str(self):
c = self.c
self.assertTrue(str((c.x == 1) & (c.y == 1)) == "(c.x==1 & c.x==c.y & c.y==1)")
def test_copy(self):
c = self.c
a = ((c.x - c.y)==1)
b = a.copy()
d = b.copy()
self.assertTrue( a == b)
b &= (c.z == 1)
d |= (c.z == 1)
self.assertFalse( a == b)
self.assertFalse( d == b)
def test_reduce(self):
c = self.c
a = (c.x >= 1) | (c.x <= 1)
self.assertTrue(a.getSize() == 2)
a.reduce()
self.assertTrue(a.getSize() == 1)
def test_convex_hull(self):
c = self.c
d1 = (c.x >= 1) & (c.x <=2) & (c.y>=1) & (c.y <=2)
d2 = (c.x >= 3) & (c.x <=4) & (c.y>=3) & (c.y <=4)
d3 = (c.x - c.y <= 1) & (c.y - c.x <= 1) & (c.x >= 1) & (c.y >= 1) & (c.x <= 4) & (c.y <= 4)
self.assertTrue((d1 + d2) == d3)
self.assertTrue((d1 | d2).convexHull() == d3)
d1 += d2
self.assertTrue(d1 == d3)
def test_sub(self):
c = self.c
d1 = (c.x >= 1) & (c.x <=2) & (c.y>=1) & (c.y <=2)
d2 = (c.x >= 3) & (c.x <=4) & (c.y>=3) & (c.y <=4)
d3 = d1 | d2
self.assertTrue(d3 - d1 == d2)
d3 -= d2
self.assertTrue(d3 == d1)
def test_up_down(self):
c = udbm.Context(["x", "y"]) # we need only two variables here
d1 = (c.x >= 1) & (c.x <=2) & (c.y>=1) & (c.y <=2)
d2 = (c.x - c.y <= 1) & (c.y - c.x <= 1) & (c.x >= 1) & (c.y >= 1)
d3 = (c.x - c.y <= 1) & (c.y - c.x <= 1) & (c.x <= 2) & (c.y <= 2)
self.assertTrue(d1.up() == d2)
self.assertTrue(d1.down() == d3)
def test_isnt_mutable(self):
c = self.c
d1 = (c.x - c.y <= 1) & (c.y - c.x <= 1) & (c.x >= 1) & (c.y >= 1) & (c.y <= 4) # some random
d2 = d1.copy()
d2.up()
self.assertTrue(d1 == d2)
d2.down()
self.assertTrue(d1 == d2)
d2.down()
self.assertTrue(d1 == d2)
d2.freeClock(c.x)
self.assertTrue(d1 == d2)
d2.convexHull()
self.assertTrue(d1 == d2)
d2.predt(d2)
self.assertTrue(d1 == d2)
d2.resetValue(c.x)
self.assertTrue(d1 == d2)
def test_set_init(self):
c = self.c
d = (c.x - c.y <= 1) & (c.y - c.x <= 1) & (c.x >= 1) & (c.y >= 1) & (c.y <= 4) # some random
d.setInit()
self.assertTrue(d == ((c.x >= 0) & (c.y >= 0) & (c.z >= 0)))
self.assertTrue(d != ((c.x >= 1) & (c.y >= 0) & (c.z >= 0)))
def test_federation_ops(self):
c = self.c
d1 = (c.x - c.y <= 1) & (c.y - c.x <= 1) & (c.x >= 1) & (c.y >= 1) & (c.y <= 4) # some random
d2 = (c.x - c.y <= 1) & (c.y - c.x <= 1) & (c.x >= 1)
self.assertTrue(d1 <= d2)
self.assertTrue(d1 < d2)
self.assertTrue(d2 >= d1)
self.assertTrue(d2 > d1)
self.assertFalse(d1 == d2)
self.assertTrue(d1 != d2)
def test_intern(self):
c = self.c
d = (c["x"] - c["y"] <= 1)
d.intern()
def testExtrapolateMaxBounds(self):
c = self.c
v = (c.x - c.y <= 1) & (c.x < 150) & (c.z < 150) & (c.x - c.z <= 1000)
a = {c.x: 100, c.y:300, c.z:400}
self.assertTrue(v.extrapolateMaxBounds(a) == ((c.x-c.y<=1) & (c.z<150)))
def test_free_clock(self):
c = self.c
self.assertTrue(((c.x >= 10) & (c.y >= 10)).freeClock(c.x) == (c.y >= 10))
def test_zero_federation(self):
c = self.c
self.assertTrue(c.getZeroFederation().isZero())
self.assertTrue(c.getZeroFederation().hasZero())
self.assertTrue(udbm.Federation(c).isZero())
self.assertFalse((c.x==1).isZero())
self.assertFalse((c.x==1) == c.getZeroFederation())
def test_hash(self):
c = self.c
self.assertTrue((c.x==1).hash() == (c.x==1).hash())
self.assertFalse((c.y==1).hash() == (c.x==1).hash())
self.assertTrue(((c.x==1) | (c.y == 1)).hash() == ((c.y == 1) | (c.x==1)).hash())
def test_isempty(self):
c = self.c
self.assertTrue(((c.x==1) & (c.x !=1)).isEmpty())
self.assertFalse(((c.x==1) | (c.x !=1)).isEmpty())
self.assertFalse((c.x==1).isEmpty())
self.assertFalse(((c.x==1) & (c.y !=1)).isEmpty())
self.assertTrue( (((c.x==1) & (c.x !=1)) | ((c.y==1) & (c.y !=1))).isEmpty())
if __name__ == '__main__':
unittest.main()