1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import unittest |
---|
4 | from math import sqrt |
---|
5 | |
---|
6 | from domain import * |
---|
7 | from Numeric import allclose, array |
---|
8 | |
---|
9 | |
---|
10 | class TestCase(unittest.TestCase): |
---|
11 | def setUp(self): |
---|
12 | pass |
---|
13 | |
---|
14 | |
---|
15 | def tearDown(self): |
---|
16 | pass |
---|
17 | |
---|
18 | |
---|
19 | def test_construct(self): |
---|
20 | |
---|
21 | a = 0.0 |
---|
22 | b = 2.0 |
---|
23 | c = 2.5 |
---|
24 | d = 3.0 |
---|
25 | e = 10.0 |
---|
26 | f = 12.0 |
---|
27 | |
---|
28 | points = [a, b, c, d, e, f] |
---|
29 | |
---|
30 | domain = Domain(points) |
---|
31 | assert allclose(domain.get_centroids(),[(a+b)*0.5, (b+c)*0.5, (c+d)*0.5, (d+e)*0.5, (e+f)*0.5]) |
---|
32 | |
---|
33 | |
---|
34 | def test_area(self): |
---|
35 | |
---|
36 | a = 0.0 |
---|
37 | b = 2.0 |
---|
38 | c = 2.5 |
---|
39 | d = 3.0 |
---|
40 | e = 10.0 |
---|
41 | f = 12.0 |
---|
42 | |
---|
43 | points = [a, b, c, d, e, f] |
---|
44 | |
---|
45 | domain = Domain(points) |
---|
46 | assert allclose(domain.get_area(0),b-a) |
---|
47 | assert allclose(domain.get_area(1),c-b) |
---|
48 | assert allclose(domain.get_area(2),d-c) |
---|
49 | assert allclose(domain.get_area(3),e-d) |
---|
50 | assert allclose(domain.get_area(4),f-e) |
---|
51 | |
---|
52 | def test_in_order(self): |
---|
53 | a = 0.0 |
---|
54 | b = 2.0 |
---|
55 | c = 2.5 |
---|
56 | d = 3.0 |
---|
57 | e = 10.0 |
---|
58 | f = 12.0 |
---|
59 | |
---|
60 | points = [a, c, b, d, e, f] |
---|
61 | |
---|
62 | try: |
---|
63 | domain = Domain(points) |
---|
64 | except: |
---|
65 | pass |
---|
66 | else: |
---|
67 | msg = 'Should have raised exception' |
---|
68 | raise msg |
---|
69 | |
---|
70 | def test_point(self): |
---|
71 | a = 0.0 |
---|
72 | b = 2.0 |
---|
73 | c = 2.5 |
---|
74 | d = 3.0 |
---|
75 | |
---|
76 | points = [a, b, c, d] |
---|
77 | |
---|
78 | domain = Domain(points) |
---|
79 | |
---|
80 | assert allclose(domain.get_coordinate(0), 0.5*(a+b)) |
---|
81 | assert allclose(domain.get_coordinate(1), 0.5*(b+c)) |
---|
82 | assert allclose(domain.get_coordinate(2), 0.5*(c+d)) |
---|
83 | |
---|
84 | try: |
---|
85 | allclose(domain.get_coordinate(3), 0.5*(a+b)) |
---|
86 | except: |
---|
87 | pass |
---|
88 | else: |
---|
89 | msg = 'Should have raised exception' |
---|
90 | raise msg |
---|
91 | |
---|
92 | assert allclose(domain.get_coordinate(0,0), a) |
---|
93 | assert allclose(domain.get_coordinate(1,0), b) |
---|
94 | assert allclose(domain.get_coordinate(2,0), c) |
---|
95 | |
---|
96 | assert allclose(domain.get_coordinate(0,1), b) |
---|
97 | assert allclose(domain.get_coordinate(1,1), c) |
---|
98 | assert allclose(domain.get_coordinate(2,1), d) |
---|
99 | |
---|
100 | |
---|
101 | |
---|
102 | |
---|
103 | #------------------------------------------------------------- |
---|
104 | if __name__ == "__main__": |
---|
105 | suite = unittest.makeSuite(TestCase,'test') |
---|
106 | runner = unittest.TextTestRunner() |
---|
107 | runner.run(suite) |
---|