feat: add tests for main exercises and configure dependency in pom.xml

This commit is contained in:
2026-04-16 17:46:09 +03:30
parent f1301499d9
commit ab02836a30
4 changed files with 207 additions and 0 deletions
+22
View File
@@ -14,4 +14,26 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.11.4</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project> </project>
+65
View File
@@ -0,0 +1,65 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
public class TestPartitions {
static Exercises ex;
@BeforeAll
static void setUp() {
ex = new Exercises();
}
@Test
void testPartition0() {
int[][] expected = {
{1}
};
assertArrayEquals(expected, ex.intPartitions(1));
}
@Test
void testPartition1() {
int[][] expected = {
{4},
{3, 1},
{2, 2},
{2, 1, 1},
{1, 1, 1, 1}
};
assertArrayEquals(expected, ex.intPartitions(4));
}
@Test
void testPartition2() {
int[][] expected = {
{5},
{4, 1},
{3, 2},
{3, 1, 1},
{2, 2, 1},
{2, 1, 1, 1},
{1, 1, 1, 1, 1}
};
assertArrayEquals(expected, ex.intPartitions(5));
}
@Test
void testPartition3() {
int[][] expected = {
{6},
{5, 1},
{4, 2},
{4, 1, 1},
{3, 3},
{3, 2, 1},
{3, 1, 1, 1},
{2, 2, 2},
{2, 2, 1, 1},
{2, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1}
};
assertArrayEquals(expected, ex.intPartitions(6));
}
}
+66
View File
@@ -0,0 +1,66 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
public class TestTraversal {
static Exercises ex;
@BeforeAll
static void setUp() {
ex = new Exercises();
}
@Test
void testTraverse0() {
int[][] nums = {
{1, 2, 3, 4, 5}
};
int rows = 1;
int cols = 5;
int[] expected = {1, 2, 3, 4, 5};
assertArrayEquals(expected, ex.spiralTraversal(nums, rows, cols));
}
@Test
void testTraverse1() {
int[][] nums = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12}
};
int rows = 4;
int cols = 3;
int[] expected = {1, 2, 3, 6, 9, 12, 11, 10, 7, 4, 5, 8};
assertArrayEquals(expected, ex.spiralTraversal(nums, rows, cols));
}
@Test
void testTraverse2() {
int[][] nums = {
{1, 2, 3, 4},
{5, 6, 7, 8}
};
int rows = 2;
int cols = 4;
int[] expected = {1, 2, 3, 4, 8, 7, 6, 5};
assertArrayEquals(expected, ex.spiralTraversal(nums, rows, cols));
}
@Test
void testTraverse3() {
int[][] nums = {
{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}
};
int rows = 5;
int cols = 6;
int[] expected = {1, 2, 3, 4, 5, 6, 12, 18, 24, 30, 29, 28, 27, 26, 25, 19, 13, 7, 8, 9, 10, 11, 17, 23, 22, 21,
20, 14, 15, 16};
assertArrayEquals(expected, ex.spiralTraversal(nums, rows, cols));
}
}
+54
View File
@@ -0,0 +1,54 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
public class TestTriangle {
static Exercises ex;
@BeforeAll
static void setUp() {
ex = new Exercises();
}
@Test
void testTriangle0() {
char[][] expected = {
{'*'},
{'*', '*'}
};
assertArrayEquals(expected, ex.generateTriangle(2));
}
@Test
void testTriangle1() {
char[][] expected = {
{'*'},
{'*', '*'},
{'*', ' ', '*'},
{'*', '*', '*', '*'}
};
assertArrayEquals(expected, ex.generateTriangle(4));
}
@Test
void testTriangle2() {
char[][] expected = {
{'*'},
{'*', '*'},
{'*', ' ', '*'},
{'*', ' ', ' ', '*'},
{'*', ' ', ' ', ' ', '*'},
{'*', ' ', ' ', ' ', ' ', '*'},
{'*', ' ', ' ', ' ', ' ', ' ', '*'},
{'*', '*', '*', '*', '*', '*', '*', '*'}
};
assertArrayEquals(expected, ex.generateTriangle(8));
}
@Test
void testTriangle3() {
char[][] expected = {};
assertArrayEquals(expected, ex.generateTriangle(0));
}
}