1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.lang3;
18
19 import static org.apache.commons.lang3.ArraySorter.sort;
20 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertFalse;
23 import static org.junit.jupiter.api.Assertions.assertNotNull;
24 import static org.junit.jupiter.api.Assertions.assertNull;
25 import static org.junit.jupiter.api.Assertions.assertSame;
26 import static org.junit.jupiter.api.Assertions.assertThrows;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28
29 import java.lang.reflect.Constructor;
30 import java.lang.reflect.Modifier;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.Collections;
34 import java.util.List;
35
36 import org.junit.jupiter.api.Test;
37
38
39
40
41 public class BooleanUtilsTest extends AbstractLangTest {
42
43 @Test
44 public void test_booleanValues() {
45 final Boolean[] expected = {Boolean.FALSE, Boolean.TRUE};
46 assertArrayEquals(sort(expected), BooleanUtils.booleanValues());
47 }
48
49 @Test
50 public void test_forEach() {
51 final List<Boolean> list = new ArrayList<>();
52 BooleanUtils.forEach(list::add);
53 assertEquals(Arrays.asList(Boolean.FALSE, Boolean.TRUE), list);
54 }
55
56 @Test
57 public void test_isFalse_Boolean() {
58 assertFalse(BooleanUtils.isFalse(Boolean.TRUE));
59 assertTrue(BooleanUtils.isFalse(Boolean.FALSE));
60 assertFalse(BooleanUtils.isFalse(null));
61 }
62
63 @Test
64 public void test_isNotFalse_Boolean() {
65 assertTrue(BooleanUtils.isNotFalse(Boolean.TRUE));
66 assertFalse(BooleanUtils.isNotFalse(Boolean.FALSE));
67 assertTrue(BooleanUtils.isNotFalse(null));
68 }
69
70 @Test
71 public void test_isNotTrue_Boolean() {
72 assertFalse(BooleanUtils.isNotTrue(Boolean.TRUE));
73 assertTrue(BooleanUtils.isNotTrue(Boolean.FALSE));
74 assertTrue(BooleanUtils.isNotTrue(null));
75 }
76
77 @Test
78 public void test_isTrue_Boolean() {
79 assertTrue(BooleanUtils.isTrue(Boolean.TRUE));
80 assertFalse(BooleanUtils.isTrue(Boolean.FALSE));
81 assertFalse(BooleanUtils.isTrue(null));
82 }
83
84 @Test
85 public void test_negate_Boolean() {
86 assertSame(null, BooleanUtils.negate(null));
87 assertSame(Boolean.TRUE, BooleanUtils.negate(Boolean.FALSE));
88 assertSame(Boolean.FALSE, BooleanUtils.negate(Boolean.TRUE));
89 }
90
91 @Test
92 public void test_primitiveValues() {
93 assertArrayEquals(new boolean[] {false, true}, BooleanUtils.primitiveValues());
94 }
95
96 @Test
97 public void test_toBoolean_Boolean() {
98 assertTrue(BooleanUtils.toBoolean(Boolean.TRUE));
99 assertFalse(BooleanUtils.toBoolean(Boolean.FALSE));
100 assertFalse(BooleanUtils.toBoolean((Boolean) null));
101 }
102
103 @Test
104 public void test_toBoolean_int() {
105 assertTrue(BooleanUtils.toBoolean(1));
106 assertTrue(BooleanUtils.toBoolean(-1));
107 assertFalse(BooleanUtils.toBoolean(0));
108 }
109
110 @Test
111 public void test_toBoolean_int_int_int() {
112 assertTrue(BooleanUtils.toBoolean(6, 6, 7));
113 assertFalse(BooleanUtils.toBoolean(7, 6, 7));
114 }
115
116 @Test
117 public void test_toBoolean_int_int_int_noMatch() {
118 assertThrows(IllegalArgumentException.class, () -> BooleanUtils.toBoolean(8, 6, 7));
119 }
120
121 @Test
122 public void test_toBoolean_Integer_Integer_Integer() {
123 final Integer six = Integer.valueOf(6);
124 final Integer seven = Integer.valueOf(7);
125
126 assertTrue(BooleanUtils.toBoolean(null, null, seven));
127 assertFalse(BooleanUtils.toBoolean(null, six, null));
128
129 assertTrue(BooleanUtils.toBoolean(Integer.valueOf(6), six, seven));
130 assertFalse(BooleanUtils.toBoolean(Integer.valueOf(7), six, seven));
131 }
132
133 @Test
134 public void test_toBoolean_Integer_Integer_Integer_noMatch() {
135 assertThrows(IllegalArgumentException.class,
136 () -> BooleanUtils.toBoolean(Integer.valueOf(8), Integer.valueOf(6), Integer.valueOf(7)));
137 }
138
139 @Test
140 public void test_toBoolean_Integer_Integer_Integer_nullValue() {
141 assertThrows(IllegalArgumentException.class,
142 () -> BooleanUtils.toBoolean(null, Integer.valueOf(6), Integer.valueOf(7)));
143 }
144
145 @Test
146 public void test_toBoolean_String() {
147 assertFalse(BooleanUtils.toBoolean((String) null));
148 assertFalse(BooleanUtils.toBoolean(""));
149 assertFalse(BooleanUtils.toBoolean("off"));
150 assertFalse(BooleanUtils.toBoolean("oof"));
151 assertFalse(BooleanUtils.toBoolean("yep"));
152 assertFalse(BooleanUtils.toBoolean("trux"));
153 assertFalse(BooleanUtils.toBoolean("false"));
154 assertFalse(BooleanUtils.toBoolean("a"));
155 assertTrue(BooleanUtils.toBoolean("true"));
156 assertTrue(BooleanUtils.toBoolean(new StringBuilder("tr").append("ue").toString()));
157 assertTrue(BooleanUtils.toBoolean("truE"));
158 assertTrue(BooleanUtils.toBoolean("trUe"));
159 assertTrue(BooleanUtils.toBoolean("trUE"));
160 assertTrue(BooleanUtils.toBoolean("tRue"));
161 assertTrue(BooleanUtils.toBoolean("tRuE"));
162 assertTrue(BooleanUtils.toBoolean("tRUe"));
163 assertTrue(BooleanUtils.toBoolean("tRUE"));
164 assertTrue(BooleanUtils.toBoolean("TRUE"));
165 assertTrue(BooleanUtils.toBoolean("TRUe"));
166 assertTrue(BooleanUtils.toBoolean("TRuE"));
167 assertTrue(BooleanUtils.toBoolean("TRue"));
168 assertTrue(BooleanUtils.toBoolean("TrUE"));
169 assertTrue(BooleanUtils.toBoolean("TrUe"));
170 assertTrue(BooleanUtils.toBoolean("TruE"));
171 assertTrue(BooleanUtils.toBoolean("True"));
172 assertTrue(BooleanUtils.toBoolean("on"));
173 assertTrue(BooleanUtils.toBoolean("oN"));
174 assertTrue(BooleanUtils.toBoolean("On"));
175 assertTrue(BooleanUtils.toBoolean("ON"));
176 assertTrue(BooleanUtils.toBoolean("yes"));
177 assertTrue(BooleanUtils.toBoolean("yeS"));
178 assertTrue(BooleanUtils.toBoolean("yEs"));
179 assertTrue(BooleanUtils.toBoolean("yES"));
180 assertTrue(BooleanUtils.toBoolean("Yes"));
181 assertTrue(BooleanUtils.toBoolean("YeS"));
182 assertTrue(BooleanUtils.toBoolean("YEs"));
183 assertTrue(BooleanUtils.toBoolean("YES"));
184 assertTrue(BooleanUtils.toBoolean("1"));
185 assertFalse(BooleanUtils.toBoolean("yes?"));
186 assertFalse(BooleanUtils.toBoolean("0"));
187 assertFalse(BooleanUtils.toBoolean("tru"));
188
189 assertFalse(BooleanUtils.toBoolean("no"));
190 assertFalse(BooleanUtils.toBoolean("off"));
191 assertFalse(BooleanUtils.toBoolean("yoo"));
192 }
193
194 @Test
195 public void test_toBoolean_String_String_String() {
196 assertTrue(BooleanUtils.toBoolean(null, null, "N"));
197 assertFalse(BooleanUtils.toBoolean(null, "Y", null));
198 assertTrue(BooleanUtils.toBoolean("Y", "Y", "N"));
199 assertTrue(BooleanUtils.toBoolean("Y", "Y", "N"));
200 assertFalse(BooleanUtils.toBoolean("N", "Y", "N"));
201 assertFalse(BooleanUtils.toBoolean("N", "Y", "N"));
202 assertTrue(BooleanUtils.toBoolean((String) null, null, null));
203 assertTrue(BooleanUtils.toBoolean("Y", "Y", "Y"));
204 assertTrue(BooleanUtils.toBoolean("Y", "Y", "Y"));
205 }
206
207 @Test
208 public void test_toBoolean_String_String_String_noMatch() {
209 assertThrows(IllegalArgumentException.class, () -> BooleanUtils.toBoolean("X", "Y", "N"));
210 }
211
212 @Test
213 public void test_toBoolean_String_String_String_nullValue() {
214 assertThrows(IllegalArgumentException.class, () -> BooleanUtils.toBoolean(null, "Y", "N"));
215 }
216
217 @Test
218 public void test_toBooleanDefaultIfNull_Boolean_boolean() {
219 assertTrue(BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, true));
220 assertTrue(BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false));
221 assertFalse(BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true));
222 assertFalse(BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, false));
223 assertTrue(BooleanUtils.toBooleanDefaultIfNull(null, true));
224 assertFalse(BooleanUtils.toBooleanDefaultIfNull(null, false));
225 }
226
227 @Test
228 public void test_toBooleanObject_int() {
229 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject(1));
230 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject(-1));
231 assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject(0));
232 }
233
234 @Test
235 public void test_toBooleanObject_int_int_int() {
236 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject(6, 6, 7, 8));
237 assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject(7, 6, 7, 8));
238 assertNull(BooleanUtils.toBooleanObject(8, 6, 7, 8));
239 }
240
241 @Test
242 public void test_toBooleanObject_int_int_int_noMatch() {
243 assertThrows(IllegalArgumentException.class, () -> BooleanUtils.toBooleanObject(9, 6, 7, 8));
244 }
245
246 @Test
247 public void test_toBooleanObject_Integer() {
248 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject(Integer.valueOf(1)));
249 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject(Integer.valueOf(-1)));
250 assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject(Integer.valueOf(0)));
251 assertNull(BooleanUtils.toBooleanObject((Integer) null));
252 }
253
254 @Test
255 public void test_toBooleanObject_Integer_Integer_Integer_Integer() {
256 final Integer six = Integer.valueOf(6);
257 final Integer seven = Integer.valueOf(7);
258 final Integer eight = Integer.valueOf(8);
259
260 assertSame(Boolean.TRUE, BooleanUtils.toBooleanObject(null, null, seven, eight));
261 assertSame(Boolean.FALSE, BooleanUtils.toBooleanObject(null, six, null, eight));
262 assertSame(null, BooleanUtils.toBooleanObject(null, six, seven, null));
263
264 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject(Integer.valueOf(6), six, seven, eight));
265 assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject(Integer.valueOf(7), six, seven, eight));
266 assertNull(BooleanUtils.toBooleanObject(Integer.valueOf(8), six, seven, eight));
267 }
268
269 @Test
270 public void test_toBooleanObject_Integer_Integer_Integer_Integer_noMatch() {
271 assertThrows(IllegalArgumentException.class,
272 () -> BooleanUtils.toBooleanObject(Integer.valueOf(9), Integer.valueOf(6), Integer.valueOf(7), Integer.valueOf(8)));
273 }
274
275 @Test
276 public void test_toBooleanObject_Integer_Integer_Integer_Integer_nullValue() {
277 assertThrows(IllegalArgumentException.class,
278 () -> BooleanUtils.toBooleanObject(null, Integer.valueOf(6), Integer.valueOf(7), Integer.valueOf(8)));
279 }
280
281 @Test
282 public void test_toBooleanObject_String() {
283 assertNull(BooleanUtils.toBooleanObject((String) null));
284 assertNull(BooleanUtils.toBooleanObject(""));
285 assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("false"));
286 assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("no"));
287 assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("off"));
288 assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("FALSE"));
289 assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("NO"));
290 assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("OFF"));
291 assertNull(BooleanUtils.toBooleanObject("oof"));
292 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("true"));
293 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("yes"));
294 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("on"));
295 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("TRUE"));
296 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("ON"));
297 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("YES"));
298 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("TruE"));
299 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("TruE"));
300
301 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("y"));
302 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("Y"));
303 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("t"));
304 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("T"));
305 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("1"));
306 assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("f"));
307 assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("F"));
308 assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("n"));
309 assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("N"));
310 assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("0"));
311 assertNull(BooleanUtils.toBooleanObject("z"));
312
313 assertNull(BooleanUtils.toBooleanObject("ab"));
314 assertNull(BooleanUtils.toBooleanObject("yoo"));
315 assertNull(BooleanUtils.toBooleanObject("true "));
316 assertNull(BooleanUtils.toBooleanObject("ono"));
317 }
318
319 @Test
320 public void test_toBooleanObject_String_String_String_String() {
321 assertSame(Boolean.TRUE, BooleanUtils.toBooleanObject(null, null, "N", "U"));
322 assertSame(Boolean.FALSE, BooleanUtils.toBooleanObject(null, "Y", null, "U"));
323 assertSame(null, BooleanUtils.toBooleanObject(null, "Y", "N", null));
324
325 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("Y", "Y", "N", "U"));
326 assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("N", "Y", "N", "U"));
327 assertNull(BooleanUtils.toBooleanObject("U", "Y", "N", "U"));
328 }
329
330 @Test
331 public void test_toBooleanObject_String_String_String_String_noMatch() {
332 assertThrows(IllegalArgumentException.class, () -> BooleanUtils.toBooleanObject("X", "Y", "N", "U"));
333 }
334
335 @Test
336 public void test_toBooleanObject_String_String_String_String_nullValue() {
337 assertThrows(IllegalArgumentException.class, () -> BooleanUtils.toBooleanObject(null, "Y", "N", "U"));
338 }
339
340 @Test
341 public void test_toInteger_boolean() {
342 assertEquals(1, BooleanUtils.toInteger(true));
343 assertEquals(0, BooleanUtils.toInteger(false));
344 }
345
346 @Test
347 public void test_toInteger_boolean_int_int() {
348 assertEquals(6, BooleanUtils.toInteger(true, 6, 7));
349 assertEquals(7, BooleanUtils.toInteger(false, 6, 7));
350 }
351
352 @Test
353 public void test_toInteger_Boolean_int_int_int() {
354 assertEquals(6, BooleanUtils.toInteger(Boolean.TRUE, 6, 7, 8));
355 assertEquals(7, BooleanUtils.toInteger(Boolean.FALSE, 6, 7, 8));
356 assertEquals(8, BooleanUtils.toInteger(null, 6, 7, 8));
357 }
358
359 @Test
360 public void test_toIntegerObject_boolean() {
361 assertEquals(Integer.valueOf(1), BooleanUtils.toIntegerObject(true));
362 assertEquals(Integer.valueOf(0), BooleanUtils.toIntegerObject(false));
363 }
364
365 @Test
366 public void test_toIntegerObject_Boolean() {
367 assertEquals(Integer.valueOf(1), BooleanUtils.toIntegerObject(Boolean.TRUE));
368 assertEquals(Integer.valueOf(0), BooleanUtils.toIntegerObject(Boolean.FALSE));
369 assertNull(BooleanUtils.toIntegerObject(null));
370 }
371
372 @Test
373 public void test_toIntegerObject_boolean_Integer_Integer() {
374 final Integer six = Integer.valueOf(6);
375 final Integer seven = Integer.valueOf(7);
376 assertEquals(six, BooleanUtils.toIntegerObject(true, six, seven));
377 assertEquals(seven, BooleanUtils.toIntegerObject(false, six, seven));
378 }
379
380 @Test
381 public void test_toIntegerObject_Boolean_Integer_Integer_Integer() {
382 final Integer six = Integer.valueOf(6);
383 final Integer seven = Integer.valueOf(7);
384 final Integer eight = Integer.valueOf(8);
385 assertEquals(six, BooleanUtils.toIntegerObject(Boolean.TRUE, six, seven, eight));
386 assertEquals(seven, BooleanUtils.toIntegerObject(Boolean.FALSE, six, seven, eight));
387 assertEquals(eight, BooleanUtils.toIntegerObject(null, six, seven, eight));
388 assertNull(BooleanUtils.toIntegerObject(null, six, seven, null));
389 }
390
391 @Test
392 public void test_toString_boolean_String_String_String() {
393 assertEquals("Y", BooleanUtils.toString(true, "Y", "N"));
394 assertEquals("N", BooleanUtils.toString(false, "Y", "N"));
395 }
396
397 @Test
398 public void test_toString_Boolean_String_String_String() {
399 assertEquals("U", BooleanUtils.toString(null, "Y", "N", "U"));
400 assertEquals("Y", BooleanUtils.toString(Boolean.TRUE, "Y", "N", "U"));
401 assertEquals("N", BooleanUtils.toString(Boolean.FALSE, "Y", "N", "U"));
402 }
403
404 @Test
405 public void test_toStringOnOff_boolean() {
406 assertEquals("on", BooleanUtils.toStringOnOff(true));
407 assertEquals("off", BooleanUtils.toStringOnOff(false));
408 }
409
410 @Test
411 public void test_toStringOnOff_Boolean() {
412 assertNull(BooleanUtils.toStringOnOff(null));
413 assertEquals("on", BooleanUtils.toStringOnOff(Boolean.TRUE));
414 assertEquals("off", BooleanUtils.toStringOnOff(Boolean.FALSE));
415 }
416
417 @Test
418 public void test_toStringTrueFalse_boolean() {
419 assertEquals("true", BooleanUtils.toStringTrueFalse(true));
420 assertEquals("false", BooleanUtils.toStringTrueFalse(false));
421 }
422
423 @Test
424 public void test_toStringTrueFalse_Boolean() {
425 assertNull(BooleanUtils.toStringTrueFalse(null));
426 assertEquals("true", BooleanUtils.toStringTrueFalse(Boolean.TRUE));
427 assertEquals("false", BooleanUtils.toStringTrueFalse(Boolean.FALSE));
428 }
429
430 @Test
431 public void test_toStringYesNo_boolean() {
432 assertEquals("yes", BooleanUtils.toStringYesNo(true));
433 assertEquals("no", BooleanUtils.toStringYesNo(false));
434 }
435
436 @Test
437 public void test_toStringYesNo_Boolean() {
438 assertNull(BooleanUtils.toStringYesNo(null));
439 assertEquals("yes", BooleanUtils.toStringYesNo(Boolean.TRUE));
440 assertEquals("no", BooleanUtils.toStringYesNo(Boolean.FALSE));
441 }
442
443 @Test
444 public void test_values() {
445 final List<Boolean> expected = Arrays.asList(Boolean.FALSE, Boolean.TRUE);
446 Collections.sort(expected);
447 assertEquals(expected, BooleanUtils.values());
448 }
449
450 @Test
451 public void testAnd_object_emptyInput() {
452 assertThrows(IllegalArgumentException.class, () -> BooleanUtils.and(new Boolean[] {}));
453 }
454
455 @Test
456 public void testAnd_object_nullElementInput() {
457 assertEquals(Boolean.FALSE, BooleanUtils.and(new Boolean[] {null}));
458 }
459
460 @Test
461 public void testAnd_object_nullInput() {
462 assertThrows(NullPointerException.class, () -> BooleanUtils.and((Boolean[]) null));
463 }
464
465 @Test
466 public void testAnd_object_validInput_2items() {
467 assertTrue(
468 BooleanUtils
469 .and(new Boolean[] { Boolean.TRUE, Boolean.TRUE })
470 .booleanValue(),
471 "False result for (true, true)");
472
473 assertFalse(
474 BooleanUtils
475 .and(new Boolean[] { Boolean.FALSE, Boolean.FALSE })
476 .booleanValue(),
477 "True result for (false, false)");
478
479 assertFalse(
480 BooleanUtils
481 .and(new Boolean[] { Boolean.TRUE, Boolean.FALSE })
482 .booleanValue(),
483 "True result for (true, false)");
484
485 assertFalse(
486 BooleanUtils
487 .and(new Boolean[] { Boolean.FALSE, Boolean.TRUE })
488 .booleanValue(),
489 "True result for (false, true)");
490 }
491
492 @Test
493 public void testAnd_object_validInput_3items() {
494 assertFalse(
495 BooleanUtils
496 .and(
497 new Boolean[] {
498 Boolean.FALSE,
499 Boolean.FALSE,
500 Boolean.TRUE })
501 .booleanValue(),
502 "True result for (false, false, true)");
503
504 assertFalse(
505 BooleanUtils
506 .and(
507 new Boolean[] {
508 Boolean.FALSE,
509 Boolean.TRUE,
510 Boolean.FALSE })
511 .booleanValue(),
512 "True result for (false, true, false)");
513
514 assertFalse(
515 BooleanUtils
516 .and(
517 new Boolean[] {
518 Boolean.TRUE,
519 Boolean.FALSE,
520 Boolean.FALSE })
521 .booleanValue(),
522 "True result for (true, false, false)");
523
524 assertTrue(
525 BooleanUtils
526 .and(new Boolean[] { Boolean.TRUE, Boolean.TRUE, Boolean.TRUE })
527 .booleanValue(),
528 "False result for (true, true, true)");
529
530 assertFalse(
531 BooleanUtils.and(
532 new Boolean[] {
533 Boolean.FALSE,
534 Boolean.FALSE,
535 Boolean.FALSE })
536 .booleanValue(),
537 "True result for (false, false)");
538
539 assertFalse(
540 BooleanUtils.and(
541 new Boolean[] {
542 Boolean.TRUE,
543 Boolean.TRUE,
544 Boolean.FALSE })
545 .booleanValue(),
546 "True result for (true, true, false)");
547
548 assertFalse(
549 BooleanUtils.and(
550 new Boolean[] {
551 Boolean.TRUE,
552 Boolean.FALSE,
553 Boolean.TRUE })
554 .booleanValue(),
555 "True result for (true, false, true)");
556
557 assertFalse(
558 BooleanUtils.and(
559 new Boolean[] {
560 Boolean.FALSE,
561 Boolean.TRUE,
562 Boolean.TRUE })
563 .booleanValue(),
564 "True result for (false, true, true)");
565 }
566
567 @Test
568 public void testAnd_primitive_emptyInput() {
569 assertThrows(IllegalArgumentException.class, () -> BooleanUtils.and(new boolean[] {}));
570 }
571
572 @Test
573 public void testAnd_primitive_nullInput() {
574 assertThrows(NullPointerException.class, () -> BooleanUtils.and((boolean[]) null));
575 }
576
577 @Test
578 public void testAnd_primitive_validInput_2items() {
579 assertTrue(
580 BooleanUtils.and(new boolean[] { true, true }),
581 "False result for (true, true)");
582
583 assertFalse(
584 BooleanUtils.and(new boolean[] { false, false }),
585 "True result for (false, false)");
586
587 assertFalse(
588 BooleanUtils.and(new boolean[] { true, false }),
589 "True result for (true, false)");
590
591 assertFalse(
592 BooleanUtils.and(new boolean[] { false, true }),
593 "True result for (false, true)");
594 }
595
596 @Test
597 public void testAnd_primitive_validInput_3items() {
598 assertFalse(
599 BooleanUtils.and(new boolean[] { false, false, true }),
600 "True result for (false, false, true)");
601
602 assertFalse(
603 BooleanUtils.and(new boolean[] { false, true, false }),
604 "True result for (false, true, false)");
605
606 assertFalse(
607 BooleanUtils.and(new boolean[] { true, false, false }),
608 "True result for (true, false, false)");
609
610 assertTrue(
611 BooleanUtils.and(new boolean[] { true, true, true }),
612 "False result for (true, true, true)");
613
614 assertFalse(
615 BooleanUtils.and(new boolean[] { false, false, false }),
616 "True result for (false, false)");
617
618 assertFalse(
619 BooleanUtils.and(new boolean[] { true, true, false }),
620 "True result for (true, true, false)");
621
622 assertFalse(
623 BooleanUtils.and(new boolean[] { true, false, true }),
624 "True result for (true, false, true)");
625
626 assertFalse(
627 BooleanUtils.and(new boolean[] { false, true, true }),
628 "True result for (false, true, true)");
629 }
630
631 @Test
632 public void testCompare() {
633 assertTrue(BooleanUtils.compare(true, false) > 0);
634 assertEquals(0, BooleanUtils.compare(true, true));
635 assertEquals(0, BooleanUtils.compare(false, false));
636 assertTrue(BooleanUtils.compare(false, true) < 0);
637 }
638
639 @Test
640 public void testConstructor() {
641 assertNotNull(new BooleanUtils());
642 final Constructor<?>[] cons = BooleanUtils.class.getDeclaredConstructors();
643 assertEquals(1, cons.length);
644 assertTrue(Modifier.isPublic(cons[0].getModifiers()));
645 assertTrue(Modifier.isPublic(BooleanUtils.class.getModifiers()));
646 assertFalse(Modifier.isFinal(BooleanUtils.class.getModifiers()));
647 }
648
649 @Test
650 public void testOneHot_object_emptyInput() {
651 assertThrows(IllegalArgumentException.class, () -> BooleanUtils.oneHot(new Boolean[] {}));
652 }
653
654 @Test
655 public void testOneHot_object_nullElementInput() {
656 assertEquals(Boolean.FALSE, BooleanUtils.oneHot(new Boolean[] {null}));
657 }
658
659 @Test
660 public void testOneHot_object_nullInput() {
661 assertThrows(NullPointerException.class, () -> BooleanUtils.oneHot((Boolean[]) null));
662 }
663
664 @Test
665 public void testOneHot_object_validInput_1item() {
666 assertTrue(BooleanUtils.oneHot(new Boolean[]{Boolean.TRUE}), "true");
667
668 assertFalse(BooleanUtils.oneHot(new Boolean[]{Boolean.FALSE}), "false");
669
670 assertFalse(BooleanUtils.oneHot(new Boolean[]{null}), "false");
671 }
672
673 @Test
674 public void testOneHot_object_validInput_2items() {
675 assertFalse(BooleanUtils.oneHot(new Boolean[]{true, true}), "both true");
676
677 assertFalse(BooleanUtils.oneHot(new Boolean[]{false, false}), "both false");
678
679 assertTrue(BooleanUtils.oneHot(new Boolean[]{true, false}), "first true");
680
681 assertTrue(BooleanUtils.oneHot(new Boolean[]{false, true}), "last true");
682 }
683
684 @Test
685 public void testOneHot_object_validInput_2ItemsNullsTreatedAsFalse() {
686 assertFalse(BooleanUtils.oneHot(null, null), "both null");
687
688 assertTrue(BooleanUtils.oneHot(true, null), "first true");
689
690 assertTrue(BooleanUtils.oneHot(null, true), "last true");
691 }
692
693 @Test
694 public void testOneHot_object_validInput_3items() {
695
696 assertFalse(BooleanUtils.oneHot(new Boolean[]{false, false, false}), "all false");
697
698
699 assertTrue(BooleanUtils.oneHot(new Boolean[]{true, false, false}), "first true");
700
701 assertTrue(BooleanUtils.oneHot(new Boolean[]{false, true, false}), "middle true");
702
703 assertTrue(BooleanUtils.oneHot(new Boolean[]{false, false, true}), "last true");
704
705
706 assertFalse(BooleanUtils.oneHot(new Boolean[]{false, true, true}), "first false");
707
708 assertFalse(BooleanUtils.oneHot(new Boolean[]{true, false, true}), "middle false");
709
710 assertFalse(BooleanUtils.oneHot(new Boolean[]{true, true, false}), "last false");
711
712
713 assertFalse(BooleanUtils.oneHot(new Boolean[]{true, true, true}), "all true");
714 }
715
716 @Test
717 public void testOneHot_primitive_emptyInput() {
718 assertThrows(IllegalArgumentException.class, () -> BooleanUtils.oneHot(new boolean[] {}));
719 }
720
721 @Test
722 public void testOneHot_primitive_nullInput() {
723 assertThrows(NullPointerException.class, () -> BooleanUtils.oneHot((boolean[]) null));
724 }
725
726 @Test
727 public void testOneHot_primitive_validInput_1item() {
728 assertTrue(BooleanUtils.oneHot(new boolean[]{true}), "true");
729
730 assertFalse(BooleanUtils.oneHot(new boolean[]{false}), "false");
731 }
732
733 @Test
734 public void testOneHot_primitive_validInput_2items() {
735 assertFalse(BooleanUtils.oneHot(new boolean[]{true, true}), "both true");
736
737 assertFalse(BooleanUtils.oneHot(new boolean[]{false, false}), "both false");
738
739 assertTrue(BooleanUtils.oneHot(new boolean[]{true, false}), "first true");
740
741 assertTrue(BooleanUtils.oneHot(new boolean[]{false, true}), "last true");
742 }
743
744 @Test
745 public void testOneHot_primitive_validInput_3items() {
746
747 assertFalse(BooleanUtils.oneHot(new boolean[]{false, false, false}), "all false");
748
749
750 assertTrue(BooleanUtils.oneHot(new boolean[]{true, false, false}), "first true");
751
752 assertTrue(BooleanUtils.oneHot(new boolean[]{false, true, false}), "middle true");
753
754 assertTrue(BooleanUtils.oneHot(new boolean[]{false, false, true}), "last true");
755
756
757 assertFalse(BooleanUtils.oneHot(new boolean[]{false, true, true}), "first false");
758
759 assertFalse(BooleanUtils.oneHot(new boolean[]{true, false, true}), "middle false");
760
761 assertFalse(BooleanUtils.oneHot(new boolean[]{true, true, false}), "last false");
762
763
764 assertFalse(BooleanUtils.oneHot(new boolean[]{true, true, true}), "all true");
765 }
766
767 @Test
768 public void testOr_object_emptyInput() {
769 assertThrows(IllegalArgumentException.class, () -> BooleanUtils.or(new Boolean[] {}));
770 }
771
772 @Test
773 public void testOr_object_nullElementInput() {
774 assertEquals(Boolean.FALSE, BooleanUtils.or(new Boolean[] {null}));
775 }
776
777 @Test
778 public void testOr_object_nullInput() {
779 assertThrows(NullPointerException.class, () -> BooleanUtils.or((Boolean[]) null));
780 }
781
782 @Test
783 public void testOr_object_validInput_2items() {
784 assertTrue(
785 BooleanUtils
786 .or(new Boolean[] { Boolean.TRUE, Boolean.TRUE })
787 .booleanValue(),
788 "False result for (true, true)");
789
790 assertFalse(
791 BooleanUtils
792 .or(new Boolean[] { Boolean.FALSE, Boolean.FALSE })
793 .booleanValue(),
794 "True result for (false, false)");
795
796 assertTrue(
797 BooleanUtils
798 .or(new Boolean[] { Boolean.TRUE, Boolean.FALSE })
799 .booleanValue(),
800 "False result for (true, false)");
801
802 assertTrue(
803 BooleanUtils
804 .or(new Boolean[] { Boolean.FALSE, Boolean.TRUE })
805 .booleanValue(),
806 "False result for (false, true)");
807 }
808
809 @Test
810 public void testOr_object_validInput_3items() {
811 assertTrue(
812 BooleanUtils
813 .or(
814 new Boolean[] {
815 Boolean.FALSE,
816 Boolean.FALSE,
817 Boolean.TRUE })
818 .booleanValue(),
819 "False result for (false, false, true)");
820
821 assertTrue(
822 BooleanUtils
823 .or(
824 new Boolean[] {
825 Boolean.FALSE,
826 Boolean.TRUE,
827 Boolean.FALSE })
828 .booleanValue(),
829 "False result for (false, true, false)");
830
831 assertTrue(
832 BooleanUtils
833 .or(
834 new Boolean[] {
835 Boolean.TRUE,
836 Boolean.FALSE,
837 Boolean.FALSE })
838 .booleanValue(),
839 "False result for (true, false, false)");
840
841 assertTrue(
842 BooleanUtils
843 .or(new Boolean[] { Boolean.TRUE, Boolean.TRUE, Boolean.TRUE })
844 .booleanValue(),
845 "False result for (true, true, true)");
846
847 assertFalse(
848 BooleanUtils.or(
849 new Boolean[] {
850 Boolean.FALSE,
851 Boolean.FALSE,
852 Boolean.FALSE })
853 .booleanValue(),
854 "True result for (false, false)");
855
856 assertTrue(
857 BooleanUtils.or(
858 new Boolean[] {
859 Boolean.TRUE,
860 Boolean.TRUE,
861 Boolean.FALSE })
862 .booleanValue(),
863 "False result for (true, true, false)");
864
865 assertTrue(
866 BooleanUtils.or(
867 new Boolean[] {
868 Boolean.TRUE,
869 Boolean.FALSE,
870 Boolean.TRUE })
871 .booleanValue(),
872 "False result for (true, false, true)");
873
874 assertTrue(
875 BooleanUtils.or(
876 new Boolean[] {
877 Boolean.FALSE,
878 Boolean.TRUE,
879 Boolean.TRUE })
880 .booleanValue(),
881 "False result for (false, true, true)");
882 }
883
884 @Test
885 public void testOr_primitive_emptyInput() {
886 assertThrows(IllegalArgumentException.class, () -> BooleanUtils.or(new boolean[] {}));
887 }
888
889 @Test
890 public void testOr_primitive_nullInput() {
891 assertThrows(NullPointerException.class, () -> BooleanUtils.or((boolean[]) null));
892 }
893
894 @Test
895 public void testOr_primitive_validInput_2items() {
896 assertTrue(
897 BooleanUtils.or(new boolean[] { true, true }),
898 "False result for (true, true)");
899
900 assertFalse(
901 BooleanUtils.or(new boolean[] { false, false }),
902 "True result for (false, false)");
903
904 assertTrue(
905 BooleanUtils.or(new boolean[] { true, false }),
906 "False result for (true, false)");
907
908 assertTrue(
909 BooleanUtils.or(new boolean[] { false, true }),
910 "False result for (false, true)");
911 }
912
913 @Test
914 public void testOr_primitive_validInput_3items() {
915 assertTrue(
916 BooleanUtils.or(new boolean[] { false, false, true }),
917 "False result for (false, false, true)");
918
919 assertTrue(
920 BooleanUtils.or(new boolean[] { false, true, false }),
921 "False result for (false, true, false)");
922
923 assertTrue(
924 BooleanUtils.or(new boolean[] { true, false, false }),
925 "False result for (true, false, false)");
926
927 assertTrue(
928 BooleanUtils.or(new boolean[] { true, true, true }),
929 "False result for (true, true, true)");
930
931 assertFalse(
932 BooleanUtils.or(new boolean[] { false, false, false }),
933 "True result for (false, false)");
934
935 assertTrue(
936 BooleanUtils.or(new boolean[] { true, true, false }),
937 "False result for (true, true, false)");
938
939 assertTrue(
940 BooleanUtils.or(new boolean[] { true, false, true }),
941 "False result for (true, false, true)");
942
943 assertTrue(
944 BooleanUtils.or(new boolean[] { false, true, true }),
945 "False result for (false, true, true)");
946 }
947
948 @Test
949 public void testXor_object_emptyInput() {
950 assertThrows(IllegalArgumentException.class, () -> BooleanUtils.xor(new Boolean[] {}));
951 }
952
953 @Test
954 public void testXor_object_nullElementInput() {
955 assertEquals(Boolean.FALSE, BooleanUtils.xor(new Boolean[] {null}));
956 }
957
958 @Test
959 public void testXor_object_nullInput() {
960 assertThrows(NullPointerException.class, () -> BooleanUtils.xor((Boolean[]) null));
961 }
962
963 @Test
964 public void testXor_object_validInput_1items() {
965 assertEquals(
966 true,
967 BooleanUtils.xor(new Boolean[] { Boolean.TRUE }).booleanValue(),
968 "true");
969
970 assertEquals(
971 false,
972 BooleanUtils.xor(new Boolean[] { Boolean.FALSE }).booleanValue(),
973 "false");
974 }
975
976 @Test
977 public void testXor_object_validInput_2items() {
978 assertEquals(
979 false ^ false,
980 BooleanUtils.xor(new Boolean[] { Boolean.FALSE, Boolean.FALSE }).booleanValue(),
981 "false ^ false");
982
983 assertEquals(
984 false ^ true,
985 BooleanUtils.xor(new Boolean[] { Boolean.FALSE, Boolean.TRUE }).booleanValue(),
986 "false ^ true");
987
988 assertEquals(
989 true ^ false,
990 BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE }).booleanValue(),
991 "true ^ false");
992
993 assertEquals(
994 true ^ true,
995 BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.TRUE }).booleanValue(),
996 "true ^ true");
997 }
998
999 @Test
1000 public void testXor_object_validInput_3items() {
1001 assertEquals(
1002 false ^ false ^ false,
1003 BooleanUtils.xor(
1004 new Boolean[] {
1005 Boolean.FALSE,
1006 Boolean.FALSE,
1007 Boolean.FALSE })
1008 .booleanValue(),
1009 "false ^ false ^ false");
1010
1011 assertEquals(
1012 false ^ false ^ true,
1013 BooleanUtils
1014 .xor(
1015 new Boolean[] {
1016 Boolean.FALSE,
1017 Boolean.FALSE,
1018 Boolean.TRUE })
1019 .booleanValue(),
1020 "false ^ false ^ true");
1021
1022 assertEquals(
1023 false ^ true ^ false,
1024 BooleanUtils
1025 .xor(
1026 new Boolean[] {
1027 Boolean.FALSE,
1028 Boolean.TRUE,
1029 Boolean.FALSE })
1030 .booleanValue(),
1031 "false ^ true ^ false");
1032
1033 assertEquals(
1034 true ^ false ^ false,
1035 BooleanUtils
1036 .xor(
1037 new Boolean[] {
1038 Boolean.TRUE,
1039 Boolean.FALSE,
1040 Boolean.FALSE })
1041 .booleanValue(),
1042 "true ^ false ^ false");
1043
1044 assertEquals(
1045 true ^ false ^ true,
1046 BooleanUtils.xor(
1047 new Boolean[] {
1048 Boolean.TRUE,
1049 Boolean.FALSE,
1050 Boolean.TRUE })
1051 .booleanValue(),
1052 "true ^ false ^ true");
1053
1054 assertEquals(
1055 true ^ true ^ false,
1056 BooleanUtils.xor(
1057 new Boolean[] {
1058 Boolean.TRUE,
1059 Boolean.TRUE,
1060 Boolean.FALSE })
1061 .booleanValue(),
1062 "true ^ true ^ false");
1063
1064 assertEquals(
1065 false ^ true ^ true,
1066 BooleanUtils.xor(
1067 new Boolean[] {
1068 Boolean.FALSE,
1069 Boolean.TRUE,
1070 Boolean.TRUE })
1071 .booleanValue(),
1072 "false ^ true ^ true");
1073
1074 assertEquals(
1075 true ^ true ^ true,
1076 BooleanUtils.xor(
1077 new Boolean[] {
1078 Boolean.TRUE,
1079 Boolean.TRUE,
1080 Boolean.TRUE })
1081 .booleanValue(),
1082 "true ^ true ^ true");
1083 }
1084
1085 @Test
1086 public void testXor_primitive_emptyInput() {
1087 assertThrows(IllegalArgumentException.class, () -> BooleanUtils.xor(new boolean[] {}));
1088 }
1089
1090 @Test
1091 public void testXor_primitive_nullInput() {
1092 assertThrows(NullPointerException.class, () -> BooleanUtils.xor((boolean[]) null));
1093 }
1094
1095 @Test
1096 public void testXor_primitive_validInput_1items() {
1097 assertEquals(
1098 true,
1099 BooleanUtils.xor(new boolean[] { true }),
1100 "true");
1101
1102 assertEquals(
1103 false,
1104 BooleanUtils.xor(new boolean[] { false }),
1105 "false");
1106 }
1107
1108 @Test
1109 public void testXor_primitive_validInput_2items() {
1110 assertEquals(
1111 true ^ true,
1112 BooleanUtils.xor(new boolean[] { true, true }),
1113 "true ^ true");
1114
1115 assertEquals(
1116 false ^ false,
1117 BooleanUtils.xor(new boolean[] { false, false }),
1118 "false ^ false");
1119
1120 assertEquals(
1121 true ^ false,
1122 BooleanUtils.xor(new boolean[] { true, false }),
1123 "true ^ false");
1124
1125 assertEquals(
1126 false ^ true,
1127 BooleanUtils.xor(new boolean[] { false, true }),
1128 "false ^ true");
1129 }
1130
1131 @Test
1132 public void testXor_primitive_validInput_3items() {
1133 assertEquals(
1134 false ^ false ^ false,
1135 BooleanUtils.xor(new boolean[] { false, false, false }),
1136 "false ^ false ^ false");
1137
1138 assertEquals(
1139 false ^ false ^ true,
1140 BooleanUtils.xor(new boolean[] { false, false, true }),
1141 "false ^ false ^ true");
1142
1143 assertEquals(
1144 false ^ true ^ false,
1145 BooleanUtils.xor(new boolean[] { false, true, false }),
1146 "false ^ true ^ false");
1147
1148 assertEquals(
1149 false ^ true ^ true,
1150 BooleanUtils.xor(new boolean[] { false, true, true }),
1151 "false ^ true ^ true");
1152
1153 assertEquals(
1154 true ^ false ^ false,
1155 BooleanUtils.xor(new boolean[] { true, false, false }),
1156 "true ^ false ^ false");
1157
1158 assertEquals(
1159 true ^ false ^ true,
1160 BooleanUtils.xor(new boolean[] { true, false, true }),
1161 "true ^ false ^ true");
1162
1163 assertEquals(
1164 true ^ true ^ false,
1165 BooleanUtils.xor(new boolean[] { true, true, false }),
1166 "true ^ true ^ false");
1167
1168 assertEquals(
1169 true ^ true ^ true,
1170 BooleanUtils.xor(new boolean[] { true, true, true }),
1171 "true ^ true ^ true");
1172 }
1173 }