source: anuga_core/source/anuga/lib/order_boundary/test_order_boundary.py @ 6211

Last change on this file since 6211 was 6210, checked in by rwilson, 15 years ago

Put test file data into test code.

File size: 14.5 KB
Line 
1import unittest
2import os
3import order_boundary as ob
4
5
6class Test_order_boundary(unittest.TestCase):
7    def setUp(self):
8        pass
9
10    def tearDown(self):
11        pass
12
13    def test_Simple(self):
14        import os
15        import csv
16
17        # filenames
18        Test_input_file_path = 'test.in.csv'
19        Test_output_file_path = 'test.out.csv'
20
21        # input data
22        Data = [('longitude','latitude','index'),
23                ( 1.0,  1.0, 'alpha', 'extra'),
24                ( 2.3,  2.0, 'bravo'),
25                ( 3.9,  3.0, 'charlie'),
26                ( 9.0,  9.9, 'delta'),
27                (10.0, 10.4, 'echo'),
28                (11.0, 11.0, 'foxtrot'),
29                (15.9, 16.0, 'golf'),
30                (17.0, 17.1, 'hotel'),
31                (17.9, 18.0, 'india'),
32                (12.2, 12.0, 'juliet'),
33                ( 4.7,  4.0, 'kilo'),
34                ( 5.2,  5.0, 'lima'),
35                ( 6.0,  6.0, 'mike'),
36                ( 7.0,  7.3, 'november', 'extra', 'extra', 'extra'),
37                ( 8.0,  8.7, 'oscar'),
38                (13.6, 13.0, 'papa'),
39                (14.9, 14.0, 'quebec'),
40                (15.8, 15.0, 'romeo'),
41                (16.0, 16.2, 'sierra'),
42                (17.1, 17.1, 'tango'),
43                (18.0, 18.7, 'uniform'),
44                (19.0, 19.9, 'victor'),
45                (20.0, 20.0, 'whisky')]
46
47        # expected output data
48        Expected = [('longitude','latitude','index'),
49                    ( 1.0,  1.0, 'alpha', 'extra'),
50                    ( 2.3,  2.0, 'bravo'),
51                    ( 3.9,  3.0, 'charlie'),
52                    ( 4.7,  4.0, 'kilo'),
53                    ( 5.2,  5.0, 'lima'),
54                    ( 6.0,  6.0, 'mike'),
55                    ( 7.0,  7.3, 'november', 'extra', 'extra', 'extra'),
56                    ( 8.0,  8.7, 'oscar'),
57                    ( 9.0,  9.9, 'delta'),
58                    (10.0, 10.4, 'echo'),
59                    (11.0, 11.0, 'foxtrot'),
60                    (12.2, 12.0, 'juliet'),
61                    (13.6, 13.0, 'papa'),
62                    (14.9, 14.0, 'quebec'),
63                    (15.8, 15.0, 'romeo'),
64                    (15.9, 16.0, 'golf'),
65                    (16.0, 16.2, 'sierra'),
66                    (17.0, 17.1, 'hotel'),
67                    (17.1, 17.1, 'tango'),
68                    (17.9, 18.0, 'india'),
69                    (18.0, 18.7, 'uniform'),
70                    (19.0, 19.9, 'victor'),
71                    (20.0, 20.0, 'whisky')]
72
73        # put test data into a file
74        fd = open(Test_input_file_path, 'wb')
75        w = csv.writer(fd)
76        for d in Data:
77            w.writerow(d)
78        fd.close()
79
80        # call routine, put sorted points into output file
81        ob.order_boundary(Test_input_file_path, Test_output_file_path)
82
83        # get sorted data into memory
84        fd = open(Test_output_file_path, 'r')
85        data_list = []
86        for data in csv.reader(fd):
87            try:
88                data[0] = float(data[0])
89            except:
90                pass
91            try:
92                data[1] = float(data[1])
93            except:
94                pass
95            data_list.append(tuple(data))
96        fd.close()
97
98        # check same as Expected
99        self.failUnless(data_list == Expected)
100
101        # clean up
102        try:
103            os.remove(Test_input_file_path)
104        except:
105            pass
106        try:
107            os.remove(Test_output_file_path)
108        except:
109            pass
110
111    # Not a correctness test, as the result was generated by the routine.
112    # Still good as a regression test - Johnno told me the output was good.
113    def test_regression(self):
114        import os
115        import csv
116
117        infile = 'test.in.csv'
118        outfile = 'test.out.csv'
119
120        data = [('longitude','latitude','index'),
121                (150.0833,-37.5,2758),
122                (150.1,-37.4667,2765),
123                (150.1167,-37.3833,2769),
124                (150.1333,-36.7167,2771),
125                (150.1333,-36.7667,2774),
126                (150.15,-36.5833,2777),
127                (150.15,-36.6333,2780),
128                (150.15,-36.6833,2783),
129                (150.15,-36.8167,2787),
130                (150.15,-36.8667,2790),
131                (150.15,-36.9167,2793),
132                (150.15,-37.35,2795),
133                (150.1667,-36.5,2796),
134                (150.1667,-36.55,2799),
135                (150.1667,-36.9667,2804),
136                (150.1667,-37.0167,2807),
137                (150.1667,-37.25,2808),
138                (150.1667,-37.3,2811),
139                (150.1833,-36.45,2815),
140                (150.1833,-37.05,2821),
141                (150.1833,-37.1,2824),
142                (150.1833,-37.2167,2827),
143                (150.2,-36.4,2831),
144                (150.2,-37.1333,2835),
145                (150.2,-37.1667,2837),
146                (150.2,-37.2,2839),
147                (150.2167,-36.3667,2841),
148                (150.2333,-36.0667,2844),
149                (150.2333,-36.1167,2847),
150                (150.25,-36.0167,2852),
151                (150.25,-36.1667,2856),
152                (150.25,-36.2167,2859),
153                (150.25,-36.3333,2862),
154                (150.2667,-36.25,2865),
155                (150.2667,-36.3,2868),
156                (150.3,-35.8833,2870),
157                (150.35,-35.75,2872),
158                (150.4333,-35.6333,2873),
159                (150.5,-35.5,2875),
160                (150.5667,-35.4,2878),
161                (150.6,-35.3667,2882),
162                (150.6333,-35.3333,2889),
163                (150.6667,-35.3,2893),
164                (150.7,-35.2667,2898),
165                (150.75,-35.2333,2903),
166                (150.8,-35.1833,2907),
167                (150.8333,-35.1333,2913),
168                (150.8667,-35.1,2918),
169                (150.9,-35.0667,2923),
170                (150.9333,-35.0167,2927),
171                (150.95,-34.9833,2938),
172                (150.9667,-34.6333,2940),
173                (150.9667,-34.6667,2942),
174                (150.9667,-34.7,2944),
175                (150.9667,-34.75,2947),
176                (150.9667,-34.8,2948),
177                (150.9667,-34.85,2951),
178                (150.9667,-34.9,2953),
179                (150.9667,-34.95,2956),
180                (150.9833,-34.5833,2958),
181                (151,-34.55,2962),
182                (151.0167,-34.5167,2965),
183                (151.0333,-34.5,2968),
184                (151.05,-34.4667,2971),
185                (151.0667,-34.4333,2974),
186                (151.0833,-34.4,2977),
187                (151.1,-34.35,2980),
188                (151.1167,-34.3167,2984),
189                (151.1333,-34.2833,2988),
190                (151.15,-34.2333,2991),
191                (151.1667,-34.1833,2995),
192                (151.2,-34.15,3002),
193                (151.2333,-34.1167,3008),
194                (151.2667,-34.0833,3012),
195                (151.3,-34.05,3016),
196                (151.3333,-34.0167,3021),
197                (151.3667,-33.9833,3023),
198                (151.3833,-33.95,3027),
199                (151.4,-33.85,3031),
200                (151.4,-33.9,3034),
201                (151.4167,-33.8,3037),
202                (151.4333,-33.75,3041),
203                (151.45,-33.7167,3046),
204                (151.4833,-33.6833,3051),
205                (151.5167,-33.65,3054),
206                (151.55,-33.6167,3058),
207                (151.5667,-33.5833,3060),
208                (151.5833,-33.55,3063),
209                (151.6167,-33.5167,3070),
210                (151.65,-33.4833,3074),
211                (151.6833,-33.45,3077),
212                (151.7333,-33.35,3084),
213                (151.7333,-33.4,3087),
214                (151.75,-33.3167,3089),
215                (151.7667,-33.2667,3091),
216                (151.7833,-33.2,3095),
217                (151.8,-33.1667,3099),
218                (151.8333,-33.1,3109),
219                (151.8667,-33.0667,3116),
220                (151.9,-33.0333,3120),
221                (151.9333,-33,3125),
222                (151.9833,-32.9667,3130),
223                (152.0333,-32.9333,3137),
224                (152.1,-32.9,3145),
225                (152.15,-32.8667,3151),
226                (152.2,-32.8333,3158),
227                (152.25,-32.8,3164),
228                (152.2833,-32.7667,3168),
229                (152.3333,-32.7167,3177),
230                (152.3667,-32.7,3181),
231                (152.4,-32.6667,3183),
232                (152.4333,-32.65,3187),
233                (152.4833,-32.6167,3193),
234                (152.5167,-32.5833,3198),
235                (152.55,-32.55,3202)]
236
237        expected = [('longitude','latitude','index'),
238                    (150.0833,-37.5,'2758'),
239                    (150.1,-37.4667,'2765'),
240                    (150.1167,-37.3833,'2769'),
241                    (150.15,-37.35,'2795'),
242                    (150.1667,-37.3,'2811'),
243                    (150.1667,-37.25,'2808'),
244                    (150.1833,-37.2167,'2827'),
245                    (150.2,-37.2,'2839'),
246                    (150.2,-37.1667,'2837'),
247                    (150.2,-37.1333,'2835'),
248                    (150.1833,-37.1,'2824'),
249                    (150.1833,-37.05,'2821'),
250                    (150.1667,-37.0167,'2807'),
251                    (150.1667,-36.9667,'2804'),
252                    (150.15,-36.9167,'2793'),
253                    (150.15,-36.8667,'2790'),
254                    (150.15,-36.8167,'2787'),
255                    (150.1333,-36.7667,'2774'),
256                    (150.1333,-36.7167,'2771'),
257                    (150.15,-36.6833,'2783'),
258                    (150.15,-36.6333,'2780'),
259                    (150.15,-36.5833,'2777'),
260                    (150.1667,-36.55,'2799'),
261                    (150.1667,-36.5,'2796'),
262                    (150.1833,-36.45,'2815'),
263                    (150.2,-36.4,'2831'),
264                    (150.2167,-36.3667,'2841'),
265                    (150.25,-36.3333,'2862'),
266                    (150.2667,-36.3,'2868'),
267                    (150.2667,-36.25,'2865'),
268                    (150.25,-36.2167,'2859'),
269                    (150.25,-36.1667,'2856'),
270                    (150.2333,-36.1167,'2847'),
271                    (150.2333,-36.0667,'2844'),
272                    (150.25,-36.0167,'2852'),
273                    (150.3,-35.8833,'2870'),
274                    (150.35,-35.75,'2872'),
275                    (150.4333,-35.6333,'2873'),
276                    (150.5,-35.5,'2875'),
277                    (150.5667,-35.4,'2878'),
278                    (150.6,-35.3667,'2882'),
279                    (150.6333,-35.3333,'2889'),
280                    (150.6667,-35.3,'2893'),
281                    (150.7,-35.2667,'2898'),
282                    (150.75,-35.2333,'2903'),
283                    (150.8,-35.1833,'2907'),
284                    (150.8333,-35.1333,'2913'),
285                    (150.8667,-35.1,'2918'),
286                    (150.9,-35.0667,'2923'),
287                    (150.9333,-35.0167,'2927'),
288                    (150.95,-34.9833,'2938'),
289                    (150.9667,-34.95,'2956'),
290                    (150.9667,-34.9,'2953'),
291                    (150.9667,-34.85,'2951'),
292                    (150.9667,-34.8,'2948'),
293                    (150.9667,-34.75,'2947'),
294                    (150.9667,-34.7,'2944'),
295                    (150.9667,-34.6667,'2942'),
296                    (150.9667,-34.6333,'2940'),
297                    (150.9833,-34.5833,'2958'),
298                    (151.0,-34.55,'2962'),
299                    (151.0167,-34.5167,'2965'),
300                    (151.0333,-34.5,'2968'),
301                    (151.05,-34.4667,'2971'),
302                    (151.0667,-34.4333,'2974'),
303                    (151.0833,-34.4,'2977'),
304                    (151.1,-34.35,'2980'),
305                    (151.1167,-34.3167,'2984'),
306                    (151.1333,-34.2833,'2988'),
307                    (151.15,-34.2333,'2991'),
308                    (151.1667,-34.1833,'2995'),
309                    (151.2,-34.15,'3002'),
310                    (151.2333,-34.1167,'3008'),
311                    (151.2667,-34.0833,'3012'),
312                    (151.3,-34.05,'3016'),
313                    (151.3333,-34.0167,'3021'),
314                    (151.3667,-33.9833,'3023'),
315                    (151.3833,-33.95,'3027'),
316                    (151.4,-33.9,'3034'),
317                    (151.4,-33.85,'3031'),
318                    (151.4167,-33.8,'3037'),
319                    (151.4333,-33.75,'3041'),
320                    (151.45,-33.7167,'3046'),
321                    (151.4833,-33.6833,'3051'),
322                    (151.5167,-33.65,'3054'),
323                    (151.55,-33.6167,'3058'),
324                    (151.5667,-33.5833,'3060'),
325                    (151.5833,-33.55,'3063'),
326                    (151.6167,-33.5167,'3070'),
327                    (151.65,-33.4833,'3074'),
328                    (151.6833,-33.45,'3077'),
329                    (151.7333,-33.4,'3087'),
330                    (151.7333,-33.35,'3084'),
331                    (151.75,-33.3167,'3089'),
332                    (151.7667,-33.2667,'3091'),
333                    (151.7833,-33.2,'3095'),
334                    (151.8,-33.1667,'3099'),
335                    (151.8333,-33.1,'3109'),
336                    (151.8667,-33.0667,'3116'),
337                    (151.9,-33.0333,'3120'),
338                    (151.9333,-33.0,'3125'),
339                    (151.9833,-32.9667,'3130'),
340                    (152.0333,-32.9333,'3137'),
341                    (152.1,-32.9,'3145'),
342                    (152.15,-32.8667,'3151'),
343                    (152.2,-32.8333,'3158'),
344                    (152.25,-32.8,'3164'),
345                    (152.2833,-32.7667,'3168'),
346                    (152.3333,-32.7167,'3177'),
347                    (152.3667,-32.7,'3181'),
348                    (152.4,-32.6667,'3183'),
349                    (152.4333,-32.65,'3187'),
350                    (152.4833,-32.6167,'3193'),
351                    (152.5167,-32.5833,'3198'),
352                    (152.55,-32.55,'3202')]
353
354        # put test data into a file
355        fd = open(infile, 'wb')
356        w = csv.writer(fd)
357        for d in data:
358            w.writerow(d)
359        fd.close()
360
361        # call routine, put sorted points into output file
362        ob.order_boundary(infile, outfile)
363
364        # get sorted data into memory
365        fd = open(outfile, 'r')
366        data_list = []
367        for data in csv.reader(fd):
368            try:
369                data[0] = float(data[0])
370            except:
371                pass
372            try:
373                data[1] = float(data[1])
374            except:
375                pass
376            data_list.append(tuple(data))
377        fd.close()
378
379        # check same as Expected
380        self.failUnless(data_list == expected)
381
382        # clean up
383        try:
384            os.remove(infile)
385        except:
386            pass
387        try:
388            os.remove(outfile)
389        except:
390            pass
391
392
393#-------------------------------------------------------------
394
395if __name__ == "__main__":
396    suite = unittest.makeSuite(Test_order_boundary, 'test')
397    runner = unittest.TextTestRunner()
398    runner.run(suite)
399
Note: See TracBrowser for help on using the repository browser.