File: test_canvas.js

package info (click to toggle)
python-pattern 2.6%2Bgit20180818-4.1
  • links: PTS
  • area: main
  • in suites: sid, trixie
  • size: 95,160 kB
  • sloc: python: 28,135; xml: 15,085; javascript: 5,810; makefile: 194
file content (219 lines) | stat: -rw-r--r-- 8,239 bytes parent folder | download | duplicates (5)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
var test_canvas = {
    
    //----------------------------------------------------------------------------------------------
    // Unit tests for the canvas.js module (see also test.html).

    TestUtility: function() {
        this.setUp = function() {
            return;
        };
        this.tearDown = function() {
            return;
        };
        this.test_closure = function() {
            var o = {"k": "v"};
            var m = function() { return this.k };
            assert(Function.closure(o, m)() == "v");
            console.log("Function.closure()");
        };
    },

    //----------------------------------------------------------------------------------------------
    
    TestGeometry: function() {
        this.setUp = function() {
            return;
        };
        this.tearDown = function() {
            return;
        };
        this.test_round = function() {
            assert(Math.round(3.445, 2) == 3.45);
            console.log("Math.round()");
        };
        this.test_sign = function() {
            assert(Math.sign(+10) == +1);
            assert(Math.sign(-10) == -1);
            console.log("Math.sign()");
        };
        this.test_degrees = function() {
            assert(Math.degrees(Math.PI) == 180);
            console.log("Math.round()");
        };
        this.test_radians = function() {
            assert(Math.radians(180) == Math.PI);
            console.log("Math.round()");
        };
        this.test_radians = function() {
            assert(Math.radians(180) == Math.PI);
            console.log("Math.round()");
        };
        this.test_clamp = function() {
            assert(Math.clamp(+10, 0, 1) == 1);
            assert(Math.clamp(-10, 0, 1) == 0);
            console.log("Math.clamp()");
        };
        this.test_dot = function() {
            assert(Math.dot([1,2,3], [4,5,6]) == 32);
            console.log("Math.dot()");
        };
        this.test_point = function() {
            assert(new Point(1,2).copy().x == 1);
            assert(new Point(1,2).copy().y == 2);
            console.log("Point()");
        };
        this.test_angle = function() {
            assert(geometry.angle(0, 0, 10, 10) == 45);
            console.log("geometry.angle()");
        };
        this.test_distance = function() {
            assert(geometry.distance(0, 0, 10, 10) == Math.sqrt(100+100));
            console.log("geometry.distance()");
        };
        this.test_coordinates = function() {
            var pt = geometry.coordinates(0, 0, Math.sqrt(200), 45);
            assert(Math.round(pt.x) == 10);
            assert(Math.round(pt.y) == 10);
            console.log("geometry.coordinates()");
        };
        this.test_rotate = function() {
            var pt = geometry.rotate(10, 10, 0, 0, 180);
            assert(Math.round(pt.x) == -10);
            assert(Math.round(pt.y) == -10);
            console.log("geometry.rotate()");
        };
        this.test_reflect = function() {
            var pt = geometry.reflect(0, 0, 10, 10, 2.0, 180);
            assert(Math.round(pt.x) == -20);
            assert(Math.round(pt.y) == -20);
            console.log("geometry.reflect()");
        };
        this.test_lerp = function() {
            assert(geometry.lerp(100, 200, 0.5) == 150);
            console.log("geometry.lerp()");
        };
        this.test_smoothstep = function() {
            assert(geometry.smoothstep(0, 1, 0.00) == 0);
            assert(geometry.smoothstep(0, 1, 0.50) == 0.5);
            assert(geometry.smoothstep(0, 1, 0.75) == 0.84375);
            console.log("geometry.lerp()");
        };
    },
    
    //----------------------------------------------------------------------------------------------
    
    TestArray: function() {
        this.setUp = function() {
            return;
        };
        this.tearDown = function() {
            return;
        };
        this.test_min = function() {
            assert(Array.min([1,2,3]) == 1);
            console.log("Array.min()");
        };
        this.test_max = function() {
            assert(Array.max([1,2,3]) == 3);
            console.log("Array.max()");
        };
        this.test_sum = function() {
            assert(Array.sum([1,2,3]) == 6);
            console.log("Array.sum()");
        };
        this.test_contains = function() {
            assert(Array.contains([1,2,3], 0) == false);
            assert(Array.contains([1,2,3], 1) == true);
            console.log("Array.contains()");
        };
        this.test_find = function() {
            assert(Array.find([1,2,3], function(x) { return x==2; }) == 1);
            console.log("Array.find()");
        };
        this.test_map = function() {
            assert(Array.eq(Array.map([1,2,3], function(x) { return x-1; }), [0,1,2]));
            console.log("Array.map()");
        };
        this.test_filter = function() {
            assert(Array.eq(Array.filter([1,2,3], function(x) { return x<3; }), [1,2]));
            console.log("Array.filter()");
        };
        this.test_sorted = function() {
            assert(Array.eq(Array.sorted([2,3,1]), [1,2,3]));
            console.log("Array.sorted()");
        };
        this.test_reversed = function() {
            assert(Array.eq(Array.reversed([1,2,3]), [3,2,1]));
            console.log("Array.reversed()");
        };
        this.test_range = function() {
            assert(Array.eq(Array.range(3), [0,1,2]));
            assert(Array.eq(Array.range(1,4), [1,2,3]));
            console.log("Array.range()");
        };
    },
    
    //----------------------------------------------------------------------------------------------
    
    TestColor: function() {
        this.setUp = function() {
            return;
        };
        this.tearDown = function() {
            return;
        };
        this.test_color = function() {
            assert(Array.eq(new Color().rgba(), [0,0,0,0]));
            assert(Array.eq(new Color(0).rgba(), [0,0,0,1]));
            assert(Array.eq(new Color(1).rgba(), [1,1,1,1]));
            assert(Array.eq(new Color(0,0).rgba(), [0,0,0,0]));
            assert(Array.eq(new Color(0,0,0.5).rgba(), [0,0,0.5,1]));
            assert(Array.eq(new Color(1,1,1,1).rgba(), [1,1,1,1]));
            assert(Array.eq(new Color([1,0,0,1]).rgba(), [1,0,0,1]));
            assert(Array.eq(new Color([1,0,0,0]).rgba(), [1,0,0,0]));
            assert(Array.eq(new Color(255,255,0,0, {base:255}).rgba(), [1,1,0,0]));
            assert(Array.eq(new Color(1,1,0.5,0, {colorspace:HSB}).rgba(), [0.5,0,0,0]));
            assert(Array.eq(new Color(new Color(0)).rgba(), [0,0,0,1]));
            console.log("Color()");
        };
        this.test_rgb = function() {
            assert(Array.eq(new Color(1,0.5,0).rgb(), [1,0.5,0]));
            console.log("Color.rgb()")
        };
        this.test_rgba = function() {
            assert(Array.eq(new Color(1,0.5,0,0).rgba(), [1,0.5,0,0]));
            console.log("Color.rgba()")
        };
        this.test_map = function() {
            assert(Array.eq(new Color(1,0,0).map({base:255}), [255,0,0,255]));
            assert(Array.eq(new Color(1,0,0).map({colorspace:HSB}), [0,1,1,1]));
            console.log("Color.map()")
        };
        this.test_rotate = function() {
            var rgba = new Color(1,0,0).rotate(180).rgba();
            assert(Math.round(rgba[0], 1) == 0.0);
            assert(Math.round(rgba[1], 1) == 1.0);
            assert(Math.round(rgba[2], 1) == 0.3);
            assert(Math.round(rgba[3], 1) == 1.0);
            console.log("Color.rotate()")
        }
        this.test_rgb2hex = function() {
            assert(_rgb2hex(0, 0, 0) === '#000000');
            assert(_rgb2hex(1, 1, 1) === '#FFFFFF');
            assert(_rgb2hex(0.01, 0.5, 0.99) === '#0380FC');
            console.log("_rgb2hex()")
        }
    },

    //----------------------------------------------------------------------------------------------
    
    suite: function() {
        return [
            new test_canvas.TestUtility(),
            new test_canvas.TestArray(),
            new test_canvas.TestGeometry(),
            new test_canvas.TestColor(),
        ];
    }
    
}