Skip to content

Commit 9859fc1

Browse files
authored
Add data type tests ... (#25)
- Primitive Types - Reference Types - Conversion for primitives - Arrays - Number systems
1 parent 57469f5 commit 9859fc1

File tree

6 files changed

+261
-1
lines changed

6 files changed

+261
-1
lines changed

basics/src/test/java/pl/mperor/lab/java/JavaBasics.java renamed to basics/src/test/java/pl/mperor/lab/java/JavaFundamentalsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import org.junit.jupiter.api.Test;
44

5-
public class JavaBasics {
5+
public class JavaFundamentalsTest {
66

77
@Test
88
public void testTypesOfComments() {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package pl.mperor.lab.java.data.type;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class ArraysTest {
7+
8+
@Test
9+
public void testArrayDefaultInitialization() {
10+
int[] array = new int[2];
11+
Assertions.assertEquals(0, array[0]);
12+
Assertions.assertArrayEquals(new int[]{0, 0}, array);
13+
}
14+
15+
@Test
16+
public void testMultiDimensionalArray() {
17+
int[][] multi = new int[2][];
18+
Assertions.assertNull(multi[0]);
19+
multi[0] = new int[1];
20+
multi[1] = new int[2];
21+
Assertions.assertArrayEquals(new int[][]{{0}, {0, 0}}, multi);
22+
}
23+
24+
@Test
25+
public void testSquareMatrix() {
26+
int[][] multi = new int[2][2];
27+
Assertions.assertArrayEquals(new int[][]{{0, 0}, {0, 0}}, multi);
28+
}
29+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package pl.mperor.lab.java.data.type;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.regex.Pattern;
7+
8+
public class NumberSystemsTest {
9+
10+
@Test
11+
public void testBinary() {
12+
String raw = "1011";
13+
int radix = 2;
14+
Assertions.assertTrue(Pattern.compile("[01]+").asMatchPredicate().test(raw));
15+
Assertions.assertEquals(0b1011, Integer.parseInt(raw, radix));
16+
}
17+
18+
@Test
19+
public void testOctal() {
20+
String raw = "13";
21+
int radix = 8;
22+
Assertions.assertTrue(Pattern.compile("[0-7]+").asMatchPredicate().test(raw));
23+
Assertions.assertEquals(013, Integer.parseInt(raw, radix));
24+
}
25+
26+
@Test
27+
public void testHex() {
28+
String raw = "1a";
29+
int radix = 16;
30+
Assertions.assertTrue(Pattern.compile("[0-9A-Fa-f]+").asMatchPredicate().test(raw));
31+
Assertions.assertEquals(0x1a, Integer.parseInt(raw, radix));
32+
}
33+
34+
@Test
35+
public void testDecimal() {
36+
String raw = "111";
37+
int radix = 10;
38+
Assertions.assertTrue(Pattern.compile("[0-9]+").asMatchPredicate().test(raw));
39+
Assertions.assertEquals(111, Integer.parseInt(raw, radix));
40+
}
41+
42+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package pl.mperor.lab.java.data.type;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class PrimitiveTypesConversionTest {
7+
8+
@Test
9+
public void testWidening() {
10+
byte b = 127;
11+
short s = b;
12+
int i = s;
13+
long l = i;
14+
Assertions.assertEquals(127, s);
15+
Assertions.assertEquals(127, i);
16+
Assertions.assertEquals(127, l);
17+
18+
float f = 0.1f;
19+
double d = f;
20+
Assertions.assertTrue(f == d);
21+
22+
char c = 'A';
23+
l = i = c;
24+
Assertions.assertTrue(i == 65 && l == 65);
25+
}
26+
27+
@Test
28+
public void testCutting() {
29+
float f = 1.123f;
30+
int i = (int) f;
31+
Assertions.assertEquals(1, i);
32+
33+
double d = 2.123;
34+
long l = (long) d;
35+
Assertions.assertEquals(2, l);
36+
37+
long number = 1_000_000_000_000_000_001L;
38+
double converted = number;
39+
Assertions.assertNotEquals(0, number - (long) converted, "Floating point numbers and integers are stored in different way in Java memory");
40+
}
41+
42+
@Test
43+
public void testNarrowingNotOverflow() {
44+
long l = 127;
45+
int i = (int) l;
46+
short s = (short) l;
47+
byte b = (byte) l;
48+
Assertions.assertEquals(127, i);
49+
Assertions.assertEquals(127, s);
50+
Assertions.assertEquals(127, b);
51+
}
52+
53+
@Test
54+
public void testNarrowingOverflow() {
55+
long l = 4_294_967_297L;
56+
int i = (int) l;
57+
short s = (short) l;
58+
byte b = (byte) l;
59+
Assertions.assertEquals(1, i);
60+
Assertions.assertEquals(1, s);
61+
Assertions.assertEquals(1, b);
62+
}
63+
64+
@Test
65+
public void testUnboxingAndAutoboxing() {
66+
Integer bigInt = 1;
67+
int smallInt = bigInt;
68+
Assertions.assertEquals(bigInt, smallInt);
69+
}
70+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package pl.mperor.lab.java.data.type;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class PrimitiveTypesTest {
7+
8+
@Test
9+
public void testByteAkaSmallInteger() {
10+
byte byteInBinary = 0b0; // same as 0
11+
Assertions.assertInstanceOf(Byte.class, byteInBinary, "Wrapper type");
12+
Assertions.assertEquals(127, Byte.MAX_VALUE, "Max value");
13+
Assertions.assertEquals(-128, Byte.MIN_VALUE, "Min value");
14+
Assertions.assertEquals(8, Byte.SIZE, "Number of bits");
15+
Assertions.assertEquals(1, Byte.BYTES, "Number of bytes");
16+
}
17+
18+
@Test
19+
public void testShortAkaShortInteger() {
20+
short shortInHex = 0x0; // same as 0
21+
Assertions.assertInstanceOf(Short.class, shortInHex, "Wrapper type");
22+
Assertions.assertEquals(32_767, Short.MAX_VALUE, "Max value");
23+
Assertions.assertEquals(-32_768, Short.MIN_VALUE, "Min value");
24+
Assertions.assertEquals(16, Short.SIZE, "Number of bits");
25+
Assertions.assertEquals(2, Short.BYTES, "Number of bytes");
26+
}
27+
28+
@Test
29+
public void testInteger() {
30+
int integer = 0;
31+
Assertions.assertInstanceOf(Integer.class, integer, "Wrapper type");
32+
Assertions.assertEquals(2_147_483_647, Integer.MAX_VALUE, "Max value");
33+
Assertions.assertEquals(-2_147_483_648, Integer.MIN_VALUE, "Min value");
34+
Assertions.assertEquals(32, Integer.SIZE, "Number of bits");
35+
Assertions.assertEquals(4, Integer.BYTES, "Number of bytes");
36+
}
37+
38+
@Test
39+
public void testLongAkaLargeInteger() {
40+
long largeInteger = 0L;
41+
Assertions.assertInstanceOf(Long.class, largeInteger, "Wrapper type");
42+
Assertions.assertEquals(9_223_372_036_854_775_807L, Long.MAX_VALUE, "Max value");
43+
Assertions.assertEquals(-9_223_372_036_854_775_808L, Long.MIN_VALUE, "Min value");
44+
Assertions.assertEquals(64, Long.SIZE, "Number of bits");
45+
Assertions.assertEquals(8, Long.BYTES, "Number of bytes");
46+
}
47+
48+
/// [IEEE 754 Standard for Floating-Point Arithmetic](https://standards.ieee.org/ieee/754/6210)
49+
@Test
50+
public void testFloatAkaSinglePrecisionFloatingPoint() {
51+
float singlePrecisionFloatingPoint = 0f;
52+
Assertions.assertInstanceOf(Float.class, singlePrecisionFloatingPoint, "Wrapper type");
53+
Assertions.assertEquals(3.4028235e+38f, Float.MAX_VALUE, "Max value (7 decimal digits)");
54+
Assertions.assertEquals(1.4e-45f, Float.MIN_VALUE, "Min value (7 decimal digits)");
55+
Assertions.assertEquals(32, Float.SIZE, "Number of bits");
56+
Assertions.assertEquals(4, Float.BYTES, "Number of bytes");
57+
}
58+
59+
@Test
60+
public void testDoubleAkaDoublePrecisionFloatingPoint() {
61+
double doublePrecisionFloatingPoint = 0d;
62+
Assertions.assertInstanceOf(Double.class, doublePrecisionFloatingPoint, "Wrapper type");
63+
Assertions.assertEquals(1.7976931348623157e+308, Double.MAX_VALUE, "Max value (15 decimal digits)");
64+
Assertions.assertEquals(4.9e-324, Double.MIN_VALUE, "Min value (15 decimal digits)");
65+
Assertions.assertEquals(64, Double.SIZE, "Number of bits");
66+
Assertions.assertEquals(8, Double.BYTES, "Number of bytes");
67+
}
68+
69+
@Test
70+
public void testCharacter() {
71+
char character = 'a';
72+
Assertions.assertInstanceOf(Character.class, character, "Wrapper type");
73+
Assertions.assertEquals('\uFFFF', Character.MAX_VALUE, "Max value");
74+
Assertions.assertEquals('\u0000', Character.MIN_VALUE, "Min value");
75+
Assertions.assertEquals(16, Character.SIZE, "Number of bits");
76+
Assertions.assertEquals(2, Character.BYTES, "Number of bytes");
77+
}
78+
79+
@Test
80+
public void testBoolean() {
81+
boolean bool = false;
82+
Assertions.assertInstanceOf(Boolean.class, bool, "Wrapper type");
83+
Assertions.assertEquals(true, Boolean.TRUE);
84+
Assertions.assertEquals(false, Boolean.FALSE);
85+
}
86+
87+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package pl.mperor.lab.java.data.type;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class ReferenceTypesTest {
7+
8+
@Test
9+
public void testReferenceTypes() {
10+
Assertions.assertTrue(int[].class.isArray());
11+
Assertions.assertTrue(isClass(Class.class));
12+
Assertions.assertTrue(Interface.class.isInterface());
13+
Assertions.assertTrue(Record.class.isRecord());
14+
Assertions.assertTrue(Enumeration.class.isEnum());
15+
Assertions.assertTrue(Annotation.class.isAnnotation());
16+
}
17+
18+
private static boolean isClass(java.lang.Class<?> clazz) {
19+
return !clazz.isInterface() && !clazz.isEnum() && !clazz.isAnnotation() && !clazz.isPrimitive();
20+
}
21+
22+
public class Class {}
23+
24+
public interface Interface {}
25+
26+
public record Record() {}
27+
28+
public enum Enumeration {INSTANCE}
29+
30+
public @interface Annotation {}
31+
32+
}

0 commit comments

Comments
 (0)