Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.junit.jupiter.api.Test;

public class JavaBasics {
public class JavaFundamentalsTest {

@Test
public void testTypesOfComments() {
Expand Down
29 changes: 29 additions & 0 deletions basics/src/test/java/pl/mperor/lab/java/data/type/ArraysTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package pl.mperor.lab.java.data.type;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class ArraysTest {

@Test
public void testArrayDefaultInitialization() {
int[] array = new int[2];
Assertions.assertEquals(0, array[0]);
Assertions.assertArrayEquals(new int[]{0, 0}, array);
}

@Test
public void testMultiDimensionalArray() {
int[][] multi = new int[2][];
Assertions.assertNull(multi[0]);
multi[0] = new int[1];
multi[1] = new int[2];
Assertions.assertArrayEquals(new int[][]{{0}, {0, 0}}, multi);
}

@Test
public void testSquareMatrix() {
int[][] multi = new int[2][2];
Assertions.assertArrayEquals(new int[][]{{0, 0}, {0, 0}}, multi);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package pl.mperor.lab.java.data.type;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.regex.Pattern;

public class NumberSystemsTest {

@Test
public void testBinary() {
String raw = "1011";
int radix = 2;
Assertions.assertTrue(Pattern.compile("[01]+").asMatchPredicate().test(raw));
Assertions.assertEquals(0b1011, Integer.parseInt(raw, radix));
}

@Test
public void testOctal() {
String raw = "13";
int radix = 8;
Assertions.assertTrue(Pattern.compile("[0-7]+").asMatchPredicate().test(raw));
Assertions.assertEquals(013, Integer.parseInt(raw, radix));
}

@Test
public void testHex() {
String raw = "1a";
int radix = 16;
Assertions.assertTrue(Pattern.compile("[0-9A-Fa-f]+").asMatchPredicate().test(raw));
Assertions.assertEquals(0x1a, Integer.parseInt(raw, radix));
}

@Test
public void testDecimal() {
String raw = "111";
int radix = 10;
Assertions.assertTrue(Pattern.compile("[0-9]+").asMatchPredicate().test(raw));
Assertions.assertEquals(111, Integer.parseInt(raw, radix));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package pl.mperor.lab.java.data.type;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class PrimitiveTypesConversionTest {

@Test
public void testWidening() {
byte b = 127;
short s = b;
int i = s;
long l = i;
Assertions.assertEquals(127, s);
Assertions.assertEquals(127, i);
Assertions.assertEquals(127, l);

float f = 0.1f;
double d = f;
Assertions.assertTrue(f == d);

char c = 'A';
l = i = c;
Assertions.assertTrue(i == 65 && l == 65);
}

@Test
public void testCutting() {
float f = 1.123f;
int i = (int) f;
Assertions.assertEquals(1, i);

double d = 2.123;
long l = (long) d;
Assertions.assertEquals(2, l);

long number = 1_000_000_000_000_000_001L;
double converted = number;
Assertions.assertNotEquals(0, number - (long) converted, "Floating point numbers and integers are stored in different way in Java memory");
}

@Test
public void testNarrowingNotOverflow() {
long l = 127;
int i = (int) l;
short s = (short) l;
byte b = (byte) l;
Assertions.assertEquals(127, i);
Assertions.assertEquals(127, s);
Assertions.assertEquals(127, b);
}

@Test
public void testNarrowingOverflow() {
long l = 4_294_967_297L;
int i = (int) l;
short s = (short) l;
byte b = (byte) l;
Assertions.assertEquals(1, i);
Assertions.assertEquals(1, s);
Assertions.assertEquals(1, b);
}

@Test
public void testUnboxingAndAutoboxing() {
Integer bigInt = 1;
int smallInt = bigInt;
Assertions.assertEquals(bigInt, smallInt);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package pl.mperor.lab.java.data.type;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class PrimitiveTypesTest {

@Test
public void testByteAkaSmallInteger() {
byte byteInBinary = 0b0; // same as 0
Assertions.assertInstanceOf(Byte.class, byteInBinary, "Wrapper type");
Assertions.assertEquals(127, Byte.MAX_VALUE, "Max value");
Assertions.assertEquals(-128, Byte.MIN_VALUE, "Min value");
Assertions.assertEquals(8, Byte.SIZE, "Number of bits");
Assertions.assertEquals(1, Byte.BYTES, "Number of bytes");
}

@Test
public void testShortAkaShortInteger() {
short shortInHex = 0x0; // same as 0
Assertions.assertInstanceOf(Short.class, shortInHex, "Wrapper type");
Assertions.assertEquals(32_767, Short.MAX_VALUE, "Max value");
Assertions.assertEquals(-32_768, Short.MIN_VALUE, "Min value");
Assertions.assertEquals(16, Short.SIZE, "Number of bits");
Assertions.assertEquals(2, Short.BYTES, "Number of bytes");
}

@Test
public void testInteger() {
int integer = 0;
Assertions.assertInstanceOf(Integer.class, integer, "Wrapper type");
Assertions.assertEquals(2_147_483_647, Integer.MAX_VALUE, "Max value");
Assertions.assertEquals(-2_147_483_648, Integer.MIN_VALUE, "Min value");
Assertions.assertEquals(32, Integer.SIZE, "Number of bits");
Assertions.assertEquals(4, Integer.BYTES, "Number of bytes");
}

@Test
public void testLongAkaLargeInteger() {
long largeInteger = 0L;
Assertions.assertInstanceOf(Long.class, largeInteger, "Wrapper type");
Assertions.assertEquals(9_223_372_036_854_775_807L, Long.MAX_VALUE, "Max value");
Assertions.assertEquals(-9_223_372_036_854_775_808L, Long.MIN_VALUE, "Min value");
Assertions.assertEquals(64, Long.SIZE, "Number of bits");
Assertions.assertEquals(8, Long.BYTES, "Number of bytes");
}

/// [IEEE 754 Standard for Floating-Point Arithmetic](https://standards.ieee.org/ieee/754/6210)
@Test
public void testFloatAkaSinglePrecisionFloatingPoint() {
float singlePrecisionFloatingPoint = 0f;
Assertions.assertInstanceOf(Float.class, singlePrecisionFloatingPoint, "Wrapper type");
Assertions.assertEquals(3.4028235e+38f, Float.MAX_VALUE, "Max value (7 decimal digits)");
Assertions.assertEquals(1.4e-45f, Float.MIN_VALUE, "Min value (7 decimal digits)");
Assertions.assertEquals(32, Float.SIZE, "Number of bits");
Assertions.assertEquals(4, Float.BYTES, "Number of bytes");
}

@Test
public void testDoubleAkaDoublePrecisionFloatingPoint() {
double doublePrecisionFloatingPoint = 0d;
Assertions.assertInstanceOf(Double.class, doublePrecisionFloatingPoint, "Wrapper type");
Assertions.assertEquals(1.7976931348623157e+308, Double.MAX_VALUE, "Max value (15 decimal digits)");
Assertions.assertEquals(4.9e-324, Double.MIN_VALUE, "Min value (15 decimal digits)");
Assertions.assertEquals(64, Double.SIZE, "Number of bits");
Assertions.assertEquals(8, Double.BYTES, "Number of bytes");
}

@Test
public void testCharacter() {
char character = 'a';
Assertions.assertInstanceOf(Character.class, character, "Wrapper type");
Assertions.assertEquals('\uFFFF', Character.MAX_VALUE, "Max value");
Assertions.assertEquals('\u0000', Character.MIN_VALUE, "Min value");
Assertions.assertEquals(16, Character.SIZE, "Number of bits");
Assertions.assertEquals(2, Character.BYTES, "Number of bytes");
}

@Test
public void testBoolean() {
boolean bool = false;
Assertions.assertInstanceOf(Boolean.class, bool, "Wrapper type");
Assertions.assertEquals(true, Boolean.TRUE);
Assertions.assertEquals(false, Boolean.FALSE);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package pl.mperor.lab.java.data.type;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class ReferenceTypesTest {

@Test
public void testReferenceTypes() {
Assertions.assertTrue(int[].class.isArray());
Assertions.assertTrue(isClass(Class.class));
Assertions.assertTrue(Interface.class.isInterface());
Assertions.assertTrue(Record.class.isRecord());
Assertions.assertTrue(Enumeration.class.isEnum());
Assertions.assertTrue(Annotation.class.isAnnotation());
}

private static boolean isClass(java.lang.Class<?> clazz) {
return !clazz.isInterface() && !clazz.isEnum() && !clazz.isAnnotation() && !clazz.isPrimitive();
}

public class Class {}

public interface Interface {}

public record Record() {}

public enum Enumeration {INSTANCE}

public @interface Annotation {}

}