--- trunk/thuban/test/test_classgen.py 2003/06/12 12:42:24 1166 +++ trunk/thuban/test/test_classgen.py 2003/07/16 13:25:04 1436 @@ -21,56 +21,78 @@ generate_uniform_distribution, \ generate_quantiles, \ calculate_quantiles, \ - GreyRamp + GreyRamp, CustomRamp, FixedRamp from Thuban.Model.range import Range +from Thuban.Model.color import Color -from Thuban.Model.classification import ClassGroupRange +from Thuban.Model.classification import ClassGroupRange, ClassGroupProperties class ClassGenTest(unittest.TestCase): def doClassRangeTest(self, clazz, ranges): self.assertEquals(clazz.GetNumGroups(), len(ranges)) - for i in range(len(ranges)): + for i in range(clazz.GetNumGroups()): group = clazz.GetGroup(i) r1 = str(Range(ranges[i])) r2 = group.GetRange() self.assertEquals(r1, r2) + self.doBoundsTest(clazz) + def doClassSingleTest(self, clazz, _list): self.assertEquals(clazz.GetNumGroups(), len(_list)) - for i in range(len(_list)): + for i in range(clazz.GetNumGroups()): group = clazz.GetGroup(i) self.assertEquals(group.GetValue(), _list[i]) + + self.doBoundsTest(clazz) + + def doBoundsTest(self, clazz, ramp = GreyRamp): + + # + # check that the properties are right (i.e. the first group + # is all white, the last is all black). This assumes that + # the GreyRamp, unless another is provided. + # + if clazz.GetNumGroups() >= 1: + groupF = clazz.GetGroup(0) + first = ramp.GetProperties(0) + self.assertEquals(groupF.GetProperties(), first) + + if clazz.GetNumGroups() >= 2: + groupL = clazz.GetGroup(clazz.GetNumGroups() - 1) + last = ramp.GetProperties(1) + self.assertEquals(groupL.GetProperties(), last) def test_generate_singletons(self): """Test generate_singletons""" eq = self.assertEquals gs = generate_singletons - ramp = GreyRamp() + ramp = GreyRamp _list = [1, 2, 3, 4] - cl = gs(_list, len(_list), ramp) + cl = gs(_list, ramp) self.doClassSingleTest(cl, _list) _list = range(0, 100) - cl = gs(_list, len(_list), ramp) + cl = gs(_list, ramp) self.doClassSingleTest(cl, _list) _list = range(-100, 100) - cl = gs(_list, len(_list), ramp) + cl = gs(_list, ramp) self.doClassSingleTest(cl, _list) _list = ['a', 'b', 'c', 'd'] - cl = gs(_list, len(_list), ramp) + cl = gs(_list, ramp) self.doClassSingleTest(cl, _list) _list = [] - cl = gs(_list, len(_list), ramp) + cl = gs(_list, ramp) self.doClassSingleTest(cl, _list) _list = [1] - cl = gs(_list, len(_list), ramp) + cl = gs(_list, ramp) self.doClassSingleTest(cl, _list) @@ -79,7 +101,7 @@ eq = self.assertEquals gud = generate_uniform_distribution - ramp = GreyRamp() + ramp = GreyRamp cl = gud(0, 99, 10, ramp, True) self.doClassRangeTest(cl, ("[0;10[", "[10;20[", "[20;30[", "[30;40[", @@ -110,7 +132,7 @@ gq = generate_quantiles - ramp = GreyRamp() + ramp = GreyRamp adj, cl = gq([1, 2, 3, 4], [.25, .5, .75, 1.0], ramp, Range("[1;4]")) self.failIf(adj) @@ -277,6 +299,43 @@ [.12, .24, .36, .50, .62, .76, .88, 1.0], Range("[1;5]")) eq(result, (1, 0, 24, [(4, .2), (9, .4), (14, .6), (19, .8), (24, 1.0)])) + +class TestCustomRamp(unittest.TestCase): + + def test_color_interpolation(self): + """Test CustomRamp color interpolation""" + start = ClassGroupProperties() + start.SetFill(Color(1, 1, 1)) + start.SetLineColor(Color(0, 0, 0)) + + end = ClassGroupProperties() + end.SetFill(Color(1, 0, 0)) + end.SetLineColor(Color(0, 1, 0)) + + ramp = CustomRamp(start, end) + half = ramp.GetProperties(0.5) + self.assertEquals(half.GetFill(), Color(1, 0.5, 0.5)) + self.assertEquals(half.GetLineColor(), Color(0, 0.5, 0)) + +class TestFixedRamp(unittest.TestCase): + + def test(self): + eq = self.assertEquals + + for lineColor, lineWidth, fillColor in \ + [(None, None, None), (Color(1, 1, 1), None, None), + (None, 4, None), (None, None, Color(0, 1, 0)), + (Color(1, 1, 1), 4, None), (Color(1, 1, 1), None, Color(0, 1, 0)), + (None, 4, Color(0, 1, 0)), (Color(1, 1, 1), 4, Color(0, 1, 0))]: + + framp = FixedRamp(GreyRamp, (lineColor, lineWidth, fillColor)) + + for i in [0.0, 0.2, 0.4, 0.6, 0.8, 1.0]: + props = framp.GetProperties(i) + if lineColor: eq(props.GetLineColor(), lineColor) + if lineWidth: eq(props.GetLineWidth(), lineWidth) + if fillColor: eq(props.GetFill(), fillColor) + if __name__ == "__main__": unittest.main()