1 |
# Copyright (c) 2002, 2003 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 CalculateQuantiles |
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 |
# |
30 |
# Test CalculateQuantiles |
31 |
# |
32 |
|
33 |
cq = CalculateQuantiles |
34 |
|
35 |
result = cq([1, 2, 3, 4], [.25, .5, .75, 1.0], Range("[1;4]")) |
36 |
eq(result, (0, 0, 3, [(0, .25), (1, .5), (2, .75), (3, 1.0)])) |
37 |
|
38 |
result = cq(range(0, 100), [.25, .5, .75, 1.0], Range("[0;100]")) |
39 |
eq(result, (0, 0, 99, [(24, .25), (49, .5), (74, .75), (99, 1.0)])) |
40 |
|
41 |
result = cq(range(0, 100), [.33, .66, 1.0], Range("[0;100]")) |
42 |
eq(result, (0, 0, 99, [(32, .33), (65, .66), (99, 1.0)])) |
43 |
|
44 |
# negative input |
45 |
result = cq(range(-100, 100), [.33, .66, 1.0], Range("[-100;100]")) |
46 |
eq(result, (0, 0, 199, [(65, .33), (131, .66), (199, 1.0)])) |
47 |
|
48 |
# unequal percentiles |
49 |
result = cq(range(0, 100), [.25, .66, .8, 1.0], Range("[0;100]")) |
50 |
eq(result, (0, 0, 99, [(24, .25), (65, .66), (79, .8), (99, 1.0)])) |
51 |
|
52 |
# input all the same |
53 |
result = cq([1, 1, 1, 1], [.25, .5, .75, 1.0], Range("[1;4]")) |
54 |
eq(result, (1, 0, 3, [(3, 1.0)])) |
55 |
|
56 |
# empty input |
57 |
result = cq([], [.25, .5, .75, 1.0], Range("[1;4]")) |
58 |
eq(result, None) |
59 |
|
60 |
# empty range |
61 |
result = cq([1, 2, 3, 4], [.25, .5, .75, 1.0], Range("]0;1[")) |
62 |
eq(result, None) |
63 |
|
64 |
# empty percentiles |
65 |
result = cq([1, 2, 3, 4], [], Range("]0;1[")) |
66 |
eq(result, None) |
67 |
|
68 |
# single percentile |
69 |
result = cq([1, 2, 3, 4], [.5], Range("[0;4]")) |
70 |
eq(result, (0, 0, 3, [(1, .5)])) |
71 |
|
72 |
# more percentiles than input |
73 |
result = cq([1], [.5, 1.0], Range("[0;4]")) |
74 |
eq(result, (1, 0, 0, [(0, 1.0)])) |
75 |
|
76 |
result = cq([1], [.1, .2, .3, .4, .5, .6, .7, .8, .9, 1.0], Range("[0;4]")) |
77 |
eq(result, (1, 0, 0, [(0, 1.0)])) |
78 |
|
79 |
# range smaller than the input |
80 |
result = cq([1, 2, 3, 4], [.5, 1.0], Range("[2;3]")) |
81 |
eq(result, (0, 1, 2, [(1, .5), (2, 1.0)])) |
82 |
|
83 |
# range outside the input |
84 |
result = cq([5, 6, 7, 8], [.5, 1.0], Range("[2;3]")) |
85 |
eq(result, None) |
86 |
|
87 |
result = cq([1, 1, 1, 1, 1, 1], [.25, .5, .75, 1.0], Range("[1;4]")) |
88 |
eq(result, (1, 0, 5, [(5, 1.0)])) |
89 |
|
90 |
result = cq([1, 1, 1, 1, 1, 2, 3], [.25, .5, .75, 1.0], Range("[1;4]")) |
91 |
eq(result, (1, 0, 6, |
92 |
[(4, 0.7142857142857143), # the algorithm generated |
93 |
(5, 0.8571428571428571), # these values, but they are |
94 |
(6, 1.0)])) # right. |
95 |
|
96 |
# adjusted quantiles |
97 |
result = cq([1, 1, 1, 1, 1, |
98 |
2, 2, 2, 2, 2, |
99 |
3, 3, 3, 3, 3, |
100 |
4, 4, 4, 4, 4, |
101 |
5, 5, 5, 5, 5], |
102 |
[.12, .24, .36, .50, .62, .76, .88, 1.0], Range("[1;5]")) |
103 |
eq(result, (1, 0, 24, [(4, .2), (9, .4), (14, .6), (19, .8), (24, 1.0)])) |
104 |
|
105 |
if __name__ == "__main__": |
106 |
unittest.main() |
107 |
|