1 /*******************************************************************************
2  * Copyright (c) 2000, 2015 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.RGB;
15 
16 import std.exception : assertThrown;
17 
18 import java.lang.exceptions;
19 import java.lang.String;
20 
21 import org.eclipse.swt.graphics.RGB;
22 
23 /*
24  * Automated Test Suite for class org.eclipse.swt.graphics.RGB
25  */
26 
27 @("test_ConstructorIII")
28 unittest
29 {
30     // Test RGB(int red, int green, int blue)
31     new RGB(20, 100, 200);
32 
33     new RGB(0, 0, 0);
34 
35     assertThrown!IllegalArgumentException(new RGB(-1, 20, 50),
36         "No exception thrown for red < 0");
37 
38     assertThrown!IllegalArgumentException(new RGB(256, 20, 50),
39         "No exception thrown for red > 255");
40 
41     assertThrown!IllegalArgumentException(new RGB(20, -1, 50),
42         "No exception thrown for green < 0");
43 
44     assertThrown!IllegalArgumentException(new RGB(20, 256, 50),
45         "No exception thrown for green > 255");
46 
47     assertThrown!IllegalArgumentException(new RGB(20, 50, -1),
48         "No exception thrown for blue < 0");
49 
50     assertThrown!IllegalArgumentException(new RGB(20, 50, 256),
51         "No exception thrown for blue > 255");
52 }
53 
54 @("test_ConstructorFFF")
55 unittest
56 {
57     new RGB(0f,0f,0f);
58 
59     new RGB(0f,1f,0f);
60     new RGB(0f,0f,1f);
61     new RGB(0f,0.6f,0.4f);
62     new RGB(1f,0f,1f);
63     new RGB(1f,1f,1f);
64     new RGB(1f,0f,1f);
65     new RGB(1f,1f,0f);
66     new RGB(1f,0.6f,0.4f);
67     new RGB(59f,0f,1f);
68     new RGB(59f,1f,1f);
69     new RGB(59f,0f,1f);
70     new RGB(59f,1f,0f);
71     new RGB(59f,0.6f,0.4f);
72     new RGB(60f,0f,1f);
73     new RGB(60f,1f,1f);
74     new RGB(60f,0f,1f);
75     new RGB(60f,1f,0f);
76     new RGB(60f,0.6f,0.4f);
77     new RGB(61f,0f,1f);
78     new RGB(61f,1f,1f);
79     new RGB(61f,0f,1f);
80     new RGB(61f,1f,0f);
81     new RGB(61f,0.6f,0.4f);
82     new RGB(119f,0f,1f);
83     new RGB(119f,1f,1f);
84     new RGB(119f,0f,1f);
85     new RGB(119f,1f,0f);
86     new RGB(119f,0.6f,0.4f);
87     new RGB(120f,0f,1f);
88     new RGB(120f,1f,1f);
89     new RGB(120f,0f,1f);
90     new RGB(120f,1f,0f);
91     new RGB(120f,0.6f,0.4f);
92     new RGB(121f,0f,1f);
93     new RGB(121f,1f,1f);
94     new RGB(121f,0f,1f);
95     new RGB(121f,1f,0f);
96     new RGB(121f,0.6f,0.4f);
97     new RGB(179f,0f,1f);
98     new RGB(179f,1f,1f);
99     new RGB(179f,0f,1f);
100     new RGB(179f,1f,0f);
101     new RGB(179f,0.6f,0.4f);
102     new RGB(180f,0f,1f);
103     new RGB(180f,1f,1f);
104     new RGB(180f,0f,1f);
105     new RGB(180f,1f,0f);
106     new RGB(180f,0.6f,0.4f);
107     new RGB(181f,0f,1f);
108     new RGB(181f,1f,1f);
109     new RGB(181f,0f,1f);
110     new RGB(181f,1f,0f);
111     new RGB(181f,0.6f,0.4f);
112     new RGB(239f,0f,1f);
113     new RGB(239f,1f,1f);
114     new RGB(239f,0f,1f);
115     new RGB(239f,1f,0f);
116     new RGB(239f,0.6f,0.4f);
117     new RGB(240f,0f,1f);
118     new RGB(240f,1f,1f);
119     new RGB(240f,0f,1f);
120     new RGB(240f,1f,0f);
121     new RGB(240f,0.6f,0.4f);
122     new RGB(241f,0f,1f);
123     new RGB(241f,1f,1f);
124     new RGB(241f,0f,1f);
125     new RGB(241f,1f,0f);
126     new RGB(241f,0.6f,0.4f);
127     new RGB(299f,0f,1f);
128     new RGB(299f,1f,1f);
129     new RGB(299f,0f,1f);
130     new RGB(299f,1f,0f);
131     new RGB(299f,0.6f,0.4f);
132     new RGB(300f,0f,1f);
133     new RGB(300f,1f,1f);
134     new RGB(300f,0f,1f);
135     new RGB(300f,1f,0f);
136     new RGB(300f,0.6f,0.4f);
137     new RGB(301f,0f,1f);
138     new RGB(301f,1f,1f);
139     new RGB(301f,0f,1f);
140     new RGB(301f,1f,0f);
141     new RGB(301f,0.6f,0.4f);
142     new RGB(359f,0f,1f);
143     new RGB(359f,1f,1f);
144     new RGB(359f,0f,1f);
145     new RGB(359f,1f,0f);
146     new RGB(359f,0.6f,0.4f);
147     new RGB(360f,0f,1f);
148     new RGB(360f,1f,1f);
149     new RGB(360f,0f,1f);
150     new RGB(360f,1f,0f);
151     new RGB(360f,0.6f,0.4f);
152 
153     assertThrown!IllegalArgumentException(new RGB(400f, 0.5f, 0.5f),
154         "No exception thrown for hue > 360");
155 
156     assertThrown!IllegalArgumentException(new RGB(-5, 0.5f, 0.5f),
157         "No exception thrown for hue < 0");
158 
159     assertThrown!IllegalArgumentException(new RGB(200f, -0.5f, 0.5f),
160         "No exception thrown for saturation < 0");
161 
162     assertThrown!IllegalArgumentException(new RGB(200f, 300f, 0.5f),
163         "No exception thrown for saturation > 1");
164 
165     assertThrown!IllegalArgumentException(new RGB(200f, 0.5f, -0.5f),
166         "No exception thrown for brightness < 0");
167 
168     assertThrown!IllegalArgumentException(new RGB(200, 0.5f, 400f),
169         "No exception thrown for brightness > 1");
170 }
171 
172 @("test_equalsLjava_lang_Object")
173 unittest
174 {
175     int r = 0, g = 127, b = 254;
176     RGB rgb1 = new RGB(r, g, b);
177     RGB rgb2;
178 
179     rgb2 = rgb1;
180     if (rgb1 != rgb2) {
181         assert(false, "Two references to the same RGB instance not found equal");
182     }
183 
184     rgb2 = new RGB(r, g, b);
185     if (rgb1 != rgb2) {
186         assert(false, "References to two different RGB instances with same R G B parameters not found equal");
187     }
188 
189     if (rgb1 == (new RGB(r+1, g, b)) ||
190         rgb1 == (new RGB(r, g+1, b)) ||
191         rgb1 == (new RGB(r, g, b+1)) ||
192         rgb1 == (new RGB(r+1, g+1, b+1))) {
193             assert(false, "Comparing two RGB instances with different combination of R G B parameters found equal");
194     }
195 
196     float hue = 220f, sat = 0.6f, bright = 0.7f;
197     rgb1 = new RGB(hue, sat, bright);
198     rgb2 = rgb1;
199     if (rgb1 != rgb2) {
200         assert(false, "Two references to the same RGB isntance not found equal");
201     }
202 
203     rgb2 = new RGB(hue, sat, bright);
204     if (rgb1 != rgb2) {
205         assert(false, "References to two different RGB instances with same H S B parameters not found equal");
206     }
207 
208     if (rgb1 == (new RGB(hue+1, sat, bright)) ||
209         rgb1 == (new RGB(hue, sat+0.1f, bright)) ||
210         rgb1 == (new RGB(hue, sat, bright+0.1f)) ||
211         rgb1 == (new RGB(hue+1, sat+0.1f, bright+0.1f))) {
212             assert(false, "Comparing two RGB instances with different combination of H S B parameters found equal");
213     }
214 }
215 
216 @("test_getHSB")
217 unittest
218 {
219     float[] hsb = [
220                 0f,0f,0f,
221                 0f,1f,1f,
222                 0f,1f,0f,
223                 0f,0f,1f,
224                 0f,0.6f,0.4f,
225                 1f,0f,1f,
226                 1f,1f,1f,
227                 1f,0f,1f,
228                 1f,1f,0f,
229                 1f,0.6f,0.4f,
230                 59f,0f,1f,
231                 59f,1f,1f,
232                 59f,0f,1f,
233                 59f,1f,0f,
234                 59f,0.6f,0.4f,
235                 60f,0f,1f,
236                 60f,1f,1f,
237                 60f,0f,1f,
238                 60f,1f,0f,
239                 60f,0.6f,0.4f,
240                 61f,0f,1f,
241                 61f,1f,1f,
242                 61f,0f,1f,
243                 61f,1f,0f,
244                 61f,0.6f,0.4f,
245                 119f,0f,1f,
246                 119f,1f,1f,
247                 119f,0f,1f,
248                 119f,1f,0f,
249                 119f,0.6f,0.4f,
250                 120f,0f,1f,
251                 120f,1f,1f,
252                 120f,0f,1f,
253                 120f,1f,0f,
254                 120f,0.6f,0.4f,
255                 121f,0f,1f,
256                 121f,1f,1f,
257                 121f,0f,1f,
258                 121f,1f,0f,
259                 121f,0.6f,0.4f,
260                 179f,0f,1f,
261                 179f,1f,1f,
262                 179f,0f,1f,
263                 179f,1f,0f,
264                 179f,0.6f,0.4f,
265                 180f,0f,1f,
266                 180f,1f,1f,
267                 180f,0f,1f,
268                 180f,1f,0f,
269                 180f,0.6f,0.4f,
270                 181f,0f,1f,
271                 181f,1f,1f,
272                 181f,0f,1f,
273                 181f,1f,0f,
274                 181f,0.6f,0.4f,
275                 239f,0f,1f,
276                 239f,1f,1f,
277                 239f,0f,1f,
278                 239f,1f,0f,
279                 239f,0.6f,0.4f,
280                 240f,0f,1f,
281                 240f,1f,1f,
282                 240f,0f,1f,
283                 240f,1f,0f,
284                 240f,0.6f,0.4f,
285                 241f,0f,1f,
286                 241f,1f,1f,
287                 241f,0f,1f,
288                 241f,1f,0f,
289                 241f,0.6f,0.4f,
290                 299f,0f,1f,
291                 299f,1f,1f,
292                 299f,0f,1f,
293                 299f,1f,0f,
294                 299f,0.6f,0.4f,
295                 300f,0f,1f,
296                 300f,1f,1f,
297                 300f,0f,1f,
298                 300f,1f,0f,
299                 300f,0.6f,0.4f,
300                 301f,0f,1f,
301                 301f,1f,1f,
302                 301f,0f,1f,
303                 301f,1f,0f,
304                 301f,0.6f,0.4f,
305                 359f,0f,1f,
306                 359f,1f,1f,
307                 359f,0f,1f,
308                 359f,1f,0f,
309                 359f,0.6f,0.4f,
310                 360f,0f,1f,
311                 360f,1f,1f,
312                 360f,0f,1f,
313                 360f,1f,0f,
314                 360f,0.6f,0.4f,
315                 220f,0.6f,0.7f];
316     for (int i = 0; i < hsb.length; i+=3) {
317         RGB rgb1 = new RGB(hsb[i], hsb[i+1], hsb[i+2]);
318         float[] hsb2 = rgb1.getHSB();
319         RGB rgb2 = new RGB(hsb2[0], hsb2[1], hsb2[2]);
320         if (rgb1 != rgb2) {
321             assert(false, "Two references to the same RGB using getHSB() function not found equal");
322         }
323     }
324 }
325 
326 @("test_hashCode")
327 unittest
328 {
329     int r = 255, g = 100, b = 0;
330     RGB rgb1 = new RGB(r, g, b);
331     RGB rgb2 = new RGB(r, g, b);
332 
333     hash_t hash1 = rgb1.toHash();
334     hash_t hash2 = rgb2.toHash();
335 
336     if (hash1 != hash2) {
337         assert(false, "Two RGB instances with same R G B parameters returned different hash codes");
338     }
339 
340     if (rgb1.toHash() == new RGB(g, b, r).toHash() ||
341         rgb1.toHash() == new RGB(b, r, g).toHash()) {
342             assert(false, "Two RGB instances with different R G B parameters returned the same hash code");
343     }
344 }
345 
346 @("test_toString")
347 unittest
348 {
349     RGB rgb = new RGB(0, 100, 200);
350 
351     String s = rgb.toString();
352 
353     if (s is null || s.length() == 0) {
354         assert(false, "RGB.toString returns null or empty String");
355     }
356 }