1 | import unittest |
---|
2 | import os |
---|
3 | import csv |
---|
4 | |
---|
5 | import order_boundary as ob |
---|
6 | |
---|
7 | |
---|
8 | class Test_order_boundary(unittest.TestCase): |
---|
9 | def setUp(self): |
---|
10 | pass |
---|
11 | |
---|
12 | def tearDown(self): |
---|
13 | pass |
---|
14 | |
---|
15 | def test_simple(self): |
---|
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 | |
---|
115 | infile = 'test.in.csv' |
---|
116 | outfile = 'test.out.csv' |
---|
117 | |
---|
118 | data = [('longitude','latitude','index'), |
---|
119 | (150.0833,-37.5,2758), |
---|
120 | (150.1,-37.4667,2765), |
---|
121 | (150.1167,-37.3833,2769), |
---|
122 | (150.1333,-36.7167,2771), |
---|
123 | (150.1333,-36.7667,2774), |
---|
124 | (150.15,-36.5833,2777), |
---|
125 | (150.15,-36.6333,2780), |
---|
126 | (150.15,-36.6833,2783), |
---|
127 | (150.15,-36.8167,2787), |
---|
128 | (150.15,-36.8667,2790), |
---|
129 | (150.15,-36.9167,2793), |
---|
130 | (150.15,-37.35,2795), |
---|
131 | (150.1667,-36.5,2796), |
---|
132 | (150.1667,-36.55,2799), |
---|
133 | (150.1667,-36.9667,2804), |
---|
134 | (150.1667,-37.0167,2807), |
---|
135 | (150.1667,-37.25,2808), |
---|
136 | (150.1667,-37.3,2811), |
---|
137 | (150.1833,-36.45,2815), |
---|
138 | (150.1833,-37.05,2821), |
---|
139 | (150.1833,-37.1,2824), |
---|
140 | (150.1833,-37.2167,2827), |
---|
141 | (150.2,-36.4,2831), |
---|
142 | (150.2,-37.1333,2835), |
---|
143 | (150.2,-37.1667,2837), |
---|
144 | (150.2,-37.2,2839), |
---|
145 | (150.2167,-36.3667,2841), |
---|
146 | (150.2333,-36.0667,2844), |
---|
147 | (150.2333,-36.1167,2847), |
---|
148 | (150.25,-36.0167,2852), |
---|
149 | (150.25,-36.1667,2856), |
---|
150 | (150.25,-36.2167,2859), |
---|
151 | (150.25,-36.3333,2862), |
---|
152 | (150.2667,-36.25,2865), |
---|
153 | (150.2667,-36.3,2868), |
---|
154 | (150.3,-35.8833,2870), |
---|
155 | (150.35,-35.75,2872), |
---|
156 | (150.4333,-35.6333,2873), |
---|
157 | (150.5,-35.5,2875), |
---|
158 | (150.5667,-35.4,2878), |
---|
159 | (150.6,-35.3667,2882), |
---|
160 | (150.6333,-35.3333,2889), |
---|
161 | (150.6667,-35.3,2893), |
---|
162 | (150.7,-35.2667,2898), |
---|
163 | (150.75,-35.2333,2903), |
---|
164 | (150.8,-35.1833,2907), |
---|
165 | (150.8333,-35.1333,2913), |
---|
166 | (150.8667,-35.1,2918), |
---|
167 | (150.9,-35.0667,2923), |
---|
168 | (150.9333,-35.0167,2927), |
---|
169 | (150.95,-34.9833,2938), |
---|
170 | (150.9667,-34.6333,2940), |
---|
171 | (150.9667,-34.6667,2942), |
---|
172 | (150.9667,-34.7,2944), |
---|
173 | (150.9667,-34.75,2947), |
---|
174 | (150.9667,-34.8,2948), |
---|
175 | (150.9667,-34.85,2951), |
---|
176 | (150.9667,-34.9,2953), |
---|
177 | (150.9667,-34.95,2956), |
---|
178 | (150.9833,-34.5833,2958), |
---|
179 | (151,-34.55,2962), |
---|
180 | (151.0167,-34.5167,2965), |
---|
181 | (151.0333,-34.5,2968), |
---|
182 | (151.05,-34.4667,2971), |
---|
183 | (151.0667,-34.4333,2974), |
---|
184 | (151.0833,-34.4,2977), |
---|
185 | (151.1,-34.35,2980), |
---|
186 | (151.1167,-34.3167,2984), |
---|
187 | (151.1333,-34.2833,2988), |
---|
188 | (151.15,-34.2333,2991), |
---|
189 | (151.1667,-34.1833,2995), |
---|
190 | (151.2,-34.15,3002), |
---|
191 | (151.2333,-34.1167,3008), |
---|
192 | (151.2667,-34.0833,3012), |
---|
193 | (151.3,-34.05,3016), |
---|
194 | (151.3333,-34.0167,3021), |
---|
195 | (151.3667,-33.9833,3023), |
---|
196 | (151.3833,-33.95,3027), |
---|
197 | (151.4,-33.85,3031), |
---|
198 | (151.4,-33.9,3034), |
---|
199 | (151.4167,-33.8,3037), |
---|
200 | (151.4333,-33.75,3041), |
---|
201 | (151.45,-33.7167,3046), |
---|
202 | (151.4833,-33.6833,3051), |
---|
203 | (151.5167,-33.65,3054), |
---|
204 | (151.55,-33.6167,3058), |
---|
205 | (151.5667,-33.5833,3060), |
---|
206 | (151.5833,-33.55,3063), |
---|
207 | (151.6167,-33.5167,3070), |
---|
208 | (151.65,-33.4833,3074), |
---|
209 | (151.6833,-33.45,3077), |
---|
210 | (151.7333,-33.35,3084), |
---|
211 | (151.7333,-33.4,3087), |
---|
212 | (151.75,-33.3167,3089), |
---|
213 | (151.7667,-33.2667,3091), |
---|
214 | (151.7833,-33.2,3095), |
---|
215 | (151.8,-33.1667,3099), |
---|
216 | (151.8333,-33.1,3109), |
---|
217 | (151.8667,-33.0667,3116), |
---|
218 | (151.9,-33.0333,3120), |
---|
219 | (151.9333,-33,3125), |
---|
220 | (151.9833,-32.9667,3130), |
---|
221 | (152.0333,-32.9333,3137), |
---|
222 | (152.1,-32.9,3145), |
---|
223 | (152.15,-32.8667,3151), |
---|
224 | (152.2,-32.8333,3158), |
---|
225 | (152.25,-32.8,3164), |
---|
226 | (152.2833,-32.7667,3168), |
---|
227 | (152.3333,-32.7167,3177), |
---|
228 | (152.3667,-32.7,3181), |
---|
229 | (152.4,-32.6667,3183), |
---|
230 | (152.4333,-32.65,3187), |
---|
231 | (152.4833,-32.6167,3193), |
---|
232 | (152.5167,-32.5833,3198), |
---|
233 | (152.55,-32.55,3202)] |
---|
234 | |
---|
235 | expected = [('longitude','latitude','index'), |
---|
236 | (150.0833,-37.5,'2758'), |
---|
237 | (150.1,-37.4667,'2765'), |
---|
238 | (150.1167,-37.3833,'2769'), |
---|
239 | (150.15,-37.35,'2795'), |
---|
240 | (150.1667,-37.3,'2811'), |
---|
241 | (150.1667,-37.25,'2808'), |
---|
242 | (150.1833,-37.2167,'2827'), |
---|
243 | (150.2,-37.2,'2839'), |
---|
244 | (150.2,-37.1667,'2837'), |
---|
245 | (150.2,-37.1333,'2835'), |
---|
246 | (150.1833,-37.1,'2824'), |
---|
247 | (150.1833,-37.05,'2821'), |
---|
248 | (150.1667,-37.0167,'2807'), |
---|
249 | (150.1667,-36.9667,'2804'), |
---|
250 | (150.15,-36.9167,'2793'), |
---|
251 | (150.15,-36.8667,'2790'), |
---|
252 | (150.15,-36.8167,'2787'), |
---|
253 | (150.1333,-36.7667,'2774'), |
---|
254 | (150.1333,-36.7167,'2771'), |
---|
255 | (150.15,-36.6833,'2783'), |
---|
256 | (150.15,-36.6333,'2780'), |
---|
257 | (150.15,-36.5833,'2777'), |
---|
258 | (150.1667,-36.55,'2799'), |
---|
259 | (150.1667,-36.5,'2796'), |
---|
260 | (150.1833,-36.45,'2815'), |
---|
261 | (150.2,-36.4,'2831'), |
---|
262 | (150.2167,-36.3667,'2841'), |
---|
263 | (150.25,-36.3333,'2862'), |
---|
264 | (150.2667,-36.3,'2868'), |
---|
265 | (150.2667,-36.25,'2865'), |
---|
266 | (150.25,-36.2167,'2859'), |
---|
267 | (150.25,-36.1667,'2856'), |
---|
268 | (150.2333,-36.1167,'2847'), |
---|
269 | (150.2333,-36.0667,'2844'), |
---|
270 | (150.25,-36.0167,'2852'), |
---|
271 | (150.3,-35.8833,'2870'), |
---|
272 | (150.35,-35.75,'2872'), |
---|
273 | (150.4333,-35.6333,'2873'), |
---|
274 | (150.5,-35.5,'2875'), |
---|
275 | (150.5667,-35.4,'2878'), |
---|
276 | (150.6,-35.3667,'2882'), |
---|
277 | (150.6333,-35.3333,'2889'), |
---|
278 | (150.6667,-35.3,'2893'), |
---|
279 | (150.7,-35.2667,'2898'), |
---|
280 | (150.75,-35.2333,'2903'), |
---|
281 | (150.8,-35.1833,'2907'), |
---|
282 | (150.8333,-35.1333,'2913'), |
---|
283 | (150.8667,-35.1,'2918'), |
---|
284 | (150.9,-35.0667,'2923'), |
---|
285 | (150.9333,-35.0167,'2927'), |
---|
286 | (150.95,-34.9833,'2938'), |
---|
287 | (150.9667,-34.95,'2956'), |
---|
288 | (150.9667,-34.9,'2953'), |
---|
289 | (150.9667,-34.85,'2951'), |
---|
290 | (150.9667,-34.8,'2948'), |
---|
291 | (150.9667,-34.75,'2947'), |
---|
292 | (150.9667,-34.7,'2944'), |
---|
293 | (150.9667,-34.6667,'2942'), |
---|
294 | (150.9667,-34.6333,'2940'), |
---|
295 | (150.9833,-34.5833,'2958'), |
---|
296 | (151.0,-34.55,'2962'), |
---|
297 | (151.0167,-34.5167,'2965'), |
---|
298 | (151.0333,-34.5,'2968'), |
---|
299 | (151.05,-34.4667,'2971'), |
---|
300 | (151.0667,-34.4333,'2974'), |
---|
301 | (151.0833,-34.4,'2977'), |
---|
302 | (151.1,-34.35,'2980'), |
---|
303 | (151.1167,-34.3167,'2984'), |
---|
304 | (151.1333,-34.2833,'2988'), |
---|
305 | (151.15,-34.2333,'2991'), |
---|
306 | (151.1667,-34.1833,'2995'), |
---|
307 | (151.2,-34.15,'3002'), |
---|
308 | (151.2333,-34.1167,'3008'), |
---|
309 | (151.2667,-34.0833,'3012'), |
---|
310 | (151.3,-34.05,'3016'), |
---|
311 | (151.3333,-34.0167,'3021'), |
---|
312 | (151.3667,-33.9833,'3023'), |
---|
313 | (151.3833,-33.95,'3027'), |
---|
314 | (151.4,-33.9,'3034'), |
---|
315 | (151.4,-33.85,'3031'), |
---|
316 | (151.4167,-33.8,'3037'), |
---|
317 | (151.4333,-33.75,'3041'), |
---|
318 | (151.45,-33.7167,'3046'), |
---|
319 | (151.4833,-33.6833,'3051'), |
---|
320 | (151.5167,-33.65,'3054'), |
---|
321 | (151.55,-33.6167,'3058'), |
---|
322 | (151.5667,-33.5833,'3060'), |
---|
323 | (151.5833,-33.55,'3063'), |
---|
324 | (151.6167,-33.5167,'3070'), |
---|
325 | (151.65,-33.4833,'3074'), |
---|
326 | (151.6833,-33.45,'3077'), |
---|
327 | (151.7333,-33.4,'3087'), |
---|
328 | (151.7333,-33.35,'3084'), |
---|
329 | (151.75,-33.3167,'3089'), |
---|
330 | (151.7667,-33.2667,'3091'), |
---|
331 | (151.7833,-33.2,'3095'), |
---|
332 | (151.8,-33.1667,'3099'), |
---|
333 | (151.8333,-33.1,'3109'), |
---|
334 | (151.8667,-33.0667,'3116'), |
---|
335 | (151.9,-33.0333,'3120'), |
---|
336 | (151.9333,-33.0,'3125'), |
---|
337 | (151.9833,-32.9667,'3130'), |
---|
338 | (152.0333,-32.9333,'3137'), |
---|
339 | (152.1,-32.9,'3145'), |
---|
340 | (152.15,-32.8667,'3151'), |
---|
341 | (152.2,-32.8333,'3158'), |
---|
342 | (152.25,-32.8,'3164'), |
---|
343 | (152.2833,-32.7667,'3168'), |
---|
344 | (152.3333,-32.7167,'3177'), |
---|
345 | (152.3667,-32.7,'3181'), |
---|
346 | (152.4,-32.6667,'3183'), |
---|
347 | (152.4333,-32.65,'3187'), |
---|
348 | (152.4833,-32.6167,'3193'), |
---|
349 | (152.5167,-32.5833,'3198'), |
---|
350 | (152.55,-32.55,'3202')] |
---|
351 | |
---|
352 | # put test data into a file |
---|
353 | fd = open(infile, 'wb') |
---|
354 | w = csv.writer(fd) |
---|
355 | for d in data: |
---|
356 | w.writerow(d) |
---|
357 | fd.close() |
---|
358 | |
---|
359 | # call routine, put sorted points into output file |
---|
360 | ob.order_boundary(infile, outfile) |
---|
361 | |
---|
362 | # get sorted data into memory |
---|
363 | fd = open(outfile, 'r') |
---|
364 | data_list = [] |
---|
365 | for data in csv.reader(fd): |
---|
366 | try: |
---|
367 | data[0] = float(data[0]) |
---|
368 | except: |
---|
369 | pass |
---|
370 | try: |
---|
371 | data[1] = float(data[1]) |
---|
372 | except: |
---|
373 | pass |
---|
374 | data_list.append(tuple(data)) |
---|
375 | fd.close() |
---|
376 | |
---|
377 | # check same as expected |
---|
378 | self.failUnless(data_list == expected) |
---|
379 | |
---|
380 | # clean up |
---|
381 | try: |
---|
382 | os.remove(infile) |
---|
383 | except: |
---|
384 | pass |
---|
385 | try: |
---|
386 | os.remove(outfile) |
---|
387 | except: |
---|
388 | pass |
---|
389 | |
---|
390 | |
---|
391 | #------------------------------------------------------------- |
---|
392 | |
---|
393 | if __name__ == "__main__": |
---|
394 | suite = unittest.makeSuite(Test_order_boundary, 'test') |
---|
395 | runner = unittest.TextTestRunner() |
---|
396 | runner.run(suite) |
---|
397 | |
---|