Skip to content

Commit 16adec7

Browse files
authored
Add data operations tests ... (#26)
- Operators - Type of expression - Primitive and reference types comparison
1 parent 9859fc1 commit 16adec7

File tree

4 files changed

+210
-0
lines changed

4 files changed

+210
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package pl.mperor.lab.java.data.operation;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.math.BigDecimal;
7+
import java.math.RoundingMode;
8+
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
import static org.junit.jupiter.api.Assertions.assertThrows;
11+
12+
public class BasicArithmeticTest {
13+
14+
@Test
15+
public void testDividingDoubleVsInt() {
16+
Assertions.assertTrue(3 == 16 / 5);
17+
Assertions.assertTrue(3.2 == (double) 16 / 5);
18+
}
19+
20+
@Test
21+
public void testDividingDoubleByZero() {
22+
Assertions.assertEquals(Double.POSITIVE_INFINITY, 3.0 / 0);
23+
Assertions.assertEquals(Double.NEGATIVE_INFINITY, -3.0 / 0);
24+
Assertions.assertEquals(0, 0 / 4.0);
25+
Assertions.assertTrue(Double.isNaN(0 / 0.0));
26+
}
27+
28+
@Test
29+
public void testDividingIntegerByZero() {
30+
assertThrows(ArithmeticException.class, () -> {
31+
int _ = 3 / 0;
32+
});
33+
assertEquals(0, 0 / 4);
34+
assertThrows(ArithmeticException.class, () -> {
35+
int _ = 0 / 0;
36+
});
37+
}
38+
39+
@Test
40+
public void testBigDecimalDividing() {
41+
BigDecimal two = BigDecimal.TWO;
42+
BigDecimal three = BigDecimal.valueOf(3);
43+
Assertions.assertThrows(ArithmeticException.class, () -> two.setScale(2).divide(three));
44+
Assertions.assertEquals(new BigDecimal("0.67"), two.setScale(2).divide(three, RoundingMode.HALF_UP));
45+
}
46+
47+
@Test
48+
public void testFloatingPointAccuracy() {
49+
Assertions.assertTrue(0.1 == 1.0 / 10.0);
50+
Assertions.assertEquals(0.1, 0.3 - 0.2, 1e-9);
51+
}
52+
53+
@Test
54+
public void testMathApi() {
55+
Assertions.assertEquals(5, Math.abs(-5));
56+
Assertions.assertEquals(1, Math.min(1, 2));
57+
Assertions.assertEquals(2, Math.max(1, 2));
58+
Assertions.assertEquals(27, Math.pow(3, 3));
59+
Assertions.assertEquals(4, Math.sqrt(16));
60+
Assertions.assertEquals(Double.NaN, Math.sqrt(-1));
61+
Assertions.assertEquals(3.14, Math.round(Math.PI * 100) / 100.0);
62+
}
63+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package pl.mperor.lab.java.data.operation;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class ExpressionTypeTest {
7+
8+
@Test
9+
public void testExpressionVsStatement() {
10+
byte b = 1;
11+
Assertions.assertEquals(2, b + b, "statement");
12+
int expression = b + b;
13+
Assertions.assertEquals(2, expression, "expression");
14+
}
15+
16+
@Test
17+
public void testTypeOfArithmeticExpression() {
18+
byte b = 1;
19+
Assertions.assertInstanceOf(Integer.class, b + b);
20+
short s = 2;
21+
Assertions.assertInstanceOf(Integer.class, s + s);
22+
Assertions.assertInstanceOf(Long.class, 1 + 1L);
23+
24+
Assertions.assertInstanceOf(Float.class, 1.0f + 1.0f);
25+
Assertions.assertInstanceOf(Double.class, 1 + 1.0);
26+
Assertions.assertInstanceOf(Double.class, 1.0f + 1.0);
27+
Assertions.assertInstanceOf(String.class, "" + 1.0);
28+
}
29+
30+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package pl.mperor.lab.java.data.operation;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class OperatorsTest {
7+
8+
@Test
9+
public void testUnaryArithmeticOperators() {
10+
Assertions.assertEquals(1, +1, "plus");
11+
Assertions.assertEquals(-1, -1, "minus");
12+
int i = 10;
13+
Assertions.assertEquals(10, i++, "post-increment");
14+
Assertions.assertEquals(10, --i, "pre-decrement");
15+
}
16+
17+
@Test
18+
public void testBinaryArithmeticOperators() {
19+
Assertions.assertEquals(3, 1 + 2, "addition");
20+
Assertions.assertEquals(1, 2 - 1, "subtraction");
21+
Assertions.assertEquals(4, 2 * 2, "multiplication");
22+
Assertions.assertEquals(2, 4 / 2, "division");
23+
Assertions.assertEquals(2, 5 % 3, "modulo");
24+
Assertions.assertEquals("ab", "a" + "b", "string concatenation");
25+
}
26+
27+
@Test
28+
public void testAssignmentOperators() {
29+
int i = 1;
30+
i *= 2; // same as i = i * 2;
31+
String s = "a";
32+
s += "b";
33+
Assertions.assertEquals(2, i);
34+
Assertions.assertEquals("ab", s);
35+
}
36+
37+
@Test
38+
public void testComparisonOperators() {
39+
Assertions.assertTrue(1 == 1, "equals to");
40+
Assertions.assertTrue(1 != 2, "not equals to");
41+
Assertions.assertTrue(2 > 1, "greater than");
42+
Assertions.assertTrue(1 < 2, "less than");
43+
Assertions.assertFalse(2 >= 4, "greater than or equals to");
44+
Assertions.assertFalse(4 <= 2, "less than or equals to");
45+
}
46+
47+
@Test
48+
public void testLogicalOperators() {
49+
Assertions.assertTrue(true & true, "and");
50+
Assertions.assertFalse(false && true, "short and");
51+
Assertions.assertTrue(true | true, "or");
52+
Assertions.assertTrue(false || true, "short or");
53+
Assertions.assertFalse(!true, "not");
54+
Assertions.assertFalse(true ^ true, "xor");
55+
}
56+
57+
@Test
58+
public void testBitwiseOperators() {
59+
Assertions.assertEquals(0b0001, 0b1001 & 0b0011, "and");
60+
Assertions.assertEquals(0b1011, 0b1001 | 0b0011, "or");
61+
Assertions.assertEquals(0b1010, 0b1001 ^ 0b0011, "xor");
62+
Assertions.assertEquals(0b11111111_11111111_11111111_11110110, ~0b1001, "flip");
63+
Assertions.assertEquals(0b0110, 0b0011 << 1, "left shift");
64+
Assertions.assertEquals(0b0001, 0b0011 >> 1, "right shift");
65+
Assertions.assertEquals(
66+
0b01000000_00000000_00000000_00000000,
67+
0b10000000_00000000_00000000_00000001 >>> 1,
68+
"unsigned right shift"
69+
);
70+
}
71+
72+
@Test
73+
public void testTernaryOperator() {
74+
Assertions.assertTrue(isEmpty(null));
75+
Assertions.assertTrue(isEmpty(""));
76+
Assertions.assertFalse(isEmpty("not empty"));
77+
}
78+
79+
private static boolean isEmpty(String string) {
80+
return string == null ? true : string.isEmpty();
81+
}
82+
83+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package pl.mperor.lab.java.data.operation;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.Arrays;
7+
8+
public class PrimitiveAndReferenceTypesComparisonTest {
9+
10+
@Test
11+
public void testComparePrimitiveTypes() {
12+
Assertions.assertTrue(1 == 1);
13+
Assertions.assertFalse(1 == 2);
14+
Assertions.assertTrue(65 == 'A');
15+
}
16+
17+
@SuppressWarnings("removal")
18+
@Test
19+
public void testCompareReferenceTypes() {
20+
Assertions.assertTrue(Integer.valueOf(127) == Integer.valueOf(127), "Integer pool (-128, 127)");
21+
Assertions.assertFalse(Integer.valueOf(128) == Integer.valueOf(128));
22+
23+
Assertions.assertTrue(new Integer(1).equals(new Integer(1)), "equals to");
24+
Assertions.assertFalse(new Integer(1) == new Integer(1), "==");
25+
}
26+
27+
@Test
28+
public void testCompareArrays() {
29+
int[] a1 = {1, 2, 3}, a2 = {1, 2, 3};
30+
Assertions.assertFalse(a1 == a2);
31+
Assertions.assertFalse((a1.equals(a2)));
32+
Assertions.assertTrue(Arrays.equals(a1, a2));
33+
}
34+
}

0 commit comments

Comments
 (0)