forked from examplehub/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBooleanExampleTest.java
More file actions
71 lines (56 loc) · 1.36 KB
/
BooleanExampleTest.java
File metadata and controls
71 lines (56 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.examplehub.basics.bool;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class BooleanExampleTest {
@Test
void testCompareOperator() {
boolean isGreater = 5 < 3;
assertFalse(isGreater);
int age = 25;
boolean isZero = age == 0;
assertFalse(isZero);
boolean isNonZero = !isZero;
assertTrue(isNonZero);
boolean isAdult = age > 18;
assertTrue(isAdult);
boolean isTeenager = age > 6 && age < 18;
assertFalse(isTeenager);
}
@Test
void testShortCircuit() {
boolean bool = false;
boolean result = bool && (5 / 0 > 0);
assertFalse(result);
bool = true;
try {
result = bool && (5 / 0 > 0);
fail(); // won't execute
} catch (ArithmeticException arithmeticException) {
assertTrue(true);
}
result = true || (5 / 0 > 0);
assertTrue(result);
}
@Test
void testTernaryOperator() {
int num = -3;
int abs = num < 0 ? -num : num;
assertEquals(3, abs);
}
@Test
void testValueOf() {
Boolean b = Boolean.valueOf(true);
assertTrue(b.booleanValue());
b = Boolean.valueOf("false");
assertFalse(b.booleanValue());
}
@Test
void testAutoBoxing() {
Boolean b = false;
System.out.println(b);
assertFalse(b);
boolean bValue = b;
System.out.println(bValue);
assertFalse(bValue);
}
}