1 /*******************************************************************************
2  * Copyright (c) 2015, 2016 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *     Red Hat Inc. - Bug 462631
11  * Port to the D Programming Language:
12  *     alice <stigma@disroot.org>
13  *******************************************************************************/
14 module tests.RGBA;
15 
16 import std.exception : assertThrown;
17 
18 import java.lang.exceptions;
19 import java.lang.String;
20 
21 import org.eclipse.swt.graphics.RGBA;
22 
23 /*
24  * Automated Test Suite for class org.eclipse.swt.graphics.RGBA
25  */
26 
27 @("test_ConstructorIIII")
28 unittest
29 {
30     // Test RGBA(int red, int green, int blue, int alpha)
31     new RGBA(20, 100, 200, 255);
32 
33     new RGBA(0, 0, 0, 0);
34 
35     assertThrown!IllegalArgumentException(new RGBA(-1, 20, 50, 255),
36         "No exception thrown for red < 0");
37 
38     assertThrown!IllegalArgumentException(new RGBA(256, 20, 50, 255),
39         "No exception thrown for red > 255");
40 
41     assertThrown!IllegalArgumentException(new RGBA(20, -1, 50, 0),
42         "No exception thrown for green < 0");
43 
44     assertThrown!IllegalArgumentException(new RGBA(20, 256, 50, 0),
45         "No exception thrown for green > 255");
46 
47     assertThrown!IllegalArgumentException(new RGBA(20, 50, -1, 0),
48         "No exception thrown for blue < 0");
49 
50     assertThrown!IllegalArgumentException(new RGBA(20, 50, 256, 0),
51         "No exception thrown for blue > 255");
52 
53     assertThrown!IllegalArgumentException(new RGBA(20, 50, 10, -1),
54         "No exception thrown for alpha < 0");
55 
56     assertThrown!IllegalArgumentException(new RGBA(20, 50, 10, 256),
57         "No exception thrown for alpha > 255");
58 }
59 
60 @("test_ConstructorFFFF")
61 unittest
62 {
63     new RGBA(0f,0f,0f,0f);
64 
65     new RGBA(0f,1f,0f,0f);
66     new RGBA(0f,0f,1f,1f);
67     new RGBA(0f,0.6f,0.4f,0.8f);
68     new RGBA(1f,0f,1f,1f);
69     new RGBA(1f,1f,1f,1f);
70     new RGBA(1f,0f,1f,0f);
71     new RGBA(1f,1f,0f,1f);
72     new RGBA(1f,0.6f,0.4f,0.8f);
73     new RGBA(59f,0f,1f,1f);
74     new RGBA(59f,1f,1f,1f);
75     new RGBA(59f,0f,1f,1f);
76     new RGBA(59f,1f,0f,1f);
77     new RGBA(59f,0.6f,0.4f,0.8f);
78     new RGBA(60f,0f,1f,1f);
79     new RGBA(60f,1f,1f,1f);
80     new RGBA(60f,0f,1f,1f);
81     new RGBA(60f,1f,0f,1f);
82     new RGBA(60f,0.6f,0.4f,0.8f);
83     new RGBA(61f,0f,1f,1f);
84     new RGBA(61f,1f,1f,1f);
85     new RGBA(61f,0f,1f,1f);
86     new RGBA(61f,1f,0f,1f);
87     new RGBA(61f,0.6f,0.4f,0.8f);
88     new RGBA(119f,0f,1f,1f);
89     new RGBA(119f,1f,1f,1f);
90     new RGBA(119f,0f,1f,1f);
91     new RGBA(119f,1f,0f,0f);
92     new RGBA(119f,0.6f,0.4f,0.8f);
93     new RGBA(120f,0f,1f,1f);
94     new RGBA(120f,1f,1f,1f);
95     new RGBA(120f,0f,1f,1f);
96     new RGBA(120f,1f,0f,0f);
97     new RGBA(120f,0.6f,0.4f,0.8f);
98     new RGBA(121f,0f,1f,1f);
99     new RGBA(121f,1f,1f,1f);
100     new RGBA(121f,0f,1f,1f);
101     new RGBA(121f,1f,0f,0f);
102     new RGBA(121f,0.6f,0.4f,0.8f);
103     new RGBA(179f,0f,1f,1f);
104     new RGBA(179f,1f,1f,1f);
105     new RGBA(179f,0f,1f,1f);
106     new RGBA(179f,1f,0f,0f);
107     new RGBA(179f,0.6f,0.4f,0.8f);
108     new RGBA(180f,0f,1f,1f);
109     new RGBA(180f,1f,1f,1f);
110     new RGBA(180f,0f,1f,1f);
111     new RGBA(180f,1f,0f,0f);
112     new RGBA(180f,0.6f,0.4f,0.8f);
113     new RGBA(181f,0f,1f,1f);
114     new RGBA(181f,1f,1f,1f);
115     new RGBA(181f,0f,1f,1f);
116     new RGBA(181f,1f,0f,0f);
117     new RGBA(181f,0.6f,0.4f,0.8f);
118     new RGBA(239f,0f,1f,1f);
119     new RGBA(239f,1f,1f,1f);
120     new RGBA(239f,0f,1f,1f);
121     new RGBA(239f,1f,0f,0f);
122     new RGBA(239f,0.6f,0.4f,0.8f);
123     new RGBA(240f,0f,1f,1f);
124     new RGBA(240f,1f,1f,1f);
125     new RGBA(240f,0f,1f,1f);
126     new RGBA(240f,1f,0f,0f);
127     new RGBA(240f,0.6f,0.4f,0.8f);
128     new RGBA(241f,0f,1f,1f);
129     new RGBA(241f,1f,1f,1f);
130     new RGBA(241f,0f,1f,1f);
131     new RGBA(241f,1f,0f,0f);
132     new RGBA(241f,0.6f,0.4f,0.8f);
133     new RGBA(299f,0f,1f,1f);
134     new RGBA(299f,1f,1f,1f);
135     new RGBA(299f,0f,1f,1f);
136     new RGBA(299f,1f,0f,0f);
137     new RGBA(299f,0.6f,0.4f,0.8f);
138     new RGBA(300f,0f,1f,1f);
139     new RGBA(300f,1f,1f,1f);
140     new RGBA(300f,0f,1f,1f);
141     new RGBA(300f,1f,0f,0f);
142     new RGBA(300f,0.6f,0.4f,0.8f);
143     new RGBA(301f,0f,1f,1f);
144     new RGBA(301f,1f,1f,1f);
145     new RGBA(301f,0f,1f,1f);
146     new RGBA(301f,1f,0f,0f);
147     new RGBA(301f,0.6f,0.4f,0.8f);
148     new RGBA(359f,0f,1f,1f);
149     new RGBA(359f,1f,1f,1f);
150     new RGBA(359f,0f,1f,1f);
151     new RGBA(359f,1f,0f,0f);
152     new RGBA(359f,0.6f,0.4f,0.8f);
153     new RGBA(360f,0f,1f,1f);
154     new RGBA(360f,1f,1f,1f);
155     new RGBA(360f,0f,1f,1f);
156     new RGBA(360f,1f,0f,0f);
157     new RGBA(360f,0.6f,0.4f,0.8f);
158 
159     assertThrown!IllegalArgumentException(new RGBA(400f, 0.5f, 0.5f, 0.8f),
160         "No exception thrown for hue > 360");
161 
162     assertThrown!IllegalArgumentException(new RGBA(-5f, 0.5f, 0.5f, 0.8f),
163         "No exception thrown for hue < 0");
164 
165     assertThrown!IllegalArgumentException(new RGBA(200f, -0.5f, 0.5f, 0.8f),
166         "No exception thrown for saturation < 0");
167 
168     assertThrown!IllegalArgumentException(new RGBA(200f, 300f, 0.5f, 0.8f),
169         "No exception thrown for saturation > 1");
170 
171     assertThrown!IllegalArgumentException(new RGBA(200f, 0.5f, -0.5, 0.8f),
172         "No exception thrown for brightness < 0");
173 
174     assertThrown!IllegalArgumentException(new RGBA(200f, 0.5f, 400f, 0.8f),
175         "No exception thrown for brightness > 1");
176 
177     assertThrown!IllegalArgumentException(new RGBA(200f, 0.5f, 0.5f, -0.5f),
178         "No exception thrown for alpha < 0");
179 
180     assertThrown!IllegalArgumentException(new RGBA(200f, 0.5f, 0.5f, 400f),
181         "No exception thrown for alpha > 1");
182 }
183 
184 @("test_equalsLjava_lang_Object")
185 unittest
186 {
187     int r = 0, g = 127, b = 254, a = 254;
188     RGBA rgba1 = new RGBA(r, g, b, a);
189     RGBA rgba2;
190 
191     rgba2 = rgba1;
192     if (rgba1 != rgba2) {
193         assert(false, "Two references to the same RGBA instance not found equal");
194     }
195 
196     rgba2 = new RGBA(r, g, b, a);
197     if (rgba1 != rgba2) {
198         assert(false, "References to two different RGBA instances with same R G B A parameters not found equal");
199     }
200 
201     if (rgba1 == (new RGBA(r+1, g, b, a)) ||
202         rgba1 == (new RGBA(r, g+1, b, a)) ||
203         rgba1 == (new RGBA(r, g, b+1, a)) ||
204         rgba1 == (new RGBA(r, g, b, a+1)) ||
205         rgba1 == (new RGBA(r+1, g+1, b+1, a+1))) {
206             assert(false, "Comparing two RGBA instances with different combination of R G B A parameters found equal");
207     }
208 
209     float hue = 220f, sat = 0.6f, bright = 0.7f, alpha = 0.5f;
210     rgba1 = new RGBA(hue, sat, bright, alpha);
211     rgba2 = rgba1;
212     if (rgba1 != (rgba2)) {
213         assert(false, "Two references to the same RGBA instance not found equal");
214     }
215 
216     rgba2 = new RGBA(hue, sat, bright, alpha);
217     if (rgba1 != (rgba2)) {
218         assert(false, "References to two different RGBA isntances with same H S B A parameters not found equal");
219     }
220 
221     if (rgba1 == (new RGBA(hue+1, sat, bright, alpha)) ||
222         rgba1 == (new RGBA(hue, sat+0.1f, bright, alpha)) ||
223         rgba1 == (new RGBA(hue, sat, bright+0.1f, alpha)) ||
224         rgba1 == (new RGBA(hue, sat, bright, alpha+1f)) ||
225         rgba1 == (new RGBA(hue+1, sat+0.1f, bright+0.1f, alpha+1f))) {
226             assert(false, "Comparing two RGBA instances with different combination of H S B A parameters found equal");
227     }
228 }
229 
230 @("test_getHSBA")
231 unittest
232 {
233     float[] hsba = [
234                 0f,0f,0f,0f,
235                 0f,1f,1f,1f,
236                 0f,1f,0f,0f,
237                 0f,0f,1f,1f,
238                 0f,0.6f,0.4f,0.8f,
239                 1f,0f,1f,1f,
240                 1f,1f,1f,1f,
241                 1f,0f,1f,1f,
242                 1f,1f,0f,0f,
243                 1f,0.6f,0.4f,0.8f,
244                 59f,0f,1f,1f,
245                 59f,1f,1f,1f,
246                 59f,0f,1f,1f,
247                 59f,1f,0f,0f,
248                 59f,0.6f,0.4f,0.8f,
249                 60f,0f,1f,1f,
250                 60f,1f,1f,1f,
251                 60f,0f,1f,1f,
252                 60f,1f,0f,0f,
253                 60f,0.6f,0.4f,0.8f,
254                 61f,0f,1f,1f,
255                 61f,1f,1f,1f,
256                 61f,0f,1f,1f,
257                 61f,1f,0f,0f,
258                 61f,0.6f,0.4f,0.8f,
259                 119f,0f,1f,1f,
260                 119f,1f,1f,1f,
261                 119f,0f,1f,1f,
262                 119f,1f,0f,0f,
263                 119f,0.6f,0.4f,0.8f,
264                 120f,0f,1f,1f,
265                 120f,1f,1f,1f,
266                 120f,0f,1f,1f,
267                 120f,1f,0f,0f,
268                 120f,0.6f,0.4f,0.8f,
269                 121f,0f,1f,1f,
270                 121f,1f,1f,1f,
271                 121f,0f,1f,1f,
272                 121f,1f,0f,0f,
273                 121f,0.6f,0.4f,0.8f,
274                 179f,0f,1f,1f,
275                 179f,1f,1f,1f,
276                 179f,0f,1f,1f,
277                 179f,1f,0f,0f,
278                 179f,0.6f,0.4f,0.8f,
279                 180f,0f,1f,1f,
280                 180f,1f,1f,1f,
281                 180f,0f,1f,1f,
282                 180f,1f,0f,0f,
283                 180f,0.6f,0.4f,0.8f,
284                 181f,0f,1f,1f,
285                 181f,1f,1f,1f,
286                 181f,0f,1f,1f,
287                 181f,1f,0f,0f,
288                 181f,0.6f,0.4f,0.8f,
289                 239f,0f,1f,1f,
290                 239f,1f,1f,1f,
291                 239f,0f,1f,1f,
292                 239f,1f,0f,0f,
293                 239f,0.6f,0.4f,0.8f,
294                 240f,0f,1f,1f,
295                 240f,1f,1f,1f,
296                 240f,0f,1f,1f,
297                 240f,1f,0f,0f,
298                 240f,0.6f,0.4f,0.8f,
299                 241f,0f,1f,1f,
300                 241f,1f,1f,1f,
301                 241f,0f,1f,1f,
302                 241f,1f,0f,0f,
303                 241f,0.6f,0.4f,0.8f,
304                 299f,0f,1f,1f,
305                 299f,1f,1f,1f,
306                 299f,0f,1f,1f,
307                 299f,1f,0f,0f,
308                 299f,0.6f,0.4f,0.8f,
309                 300f,0f,1f,1f,
310                 300f,1f,1f,1f,
311                 300f,0f,1f,1f,
312                 300f,1f,0f,0f,
313                 300f,0.6f,0.4f,0.8f,
314                 301f,0f,1f,1f,
315                 301f,1f,1f,1f,
316                 301f,0f,1f,1f,
317                 301f,1f,0f,0f,
318                 301f,0.6f,0.4f,0.8f,
319                 359f,0f,1f,1f,
320                 359f,1f,1f,1f,
321                 359f,0f,1f,1f,
322                 359f,1f,0f,0f,
323                 359f,0.6f,0.4f,0.8f,
324                 360f,0f,1f,1f,
325                 360f,1f,1f,1f,
326                 360f,0f,1f,1f,
327                 360f,1f,0f,0f,
328                 360f,0.6f,0.4f,0.8f,
329                 220f,0.6f,0.7f,0.8f];
330     for (int i = 0; i < hsba.length; i+=4) {
331         RGBA rgba1 = new RGBA(hsba[i], hsba[i+1], hsba[i+2], hsba[i+3]);
332         float[] hsba2 = rgba1.getHSBA();
333         RGBA rgba2 = new RGBA(hsba2[0], hsba2[1], hsba2[2], hsba2[3]);
334         if (rgba1 != rgba2) {
335             assert(false, "Two references to the same RGBA using getHSBA() function not found equal");
336         }
337     }
338 }
339 
340 @("test_hashCode")
341 unittest
342 {
343     int r = 255, g = 100, b = 0, a = 0, different = 150;
344     RGBA rgba1 = new RGBA(r, g, b, a);
345     RGBA rgba2 = new RGBA(r, g, b, a);
346 
347     size_t hash1 = rgba1.toHash();
348     size_t hash2 = rgba2.toHash();
349 
350     if (hash1 != hash2) {
351         assert(false, "Two RGBA instances with same R G B A parameters returned different hash codes");
352     }
353 
354     if (rgba1.toHash() == new RGBA(g, b, r, a).toHash() ||
355         rgba1.toHash() == new RGBA(b, r, g, a).toHash()) {
356             assert(false, "Two RGBA instances with different R G B A parameters returned the same hash code");
357     }
358 
359     if (rgba1.toHash() == new RGBA(different, g, b, a).toHash()) {
360         assert(false, "Two RGBA instances with different RED parameters returned the same hash code");
361     }
362 
363     if (rgba1.toHash() == new RGBA(r, different, b, a).toHash()) {
364         assert(false, "Two RGBA instances with different GREEN parameters returned the same hash code");
365     }
366 
367     if (rgba1.toHash() == new RGBA(r, g, different, a).toHash()) {
368         assert(false, "Two RGBA instances with different BLUE parameters returned the same hash code");
369     }
370 
371     if (rgba1.toHash() == new RGBA(r, g, b, different).toHash()) {
372         assert(false, "Two RGBA instances with different ALPHA parameters returned the same hash code");
373     }
374 }
375 
376 @("test_toString")
377 unittest
378 {
379     RGBA rgba = new RGBA(0, 100, 200, 255);
380 
381     String s = rgba.toString();
382 
383     if (s is null || s.length() == 0) {
384         assert(false, "RGBA.toString returns a null or empty string");
385     }
386 }