1 |
jonathan |
890 |
# Copyright (c) 2002 by Intevation GmbH |
2 |
|
|
# Authors: |
3 |
|
|
# Bernhard Herzog <[email protected]> |
4 |
|
|
# |
5 |
|
|
# This program is free software under the GPL (>=v2) |
6 |
|
|
# Read the file COPYING coming with Thuban for details. |
7 |
|
|
|
8 |
|
|
""" |
9 |
|
|
Test the Menu |
10 |
|
|
""" |
11 |
|
|
|
12 |
|
|
__version__ = "$Revision$" |
13 |
|
|
|
14 |
|
|
import unittest |
15 |
|
|
|
16 |
|
|
import support |
17 |
|
|
support.initthuban() |
18 |
|
|
|
19 |
|
|
from Thuban.Model.classgen import ClassGenerator |
20 |
|
|
from Thuban.Model.range import Range |
21 |
|
|
|
22 |
|
|
class ClassGenTest(unittest.TestCase): |
23 |
|
|
|
24 |
|
|
def test(self): |
25 |
|
|
"""Test ClassGenerator methods""" |
26 |
|
|
|
27 |
|
|
eq = self.assertEquals |
28 |
|
|
|
29 |
|
|
cg = ClassGenerator() |
30 |
|
|
|
31 |
|
|
# |
32 |
|
|
# Test CalculateQuantiles |
33 |
|
|
# |
34 |
|
|
|
35 |
|
|
cq = cg.CalculateQuantiles |
36 |
|
|
|
37 |
|
|
result = cq([1, 2, 3, 4], [.25, .5, .75, 1.0], Range("[1;4]")) |
38 |
|
|
eq(result, (0, [(0, .25), (1, .5), (2, .75), (3, 1.0)])) |
39 |
|
|
|
40 |
|
|
result = cq(range(0, 100), [.25, .5, .75, 1.0], Range("[0;100]")) |
41 |
|
|
eq(result, (0, [(24, .25), (49, .5), (74, .75), (99, 1.0)])) |
42 |
|
|
|
43 |
|
|
result = cq(range(0, 100), [.33, .66, 1.0], Range("[0;100]")) |
44 |
|
|
eq(result, (0, [(32, .33), (65, .66), (99, 1.0)])) |
45 |
|
|
|
46 |
|
|
# negative input |
47 |
|
|
result = cq(range(-100, 100), [.33, .66, 1.0], Range("[-100;100]")) |
48 |
|
|
eq(result, (0, [(65, .33), (131, .66), (199, 1.0)])) |
49 |
|
|
|
50 |
|
|
# unequal percentiles |
51 |
|
|
result = cq(range(0, 100), [.25, .66, .8, 1.0], Range("[0;100]")) |
52 |
|
|
eq(result, (0, [(24, .25), (65, .66), (79, .8), (99, 1.0)])) |
53 |
|
|
|
54 |
|
|
# input all the same |
55 |
|
|
result = cq([1, 1, 1, 1], [.25, .5, .75, 1.0], Range("[1;4]")) |
56 |
|
|
eq(result, (0, [(3, 1.0)])) |
57 |
|
|
|
58 |
|
|
# empty input |
59 |
|
|
result = cq([], [.25, .5, .75, 1.0], Range("[1;4]")) |
60 |
|
|
eq(result, (0, [])) |
61 |
|
|
|
62 |
|
|
# empty range |
63 |
|
|
result = cq([1, 2, 3, 4], [.25, .5, .75, 1.0], Range("]0;1[")) |
64 |
|
|
eq(result, (0, [])) |
65 |
|
|
|
66 |
|
|
# empty percentiles |
67 |
|
|
result = cq([1, 2, 3, 4], [], Range("]0;1[")) |
68 |
|
|
eq(result, (0, [])) |
69 |
|
|
|
70 |
|
|
# single percentile |
71 |
|
|
result = cq([1, 2, 3, 4], [.5], Range("[0;4]")) |
72 |
|
|
eq(result, (0, [(1, .5)])) |
73 |
|
|
|
74 |
|
|
# more percentiles than input |
75 |
|
|
result = cq([1], [.5, 1.0], Range("[0;4]")) |
76 |
|
|
eq(result, (1, [(0, 1.0)])) |
77 |
|
|
|
78 |
|
|
result = cq([1], [.1, .2, .3, .4, .5, .6, .7, .8, .9, 1.0], Range("[0;4]")) |
79 |
|
|
eq(result, (1, [(0, 1.0)])) |
80 |
|
|
|
81 |
|
|
# range smaller than the input |
82 |
|
|
result = cq([1, 2, 3, 4], [.5, 1.0], Range("[2;3]")) |
83 |
|
|
eq(result, (0, [(1, .5), (2, 1.0)])) |
84 |
|
|
|
85 |
|
|
# range outside the input |
86 |
|
|
result = cq([5, 6, 7, 8], [.5, 1.0], Range("[2;3]")) |
87 |
|
|
eq(result, (0, [])) |
88 |
|
|
|
89 |
|
|
result = cq([1, 1, 1, 1, 1, 1], [.25, .5, .75, 1.0], Range("[1;4]")) |
90 |
|
|
eq(result, (1, [(5, 1.0)])) |
91 |
|
|
|
92 |
|
|
result = cq([1, 1, 1, 1, 1, 2, 3], [.25, .5, .75, 1.0], Range("[1;4]")) |
93 |
|
|
eq(result, (1, [(4, 0.7142857142857143), # the algorithm generated |
94 |
|
|
(5, 0.8571428571428571), # these values, but they are |
95 |
|
|
(6, 1.0)])) # right. |
96 |
|
|
|
97 |
|
|
# adjusted quantiles |
98 |
|
|
result = cq([1, 1, 1, 1, 1, |
99 |
|
|
2, 2, 2, 2, 2, |
100 |
|
|
3, 3, 3, 3, 3, |
101 |
|
|
4, 4, 4, 4, 4, |
102 |
|
|
5, 5, 5, 5, 5], |
103 |
|
|
[.12, .24, .36, .50, .62, .76, .88, 1.0], Range("[1;5]")) |
104 |
|
|
eq(result, (1, [(4, .2), (9, .4), (14, .6), (19, .8), (24, 1.0)])) |
105 |
|
|
|
106 |
|
|
if __name__ == "__main__": |
107 |
|
|
unittest.main() |
108 |
|
|
|