File: affine_matrix_test_case.py

package info (click to toggle)
python-enable 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 7,220 kB
  • sloc: cpp: 57,417; python: 28,437; makefile: 314; sh: 43
file content (143 lines) | stat: -rw-r--r-- 4,374 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from math import pi
import unittest

from numpy import array, allclose, ones, alltrue

from kiva import agg

class AffineMatrixTestCase(unittest.TestCase):

    def test_init(self):
        m = agg.AffineMatrix()

    def test_init_from_array(self):
        a = ones(6,'d')
        m = agg.AffineMatrix(a)
        desired = ones(6,'d')
        result = m.asarray()
        assert(alltrue(result == desired))

    def test_init_from_array1(self):
        a = ones(6,'D')
        try:
            m = agg.AffineMatrix(a)
        except NotImplementedError:
            pass # can't init from complex value.

    def test_init_from_array2(self):
        a = ones(7,'d')
        try:
            m = agg.AffineMatrix(a)
        except ValueError:
            pass # can't init from array that isn't 6 element.

    def test_init_from_array3(self):
        a = ones((2,3),'d')
        try:
            m = agg.AffineMatrix(a)
        except ValueError:
            pass # can't init from array that isn't 1d.

    def test_imul(self):
        a = agg.AffineMatrix((2.0,0,0,2.0,0,0))
        a *= a
        actual = a
        desired = agg.AffineMatrix((4.0,0,0,4.0,0,0))
        assert(alltrue(desired==actual))

    def test_asarray(self):
        m = agg.AffineMatrix()
        result = m.asarray()
        desired = array((1.0,0.0,0.0,1.0,0.0,0.0))
        assert(alltrue(result == desired))

    def _test_zero_arg_transform(self,method, orig, desired):
        m = agg.AffineMatrix(orig)
        method(m)
        result = m.asarray()
        assert(alltrue(result == desired))

    def test_flip_x(self):
        method = agg.AffineMatrix.flip_x
        orig = array((1.0,2.0,3.0,1.0,4.0,5.0))
        desired = array([-1.,-2.,3.,1.,-4.,5.])
        self._test_zero_arg_transform(method,orig,desired)

    def test_flip_y(self):
        method = agg.AffineMatrix.flip_y
        orig = array((1.0,2.0,3.0,1.0,4.0,5.0))
        desired = array([ 1.,2.,-3.,-1.,4.,-5.])
        self._test_zero_arg_transform(method,orig,desired)

    def test_reset(self):
        method = agg.AffineMatrix.reset
        orig = array((1.0,2.0,3.0,1.0,4.0,5.0))
        desired = array([ 1.,0.,0.,1.,0.,0.])
        self._test_zero_arg_transform(method,orig,desired)

    def test_multiply(self):
        orig = array((1.0,2.0,3.0,1.0,4.0,5.0))
        desired = array([  7.,4.,6.,7.,23.,18.])
        m = agg.AffineMatrix(orig)
        other = agg.AffineMatrix(orig)
        m.multiply(other)
        result = m.asarray()
        assert(alltrue(result == desired))

    def test_determinant(self):
        orig = array((1.0,2.0,3.0,1.0,4.0,5.0))
        desired = -0.2
        m = agg.AffineMatrix(orig)
        result = m.determinant()
        assert(alltrue(result == desired))

    def test_invert(self):
        orig = agg.AffineMatrix((1.0,2.0,3.0,1.0,4.0,5.0))
        orig.invert()
        actual = orig.asarray()
        desired = array([-0.2,0.4,0.6,-0.2,-2.2,-0.6])
        assert(allclose(desired,actual))

    def test_rotation_matrix(self):
        val = agg.rotation_matrix(pi/2.)
        desired = array([ 0.0,1.0,-1.0,0.0,0.0,0.0])
        actual = val.asarray()
        assert(allclose(desired,actual))

    def test_translation_matrix(self):
        val = agg.translation_matrix(2.0,3.0)
        desired = array([ 1.0,0.0,0.0,1.0,2.0,3.0])
        actual = val.asarray()
        assert(allclose(desired,actual))

    def test_scaling_matrix(self):
        val = agg.scaling_matrix(4.0,4.0)
        desired = array([ 4.0,0.0,0.0,4.0,0.0,0.0])
        actual = val.asarray()
        assert(allclose(desired,actual))

    def test_skewing_matrix(self):
        val = agg.skewing_matrix(pi/4.,pi/4.)
        desired = array([ 1.0,1.0,1.0,1.0,0.0,0.0])
        actual = val.asarray()
        assert(allclose(desired,actual))

#----------------------------------------------------------------------------
# test setup code.
#----------------------------------------------------------------------------

def test_suite(level=1):
    suites = []
    if level > 0:
        suites.append( unittest.makeSuite(AffineMatrixTestCase,'test_') )
    total_suite = unittest.TestSuite(suites)
    return total_suite

def test(level=10):
    all_tests = test_suite(level)
    runner = unittest.TextTestRunner()
    runner.run(all_tests)
    return runner

if __name__ == "__main__":
    test()