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