├── w01 └── test │ └── gad │ ├── logging │ ├── README.md │ └── TestLogging.java │ ├── ExampleTest.java │ ├── ExampleResultImplementation.java │ └── maze │ ├── README.md │ ├── cases.txt │ └── TestExecutor.java ├── w05 └── test │ └── gad │ └── radixsort │ ├── StudentResultTest.java │ ├── RadixSortTest.java │ └── BinaryRadixSortTest.java ├── w06 └── test │ └── gad │ └── sortingHeaps │ ├── HeapSortTest.java │ ├── MergeTest.java │ ├── InsertTest.java │ ├── IndexCalcTest.java │ └── ExtractMaxTest.java ├── w07 └── test │ └── gad │ └── binomilia │ ├── StudentResultTest.java │ ├── BinomialTreeNodeTest.java │ └── BinomialHeapTest.java ├── w10 └── test │ └── gad │ └── simplehash │ ├── PowerOfTwoTest.java │ ├── FastModuloTest.java │ ├── HashTest.java │ ├── InsertRemoveTest.java │ └── FindFindAllTest.java ├── LICENSE ├── w03 └── test │ └── gad │ └── dynamicarray │ ├── DynamicStackCorrectSizeTest.java │ ├── DynamicStackTest.java │ ├── array │ └── DynamicArrayTest.java │ ├── DynamicArrayTest.java │ ├── StackyQueueTest.java │ ├── ArrayHelper.java │ ├── RingQueueTest.java │ └── UnitTest.java ├── w04 └── test │ └── gad │ └── simplesort │ ├── MidPivotTest.java │ ├── RandomPivotTest.java │ ├── MedianPivotFrontTest.java │ ├── QuicksortTest.java │ ├── MergesortTest.java │ ├── DoubleDistributedMedianPivotTest.java │ ├── MedianPivotDistributedTest.java │ ├── MedianPivotTest.java │ └── DoubleMedianPivotTest.java ├── w02 └── test │ └── gad │ ├── binarysearch │ ├── SimpleSearchTest.java │ ├── IntervalSearchTest.java │ └── BoundSearchTest.java │ └── logging │ ├── TestResultImplementation.java │ └── loggingTest.java ├── README.md └── CONTRIBUTING.md /w01/test/gad/logging/README.md: -------------------------------------------------------------------------------- 1 | # Test Logging Documentation 2 | 3 | This test in `TestLogging.java` is testing the Logging of the maze whether the right way was chosen and has the right length. 4 | 5 | ## `TestLogging.java` 6 | 7 | - `checkLogging() : Arguments` contains a Stream of Arguments which could be expanded if necessary. The arguments have the structure of `int width, int height, int seed, int[] solutionX, int[] solutionY, boolean expect` 8 | - `checkLogging(args...)` checks the Logging Data of an `Walker` instance, and it's behaviour -------------------------------------------------------------------------------- /w05/test/gad/radixsort/StudentResultTest.java: -------------------------------------------------------------------------------- 1 | package gad.radixsort; 2 | 3 | import gad.radix.Result; 4 | 5 | public class StudentResultTest implements Result { 6 | /** 7 | *
8 | * This class and method exists only that during the tests on error is produced. 9 | * It has the same structure as the StudentResult which is given in the code of Artemis, 10 | * but without the "System.out.println(Arrays.toString(array));" 11 | * which is for the sorting process irrelevant. 12 | *
13 | */ 14 | @Override 15 | public void logArray(int[] array) {} 16 | } 17 | -------------------------------------------------------------------------------- /w06/test/gad/sortingHeaps/HeapSortTest.java: -------------------------------------------------------------------------------- 1 | package gad.sortingHeaps; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 4 | 5 | import org.junit.jupiter.api.BeforeEach; 6 | import org.junit.jupiter.api.Test; 7 | 8 | public class HeapSortTest { 9 | Result result; 10 | 11 | @BeforeEach 12 | void beforeEach() { 13 | result = new StudentResult(); 14 | } 15 | 16 | @Test 17 | void sortArray() { 18 | int[] array = { 4, 1, 6, 7, 2, 3, 5 }; 19 | NaryHeap.heapSort(array, 2, result); 20 | 21 | int[] expectedArray = { 1, 2, 3, 4, 5, 6, 7 }; 22 | assertArrayEquals(expectedArray, array); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /w01/test/gad/ExampleTest.java: -------------------------------------------------------------------------------- 1 | package gad; 2 | 3 | import org.junit.jupiter.api.*; 4 | import gad.maze.Walker; 5 | import gad.maze.Maze; 6 | 7 | import static org.junit.jupiter.api.Assertions.assertTrue; 8 | 9 | public class ExampleTest { 10 | 11 | @Test 12 | public void checkPenguInfoOutNegative() { 13 | boolean[][] maze = Maze.generateStandardMaze(10, 10); 14 | int[] solutionX = new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 3, 4, 4, 4, 3, 2, 1, 2, 3, 4, 4, 5, 6, 6, 7, 8, 9 }; 15 | int[] solutionY = new int[] { 0, 1, 2, 3, 4, 5, 6, 5, 5, 5, 6, 6, 7, 8, 8, 8, 8, 8, 8, 8, 7, 7, 7, 8, 8, 8, 8 }; 16 | ExampleResultImplementation result = new ExampleResultImplementation(solutionX.length); 17 | Walker walker = new Walker(maze, result); 18 | assertTrue(walker.walk(), "Walker should find exit"); 19 | result.testLogging(solutionX, solutionY); 20 | } 21 | } -------------------------------------------------------------------------------- /w07/test/gad/binomilia/StudentResultTest.java: -------------------------------------------------------------------------------- 1 | package gad.binomilia; 2 | 3 | import java.util.Collection; 4 | 5 | public class StudentResultTest implements Result { 6 | @Override 7 | public void startInsert(int value, Collection heap) { 8 | 9 | } 10 | 11 | @Override 12 | public void startInsert(int value, BinomialTreeNode[] heap) { 13 | 14 | } 15 | 16 | @Override 17 | public void startDeleteMin(Collection heap) { 18 | 19 | } 20 | 21 | @Override 22 | public void startDeleteMin(BinomialTreeNode[] heap) { 23 | 24 | } 25 | 26 | @Override 27 | public void printCurrentIntermediateStep() { 28 | 29 | } 30 | 31 | @Override 32 | public void logMerge(BinomialTreeNode treeOne, BinomialTreeNode treeTwo) { 33 | } 34 | 35 | @Override 36 | public void logMergedTree(BinomialTreeNode mergedTree) { 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /w06/test/gad/sortingHeaps/MergeTest.java: -------------------------------------------------------------------------------- 1 | package gad.sortingHeaps; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 4 | 5 | import org.junit.jupiter.api.BeforeEach; 6 | import org.junit.jupiter.api.Test; 7 | 8 | public class MergeTest { 9 | Result result; 10 | 11 | @BeforeEach 12 | void beforeEach() { 13 | result = new StudentResult(); 14 | } 15 | 16 | @Test 17 | void testMerge() { 18 | NaryHeap binaryHeap1 = new NaryHeap(new int[] { 4, 1, 6, 7, 2, 3, 5 }, 2, 15, result); 19 | NaryHeap binaryHeap2 = new NaryHeap(new int[] { 7, 4, 10, 6, 9, 5, 8 }, 2, 7, result); 20 | 21 | binaryHeap1.merge(binaryHeap2); 22 | 23 | // Please note that there are multiple correct answers for this test 24 | // case. This is the one from Artemis. 25 | int[] expectedHeap = { 10, 9, 7, 7, 8, 5, 6, 1, 4, 2, 6, 4, 3, 5, 0 }; 26 | assertArrayEquals(expectedHeap, binaryHeap1.getHeap()); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /w10/test/gad/simplehash/PowerOfTwoTest.java: -------------------------------------------------------------------------------- 1 | package gad.simplehash; 2 | 3 | import org.junit.jupiter.params.ParameterizedTest; 4 | import org.junit.jupiter.params.provider.Arguments; 5 | import org.junit.jupiter.params.provider.MethodSource; 6 | 7 | import java.util.stream.Stream; 8 | 9 | import static org.junit.jupiter.api.Assertions.assertEquals; 10 | 11 | public class PowerOfTwoTest { 12 | 13 | private static Stream testPowerOfTwo() { 14 | return Stream.of( 15 | Arguments.of(0, 1), 16 | Arguments.of(2, 2), 17 | Arguments.of(33, 64), 18 | Arguments.of(63, 64), 19 | Arguments.of(536870913, 1073741824), 20 | Arguments.of(1073741823, 1073741824), 21 | Arguments.of(1073741824, 1073741824) 22 | 23 | ); 24 | } 25 | 26 | @ParameterizedTest 27 | @MethodSource 28 | public void testPowerOfTwo(int i, int expect) { 29 | assertEquals(expect, Hashtable.getNextPowerOfTwo(i)); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /w01/test/gad/ExampleResultImplementation.java: -------------------------------------------------------------------------------- 1 | package gad; 2 | 3 | import java.util.Arrays; 4 | import gad.maze.Result; 5 | 6 | import static org.junit.jupiter.api.Assertions.*; 7 | 8 | public class ExampleResultImplementation implements Result { 9 | private int[] pathX; 10 | private int[] pathY; 11 | private int pathIndex; 12 | 13 | public ExampleResultImplementation(int expectedSize) { 14 | pathX = new int[expectedSize]; 15 | pathY = new int[expectedSize]; 16 | } 17 | 18 | @Override 19 | public void addLocation(int x, int y) { 20 | if (pathIndex < pathX.length) { 21 | pathX[pathIndex] = x; 22 | pathY[pathIndex] = y; 23 | } 24 | pathIndex++; 25 | } 26 | 27 | public void testLogging(int[] solutionX, int[] solutionY) { 28 | assertEquals(solutionX.length, pathIndex, "Number of logged steps wrong"); 29 | 30 | assertArrayEquals(solutionX, pathX, "Logged wrong path"); 31 | assertArrayEquals(solutionY, pathY, "Logged wrong path"); 32 | } 33 | } -------------------------------------------------------------------------------- /w01/test/gad/maze/README.md: -------------------------------------------------------------------------------- 1 | # Test Code Documentation 2 | 3 | The following test consists of test case file `cases.txt` and test executor file `TestExecutor.java`. It is assumed that in the future exercises the tasks will consist more of 'many cases'. This is a **reusable** test case generator. 4 | 5 | ## `cases.txt` 6 | 7 | ```html 8 | 9 | exit: 10 | ``` 11 | 12 | Comments are annotated with `#`, test cases are separated with five equal signs (`=====`) 13 | 14 | ## `TestExecutor.java` 15 | 16 | - `PATH : String` is the path location of the text cases. 17 | - `greenTest()` verification that tests are running for non-mainstream Java IDEs such as Visual Studio Code 18 | - `testMaze(args...)` the verifier of a `Walker` instance behaviour 19 | - `getTestData(String) : Arguments` converts test data into an argument instance, which is used to call `testMaze(args...)` by junit 20 | - `testMaze() : Stream` is the core of the parameterized tests and collects the case files, which is then ran through `getTestData(String)` 21 | -------------------------------------------------------------------------------- /w01/test/gad/maze/cases.txt: -------------------------------------------------------------------------------- 1 | # Small Maze 2 | 3 | 3 3 0 4 | exit: true 5 | 6 | ===== 7 | 8 | # Example Test Case (Test for Test) 9 | 10 | 4 4 1 11 | exit: true 12 | 13 | ===== 14 | 15 | # Artemis Test Case 16 | 17 | 10 10 0 18 | exit: true 19 | 20 | ===== 21 | 22 | # Test Case no exit 23 | 24 | 10 10 1 25 | exit: false 26 | 27 | ===== 28 | 29 | # Test Case no exit 30 | 31 | 10 10 7 32 | exit: false 33 | 34 | ===== 35 | 36 | # Huge Test Case no exit + huge 37 | 38 | 200 10 9 39 | exit: false 40 | 41 | ===== 42 | 43 | # Huge Test Case no exit + huge 44 | 45 | 200 10 6 46 | exit: false 47 | 48 | ===== 49 | 50 | # Huge Test Case exit + ultra huge 51 | 52 | 200 50 9 53 | exit: true 54 | 55 | ===== 56 | 57 | # Huge Test Case no exit + ultra huge 58 | 59 | 200 50 19 60 | exit: false 61 | 62 | ===== 63 | 64 | # Test Case exit + complex path going through some points multiple times 65 | 66 | 50 50 5 67 | exit: true 68 | 69 | ===== 70 | 71 | # Test Case exit + complex path going through some points multiple times + circle in the path 72 | 73 | 50 50 10 74 | exit: true -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Nils Reichardt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /w06/test/gad/sortingHeaps/InsertTest.java: -------------------------------------------------------------------------------- 1 | package gad.sortingHeaps; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 4 | 5 | import org.junit.jupiter.api.BeforeEach; 6 | import org.junit.jupiter.api.Test; 7 | 8 | public class InsertTest { 9 | Result result; 10 | 11 | @BeforeEach 12 | void beforeEach() { 13 | result = new StudentResult(); 14 | } 15 | 16 | @Test 17 | public void testConstructorBinary() { 18 | NaryHeap binaryHeap = new NaryHeap(new int[] { 4, 1, 6, 7, 2, 3, 5 }, 2, 7, result); 19 | 20 | int[] expectedHeap = { 7, 4, 6, 1, 2, 3, 5 }; 21 | assertArrayEquals(expectedHeap, binaryHeap.getHeap()); 22 | } 23 | 24 | @Test 25 | public void testInsertIntoBinaryHeap2() { 26 | NaryHeap binaryHeap = new NaryHeap(new int[] { 4, 1, 6, 7, 2, 3, 5 }, 2, 15, result); 27 | 28 | binaryHeap.insert(8); 29 | binaryHeap.insert(3); 30 | binaryHeap.insert(9); 31 | binaryHeap.insert(0); 32 | binaryHeap.insert(6); 33 | 34 | int[] expectedHeap = { 9, 8, 6, 4, 7, 6, 5, 1, 3, 2, 0, 3, 0, 0, 0 }; 35 | 36 | assertArrayEquals(expectedHeap, binaryHeap.getHeap()); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /w03/test/gad/dynamicarray/DynamicStackCorrectSizeTest.java: -------------------------------------------------------------------------------- 1 | package gad.dynamicarray; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.junit.jupiter.api.DisplayName; 5 | 6 | import static org.junit.jupiter.api.Assertions.assertEquals; 7 | 8 | public class DynamicStackCorrectSizeTest { 9 | 10 | @Test 11 | @DisplayName("DynamicStackCorrectSize") 12 | void TestSizeAfterPush(){ 13 | DynamicStack dynamicStack = new DynamicStack(3, 4, new StudentResult()); 14 | dynamicStack.pushBack(10); 15 | assertEquals(1,dynamicStack.size()); 16 | dynamicStack.pushBack(20); 17 | assertEquals(2,dynamicStack.size()); 18 | dynamicStack.pushBack(30); 19 | assertEquals(3,dynamicStack.size()); 20 | dynamicStack.pushBack(40); 21 | assertEquals(4,dynamicStack.size()); 22 | } 23 | 24 | @Test 25 | @DisplayName("DynamicStackCorrectSize") 26 | void TestSizeAfterPop(){ 27 | DynamicStack dynamicStack = new DynamicStack(3, 4, new StudentResult()); 28 | dynamicStack.pushBack(10); 29 | dynamicStack.pushBack(20); 30 | dynamicStack.pushBack(30); 31 | dynamicStack.pushBack(40); 32 | dynamicStack.popBack(); 33 | assertEquals(3,dynamicStack.size()); 34 | dynamicStack.popBack(); 35 | assertEquals(2,dynamicStack.size()); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /w04/test/gad/simplesort/MidPivotTest.java: -------------------------------------------------------------------------------- 1 | package gad.simplesort; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | 5 | import java.util.stream.Stream; 6 | 7 | import org.junit.jupiter.params.ParameterizedTest; 8 | import org.junit.jupiter.params.provider.Arguments; 9 | import org.junit.jupiter.params.provider.MethodSource; 10 | 11 | public class MidPivotTest { 12 | 13 | private static Stream midPivotTest() { 14 | return Stream.of( 15 | Arguments.of( 16 | new int[]{0, 1, 2, 3, 4}, 17 | 0, 18 | 4, 19 | 2 20 | ), 21 | Arguments.of( 22 | new int[]{0, 1, 2, 3}, 23 | 0, 24 | 3, 25 | 1 26 | ), 27 | Arguments.of( 28 | new int[]{0, 1, 2, 3}, 29 | 1, 30 | 3, 31 | 2 32 | ) 33 | ); 34 | } 35 | 36 | @ParameterizedTest 37 | @MethodSource 38 | public void midPivotTest(int[] numbers, int from, int to, int expected) { 39 | int actual = PivotFinder.getMidPivot().findPivot(numbers, from, to); 40 | assertEquals(expected, actual); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /w04/test/gad/simplesort/RandomPivotTest.java: -------------------------------------------------------------------------------- 1 | package gad.simplesort; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | 5 | import java.util.stream.Stream; 6 | 7 | import org.junit.jupiter.params.ParameterizedTest; 8 | import org.junit.jupiter.params.provider.Arguments; 9 | import org.junit.jupiter.params.provider.MethodSource; 10 | 11 | public class RandomPivotTest { 12 | 13 | private static Stream randomPivotTest() { 14 | return Stream.of( 15 | Arguments.of( 16 | new int[]{0, 1, 2, 3, 4}, 17 | 0, 18 | 4, 19 | 0 20 | ), 21 | Arguments.of( 22 | new int[]{0, 1, 2, 3}, 23 | 0, 24 | 3, 25 | 1 26 | ), 27 | Arguments.of( 28 | new int[]{0, 1, 2, 3}, 29 | 1, 30 | 3, 31 | 3 32 | ) 33 | ); 34 | } 35 | 36 | @ParameterizedTest 37 | @MethodSource 38 | public void randomPivotTest(int[] numbers, int from, int to, int expected) { 39 | int actual = PivotFinder.getRandomPivot(42).findPivot(numbers, from, to); 40 | assertEquals(expected, actual); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /w10/test/gad/simplehash/FastModuloTest.java: -------------------------------------------------------------------------------- 1 | package gad.simplehash; 2 | 3 | import org.junit.jupiter.params.ParameterizedTest; 4 | import org.junit.jupiter.params.provider.Arguments; 5 | import org.junit.jupiter.params.provider.MethodSource; 6 | 7 | import java.util.stream.Stream; 8 | 9 | import static org.junit.jupiter.api.Assertions.assertEquals; 10 | 11 | public class FastModuloTest { 12 | private static Stream testFastModulo() { 13 | return Stream.of( 14 | Arguments.of(0, 1, 0), 15 | Arguments.of(1, 1, 0), 16 | Arguments.of(2, 1, 0), 17 | Arguments.of(3, 1, 0), 18 | Arguments.of(2, 2, 0), 19 | Arguments.of(3, 2, 1), 20 | Arguments.of(4, 2, 0), 21 | Arguments.of(5, 2, 1), 22 | Arguments.of(4, 4, 0), 23 | Arguments.of(5, 4, 1), 24 | Arguments.of(6, 4, 2), 25 | Arguments.of(7, 4, 3), 26 | Arguments.of(8, 8, 0), 27 | Arguments.of(9, 8, 1), 28 | Arguments.of(10, 8, 2), 29 | Arguments.of(11, 8, 3), 30 | Arguments.of(12, 8, 4), 31 | Arguments.of(13, 8, 5), 32 | Arguments.of(14, 8, 6), 33 | Arguments.of(15, 8, 7) 34 | 35 | ); 36 | } 37 | 38 | @ParameterizedTest 39 | @MethodSource 40 | public void testFastModulo(int i, int divisor, int expect) { 41 | assertEquals(expect, Hashtable.fastModulo(i, divisor)); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /w04/test/gad/simplesort/MedianPivotFrontTest.java: -------------------------------------------------------------------------------- 1 | package gad.simplesort; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | 5 | import java.util.stream.Stream; 6 | 7 | import org.junit.jupiter.params.ParameterizedTest; 8 | import org.junit.jupiter.params.provider.Arguments; 9 | import org.junit.jupiter.params.provider.MethodSource; 10 | 11 | public class MedianPivotFrontTest { 12 | 13 | private static Stream artemisExampleTest() 14 | { 15 | return Stream.of( 16 | Arguments.of( 17 | new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, 18 | 0, 19 | 9, 20 | 3, 21 | 1 22 | ), 23 | Arguments.of( 24 | new int[]{9, 1, 8, 5, 2, 3, 5, 1, 0, 7}, 25 | 0, 26 | 9, 27 | 5, 28 | 3 29 | ), 30 | Arguments.of( 31 | new int[]{9, 1, 8, 5, 2, 3, 5, 1, 0, 7}, 32 | 2, 33 | 8, 34 | 5, 35 | 3 36 | ) 37 | ); 38 | } 39 | 40 | @ParameterizedTest 41 | @MethodSource 42 | public void artemisExampleTest(int[] numbers, int from, int to, int numbersOfConsideredElements, int expected) 43 | { 44 | int actual = PivotFinder.getMedianPivotFront(numbersOfConsideredElements).findPivot(numbers,from,to); 45 | assertEquals(expected,actual); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /w10/test/gad/simplehash/HashTest.java: -------------------------------------------------------------------------------- 1 | package gad.simplehash; 2 | 3 | import org.junit.jupiter.params.ParameterizedTest; 4 | import org.junit.jupiter.params.provider.Arguments; 5 | import org.junit.jupiter.params.provider.MethodSource; 6 | 7 | import java.util.stream.Stream; 8 | 9 | import static org.junit.jupiter.api.Assertions.assertEquals; 10 | 11 | public class HashTest { 12 | private static Stream testHash() { 13 | return Stream.of( 14 | Arguments.of(1, new int[]{1}, "x", 0), 15 | Arguments.of(1, new int[]{1}, "y", 0), 16 | Arguments.of(1, new int[]{1}, "z", 0), 17 | 18 | Arguments.of(2, new int[]{1, 2}, "x", 0), 19 | Arguments.of(2, new int[]{1, 2}, "y", 1), 20 | Arguments.of(2, new int[]{1, 2}, "z", 0), 21 | 22 | Arguments.of(4, new int[]{1, 2}, "x", 0), 23 | Arguments.of(4, new int[]{1, 2}, "y", 1), 24 | Arguments.of(4, new int[]{1, 2}, "z", 2), 25 | 26 | Arguments.of(123, new int[]{123}, "foo", 44), 27 | Arguments.of(123, new int[]{123}, "bar", 119), 28 | 29 | Arguments.of(12345678, new int[]{1, 2, 3, 4, 5, 6, 7, 8}, "Lorem ipsum dolor sit amet", 10625) 30 | 31 | ); 32 | } 33 | 34 | @ParameterizedTest 35 | @MethodSource 36 | public void testHash(int minSize, int[] a, String k, int expect) { 37 | Hashtable hashtable = new Hashtable<>(minSize, a); 38 | 39 | ModuloHelper mH = (i, divisor) -> i % divisor; 40 | 41 | assertEquals(expect, hashtable.h(k, mH)); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /w02/test/gad/binarysearch/SimpleSearchTest.java: -------------------------------------------------------------------------------- 1 | package gad.binarysearch; 2 | 3 | import gad.binarysearch.BinSea; 4 | import gad.binarysearch.StudentResult; 5 | import org.junit.jupiter.api.Assertions; 6 | import org.junit.jupiter.api.Nested; 7 | import org.junit.jupiter.params.ParameterizedTest; 8 | import org.junit.jupiter.params.provider.Arguments; 9 | import org.junit.jupiter.params.provider.MethodSource; 10 | 11 | 12 | import java.util.stream.Stream; 13 | 14 | 15 | @Nested 16 | class SimpleSearchTest { 17 | 18 | static Stream simple() { 19 | return Stream.of( 20 | Arguments.of(new int[]{1, 2, 3, 4}, 1, 0), 21 | Arguments.of(new int[]{1, 2, 3, 4}, 2, 1), 22 | Arguments.of(new int[]{1, 2, 3, 4}, 3, 2), 23 | Arguments.of(new int[]{1, 2, 3, 4}, 4, 3), 24 | Arguments.of(new int[]{6, 23, 45, 67, 89, 90, 100}, 23, 1), 25 | Arguments.of(new int[]{6, 23, 45, 67, 89, 90, 100}, 90, 5) 26 | ); 27 | } 28 | 29 | static Stream valuesNotInArray() { 30 | return Stream.of( 31 | Arguments.of(new int[]{1, 2, 3, 4}, 5, 3), 32 | Arguments.of(new int[]{1, 2, 3, 4}, 0, 0), 33 | Arguments.of(new int[]{1, 2, 3, 4}, -1, 0), 34 | Arguments.of(new int[]{1, 2, 3, 4}, -5, 0), 35 | Arguments.of(new int[]{1, 2}, 0, 0) 36 | ); 37 | } 38 | 39 | static Stream duplicatesInArray() { 40 | return Stream.of( 41 | Arguments.of(new int[]{1, 2, 2, 2, 6, 7, 8, 9, 10}, 2, 1), 42 | Arguments.of(new int[]{1, 2, 2, 2, 6, 7, 8, 9, 10}, 6, 4), 43 | Arguments.of(new int[]{1, 2, 2, 2, 2, 2, 2, 9, 10}, 2, 4) 44 | ); 45 | } 46 | 47 | @ParameterizedTest 48 | @MethodSource({"simple", "valuesNotInArray", "duplicatesInArray"}) 49 | void testSearch(int[] arr, int value, int expectedIdx) { 50 | Assertions.assertEquals(expectedIdx, BinSea.search(arr, value, new StudentResult())); 51 | } 52 | } 53 | 54 | 55 | -------------------------------------------------------------------------------- /w02/test/gad/binarysearch/IntervalSearchTest.java: -------------------------------------------------------------------------------- 1 | package gad.binarysearch; 2 | 3 | import gad.binarysearch.BinSea; 4 | import gad.binarysearch.Interval; 5 | import gad.binarysearch.StudentResult; 6 | import org.junit.jupiter.params.ParameterizedTest; 7 | import org.junit.jupiter.params.provider.Arguments; 8 | import org.junit.jupiter.params.provider.MethodSource; 9 | 10 | import java.util.stream.Stream; 11 | 12 | import static org.junit.jupiter.api.Assertions.assertEquals; 13 | 14 | 15 | class IntervalSearchTest { 16 | 17 | static Stream random() { 18 | return Stream.of( 19 | Arguments.of( 20 | new int[]{1, 4, 4, 4, 5, 5, 5, 5, 8}, 21 | Interval.NonEmptyInterval.fromArrayIndices(1, 4), 22 | Interval.fromArrayIndices(0, 3)), 23 | Arguments.of( 24 | new int[]{1, 4, 4, 4, 5, 5, 5, 5, 8}, 25 | Interval.NonEmptyInterval.fromArrayIndices(2, 5), 26 | Interval.fromArrayIndices(1, 7)), 27 | Arguments.of( 28 | new int[]{1, 4, 4, 4, 5, 5, 5, 5, 8, 10, 23}, 29 | Interval.NonEmptyInterval.fromArrayIndices(3, 9), 30 | Interval.fromArrayIndices(1, 8)), 31 | Arguments.of( 32 | new int[]{1, 4, 4, 4, 5, 5, 5, 5, 8, 10, 23}, 33 | Interval.NonEmptyInterval.fromArrayIndices(8, 8), 34 | Interval.fromArrayIndices(8, 8)) 35 | ); 36 | } 37 | 38 | static Stream empty() { 39 | return Stream.of( 40 | Arguments.of( 41 | new int[]{1, 4, 4, 4, 5, 5, 5, 5, 8}, 42 | Interval.NonEmptyInterval.fromArrayIndices(6, 7), 43 | Interval.EmptyInterval.getEmptyInterval()), 44 | Arguments.of( 45 | new int[]{1, 4, 4, 4, 5, 5, 5, 5, 8}, 46 | Interval.NonEmptyInterval.fromArrayIndices(10, 16), 47 | Interval.EmptyInterval.getEmptyInterval()), 48 | Arguments.of( 49 | new int[]{1, 4, 4, 4, 5, 5, 5, 5, 8, 2325}, 50 | Interval.NonEmptyInterval.fromArrayIndices(235, 2311), 51 | Interval.EmptyInterval.getEmptyInterval()) 52 | ); 53 | } 54 | 55 | @ParameterizedTest 56 | @MethodSource({"random", "empty"}) 57 | void testSearch(int[] arr, Interval.NonEmptyInterval interval, Interval expectedInterval) { 58 | 59 | var resIntervall = BinSea.search(arr, interval, new StudentResult(), new StudentResult()); 60 | 61 | assertEquals(expectedInterval, resIntervall); 62 | } 63 | 64 | } -------------------------------------------------------------------------------- /w02/test/gad/logging/TestResultImplementation.java: -------------------------------------------------------------------------------- 1 | package gad.logging; 2 | 3 | import gad.binarysearch.Result; 4 | 5 | import java.util.Arrays; 6 | import java.util.stream.Collectors; 7 | 8 | import static org.junit.jupiter.api.Assertions.*; 9 | 10 | public class TestResultImplementation implements Result { 11 | 12 | private final int[] steps; 13 | private int index; 14 | 15 | public TestResultImplementation(int[][] possibleSolutions) { 16 | this(Arrays.stream(possibleSolutions) 17 | .mapToInt(s -> s.length) 18 | .max().orElse(0)); 19 | } 20 | public TestResultImplementation(int expectedSize) { 21 | steps = new int[expectedSize]; 22 | } 23 | 24 | @Override 25 | public void addStep(int step) { 26 | System.out.println("added step to index " + step); 27 | if (index < steps.length) { 28 | steps[index] = step; 29 | } 30 | index++; 31 | } 32 | 33 | public void testLogging(int[] solution) { 34 | assertEquals(solution.length, index, "Number of logged steps wrong"); 35 | 36 | assertArrayEquals(solution, steps, "Logged wrong path"); 37 | } 38 | 39 | public void testLogging(int[][] possibleSolutions) { 40 | testLogging(possibleSolutions, ""); 41 | } 42 | public void testLogging(int[][] possibleSolutions, String messagePrefix) { 43 | assertTrue(Arrays.stream(possibleSolutions).anyMatch( 44 | solution -> index == solution.length && testArrayEquals(solution, steps) 45 | ), 46 | messagePrefix + "steps did not match any valid solution (possibilities are: " + 47 | Arrays.stream(possibleSolutions) 48 | .map(Arrays::toString) 49 | .collect(Collectors.joining(" or ")) + 50 | ")"); 51 | } 52 | 53 | /** 54 | * assumes arrays have same length 55 | * @param a first array 56 | * @param b second array 57 | * @return whether array a and b are equal 58 | */ 59 | private boolean testArrayEquals(int[] a, int[] b) { 60 | for (int i = 0; i < a.length; i++) { 61 | if(a[i] != b[i]) { 62 | return false; 63 | } 64 | } 65 | return true; 66 | } 67 | 68 | } -------------------------------------------------------------------------------- /w05/test/gad/radixsort/RadixSortTest.java: -------------------------------------------------------------------------------- 1 | package gad.radixsort; 2 | 3 | import gad.radix.RadixSort; 4 | import org.junit.jupiter.params.ParameterizedTest; 5 | import org.junit.jupiter.params.provider.Arguments; 6 | import org.junit.jupiter.params.provider.MethodSource; 7 | 8 | import java.util.stream.Stream; 9 | 10 | import static org.junit.jupiter.api.Assertions.assertEquals; 11 | 12 | public class RadixSortTest { 13 | 14 | @ParameterizedTest 15 | @MethodSource("provideMaxDecimalPlaces") 16 | void testMaxDecimalPlaces(int[] elements, int expectedMax) { 17 | assertEquals(expectedMax, RadixSort.getMaxDecimalPlaces(elements)); 18 | } 19 | 20 | private static Stream provideMaxDecimalPlaces() { 21 | return Stream.of( 22 | Arguments.of(new int[]{1, 2, 5675, 222222, 930}, 6), 23 | Arguments.of(new int[]{1, 2, 5675990, -222222, 930}, 7), 24 | Arguments.of(new int[]{0, -1, 0, 0}, 1), 25 | Arguments.of(new int[]{0, -889, 0, 0}, 3), 26 | Arguments.of(new int[]{Integer.MAX_VALUE, Integer.MIN_VALUE, 0}, 10), 27 | Arguments.of(new int[]{1, 2, 5675, -222222, 930, -1, 999999, 1000000}, 7) 28 | ); 29 | } 30 | 31 | @ParameterizedTest 32 | @MethodSource("provideKey") 33 | void testKey(int element, int decimalPlace, int expected) { 34 | assertEquals(expected, RadixSort.key(element, decimalPlace)); 35 | } 36 | 37 | private static Stream provideKey() { 38 | return Stream.of( 39 | Arguments.of(1234, 3, 1), 40 | Arguments.of(981739, 2, 7), 41 | Arguments.of(439183, 0, 3), 42 | Arguments.of(0, 99, 0), 43 | //Edge-Cases 44 | Arguments.of(6, 0, 6), 45 | Arguments.of(0, 0, 0) 46 | ); 47 | } 48 | 49 | @ParameterizedTest 50 | @MethodSource("provideKeyInvalidIndex") 51 | void testKeyWithInvalidIndex(int element, int decimalPlace, int expected) { 52 | assertEquals(expected, RadixSort.key(element, decimalPlace)); 53 | } 54 | 55 | private static Stream provideKeyInvalidIndex() { 56 | return Stream.of( 57 | Arguments.of(12345, 5, 0), 58 | Arguments.of(987654321, 10, 0), 59 | Arguments.of(0, 0, 0) 60 | ); 61 | } 62 | } -------------------------------------------------------------------------------- /w03/test/gad/dynamicarray/DynamicStackTest.java: -------------------------------------------------------------------------------- 1 | package gad.dynamicarray; 2 | 3 | import gad.dynamicarray.DynamicStack; 4 | import gad.dynamicarray.StudentResult; 5 | import org.junit.jupiter.api.Test; 6 | 7 | import static org.junit.jupiter.api.Assertions.assertEquals; 8 | 9 | public class DynamicStackTest { 10 | 11 | void checkStack(DynamicStack ds, int[] elementsShould, String... errorMsg) throws NoSuchFieldException, 12 | IllegalAccessException { 13 | ArrayHelper.checkUnderlyingArray(elementsShould, ds, errorMsg); 14 | } 15 | 16 | 17 | @Test 18 | void artemisTest() throws NoSuchFieldException, IllegalAccessException { 19 | var ds = new DynamicStack(3, 4, new StudentResult()); 20 | 21 | checkStack(ds, new int[]{}, "Stack should initially be empty"); 22 | 23 | ds.pushBack(1); 24 | checkStack(ds, new int[]{1, 0, 0}, "just pushed 1"); 25 | 26 | ds.pushBack(2); 27 | checkStack(ds, new int[]{1, 2, 0}, "just pushed 2"); 28 | 29 | ds.pushBack(3); 30 | checkStack(ds, new int[]{1, 2, 3}, "just pushed 3"); 31 | 32 | ds.pushBack(4); 33 | checkStack(ds, new int[]{1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0}, "just pushed 4 stack should grow"); 34 | 35 | assertEquals(4, ds.popBack()); 36 | checkStack(ds, new int[]{1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0}, "pop should not change the stack"); 37 | 38 | ds.pushBack(5); 39 | checkStack(ds, new int[]{1, 2, 3, 5, 0, 0, 0, 0, 0, 0, 0, 0}, "just pushed 5"); 40 | assertEquals(5, ds.popBack()); 41 | 42 | checkStack(ds, new int[]{1, 2, 3, 5, 0, 0, 0, 0, 0, 0, 0, 0}, "pop Should not change the stack"); 43 | 44 | assertEquals(3, ds.popBack()); 45 | checkStack(ds, new int[]{1, 2, 0, 0, 0, 0}, "Stack should shrink and only copy the needed elements"); 46 | 47 | } 48 | 49 | 50 | @Test 51 | void addAndRemove() throws NoSuchFieldException, IllegalAccessException { 52 | var ds = new DynamicStack(3, 4, new StudentResult()); 53 | 54 | for (int i = 0; i < 10; i++) { 55 | ds.pushBack(i); 56 | } 57 | 58 | checkStack(ds, new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0}, "just pushed 10 elements"); 59 | 60 | for (int i = 10 - 1; i >= 0; i--) { 61 | assertEquals(i, ds.popBack()); 62 | } 63 | 64 | checkStack(ds, new int[]{}, "removed all elements"); 65 | } 66 | } -------------------------------------------------------------------------------- /w01/test/gad/logging/TestLogging.java: -------------------------------------------------------------------------------- 1 | package gad.logging; 2 | 3 | import gad.ExampleResultImplementation; 4 | import gad.maze.Maze; 5 | import gad.maze.Walker; 6 | import org.junit.jupiter.api.Test; 7 | import org.junit.jupiter.params.ParameterizedTest; 8 | import org.junit.jupiter.params.provider.Arguments; 9 | import org.junit.jupiter.params.provider.MethodSource; 10 | 11 | import java.util.stream.Stream; 12 | 13 | import static org.junit.jupiter.api.Assertions.*; 14 | 15 | public class TestLogging { 16 | 17 | @ParameterizedTest 18 | @MethodSource 19 | public void checkLogging(int width, int height, int seed, int[] solutionX, int[] solutionY, boolean expect) { 20 | boolean[][] maze = Maze.generateMaze(width, height, seed); 21 | 22 | ExampleResultImplementation result = new ExampleResultImplementation(solutionX.length); 23 | Walker walker = new Walker(maze, result); 24 | assertEquals(expect, walker.walk(), "Walker should" + (expect ? "" : " not") + " find exit"); 25 | result.testLogging(solutionX, solutionY); 26 | } 27 | 28 | private static Stream checkLogging() { 29 | return Stream.of( 30 | Arguments.of( 31 | 10, 10, 0, 32 | new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 3, 4, 4, 4, 3, 2, 1, 2, 3, 4, 4, 5, 6, 6, 7, 8, 9 }, 33 | new int[] { 0, 1, 2, 3, 4, 5, 6, 5, 5, 5, 6, 6, 7, 8, 8, 8, 8, 8, 8, 8, 7, 7, 7, 8, 8, 8, 8 }, 34 | true 35 | ), 36 | Arguments.of( 37 | 3,3,0, 38 | new int[] { 1, 1, 2 }, 39 | new int[] { 0, 1, 1 }, 40 | true 41 | ), 42 | Arguments.of( 43 | 4,4,1, 44 | new int[] { 1, 1, 1, 2, 3 }, 45 | new int[] { 0, 1, 2, 2, 2 }, 46 | true 47 | ), 48 | Arguments.of( 49 | 10,10,1, 50 | new int[] { 1, 1, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 1 }, 51 | new int[] { 0, 1, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 0 }, 52 | false 53 | ), 54 | Arguments.of( 55 | 10,10,7, 56 | new int[] { 1, 1, 1 }, 57 | new int[] { 0, 1, 0 }, 58 | false 59 | ) 60 | //, More Arguments here 61 | );} 62 | } 63 | -------------------------------------------------------------------------------- /w04/test/gad/simplesort/QuicksortTest.java: -------------------------------------------------------------------------------- 1 | package gad.simplesort; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 4 | 5 | import java.util.stream.Stream; 6 | 7 | import org.junit.jupiter.params.ParameterizedTest; 8 | import org.junit.jupiter.params.provider.Arguments; 9 | import org.junit.jupiter.params.provider.MethodSource; 10 | 11 | public class QuicksortTest { 12 | 13 | private static Stream quickSortTest() { 14 | return Stream.of( 15 | Arguments.of( 16 | new int[]{2, 4, 1, 9, 10}, 17 | new int[]{1, 2, 4, 9, 10} 18 | ), 19 | Arguments.of( 20 | new int[]{2, 4, 1, 9, 10, 11, 12, 13, 14, 15, 26, 37, 45}, 21 | new int[]{1, 2, 4, 9, 10, 11, 12, 13, 14, 15, 26, 37, 45} 22 | ), 23 | Arguments.of( 24 | new int[]{44, 23, 2, 4, 1, 99, 9, 10, 11, 12, 13, 14, 15, 26, 37, 45}, 25 | new int[]{1, 2, 4, 9, 10, 11, 12, 13, 14, 15, 23, 26, 37, 44, 45, 99} 26 | ), 27 | Arguments.of( 28 | new int[]{11, 21, 30, 4, 15, 1, 26, 13, 2, 29, 12, 10, 17, 22, 25, 24, 18, 28, 6, 14, 16, 19, 3, 5, 7, 27, 8, 9, 20, 23, 0}, 29 | new int[]{0, 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} 30 | ), 31 | Arguments.of( 32 | new int[]{11, 21, 30, 4, 15, 1, 26, 13, 2, 29, 12, 10, 17, 22, 25, 24, 18, 28, 6, 14, 16, 19, 3, 5, 7, 27, 8, 9, 20, 23, 0}, 33 | new int[]{0, 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} 34 | ), 35 | Arguments.of( 36 | new int[]{5, 6, 5, 6, 5, 6, 55, 6, 5, 6, 5, 6, 55, 6, 5, 6, 5, 6, 55, 6, 5, 6, 5, 6, 5}, 37 | new int[]{5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 55, 55, 55} 38 | ), 39 | Arguments.of( 40 | new int[]{30, 25, 20, 15, 14, 10, 5, 1, 0, -5, -20, -100}, 41 | new int[]{-100, -20, -5, 0, 1, 5, 10, 14, 15, 20, 25, 30} 42 | ) 43 | ); 44 | } 45 | 46 | @ParameterizedTest 47 | @MethodSource 48 | public void quickSortTest(int[] numbers, int[] expected) { 49 | Quicksort quicksort = new Quicksort(PivotFinder.getLastPivot(), 5); 50 | StudentResult result = new StudentResult(); 51 | 52 | quicksort.sort(numbers, result, 0, numbers.length - 1); 53 | assertArrayEquals(expected, numbers); 54 | } 55 | } -------------------------------------------------------------------------------- /w03/test/gad/dynamicarray/array/DynamicArrayTest.java: -------------------------------------------------------------------------------- 1 | package gad.dynamicarray.array; 2 | 3 | import gad.dynamicarray.Interval; 4 | import org.junit.jupiter.api.Test; 5 | import org.junit.jupiter.api.DisplayName; 6 | import gad.dynamicarray.DynamicArray; 7 | import gad.dynamicarray.Interval.EmptyInterval; 8 | import gad.dynamicarray.Interval.NonEmptyInterval; 9 | import org.junit.jupiter.api.function.Executable; 10 | 11 | import static org.junit.jupiter.api.Assertions.assertEquals; 12 | import static org.junit.jupiter.api.Assertions.assertThrows; 13 | 14 | public class DynamicArrayTest { 15 | @Test 16 | @DisplayName("Artemis Example") 17 | void testArtemis() { 18 | DynamicArray array = new DynamicArray(3, 4); 19 | assertEquals(Interval.EmptyInterval.getEmptyInterval(), array.reportUsage(EmptyInterval.getEmptyInterval(), 1)); 20 | assertEquals("[0, 0, 0]", array.toString()); 21 | 22 | array.set(2, 1); 23 | assertEquals("[0, 0, 1]", array.toString()); 24 | assertEquals(0, array.get(1)); 25 | 26 | array.set(0, 3); 27 | assertEquals("[3, 0, 1]", array.toString()); 28 | 29 | assertEquals(new NonEmptyInterval(0, 1), array.reportUsage(new NonEmptyInterval(1,2), 4)); 30 | assertEquals("[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]", array.toString()); 31 | 32 | assertEquals(new NonEmptyInterval(3, 6), array.reportUsage(new NonEmptyInterval(3,6), 4)); 33 | assertEquals("[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]", array.toString()); 34 | 35 | assertEquals(new NonEmptyInterval(0,0), array.reportUsage(new NonEmptyInterval(1,1), 1)); 36 | assertEquals("[1, 0, 0]", array.toString()); 37 | 38 | assertEquals(EmptyInterval.getEmptyInterval(), array.reportUsage(EmptyInterval.getEmptyInterval(), 2)); 39 | assertEquals("[1, 0, 0]", array.toString()); 40 | 41 | assertEquals(new NonEmptyInterval(0, 1), array.reportUsage(new NonEmptyInterval(2,0), 4)); 42 | assertEquals("[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]", array.toString()); 43 | 44 | assertEquals(new NonEmptyInterval(5, 1), array.reportUsage(new NonEmptyInterval(5,1), 9)); 45 | assertEquals("[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]", array.toString()); 46 | 47 | assertEquals(EmptyInterval.getEmptyInterval(), array.reportUsage(EmptyInterval.getEmptyInterval(), 0)); 48 | assertEquals("[]", array.toString()); 49 | } 50 | 51 | @Test 52 | @DisplayName("Throws IllegalArgumentException") 53 | void throwsTest() { 54 | assertThrows(IllegalArgumentException.class, () -> new DynamicArray(0, 5)); 55 | assertThrows(IllegalArgumentException.class, () -> new DynamicArray(5, -5)); 56 | assertThrows(IllegalArgumentException.class, () -> new DynamicArray(2, 1)); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /w03/test/gad/dynamicarray/DynamicArrayTest.java: -------------------------------------------------------------------------------- 1 | package gad.dynamicarray; 2 | 3 | import org.junit.jupiter.params.ParameterizedTest; 4 | import org.junit.jupiter.params.provider.Arguments; 5 | import org.junit.jupiter.params.provider.MethodSource; 6 | 7 | import java.util.Arrays; 8 | import java.util.stream.Stream; 9 | 10 | import static org.junit.jupiter.api.Assertions.*; 11 | 12 | class DynamicArrayTest { 13 | 14 | static DynamicArray getArtemisArray() { 15 | DynamicArray array = new DynamicArray(3, 4); 16 | array.reportUsage(Interval.EmptyInterval.getEmptyInterval(), 1); 17 | array.set(2, 1); 18 | array.set(0, 3); 19 | return array; 20 | } 21 | 22 | static Stream artemisTestArguments() { 23 | DynamicArray array = getArtemisArray(); 24 | return Stream.of( 25 | Arguments.of(array, new Interval.NonEmptyInterval(1, 2), 4, new Interval.NonEmptyInterval(0, 1), new int[]{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}), 26 | Arguments.of(array, new Interval.NonEmptyInterval(3, 6), 4, new Interval.NonEmptyInterval(3, 6), new int[]{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}), 27 | Arguments.of(array, new Interval.NonEmptyInterval(1, 1), 1, new Interval.NonEmptyInterval(0, 0), new int[]{1, 0, 0}), 28 | Arguments.of(array, Interval.EmptyInterval.getEmptyInterval(), 2, Interval.EmptyInterval.getEmptyInterval(), new int[]{1, 0, 0}), 29 | Arguments.of(array, new Interval.NonEmptyInterval(2, 0), 4, new Interval.NonEmptyInterval(0, 1), new int[]{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}), 30 | Arguments.of(array, new Interval.NonEmptyInterval(5, 1), 9, new Interval.NonEmptyInterval(5, 1), new int[]{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}), 31 | Arguments.of(array, Interval.EmptyInterval.getEmptyInterval(), 0, Interval.EmptyInterval.getEmptyInterval(), new int[0]) 32 | ); 33 | } 34 | static Stream illegalArgumentsExceptionTest() { 35 | return Stream.of( 36 | Arguments.of(0,1, IllegalArgumentException.class), 37 | Arguments.of(4,-3, IllegalArgumentException.class), 38 | Arguments.of(9,2, IllegalArgumentException.class) 39 | ); 40 | } 41 | 42 | @ParameterizedTest 43 | @MethodSource({"artemisTestArguments"}) 44 | void artemisTest(DynamicArray array, Interval interval, int minSize, Interval expectedInterval, int[] exptectedArray) { 45 | assertEquals(expectedInterval, array.reportUsage(interval, minSize)); 46 | assertEquals(Arrays.toString(exptectedArray), array.toString()); 47 | } 48 | 49 | @ParameterizedTest 50 | @MethodSource({"illegalArgumentsExceptionTest"}) 51 | void illegalArgumentExceptionTest(int growthFactor, int maxOverhead, Class exception) 52 | { 53 | assertThrows(exception, () -> new DynamicArray(growthFactor,maxOverhead)); 54 | } 55 | } -------------------------------------------------------------------------------- /w07/test/gad/binomilia/BinomialTreeNodeTest.java: -------------------------------------------------------------------------------- 1 | package gad.binomilia; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.junit.jupiter.params.ParameterizedTest; 5 | import org.junit.jupiter.params.provider.Arguments; 6 | import org.junit.jupiter.params.provider.MethodSource; 7 | 8 | import java.util.stream.Stream; 9 | 10 | import static org.junit.jupiter.api.Assertions.assertEquals; 11 | 12 | public class BinomialTreeNodeTest { 13 | 14 | private static Stream simpleTest() 15 | { 16 | return Stream.of( 17 | Arguments.of(305818196), 18 | Arguments.of(2002281049), 19 | Arguments.of(-858398579), 20 | Arguments.of(1834927768), 21 | Arguments.of(1544841234), 22 | Arguments.of(1442756015), 23 | Arguments.of(1235069899), 24 | Arguments.of(-960568800), 25 | Arguments.of(-253372286), 26 | Arguments.of(1042475511) 27 | 28 | ); 29 | } 30 | 31 | @ParameterizedTest 32 | @MethodSource("simpleTest") 33 | void minSimpleTest(int i) 34 | { 35 | BinomialTreeNode treeNode = new BinomialTreeNode(i); 36 | assertEquals(i,treeNode.min()); 37 | } 38 | 39 | @ParameterizedTest 40 | @MethodSource("simpleTest") 41 | void rankSimpleTest(int i) 42 | { 43 | BinomialTreeNode treeNode = new BinomialTreeNode(i); 44 | assertEquals(0,treeNode.rank()); 45 | } 46 | 47 | @Test 48 | void mergeTest() 49 | { 50 | BinomialTreeNode treeNode1 = new BinomialTreeNode(50); 51 | BinomialTreeNode treeNode2 = new BinomialTreeNode(69); 52 | 53 | assertEquals(treeNode1, BinomialTreeNode.merge(treeNode1, treeNode2), "You returned the wrong node!"); 54 | assertEquals(1, treeNode1.rank()); 55 | assertEquals(50, treeNode1.min()); 56 | 57 | BinomialTreeNode treeNode3 = new BinomialTreeNode(12); 58 | BinomialTreeNode treeNode4 = new BinomialTreeNode(420); 59 | assertEquals(treeNode3, BinomialTreeNode.merge(treeNode4, treeNode3), "You returned the wrong node!"); 60 | assertEquals(1, treeNode3.rank()); 61 | assertEquals(12, treeNode3.min()); 62 | assertEquals(treeNode3, BinomialTreeNode.merge(treeNode1, treeNode3), "You returned the wrong node!"); 63 | assertEquals(2, treeNode3.rank()); 64 | assertEquals(12, treeNode3.min()); 65 | 66 | BinomialTreeNode bigTreeNode = BinomialTreeNode.merge( 67 | BinomialTreeNode.merge(new BinomialTreeNode(911), new BinomialTreeNode(133)), 68 | BinomialTreeNode.merge(new BinomialTreeNode(18), new BinomialTreeNode(3)) 69 | ); 70 | assertEquals(3, bigTreeNode.min()); 71 | assertEquals(2, bigTreeNode.rank()); 72 | 73 | assertEquals(bigTreeNode, BinomialTreeNode.merge(bigTreeNode, treeNode3)); 74 | assertEquals(3, bigTreeNode.min()); 75 | assertEquals(3, bigTreeNode.rank()); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /w01/test/gad/maze/TestExecutor.java: -------------------------------------------------------------------------------- 1 | package gad.maze; 2 | 3 | import java.nio.file.Paths; 4 | import java.io.IOException; 5 | import java.nio.file.Files; 6 | import java.util.Arrays; 7 | import java.util.regex.Pattern; 8 | import java.util.stream.Collectors; 9 | import java.util.stream.Stream; 10 | 11 | import org.junit.jupiter.api.DisplayName; 12 | import org.junit.jupiter.api.Test; 13 | import org.junit.jupiter.params.ParameterizedTest; 14 | import org.junit.jupiter.params.provider.Arguments; 15 | import org.junit.jupiter.params.provider.MethodSource; 16 | 17 | import static org.junit.jupiter.api.Assertions.assertEquals; 18 | import static org.junit.jupiter.params.provider.Arguments.arguments; 19 | 20 | public class TestExecutor { 21 | public static String PATH = "test/gad/maze/cases.txt"; 22 | 23 | private static class TestResult implements Result { 24 | public void addLocation(int x, int y) { 25 | // TODO USE THIS 26 | } 27 | } 28 | 29 | @Test 30 | @DisplayName("This is a green test to motivate you for the semester! <3") 31 | public void greenTest() throws IOException { 32 | // Green Test 33 | } 34 | 35 | @ParameterizedTest 36 | @MethodSource 37 | void testMaze(int[] maze_data, boolean exit_expect) { 38 | var maze = Maze.generateMaze(maze_data[0], maze_data[1], maze_data[2]); 39 | var result = new TestResult(); 40 | var walker = new Walker(maze, result); 41 | 42 | assertEquals(exit_expect, walker.walk(), exit_expect 43 | ? "Expected to find exit, found none." 44 | : "Expected not to find any exit, but apparently you found one!?"); 45 | } 46 | 47 | /** 48 | * # TEST CASE FORMAT 49 | * 50 | * ``` 51 | * width height seed 52 | * should fine exit? 53 | * ``` 54 | * 55 | * @return 56 | * @throws IOException 57 | */ 58 | private static Arguments getTestData(String data) { 59 | var match = Pattern.compile("(\\d+ \\d+ \\d+)\nexit: (true|false)").matcher(data); 60 | 61 | // Fix "No Match Found" 62 | // Citation: https://stackoverflow.com/a/5674321/16002144 63 | match.matches(); 64 | 65 | var maze_data = Arrays.stream(match.group(1).split(" ")).mapToInt(Integer::parseInt).toArray(); 66 | var can_exit = Boolean.parseBoolean(match.group(2)); 67 | 68 | return arguments(maze_data, can_exit); 69 | } 70 | 71 | private static Stream testMaze() throws IOException { 72 | // The following gets the test case file, erases comments, then splits with the 73 | // special test case separator. Then it is converted to a stream and the test 74 | // data is mapped. 75 | 76 | return Arrays.stream(Files.lines(Paths.get(PATH)) 77 | .filter(i -> !i.startsWith("#")) 78 | .filter(i -> i.length() > 0) 79 | .collect(Collectors.joining("\n")) 80 | .split("\n=====\n")).map(TestExecutor::getTestData); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /w06/test/gad/sortingHeaps/IndexCalcTest.java: -------------------------------------------------------------------------------- 1 | package gad.sortingHeaps; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | 5 | import org.junit.jupiter.api.BeforeEach; 6 | import org.junit.jupiter.api.Test; 7 | 8 | public class IndexCalcTest { 9 | Result result; 10 | 11 | @BeforeEach 12 | void beforeEach() { 13 | result = new StudentResult(); 14 | } 15 | 16 | @Test 17 | public void testGetParentIndexBinaryHeap() { 18 | NaryHeap binaryHeap = new NaryHeap(new int[] { 0, 1, 2, 3, 4, 5, 6 }, 2, result); 19 | assertEquals(0, binaryHeap.getParentIndex(1)); 20 | assertEquals(0, binaryHeap.getParentIndex(2)); 21 | assertEquals(1, binaryHeap.getParentIndex(3)); 22 | assertEquals(1, binaryHeap.getParentIndex(4)); 23 | assertEquals(2, binaryHeap.getParentIndex(5)); 24 | assertEquals(2, binaryHeap.getParentIndex(6)); 25 | } 26 | 27 | @Test 28 | public void testGetParentIndexTernaryHeap() { 29 | NaryHeap ternaryHeap = new NaryHeap(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }, 3, result); 30 | assertEquals(0, ternaryHeap.getParentIndex(1)); 31 | assertEquals(0, ternaryHeap.getParentIndex(2)); 32 | assertEquals(0, ternaryHeap.getParentIndex(3)); 33 | assertEquals(1, ternaryHeap.getParentIndex(4)); 34 | assertEquals(1, ternaryHeap.getParentIndex(5)); 35 | assertEquals(1, ternaryHeap.getParentIndex(6)); 36 | assertEquals(2, ternaryHeap.getParentIndex(7)); 37 | assertEquals(2, ternaryHeap.getParentIndex(8)); 38 | assertEquals(2, ternaryHeap.getParentIndex(9)); 39 | assertEquals(3, ternaryHeap.getParentIndex(10)); 40 | assertEquals(3, ternaryHeap.getParentIndex(11)); 41 | assertEquals(3, ternaryHeap.getParentIndex(12)); 42 | } 43 | 44 | @Test 45 | public void testGetChildIndexBinaryHeap() { 46 | NaryHeap binaryHeap = new NaryHeap(new int[] { 0, 1, 2, 3, 4, 5, 6 }, 2, result); 47 | assertEquals(1, binaryHeap.getChildIndex(0, 0)); 48 | assertEquals(2, binaryHeap.getChildIndex(1, 0)); 49 | assertEquals(3, binaryHeap.getChildIndex(0, 1)); 50 | assertEquals(4, binaryHeap.getChildIndex(1, 1)); 51 | assertEquals(5, binaryHeap.getChildIndex(0, 2)); 52 | assertEquals(6, binaryHeap.getChildIndex(1, 2)); 53 | } 54 | 55 | @Test 56 | public void testGetChildIndexTernaryHeap() { 57 | NaryHeap ternaryHeap = new NaryHeap(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }, 3, result); 58 | assertEquals(1, ternaryHeap.getChildIndex(0, 0)); 59 | assertEquals(2, ternaryHeap.getChildIndex(1, 0)); 60 | assertEquals(3, ternaryHeap.getChildIndex(2, 0)); 61 | assertEquals(4, ternaryHeap.getChildIndex(0, 1)); 62 | assertEquals(5, ternaryHeap.getChildIndex(1, 1)); 63 | assertEquals(6, ternaryHeap.getChildIndex(2, 1)); 64 | assertEquals(7, ternaryHeap.getChildIndex(0, 2)); 65 | assertEquals(8, ternaryHeap.getChildIndex(1, 2)); 66 | assertEquals(9, ternaryHeap.getChildIndex(2, 2)); 67 | assertEquals(10, ternaryHeap.getChildIndex(0, 3)); 68 | assertEquals(11, ternaryHeap.getChildIndex(1, 3)); 69 | assertEquals(12, ternaryHeap.getChildIndex(2, 3)); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /w04/test/gad/simplesort/MergesortTest.java: -------------------------------------------------------------------------------- 1 | package gad.simplesort; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 4 | 5 | import java.util.stream.Stream; 6 | 7 | import org.junit.jupiter.api.Test; 8 | import org.junit.jupiter.params.ParameterizedTest; 9 | import org.junit.jupiter.params.provider.Arguments; 10 | import org.junit.jupiter.params.provider.MethodSource; 11 | 12 | public class MergesortTest { 13 | 14 | private static Stream mergeSortTest() { 15 | return Stream.of( 16 | Arguments.of( 17 | new int[]{2, 4, 1, 9, 10}, 18 | new int[]{1, 2, 4, 9, 10} 19 | ), 20 | Arguments.of( 21 | new int[]{2, 4, 1, 9, 10, 11, 12, 13, 14, 15, 26, 37, 45}, 22 | new int[]{1, 2, 4, 9, 10, 11, 12, 13, 14, 15, 26, 37, 45} 23 | ), 24 | Arguments.of( 25 | new int[]{44, 23, 2, 4, 1, 99, 9, 10, 11, 12, 13, 14, 15, 26, 37, 45}, 26 | new int[]{1, 2, 4, 9, 10, 11, 12, 13, 14, 15, 23, 26, 37, 44, 45, 99} 27 | ), 28 | Arguments.of( 29 | new int[]{11, 21, 30, 4, 15, 1, 26, 13, 2, 29, 12, 10, 17, 22, 25, 24, 18, 28, 6, 14, 16, 19, 3, 5, 7, 27, 8, 9, 20, 23, 0}, 30 | new int[]{0, 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 | Arguments.of( 33 | new int[]{11, 21, 30, 4, 15, 1, 26, 13, 2, 29, 12, 10, 17, 22, 25, 24, 18, 28, 6, 14, 16, 19, 3, 5, 7, 27, 8, 9, 20, 23, 0}, 34 | new int[]{0, 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} 35 | ), 36 | Arguments.of( 37 | new int[]{5, 6, 5, 6, 5, 6, 55, 6, 5, 6, 5, 6, 55, 6, 5, 6, 5, 6, 55, 6, 5, 6, 5, 6, 5}, 38 | new int[]{5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 55, 55, 55} 39 | ), 40 | Arguments.of( 41 | new int[]{30, 25, 20, 15, 14, 10, 5, 1, 0, -5, -20, -100}, 42 | new int[]{-100, -20, -5, 0, 1, 5, 10, 14, 15, 20, 25, 30} 43 | ) 44 | ); 45 | } 46 | 47 | @ParameterizedTest 48 | @MethodSource 49 | public void mergeSortTest(int[] numbers, int[] expected) { 50 | Mergesort mergesort = new Mergesort(5); 51 | StudentResult result = new StudentResult(); 52 | 53 | mergesort.sort(numbers, result, 0, numbers.length - 1); 54 | assertArrayEquals(expected, numbers); 55 | 56 | } 57 | 58 | @Test 59 | void mergeSortCompilerTest() { 60 | Mergesort mergesort = new Mergesort(0); 61 | StudentResult studentResult = new StudentResult(); 62 | // Tests for public access and correctness of parameters 63 | // This is a compiler-test, your compiler will tell you 64 | // if there's something wrong with your code before you can even run the test 65 | mergesort.sort(new int[]{1}, studentResult); 66 | mergesort.sort(new int[]{1}, studentResult, 0, 0); 67 | mergesort.sort(new int[]{1}, studentResult, 0, 0, new int[]{1, 2}); 68 | } 69 | } -------------------------------------------------------------------------------- /w03/test/gad/dynamicarray/StackyQueueTest.java: -------------------------------------------------------------------------------- 1 | package gad.dynamicarray; 2 | 3 | import gad.dynamicarray.StackyQueue; 4 | import gad.dynamicarray.StudentResult; 5 | import org.junit.jupiter.api.Test; 6 | 7 | import static org.junit.jupiter.api.Assertions.assertEquals; 8 | 9 | public class StackyQueueTest { 10 | 11 | void checkQueueFirst(StackyQueue sq, int[] elementsShould1, String... errorMsg) throws NoSuchFieldException, 12 | IllegalAccessException { 13 | ArrayHelper.checkUnderlyingArrayFirst(elementsShould1, sq, errorMsg); 14 | } 15 | void checkQueueSecond(StackyQueue sq, int[] elementsShould1, String... errorMsg) throws NoSuchFieldException, 16 | IllegalAccessException { 17 | ArrayHelper.checkUnderlyingArraySecond(elementsShould1, sq, errorMsg); 18 | } 19 | 20 | @Test 21 | void artemisTest() throws NoSuchFieldException, IllegalAccessException { 22 | var sq = new StackyQueue(3,4,new StudentResult(),new StudentResult()); 23 | 24 | checkQueueFirst(sq, new int[]{}, "First stack should initially be empty"); 25 | checkQueueSecond(sq, new int[]{}, "Second stack should initially be empty"); 26 | 27 | sq.pushBack(1); 28 | checkQueueFirst(sq, new int[]{1, 0, 0},"just pushed 1"); 29 | checkQueueSecond(sq, new int[]{}, "Second stack should be empty"); 30 | 31 | sq.pushBack(2); 32 | checkQueueFirst(sq, new int[]{1, 2, 0},"just pushed 2"); 33 | checkQueueSecond(sq, new int[]{}, "Second stack should be empty"); 34 | 35 | sq.pushBack(3); 36 | checkQueueFirst(sq, new int[]{1, 2, 3},"just pushed 3"); 37 | checkQueueSecond(sq, new int[]{}, "Second stack should be empty"); 38 | 39 | sq.pushBack(4); 40 | checkQueueFirst(sq, new int[]{1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0},"just pushed 4; queue should grow"); 41 | checkQueueSecond(sq, new int[]{}, "Second stack should be empty"); 42 | 43 | assertEquals(1, sq.popFront()); 44 | checkQueueFirst(sq, new int[]{},"first stack should be empty"); 45 | checkQueueSecond(sq, new int[]{4, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0}, "pop should copy first stack into second and revers intervall"); 46 | 47 | assertEquals(2, sq.popFront()); 48 | checkQueueFirst(sq, new int[]{},"first stack should be empty"); 49 | checkQueueSecond(sq, new int[]{4, 3, 0, 0, 0, 0}, "pop should shrink the second stack"); 50 | 51 | assertEquals(3, sq.popFront()); 52 | checkQueueFirst(sq, new int[]{},"first stack should be empty"); 53 | checkQueueSecond(sq, new int[]{4, 0, 0}, "pop should shrink the second stack"); 54 | 55 | sq.pushBack(5); 56 | checkQueueFirst(sq, new int[]{5, 0, 0},"just pushed 5"); 57 | checkQueueSecond(sq, new int[]{4, 0, 0}, "push should not change the second stack"); 58 | 59 | sq.pushBack(6); 60 | checkQueueFirst(sq, new int[]{5, 6, 0},"just pushed 6"); 61 | checkQueueSecond(sq, new int[]{4, 0, 0}, "push should not change the second stack"); 62 | 63 | assertEquals(4, sq.popFront()); 64 | checkQueueFirst(sq, new int[]{5, 6, 0},"pop should not change the first stack"); 65 | checkQueueSecond(sq, new int[]{}, "pop should shrink the second stack, to be empty"); 66 | 67 | assertEquals(5, sq.popFront()); 68 | checkQueueFirst(sq, new int[]{},"first stack should be empty"); 69 | checkQueueSecond(sq, new int[]{6, 5, 0}, "pop should copy first stack into second and revers intervall"); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /w03/test/gad/dynamicarray/ArrayHelper.java: -------------------------------------------------------------------------------- 1 | package gad.dynamicarray; 2 | 3 | import gad.dynamicarray.DynamicArray; 4 | import gad.dynamicarray.DynamicStack; 5 | 6 | import java.util.Arrays; 7 | 8 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 9 | 10 | public class ArrayHelper { 11 | 12 | static void checkUnderlyingArray(int[] expected, Object o, String[] errorMsg) throws NoSuchFieldException, 13 | IllegalAccessException { 14 | 15 | //get dynamicArray variable from object 16 | var dynamicalArrayField = o.getClass().getDeclaredField("array"); 17 | dynamicalArrayField.setAccessible(true); 18 | 19 | var dynamicArray = (DynamicArray) dynamicalArrayField.get(o); 20 | 21 | //get int[] array from object 22 | var elementsField = DynamicArray.class.getDeclaredField("elements"); 23 | elementsField.setAccessible(true); 24 | 25 | var actual = (int[]) elementsField.get(dynamicArray); 26 | 27 | assertArrayEquals( 28 | expected, 29 | actual, 30 | "\nShould be:\n" + 31 | Arrays.toString(expected) + " but is:\n" + 32 | Arrays.toString(actual) + "\n" + 33 | Arrays.toString(errorMsg) + "\n\n"); 34 | } 35 | 36 | static void checkUnderlyingArrayFirst(int[] expected, Object o, String[] errorMsg) throws NoSuchFieldException, 37 | IllegalAccessException { 38 | 39 | //get stackyQueue variable from object 40 | var stackyQueueField = o.getClass().getDeclaredField("first"); 41 | stackyQueueField.setAccessible(true); 42 | 43 | var p = (DynamicStack) stackyQueueField.get(o); 44 | 45 | //get dynamicArray variable from object 46 | var dynamicalArrayField = p.getClass().getDeclaredField("array"); 47 | dynamicalArrayField.setAccessible(true); 48 | 49 | var dynamicArray = (DynamicArray) dynamicalArrayField.get(p); 50 | 51 | //get int[] array from object 52 | var elementsField = DynamicArray.class.getDeclaredField("elements"); 53 | elementsField.setAccessible(true); 54 | 55 | var actual = (int[]) elementsField.get(dynamicArray); 56 | 57 | assertArrayEquals( 58 | expected, 59 | actual, 60 | "\nFirst should be:\n" + 61 | Arrays.toString(expected) + " but is:\n" + 62 | Arrays.toString(actual) + "\n" + 63 | Arrays.toString(errorMsg) + "\n\n"); 64 | } 65 | 66 | static void checkUnderlyingArraySecond(int[] expected, Object o, String[] errorMsg) throws NoSuchFieldException, 67 | IllegalAccessException { 68 | 69 | //get stackyQueue variable from object 70 | var stackyQueueField = o.getClass().getDeclaredField("second"); 71 | stackyQueueField.setAccessible(true); 72 | 73 | var p = (DynamicStack) stackyQueueField.get(o); 74 | 75 | //get dynamicArray variable from object 76 | var dynamicalArrayField = p.getClass().getDeclaredField("array"); 77 | dynamicalArrayField.setAccessible(true); 78 | 79 | var dynamicArray = (DynamicArray) dynamicalArrayField.get(p); 80 | 81 | //get int[] array from object 82 | var elementsField = DynamicArray.class.getDeclaredField("elements"); 83 | elementsField.setAccessible(true); 84 | 85 | var actual = (int[]) elementsField.get(dynamicArray); 86 | 87 | assertArrayEquals( 88 | expected, 89 | actual, 90 | "\nSecond should be:\n" + 91 | Arrays.toString(expected) + " but is:\n" + 92 | Arrays.toString(actual) + "\n" + 93 | Arrays.toString(errorMsg) + "\n\n"); 94 | } 95 | } -------------------------------------------------------------------------------- /w06/test/gad/sortingHeaps/ExtractMaxTest.java: -------------------------------------------------------------------------------- 1 | package gad.sortingHeaps; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 4 | 5 | import java.util.Random; 6 | import java.util.PriorityQueue; 7 | 8 | import org.junit.jupiter.api.BeforeEach; 9 | import org.junit.jupiter.api.Test; 10 | 11 | public class ExtractMaxTest { 12 | Result result; 13 | 14 | @BeforeEach 15 | void beforeEach() { 16 | result = new StudentResult(); 17 | } 18 | 19 | @Test 20 | void testExtractMaxBinary() { 21 | NaryHeap binaryHeap = new NaryHeap(new int[] { 4, 1, 6, 7, 2, 3, 5 }, 2, 7, result); 22 | 23 | binaryHeap.extractMax(); 24 | int[] expectedHeapAfter7 = { 6, 4, 5, 1, 2, 3, 7 }; 25 | assertArrayEquals(expectedHeapAfter7, binaryHeap.getHeap()); 26 | 27 | binaryHeap.extractMax(); 28 | int[] expectedHeapAfter6 = { 5, 4, 3, 1, 2, 6, 7 }; 29 | assertArrayEquals(expectedHeapAfter6, binaryHeap.getHeap()); 30 | 31 | binaryHeap.extractMax(); 32 | int[] expectedHeapAfter5 = { 4, 2, 3, 1, 5, 6, 7 }; 33 | assertArrayEquals(expectedHeapAfter5, binaryHeap.getHeap()); 34 | 35 | binaryHeap.extractMax(); 36 | int[] expectedHeapAfter4 = { 3, 2, 1, 4, 5, 6, 7 }; 37 | assertArrayEquals(expectedHeapAfter4, binaryHeap.getHeap()); 38 | 39 | binaryHeap.extractMax(); 40 | int[] expectedHeapAfter3 = { 2, 1, 3, 4, 5, 6, 7 }; 41 | assertArrayEquals(expectedHeapAfter3, binaryHeap.getHeap()); 42 | 43 | binaryHeap.extractMax(); 44 | int[] expectedHeapAfter2 = { 1, 2, 3, 4, 5, 6, 7 }; 45 | assertArrayEquals(expectedHeapAfter2, binaryHeap.getHeap()); 46 | 47 | binaryHeap.extractMax(); 48 | int[] expectedHeapAfter1 = { 1, 2, 3, 4, 5, 6, 7 }; 49 | assertArrayEquals(expectedHeapAfter1, binaryHeap.getHeap()); 50 | } 51 | 52 | @Test 53 | void testSingleElementHeap() { 54 | NaryHeap binaryHeap = new NaryHeap(new int[] { 1 }, 2, 1, result); 55 | 56 | binaryHeap.extractMax(); 57 | int[] expectedHeapAfterEmpty = { 1 }; 58 | assertArrayEquals(expectedHeapAfterEmpty, binaryHeap.getHeap()); 59 | } 60 | 61 | @Test 62 | void testExtractMaxBinaryBigTest() { 63 | int[] values = new int[100_000]; 64 | Random random = new Random(42); 65 | 66 | for (int i = 0; i < values.length; i++) { 67 | values[i] = random.nextInt(); 68 | } 69 | 70 | NaryHeap binaryHeap = new NaryHeap(values, 2, values.length, result); 71 | PriorityQueue priorityQueue = new PriorityQueue<>(java.util.Collections.reverseOrder()); 72 | 73 | for (int i = 0; i < values.length; i++) { 74 | priorityQueue.add(values[i]); 75 | } 76 | 77 | for (int i = 0; i < values.length; i++) { 78 | int expected = priorityQueue.poll(); 79 | int actual = binaryHeap.extractMax(); 80 | assertEquals(expected, actual, "Extracted max elements differ at index " + i); 81 | } 82 | } 83 | 84 | @Test 85 | void testExtractMaxTernaryBigTest() { 86 | int[] values = new int[100_000]; 87 | Random random = new Random(42); 88 | 89 | for (int i = 0; i < values.length; i++) { 90 | values[i] = random.nextInt(); 91 | } 92 | 93 | NaryHeap ternaryHeap = new NaryHeap(values, 3, values.length, result); 94 | PriorityQueue javaHeap = new PriorityQueue<>(java.util.Collections.reverseOrder()); 95 | for (int value : values) { 96 | javaHeap.add(value); 97 | } 98 | 99 | for (int i = 0; i < values.length; i++) { 100 | int expectedMax = javaHeap.poll(); 101 | int actualMax = ternaryHeap.extractMax(); 102 | assertEquals(expectedMax, actualMax, "Extracted max elements differ at index " + i); 103 | } 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /w02/test/gad/binarysearch/BoundSearchTest.java: -------------------------------------------------------------------------------- 1 | package gad.binarysearch; 2 | 3 | import gad.binarysearch.BinSea; 4 | import gad.binarysearch.StudentResult; 5 | import org.junit.jupiter.params.ParameterizedTest; 6 | import org.junit.jupiter.params.provider.Arguments; 7 | import org.junit.jupiter.params.provider.MethodSource; 8 | 9 | import java.util.stream.Stream; 10 | 11 | import static org.junit.jupiter.api.Assertions.assertEquals; 12 | 13 | 14 | class BoundSearchTest { 15 | static Stream simple() { 16 | return Stream.of( 17 | Arguments.of(new int[]{1, 2, 3, 4}, 1, true, 0), 18 | Arguments.of(new int[]{1, 2, 3, 4}, 2, true, 1), 19 | Arguments.of(new int[]{1, 2, 3, 4}, 3, true, 2), 20 | Arguments.of(new int[]{1, 2, 3, 4}, 3, false, 2) 21 | ); 22 | } 23 | 24 | static Stream randomValues() { 25 | return Stream.of( 26 | Arguments.of(new int[]{0, 1, 1, 1, 1, 1, 1, 1}, 1, true, 1), 27 | Arguments.of(new int[]{1, 1, 1, 1, 1, 1, 1}, 2, true, -1), 28 | Arguments.of(new int[]{1, 1, 1, 1, 1, 1, 1, 4}, 1, false, 6), 29 | Arguments.of(new int[]{1, 4, 4, 4, 5, 5, 5, 5, 8}, 4, true, 1), 30 | Arguments.of(new int[]{1, 4, 4, 4, 5, 5, 5, 5, 8}, 5, true, 4), 31 | Arguments.of(new int[]{1, 4, 4, 4, 5, 5, 5, 5, 8}, 5, false, 7), 32 | Arguments.of(new int[]{1, 4, 4, 4, 5, 5, 5, 5, 8}, 9, false, 8), 33 | Arguments.of(new int[]{1, 4, 4, 4, 5, 5, 5, 5, 8, 10, 23}, 6, true, 8), 34 | Arguments.of(new int[]{1, 4, 4, 4, 5, 5, 5, 5, 8, 10, 23}, 6, false, 7), 35 | Arguments.of(new int[]{1, 4, 4, 4, 5, 5, 5, 5, 8}, 9, true, -1), 36 | Arguments.of(new int[]{1, 2, 2, 4, 4, 4, 4, 8, 8, 27, 89}, 25, true, 9), 37 | Arguments.of(new int[]{1, 2, 2, 4, 4, 4, 4, 8, 8, 27, 89}, 25, false, 8), 38 | Arguments.of(new int[]{1, 2, 2, 4, 4, 4, 4, 8, 8, 27, 89}, 99, true, -1), 39 | Arguments.of(new int[]{1, 2, 2, 4, 4, 4, 4, 8, 8, 27, 89}, 99, false, 10), 40 | Arguments.of(new int[]{1, 2, 2, 4, 4, 4, 4, 8, 8, 27, 89}, -1, true, 0), 41 | Arguments.of(new int[]{1, 2, 2, 4, 4, 4, 4, 8, 8, 27, 89}, -1, false, -1) 42 | ); 43 | } 44 | 45 | static Stream sameValues() { 46 | return Stream.of( 47 | Arguments.of(new int[]{1, 1, 1, 1, 1, 1, 1}, 1, true, 0), 48 | Arguments.of(new int[]{1, 1, 1, 1, 1, 1, 1}, 1, false, 6) 49 | ); 50 | } 51 | 52 | static Stream valueNotInArray() { 53 | return Stream.of( 54 | Arguments.of(new int[]{1, 2, 3, 4}, 5, true, -1), 55 | Arguments.of(new int[]{1, 2, 3, 4}, 5, false, 3), 56 | Arguments.of(new int[]{1, 2, 3, 4}, 0, true, 0), 57 | Arguments.of(new int[]{1, 2, 2, 2, 6, 7, 8, 9, 10}, 0, false, -1), 58 | Arguments.of(new int[]{1, 2, 2, 2, 6, 7, 8, 9, 10}, -1, true, 0), 59 | Arguments.of(new int[]{1, 2, 2, 4, 4, 4, 4, 8, 8, 27, 89}, 3, true, 3), 60 | Arguments.of(new int[]{1, 2, 2, 4, 4, 4, 4, 8, 8, 27, 89}, 3, false, 2), 61 | Arguments.of(new int[]{1, 2, 2, 4, 4, 4, 4, 8, 8, 27, 89}, 7, true, 7), 62 | Arguments.of(new int[]{1, 2, 2, 4, 4, 4, 4, 8, 8, 27, 89}, 7, false, 6) 63 | ); 64 | } 65 | 66 | static Stream duplicatesInArray() { 67 | return Stream.of( 68 | Arguments.of(new int[]{1, 2, 2, 2, 6, 7, 8, 9, 10}, 2, true, 1), 69 | Arguments.of(new int[]{1, 2, 2, 2, 6, 7, 8, 9, 10}, 2, false, 3), 70 | Arguments.of(new int[]{1, 2, 2, 2, 6, 7, 8, 9, 10}, 6, true, 4), 71 | Arguments.of(new int[]{1, 2, 2, 2, 6, 7, 8, 9, 10}, 6, false, 4), 72 | Arguments.of(new int[]{1, 2, 2, 4, 4, 4, 4, 8, 8, 27, 89}, 4, true, 3), 73 | Arguments.of(new int[]{1, 2, 2, 4, 4, 4, 4, 8, 8, 27, 89}, 4, false, 6), 74 | Arguments.of(new int[]{1, 2, 2, 4, 4, 4, 4, 8, 8, 27, 89}, 8, true, 7), 75 | Arguments.of(new int[]{1, 2, 2, 4, 4, 4, 4, 8, 8, 27, 89}, 8, false, 8) 76 | ); 77 | } 78 | 79 | static Stream valuesOnArrayBoundaries() { 80 | return Stream.of( 81 | Arguments.of(new int[]{0, 4, 4, 4, 4, 9, 10}, 2, true, 1), 82 | Arguments.of(new int[]{0, 4, 4, 4, 4, 9, 10}, 11, true, -1), 83 | Arguments.of(new int[]{0, 4, 4, 4, 4, 9, 10}, 11, false, 6), 84 | Arguments.of(new int[]{1, 1, 4, 4, 4, 9, 10}, 1, false, 1) 85 | ); 86 | } 87 | 88 | @ParameterizedTest 89 | @MethodSource({"simple", "randomValues", "sameValues", "valueNotInArray", "duplicatesInArray", "valuesOnArrayBoundaries"}) 90 | void testSearch(int[] arr, int value, boolean lowerBound, int expectedIdx) { 91 | assertEquals(expectedIdx, BinSea.search(arr, value, lowerBound, new StudentResult())); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /w10/test/gad/simplehash/InsertRemoveTest.java: -------------------------------------------------------------------------------- 1 | package gad.simplehash; 2 | 3 | import org.junit.jupiter.params.ParameterizedTest; 4 | import org.junit.jupiter.params.provider.Arguments; 5 | import org.junit.jupiter.params.provider.MethodSource; 6 | 7 | import java.lang.reflect.Field; 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | import java.util.stream.Stream; 11 | 12 | import static org.junit.jupiter.api.Assertions.assertEquals; 13 | 14 | public class InsertRemoveTest { 15 | 16 | // --- Insert --- // 17 | 18 | private static Stream testInsert() { 19 | return Stream.of( 20 | Arguments.of(1, new int[]{1}, new String[]{"x"}, new String[]{"foo"}, new List[]{ 21 | new ArrayList>() { 22 | { 23 | add(new Pair("x", "foo")); 24 | } 25 | }}), 26 | Arguments.of(1, new int[]{1}, new String[]{"x", "y"}, new String[]{"foo", "bar"}, 27 | new List[]{new ArrayList>() { 28 | { 29 | add(new Pair("x", "foo")); 30 | add(new Pair("y", "bar")); 31 | } 32 | }}) 33 | 34 | ); 35 | 36 | } 37 | 38 | @ParameterizedTest 39 | @MethodSource 40 | public void testInsert(int minSize, int[] a, String[] k, String[] v, List>[] expect) { 41 | Hashtable hashtable = new Hashtable<>(minSize, a); 42 | 43 | ModuloHelper mH = (i, divisor) -> i % divisor; 44 | 45 | for (int i = 0; i < k.length; i++) { 46 | hashtable.insert(k[i], v[i], mH); 47 | } 48 | 49 | List>[] table = hashtable.getTable(); 50 | 51 | for (int i = 0; i < expect.length; i++) { 52 | assertEquals(expect[i], table[i]); 53 | } 54 | 55 | } 56 | 57 | // --- Remove --- // 58 | 59 | private static Stream testRemove() { 60 | return Stream.of( 61 | Arguments.of(1, new int[]{1}, "x", false, new List[]{new ArrayList>()}, 62 | new List[]{new ArrayList>()}), 63 | Arguments.of(1, new int[]{1}, "x", true, new List[]{new ArrayList>() { 64 | { 65 | add(new Pair<>("x", "foo")); 66 | } 67 | }}, new List[]{new ArrayList>()}), 68 | Arguments.of(1, new int[]{1}, "x", true, new List[]{new ArrayList>() { 69 | { 70 | add(new Pair<>("x", "foo")); 71 | add(new Pair<>("y", "bar")); 72 | } 73 | }}, new List[]{new ArrayList>() { 74 | { 75 | add(new Pair<>("y", "bar")); 76 | } 77 | }}), 78 | Arguments.of(1, new int[]{1}, new String("x"), true, new List[]{new ArrayList>() { 79 | { 80 | add(new Pair<>("x", "foo")); 81 | add(new Pair<>("y", "bar")); 82 | } 83 | }}, new List[]{new ArrayList>() { 84 | { 85 | add(new Pair<>("y", "bar")); 86 | } 87 | }}) 88 | ); 89 | } 90 | 91 | @ParameterizedTest 92 | @MethodSource 93 | public void testRemove(int minSize, int[] a, String k, boolean expect, List>[] before, 94 | List>[] after) throws Exception { 95 | Hashtable hashtable = new Hashtable<>(minSize, a); 96 | 97 | ModuloHelper mH = (i, divisor) -> i % divisor; 98 | 99 | Field tableField = hashtable.getClass().getDeclaredField("table"); 100 | tableField.setAccessible(true); 101 | tableField.set(hashtable, before); 102 | 103 | assertEquals(expect, hashtable.remove(k, mH)); 104 | 105 | List>[] table = hashtable.getTable(); 106 | 107 | for (int i = 0; i < after.length; i++) { 108 | assertEquals(after[i], table[i]); 109 | } 110 | 111 | } 112 | 113 | } 114 | -------------------------------------------------------------------------------- /w03/test/gad/dynamicarray/RingQueueTest.java: -------------------------------------------------------------------------------- 1 | package gad.dynamicarray; 2 | 3 | import gad.dynamicarray.RingQueue; 4 | import gad.dynamicarray.StudentResult; 5 | import org.junit.jupiter.api.Test; 6 | 7 | import static org.junit.jupiter.api.Assertions.*; 8 | 9 | class RingQueueTest { 10 | 11 | 12 | void checkQueue(RingQueue rq, int[] elementsShould, String... errorMsg) throws NoSuchFieldException, 13 | IllegalAccessException { 14 | ArrayHelper.checkUnderlyingArray(elementsShould, rq, errorMsg); 15 | } 16 | 17 | @Test 18 | void artemisTest() throws NoSuchFieldException, IllegalAccessException { 19 | var rq = new RingQueue(3, 4, new StudentResult()); 20 | 21 | checkQueue(rq, new int[]{}, "Stack should initially be empty"); 22 | 23 | rq.pushBack(1); 24 | checkQueue(rq, new int[]{1, 0, 0}, "just pushed 1"); 25 | 26 | rq.pushBack(2); 27 | checkQueue(rq, new int[]{1, 2, 0}, "just pushed 2"); 28 | 29 | rq.pushBack(3); 30 | checkQueue(rq, new int[]{1, 2, 3}, "just pushed 3"); 31 | 32 | rq.pushBack(4); 33 | checkQueue(rq, new int[]{1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0}, "just pushed 4; queue should grow"); 34 | 35 | assertEquals(1, rq.popFront()); 36 | checkQueue(rq, new int[]{1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0}, "pop should not change the stack"); 37 | 38 | assertEquals(2, rq.popFront()); 39 | checkQueue(rq, new int[]{3, 4, 0, 0, 0, 0}, "pop should shrink the stack"); 40 | 41 | assertEquals(3, rq.popFront()); 42 | checkQueue(rq, new int[]{4, 0, 0}, "pop should shrink the stack"); 43 | 44 | rq.pushBack(5); 45 | checkQueue(rq, new int[]{4, 5, 0}, "just pushed 5"); 46 | 47 | rq.pushBack(6); 48 | checkQueue(rq, new int[]{4, 5, 6}, "just pushed 6"); 49 | 50 | assertEquals(4, rq.popFront()); 51 | checkQueue(rq, new int[]{4, 5, 6}, "pop should not change the stack"); 52 | 53 | rq.pushBack(7); 54 | checkQueue(rq, new int[]{7, 5, 6}, "just pushed 7"); 55 | 56 | rq.pushBack(8); 57 | checkQueue(rq, new int[]{5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 58 | 0}, "just pushed 8; stack should grow, and be relocated to the beginning"); 59 | 60 | } 61 | 62 | 63 | @Test 64 | void addAndRemove() throws NoSuchFieldException, IllegalAccessException { 65 | var rq = new RingQueue(3, 4, new StudentResult()); 66 | 67 | for (int i = 0; i < 10; i++) { 68 | rq.pushBack(i); 69 | } 70 | 71 | checkQueue(rq, new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0}, "just pushed 10 elements"); 72 | 73 | for (int i = 0; i < 10; i++) { 74 | assertEquals(i, rq.popFront()); 75 | } 76 | 77 | checkQueue(rq, new int[]{}, "removed all elements"); 78 | } 79 | 80 | 81 | @Test 82 | void fillAndEmptyAgain() throws NoSuchFieldException, IllegalAccessException { 83 | var rq = new RingQueue(3, 4, new StudentResult()); 84 | 85 | //fill the queue 86 | rq.pushBack(4); 87 | rq.pushBack(5); 88 | rq.pushBack(6); 89 | 90 | checkQueue(rq, new int[]{4, 5, 6}, "just pushed 3 elements"); 91 | 92 | //begin to empty the queue 93 | assertEquals(4, rq.popFront()); 94 | checkQueue(rq, new int[]{4, 5, 6}, "no resize; array should stay the same"); 95 | 96 | assertEquals(5, rq.popFront()); 97 | checkQueue(rq, new int[]{4, 5, 6}, "no resize; array should stays the same"); 98 | 99 | assertEquals(6, rq.popFront()); 100 | checkQueue(rq, new int[]{}, "resized; array should be empty"); 101 | } 102 | 103 | @Test 104 | void wrapAround() throws NoSuchFieldException, IllegalAccessException { 105 | var rq = new RingQueue(3, 4, new StudentResult()); 106 | 107 | rq.pushBack(1); 108 | checkQueue(rq, new int[]{1, 0, 0}); 109 | 110 | rq.pushBack(2); 111 | checkQueue(rq, new int[]{1, 2, 0}); 112 | 113 | assertEquals(1, rq.popFront()); 114 | checkQueue(rq, new int[]{1, 2, 0}); 115 | 116 | rq.pushBack(3); 117 | checkQueue(rq, new int[]{1, 2, 3}); 118 | 119 | rq.pushBack(4); 120 | checkQueue(rq, new int[]{4, 2, 3}, "pushing should wrap around"); 121 | 122 | assertEquals(2, rq.popFront()); 123 | checkQueue(rq, new int[]{4, 2, 3}); 124 | 125 | rq.pushBack(5); 126 | checkQueue(rq, new int[]{4, 5, 3}); 127 | 128 | assertEquals(3, rq.popFront()); 129 | checkQueue(rq, new int[]{4, 5, 3}); 130 | 131 | rq.pushBack(6); 132 | checkQueue(rq, new int[]{4, 5, 6}); 133 | 134 | assertEquals(4, rq.popFront(),"popping should wrap around"); 135 | checkQueue(rq, new int[]{4, 5, 6}); 136 | } 137 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GAD 24 - Tests 2 | 3 | This is a repository to collect JUnit Tests for GAD exercises at TUM in SoSe24. These tests have been written by students for students and are not part of the official course. 4 | 5 | ## Legal Status 6 | 7 | > Es ist alles erlaubt, was die Lösung nicht verrät. 8 | 9 | The so-called *Übungsleitung* will have an eye on this repository. But as long as the tests do not reveal the solution of the homework, this repository will tolerate them. To ensure the solution is not getting revealed, the tests should be Input/Output-Tests only. 10 | 11 | Furthermore, there is no guarantee of the correctness of the tests. Neither the *Übungsleitung* nor any of the other tutors will contribute own tests. It is your own responsibility to validate the correctness and if necessary report bugs or even provide/contribute a fix for your fellow student. 12 | 13 | ### Examples 14 | 15 | **correct**: 16 | ```java 17 | // Test Area calculation 18 | // Arrange 19 | Rectangle r1 = new Rectangle(length: 4, width: 4) 20 | // Act & Assert 21 | assertEquals(16, r1.calculateArea()); 22 | ``` 23 | 24 | **wrong!**: 25 | ```java 26 | // Test Area calculation 27 | // Arrange 28 | Rectangle r1 = new Rectangle(length: 4, width: 6) 29 | // Act & Assert 30 | // Calculate using length * width // <-- reveals the solution 31 | assertEquals(4*6, r1.calculateArea()); // <-- reveals the solution 32 | ``` 33 | 34 | ## Usage 35 | 36 | https://github.com/nilsreichardt/gad24-tests/assets/24459435/6d3126b4-487d-4e40-bdc9-3b15a01170d7 37 | 38 | 1. Clone this repository 39 | ```shell 40 | git clone https://github.com/nilsreichardt/gad24-tests.git 41 | ``` 42 | 43 | 2. Check for updates 44 | ```shell 45 | git pull 46 | ``` 47 | 48 | 3. Copy the `test` directory to the root of your repository (next to `src`) and *not* inside your src folder 49 | ``` 50 | ./your-gad-project 51 | ├───src 52 | ├───test 53 | └───... 54 | ``` 55 | Or use the symlink instruction below. 56 | 57 | 4. Make sure in the `build.gradle` file `sourceSets`, `test` and `dependencies` look somewhat like that: 58 | ```groovy 59 | sourceSets { 60 | main { 61 | java { 62 | srcDir 'src' 63 | } 64 | } 65 | test { 66 | java { 67 | srcDirs = ['test'] 68 | } 69 | } 70 | } 71 | 72 | test { 73 | useJUnitPlatform() 74 | } 75 | 76 | dependencies { 77 | testImplementation('org.junit.jupiter:junit-jupiter:5.9.0') 78 | } 79 | ``` 80 | 81 | 5. Open the gradle tool window 82 | 6. Click reload 83 | 7. Run the tests via `tasks -> verification -> test` and check your mistakes 84 | 85 | ### Important note 86 | As this is a student-driven project please keep in mind that mistakes can happen. If you think a test is wrong, please feel free to open an Issue: 87 | [Open Issue](https://github.com/nilsreichardt/gad24-tests/issues) 88 | 89 | ### How to use symlinks (Advanced Users only) 90 | **Windows:** 91 | This has to be executed in the command line, run as administrator: 92 | ```shell 93 | mklink /d \path\to\artemis-repository\test\gad \path\to\test-repository\aufgabe\test\gad 94 | ``` 95 | 96 | **Linux/MacOS:** 97 | ```shell 98 | ln -s /path/to/test-repository/aufgabe/test/gad /path/to/artemis-repository/test/gad 99 | ``` 100 | 101 | ## How to Contribute 102 | 103 | 1. Read our guidelines in our [CONTRIBUTING.md](https://github.com/nilsreichardt/gad24-tests/blob/main/CONTRIBUTING.md) 104 | 2. Fork Repository ([GitHub Documentation](https://docs.github.com/en/get-started/quickstart/fork-a-repo#forking-a-repository)) 105 | 3. Add / edit tests under `/test/` 106 | 4. Ensure no solution or homework-code is getting revealed 107 | 5. Commit and push to your fork repository 108 | 6. Open a Pull Request to this repository ([GitHub Documentation](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests)) 109 | 7. Wait until another student reviewed the PR 110 | 111 | ## Contributors 112 | ![Contributors](https://contrib.rocks/image?repo=nilsreichardt/gad24-tests) 113 | 114 | ## Helpful Links 115 | 116 | - [PGDP WS 22/23 Tests](https://github.com/MaximilianAnzinger/pgdp2223-tests) 117 | - [GAD SS23 Tests](https://github.com/JohannesStoehr/gad23-tests) 118 | - [GAD SS22 Tests](https://github.com/MaximilianAnzinger/gad2022-tests) 119 | - [Writing Tests using JUnit 5](https://junit.org/junit5/docs/current/user-guide/#writing-tests) 120 | - [Learning Git with Bitbucket](https://www.atlassian.com/de/git/tutorials/learn-git-with-bitbucket-cloud) 121 | - [Getting Started with GitHub](https://docs.github.com/en/get-started/quickstart/hello-world) 122 | 123 | ![Made with ersti tears](https://img.shields.io/badge/made%20with-tears-blue?style=svg) 124 | -------------------------------------------------------------------------------- /w07/test/gad/binomilia/BinomialHeapTest.java: -------------------------------------------------------------------------------- 1 | package gad.binomilia; 2 | 3 | import org.junit.jupiter.api.BeforeEach; 4 | import org.junit.jupiter.api.Test; 5 | import org.junit.jupiter.api.Timeout; 6 | 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | import java.util.Random; 10 | import java.util.concurrent.TimeUnit; 11 | 12 | import static org.junit.jupiter.api.Assertions.assertEquals; 13 | import static org.junit.jupiter.api.Assertions.assertThrows; 14 | 15 | public class BinomialHeapTest { 16 | 17 | private BinomialHeap heap; 18 | private StudentResultTest studentResult; 19 | 20 | @BeforeEach 21 | void init() { 22 | heap = new BinomialHeap(); 23 | studentResult = new StudentResultTest(); 24 | } 25 | 26 | @Test 27 | void exceptionTest() { 28 | assertThrows(RuntimeException.class, () -> heap.min(), 29 | "Please throw an exception as required by the Artemis quest, " + 30 | "because there can't be a minimal element in an empty heap."); 31 | assertThrows(RuntimeException.class, () -> heap.deleteMin(studentResult), 32 | "Please throw an exception as required by the Artemis quest, " + 33 | "because you can't delete anything from an empty list."); 34 | } 35 | 36 | @Test 37 | void minTest() { 38 | for (int i = 0; i < 500; i++) { 39 | heap.insert(i, studentResult); 40 | assertEquals(0, heap.min(), "Wrong value for min!"); 41 | } 42 | } 43 | 44 | @Test 45 | void testDeleteMin() { 46 | List nums = new ArrayList<>(); 47 | for (int i = 0; i < 500; i++) { 48 | heap.insert(i, studentResult); 49 | nums.add(i); 50 | } 51 | 52 | for (int x : nums) { 53 | assertEquals(x, heap.min(), "Wrong value for min!"); 54 | int deleted = heap.deleteMin(studentResult); 55 | assertEquals(x, deleted, 56 | "Either did not delete the smallest number or the expected number wasnt in the heap"); 57 | } 58 | } 59 | 60 | @Test 61 | void testBigNumbers() { 62 | List nums = new ArrayList<>(); 63 | for (int i = 0; i < 1000; i++) { 64 | int num = new Random().nextInt(); 65 | nums.add(num); 66 | heap.insert(num, studentResult); 67 | int minList = nums.stream().min(Integer::compare).get(); 68 | int minHeap = heap.min(); 69 | assertEquals(minList, minHeap, "Wrong minimum " + i); 70 | } 71 | for (int i = 0; i < 500; i++) { 72 | int num = new Random().nextInt(); 73 | nums.add(num); 74 | heap.insert(num, studentResult); 75 | Integer min = nums.stream().min(Integer::compare).get(); 76 | nums.remove(min); 77 | assertEquals(min, heap.deleteMin(studentResult), "Wrong number removed " + i); 78 | } 79 | } 80 | 81 | /** 82 | * This test is just an indicator if the code can be fast enough. 83 | * The task on Artemis states that the code must complete 5,000 operations in 84 | * under 2 seconds. 85 | * The 700 millisec comes from this 87 | * Zulip message. 88 | * That's why I'm testing here with these 700 millisec. 89 | * 90 | */ 91 | 92 | @Test 93 | @Timeout(value = 700, unit = TimeUnit.MILLISECONDS) 94 | void performanceTest1() { 95 | Random random = new Random(400); 96 | for (int i = 0; i < 5_000; i++) { 97 | heap.insert(random.nextInt(), new StudentResult()); 98 | } 99 | } 100 | 101 | @Test 102 | @Timeout(value = 700, unit = TimeUnit.MILLISECONDS) 103 | void performanceTest2() { 104 | Random random = new Random(400); 105 | for (int i = 0; i < 2_500; i++) { 106 | heap.insert(random.nextInt(), new StudentResult()); 107 | } 108 | 109 | for (int i = 2_500; i > 0; i--) { 110 | heap.deleteMin(new StudentResult()); 111 | } 112 | } 113 | 114 | @Test 115 | @Timeout(value = 700, unit = TimeUnit.MILLISECONDS) 116 | void performanceTest3() { 117 | Random random = new Random(400); 118 | for (int i = 0; i < 1_250; i++) { 119 | heap.insert(random.nextInt(), new StudentResult()); 120 | } 121 | 122 | for (int i = 1_000; i > 0; i--) { 123 | heap.deleteMin(new StudentResult()); 124 | } 125 | 126 | for (int i = 0; i < 1_250; i++) { 127 | heap.insert(random.nextInt(), new StudentResult()); 128 | } 129 | 130 | for (int i = 1_500; i > 0; i--) { 131 | heap.deleteMin(new StudentResult()); 132 | } 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /w04/test/gad/simplesort/DoubleDistributedMedianPivotTest.java: -------------------------------------------------------------------------------- 1 | package gad.simplesort; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 4 | 5 | import java.util.stream.Stream; 6 | 7 | import org.junit.jupiter.api.DisplayName; 8 | import org.junit.jupiter.params.ParameterizedTest; 9 | import org.junit.jupiter.params.provider.Arguments; 10 | import org.junit.jupiter.params.provider.MethodSource; 11 | 12 | public class DoubleDistributedMedianPivotTest { 13 | @ParameterizedTest 14 | @MethodSource({"numbersOfConsideredElementsDeviating", 15 | "allParameterDiffer"}) 16 | @DisplayName("Based on Artemis") 17 | void doubleDistributedMedianPivotTest(int[] numbers, int[] expected, int numbersOfConsideredElements, int from, 18 | int to) { 19 | DualPivotFinder dualPivotFinder = DualPivotFinder 20 | .getMedianPivotDistributed(numbersOfConsideredElements); 21 | assertArrayEquals(expected, dualPivotFinder.findPivot(numbers, from, to)); 22 | } 23 | 24 | @ParameterizedTest 25 | @MethodSource("notRequire") 26 | @DisplayName("General Tests not necessary") 27 | void doubleDistributedMedianPivotTest2(int[] numbers, int[] expected, int numbersOfConsideredElements, int from, 28 | int to) { 29 | DualPivotFinder dualPivotFinder = DualPivotFinder 30 | .getMedianPivotDistributed(numbersOfConsideredElements); 31 | assertArrayEquals(expected, dualPivotFinder.findPivot(numbers, from, to)); 32 | } 33 | 34 | private static Stream numbersOfConsideredElementsDeviating() { 35 | return Stream.of( 36 | Arguments.of( 37 | new int[]{4, 9, 1, 10, 2, 15, 13, 26, 17, 55, 3, 11}, 38 | new int[]{4, 6}, 39 | 5, 40 | 0, 41 | 11)); 42 | } 43 | 44 | private static Stream allParameterDiffer() { 45 | return Stream.of( 46 | Arguments.of( 47 | new int[]{30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 48 | 15, 12, 9, 6, 3, 0}, 49 | new int[]{2, 6}, 50 | 5, 51 | 0, 52 | 11), 53 | Arguments.of( 54 | new int[]{30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 55 | 15, 12, 9, 6, 3, 0}, 56 | new int[]{14, 18}, 57 | 5, 58 | 12, 59 | 21)); 60 | } 61 | 62 | private static Stream notRequire() { 63 | return Stream.of( 64 | Arguments.of( 65 | new int[]{4, 9, 1, 10, 2, 15, 13, 26, 17, 55, 3, 11}, 66 | new int[]{0, 6}, 67 | 12, 68 | 0, 69 | 11), 70 | Arguments.of( 71 | new int[]{4, 9, 1, 10, 2}, 72 | new int[]{2, 4}, 73 | 3, 74 | 0, 75 | 4), 76 | Arguments.of( 77 | new int[]{4, 9, 1, 10, 2, 15, 13, 26, 17, 55, 3, 11}, 78 | new int[]{0, 10}, 79 | 3, 80 | 0, 81 | 11), 82 | Arguments.of( 83 | new int[]{4, 9, 1, 10, 2, 15, 13, 26, 17, 55, 3, 11}, 84 | new int[]{0, 6}, 85 | 4, 86 | 0, 87 | 11), 88 | Arguments.of( 89 | new int[]{30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 90 | 15, 12, 9, 6, 3, 0}, 91 | new int[]{4, 8}, 92 | 21, 93 | 0, 94 | 11), 95 | Arguments.of( 96 | new int[]{5, 6, 5, 6, 55, 6, 5, 6, 55, 6, 5, 6, 5}, 97 | new int[]{4, 5}, 98 | 13, 99 | 4, 100 | 8), 101 | Arguments.of( 102 | new int[]{5, 6, 5, 6, 5, 6, 55, 6, 5, 6, 5, 6, 55, 6, 5, 6, 5, 6, 55, 103 | 6, 5, 6, 5, 6, 5}, 104 | new int[]{2, 3}, 105 | 25, 106 | 2, 107 | 18), 108 | Arguments.of( 109 | new int[]{5, 6, 5, 6, 5, 6, 55, 6, 5, 6, 5, 6, 55, 6, 5, 6, 5, 6, 55, 110 | 6, 5, 6, 5, 6, 5}, 111 | new int[]{2, 4}, 112 | 8, 113 | 2, 114 | 18) 115 | ); 116 | } 117 | } -------------------------------------------------------------------------------- /w05/test/gad/radixsort/BinaryRadixSortTest.java: -------------------------------------------------------------------------------- 1 | package gad.radixsort; 2 | 3 | import gad.radix.BinaryBucket; 4 | import gad.radix.BinaryRadixSort; 5 | import gad.radix.StudentResult; 6 | import org.junit.jupiter.api.DisplayName; 7 | import org.junit.jupiter.api.Order; 8 | import org.junit.jupiter.api.Test; 9 | import org.junit.jupiter.params.ParameterizedTest; 10 | import org.junit.jupiter.params.provider.Arguments; 11 | import org.junit.jupiter.params.provider.MethodSource; 12 | 13 | import java.time.Duration; 14 | import java.util.Arrays; 15 | import java.util.Random; 16 | import java.util.stream.Stream; 17 | 18 | import static gad.radix.BinaryRadixSort.sort; 19 | import static org.junit.jupiter.api.Assertions.*; 20 | 21 | public class BinaryRadixSortTest { 22 | 23 | 24 | @ParameterizedTest 25 | @MethodSource("provideTestDataBinaryKey") 26 | @DisplayName("Binary Key") 27 | @Order(1) 28 | void testBinaryKey(int element, int binPlace, int expected) { 29 | assertEquals(expected, BinaryRadixSort.key(element, binPlace)); 30 | } 31 | 32 | private static Stream provideTestDataBinaryKey() { 33 | return Stream.of( 34 | Arguments.of(0b00000000000000000000000000000001, 0, 1), 35 | Arguments.of(0b00000000000000000000010000000010, 1, 1), 36 | Arguments.of(0b00000000000000000000010000000100, 2, 1), 37 | Arguments.of(0b10000000000000000000000000000001, 31, 1), 38 | Arguments.of(0b01000000000000000000000000000000, 30, 1), 39 | Arguments.of(0b1111011111111111111111111111111, 26, 0), 40 | Arguments.of(0b00000000000000000000010000000000, 10, 1) 41 | ); 42 | } 43 | 44 | @Test 45 | @Order(1) 46 | @DisplayName("Binary Bucket") 47 | void testBinaryBucket() { 48 | BinaryBucket binaryBucket = new BinaryBucket(10); 49 | binaryBucket.insertLeft(2); 50 | binaryBucket.insertLeft(5); 51 | binaryBucket.insertRight(10); 52 | assertEquals(2, binaryBucket.getMid()); 53 | binaryBucket = new BinaryBucket(100); 54 | for (int i = 0; i < 99; i++) { 55 | binaryBucket.insertLeft(1); 56 | } 57 | assertEquals(99, binaryBucket.getMid()); 58 | } 59 | 60 | private void sortTest(int[] elements, int[] expects) { 61 | Arrays.sort(expects); 62 | sort(elements, new StudentResult()); 63 | assertArrayEquals(expects, elements); 64 | } 65 | 66 | @Test 67 | @DisplayName("Only negativ Numbers as elements") 68 | @Order(3) 69 | void sortOnlyNegativTest() { 70 | Random random = new Random(100); 71 | int[] numbers = new int[500]; 72 | for (int i = 0; i < numbers.length; i++) { 73 | numbers[i] = random.nextInt(Integer.MIN_VALUE, -1); 74 | } 75 | int[] expects = Arrays.copyOf(numbers, numbers.length); 76 | sortTest(numbers, expects); 77 | } 78 | 79 | @Test 80 | @DisplayName("Only positive Numbers as elements") 81 | @Order(2) 82 | void sortOnlyPositiveTest() { 83 | Random random = new Random(100); 84 | int[] numbers = new int[500]; 85 | for (int i = 0; i < numbers.length; i++) { 86 | numbers[i] = random.nextInt(0, Integer.MAX_VALUE); 87 | } 88 | int[] expects = Arrays.copyOf(numbers, numbers.length); 89 | sortTest(numbers, expects); 90 | } 91 | 92 | @Test 93 | @DisplayName("Usual elements") 94 | @Order(4) 95 | void sortUsualTest() { 96 | Random random = new Random(100); 97 | int[] numbers = new int[500]; 98 | for (int i = 0; i < numbers.length; i++) { 99 | numbers[i] = random.nextInt(Integer.MIN_VALUE, Integer.MAX_VALUE); 100 | } 101 | int[] expects = Arrays.copyOf(numbers, numbers.length); 102 | sortTest(numbers, expects); 103 | } 104 | 105 | /** 106 | *
107 | * This test may not pass on every device. 108 | * This test is only an indicator whether the sort-Method might be fast enough for Artemis. 109 | * There is no logging for this test, because this is slowing down the method. 110 | * The timeout only serves as a barrier if the test should run too long on a device. 111 | * I need an average of 700 milliseconds for this test specification. 112 | *
113 | *
114 | * If you would like to have also logging for this test, you could change from "new int[2_000_000] to new int[250_000]" 115 | * and "new StudentResultTest()" to "new StudentResult()". 116 | * After this change the test doesn't reflect the requirement of the performance description on Artemis. 117 | * I need an average of 1.8 seconds for this test specification. 118 | *
119 | */ 120 | @Test 121 | @DisplayName("Big Test") 122 | @Order(5) 123 | void sortBigTest() { 124 | Random random = new Random(100); 125 | int[] numbers = new int[2_000_000]; 126 | for (int i = 0; i < numbers.length; i++) { 127 | numbers[i] = random.nextInt(Integer.MIN_VALUE, Integer.MAX_VALUE); 128 | } 129 | int[] expects = Arrays.copyOf(numbers, numbers.length); 130 | Arrays.sort(expects); 131 | sort(numbers,new StudentResultTest()); 132 | assertTimeoutPreemptively(Duration.ofSeconds(2), () -> assertArrayEquals(expects,numbers)); 133 | } 134 | } -------------------------------------------------------------------------------- /w04/test/gad/simplesort/MedianPivotDistributedTest.java: -------------------------------------------------------------------------------- 1 | package gad.simplesort; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | 5 | import java.util.stream.Stream; 6 | 7 | import org.junit.jupiter.params.ParameterizedTest; 8 | import org.junit.jupiter.params.provider.Arguments; 9 | import org.junit.jupiter.params.provider.MethodSource; 10 | 11 | public class MedianPivotDistributedTest { 12 | 13 | @ParameterizedTest 14 | @MethodSource({"artemisBeispiele", "fromEquals0", "edgeCases"}) 15 | public void distributedMedianPivotTest(int[] numbers, int expected, int numberOfConsideredElements, int from, 16 | int to) { 17 | PivotFinder medianPivotFinder = PivotFinder.getMedianPivotDistributed(numberOfConsideredElements); 18 | assertEquals(expected, medianPivotFinder.findPivot(numbers, from, to)); 19 | } 20 | 21 | private static Stream artemisBeispiele() { 22 | return Stream.of( 23 | Arguments.of( 24 | new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, 25 | 4, 26 | 3, 27 | 0, 28 | 9), 29 | Arguments.of( 30 | new int[]{9, 1, 8, 5, 2, 3, 5, 1, 0, 7}, 31 | 6, 32 | 5, 33 | 0, 34 | 9), 35 | Arguments.of( 36 | new int[]{9, 1, 8, 5, 2, 3, 5, 1, 0, 7}, 37 | 5, 38 | 5, 39 | 1, 40 | 9)); 41 | } 42 | 43 | private static Stream fromEquals0() { 44 | return Stream.of( 45 | Arguments.of( 46 | new int[]{1, 2, 3, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 9}, 47 | 6, 48 | 5, 49 | 0, 50 | 15), 51 | Arguments.of( 52 | new int[]{2, 4, 1, 9, 10}, 53 | 0, 54 | 3, 55 | 0, 56 | 4), 57 | Arguments.of( 58 | new int[]{44, 23, 2, 4, 1, 99, 9, 10, 11, 12, 13, 14, 15, 26, 37, 59 | 45}, 60 | 14, 61 | 3, 62 | 0, 63 | 14), 64 | Arguments.of( 65 | new int[]{11, 21, 30, 4, 15, 1, 26, 13, 2, 29, 12, 10, 17, 22, 25, 23, 66 | 18, 28, 6, 14, 16, 19, 0, 3, 5, 7, 27, 8, 9, 20, 24}, 67 | 15, 68 | 3, 69 | 0, 70 | 30)); 71 | } 72 | 73 | private static Stream edgeCases() { 74 | return Stream.of( 75 | Arguments.of( 76 | new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, 77 | 4, 78 | 3, 79 | 0, 80 | 9), 81 | Arguments.of( 82 | new int[]{9, 1, 8, 5, 2, 3, 5, 1, 0, 7}, 83 | 6, 84 | 5, 85 | 0, 86 | 9), 87 | Arguments.of( 88 | new int[]{9, 1, 8, 5, 2, 3, 5, 1, 0, 7}, 89 | 5, 90 | 5, 91 | 1, 92 | 9), 93 | Arguments.of( 94 | new int[]{9, 6, 2, 2, 2}, 95 | 4, 96 | 5, 97 | 4, 98 | 4), 99 | Arguments.of( 100 | new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, 101 | 4, 102 | 3, 103 | 0, 104 | 9), 105 | Arguments.of( 106 | new int[]{9, 1, 8, 5, 2, 3, 5, 1, 0, 7}, 107 | 6, 108 | 5, 109 | 0, 110 | 9), 111 | Arguments.of( 112 | new int[]{9, 1, 8, 5, 2, 3, 5, 1, 0, 7}, 113 | 5, 114 | 5, 115 | 1, 116 | 9), 117 | Arguments.of( 118 | new int[]{9, 1, 8}, 119 | 2, 120 | 3, 121 | 0, 122 | 2), 123 | Arguments.of( 124 | new int[]{9, 1}, 125 | 0, 126 | 3, 127 | 0, 128 | 1), 129 | Arguments.of( 130 | new int[]{9}, 131 | 0, 132 | 5, 133 | 0, 134 | 0), 135 | Arguments.of( 136 | new int[]{9, 8}, 137 | 0, 138 | 3, 139 | 0, 140 | 1) 141 | ); 142 | } 143 | } -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | You want to contribute and add tests to this repository? Great! We are happy to 4 | accept your contributions. Please read the following guidelines before you 5 | start. 6 | 7 | ## Guidelines for contributing 8 | 9 | **TL;DR** 10 | 11 | * PR title and description should use a prefix like `[W09] Add behavioral tests` 12 | * Separate your tests logically into small files 13 | * Don't add tests that are not needed for the exercise 14 | * Write deterministic tests 15 | * Use JUnit 5.9.1 16 | * Follow the Java Style Guide 17 | * Don't depend on changing the code of the implementation 18 | * Every test file should end with `Test.java` 19 | * Follow the package structure of the implementation 20 | * Only one test case per test method 21 | * Use descriptive names for test methods. 22 | 23 | ### PR Title and Description 24 | 25 | #### Title 26 | 27 | Please as prefix for your PR title the name of the exercise you are working on. 28 | 29 | ``` 30 | [W09] Add behavioral tests 31 | ``` 32 | 33 | If you don't changed the tests for an exercise, you can use `[*]` as prefix: 34 | 35 | ``` 36 | [*] ... 37 | ``` 38 | 39 | #### Description 40 | 41 | The description of your PR should help us understand what you are doing: For example, when fixing a bug, provide information on the bug and a brief sentence on how you fixed it. 42 | 43 | ### Separate your tests logically into small files 44 | 45 | Please don't add all your tests into one file. Instead, please create one file 46 | per test class. For example, you can create one test for every sub task. 47 | 48 | ### Don't add tests that are not needed for the exercise 49 | 50 | Please don't add tests that are not needed for the exercise. For example, if the 51 | exercise only requires you to implement a method `foo(int a)` and the exercise 52 | says that `a` is never negative, you don't need to test if `a` is negative. 53 | 54 | ### Write deterministic tests 55 | 56 | Please make sure that your tests are deterministic. For example, if you test a 57 | method that returns a random number, you should use a fixed seed for the 58 | random number generator. 59 | 60 | **BAD**: 61 | ```java 62 | Random random = new Random(); 63 | ``` 64 | 65 | **GOOD**: 66 | ```java 67 | // Use a fixed seed for the random number generator 68 | Random random = new Random(42); 69 | ``` 70 | 71 | ### Use JUnit 5.9.1 72 | 73 | We use JUnit 5.9.1 for testing. You can find the documentation 74 | [here](https://junit.org/junit5/docs/current/user-guide/). 75 | 76 | To use JUnit 5.9.1 in your project, you need to add the following dependency to your `build.gradle`. 77 | 78 | ```gradle 79 | dependencies { 80 | testImplementation 'org.junit.jupiter:junit-jupiter:5.9.1' 81 | } 82 | ``` 83 | 84 | ### Format your code 85 | 86 | Make sure that your code is formatted according to the [Google Java Style 87 | Guide](https://google.github.io/styleguide/javaguide.html). 88 | 89 | ### Don't depend on changing the code of the implementation 90 | 91 | The test should not say "you need to change the class `Foo` from `private` to `public`". 92 | 93 | In case you need to test a private method, you can use 94 | [Reflection](https://docs.oracle.com/javase/tutorial/reflect/index.html). 95 | 96 | ### Every test file should end with `Test.java` 97 | 98 | **BAD** 99 | ``` 100 | ReadWayPoints.java 101 | ``` 102 | 103 | **GOOD** 104 | ``` 105 | ReadWayPointsTest.java 106 | ``` 107 | 108 | ### Follow the package structure of the implementation 109 | 110 | You should copy the package structure of the implementation to the test. 111 | 112 | **BAD** 113 | ```java 114 | // Location of the implementation 115 | src/gad/pingu/ReadWayPoints.java 116 | 117 | // Location of the test file 118 | test/pingu/ReadWayPointsTest.java 119 | ``` 120 | 121 | **GOOD** 122 | ```java 123 | // Location of the implementation 124 | src/gad/pingu/ReadWayPoints.java 125 | 126 | // Location of the test file 127 | test/gad/pingu/ReadWayPointsTest.java 128 | ``` 129 | 130 | ### Only one test case per test method 131 | 132 | **BAD** 133 | 134 | ```java 135 | @Test 136 | public void testAddition() { 137 | assertEquals(2, 1 + 1); 138 | assertEquals(0, 0 + 0); 139 | assertEquals(-2, 5 + -7); 140 | } 141 | ``` 142 | 143 | **GOOD** 144 | 145 | ```java 146 | @Test 147 | public void testAdditionPositive() { 148 | assertEquals(2, 1 + 1); 149 | } 150 | 151 | @Test 152 | public void testAdditionZero() { 153 | assertEquals(0, 0 + 0); 154 | } 155 | 156 | @Test 157 | public void testAdditionNegative() { 158 | assertEquals(-2, 5 + -7); 159 | } 160 | ``` 161 | 162 | ### Use descriptive names for test methods. 163 | 164 | **BAD**: 165 | ```java 166 | @Test 167 | public void test1() { 168 | // ... 169 | } 170 | 171 | @Test 172 | public void test2() { 173 | // ... 174 | } 175 | 176 | @Test 177 | public void test3() { 178 | // ... 179 | } 180 | ``` 181 | 182 | 183 | **GOOD**: 184 | 185 | ```java 186 | @Test 187 | public void testEmptyList() { 188 | // ... 189 | } 190 | 191 | @Test 192 | public void testSingleElementList() { 193 | // ... 194 | } 195 | 196 | @Test 197 | public void testMultipleElementList() { 198 | // ... 199 | } 200 | ``` 201 | 202 | ### Make sure you set `expected` and `actual` correctly. 203 | 204 | The `assertEquals` (and other) methods have the following signature: 205 | 206 | ```java 207 | assertEquals(expected, actual); 208 | ``` 209 | 210 | **BAD**: 211 | ```java 212 | int expected = 2; 213 | int actual = 1 + 1; 214 | 215 | assertEquals(actual, expected); 216 | ``` 217 | 218 | **GOOD**: 219 | ```java 220 | int expected = 2; 221 | int actual = 1 + 1; 222 | 223 | assertEquals(expected, actual); 224 | ``` 225 | 226 | ### Use English as language 227 | 228 | Please use English as language for your test methods and comments. -------------------------------------------------------------------------------- /w10/test/gad/simplehash/FindFindAllTest.java: -------------------------------------------------------------------------------- 1 | package gad.simplehash; 2 | 3 | import org.junit.jupiter.params.ParameterizedTest; 4 | import org.junit.jupiter.params.provider.Arguments; 5 | import org.junit.jupiter.params.provider.MethodSource; 6 | 7 | import java.lang.reflect.Field; 8 | import java.util.ArrayList; 9 | import java.util.Arrays; 10 | import java.util.List; 11 | import java.util.Optional; 12 | import java.util.stream.Stream; 13 | 14 | import static org.junit.jupiter.api.Assertions.assertEquals; 15 | 16 | public class FindFindAllTest { 17 | // --- Find --- // 18 | 19 | private static Stream testFind() { 20 | return Stream.of( 21 | Arguments.of(1, new int[]{1}, "x", new List[]{new ArrayList>()}, null), 22 | Arguments.of(1, new int[]{1}, "x", new List[]{new ArrayList>() { 23 | { 24 | add(new Pair<>("x", "foo")); 25 | } 26 | }}, "foo"), 27 | Arguments.of(1, new int[]{1}, "x", new List[]{new ArrayList>() { 28 | { 29 | add(new Pair<>("x", "foo")); 30 | add(new Pair<>("y", "bar")); 31 | } 32 | }}, "foo"), 33 | Arguments.of(1, new int[]{1}, "y", new List[]{new ArrayList>() { 34 | { 35 | add(new Pair<>("x", "foo")); 36 | add(new Pair<>("y", "bar")); 37 | } 38 | }}, "bar"), 39 | 40 | Arguments.of(1, new int[]{1}, new String("y"), new List[]{new ArrayList>() { 41 | { 42 | add(new Pair<>("x", "foo")); 43 | add(new Pair<>("y", "bar")); 44 | } 45 | }}, "bar") 46 | 47 | ); 48 | 49 | } 50 | 51 | @ParameterizedTest 52 | @MethodSource 53 | public void testFind(int minSize, int[] a, String k, List>[] table, String expect) 54 | throws Exception { 55 | Hashtable hashtable = new Hashtable<>(minSize, a); 56 | 57 | ModuloHelper mH = (i, divisor) -> i % divisor; 58 | 59 | Field tableField = hashtable.getClass().getDeclaredField("table"); 60 | tableField.setAccessible(true); 61 | tableField.set(hashtable, table); 62 | 63 | Optional result = hashtable.find(k, mH); 64 | 65 | if (expect == null) { 66 | assertEquals(Optional.empty(), result); 67 | } else { 68 | assertEquals(Optional.of(expect), result); 69 | } 70 | } 71 | 72 | // --- FindAll --- // 73 | 74 | private static Stream testFindAll() { 75 | return Stream.of( 76 | Arguments.of(1, new int[]{1}, "x", new List[]{new ArrayList>()}, 77 | new String[]{}), 78 | Arguments.of(1, new int[]{1}, "x", new List[]{new ArrayList>() { 79 | { 80 | add(new Pair<>("x", "foo")); 81 | } 82 | }}, new String[]{"foo"}), 83 | Arguments.of(1, new int[]{1}, "x", new List[]{new ArrayList>() { 84 | { 85 | add(new Pair<>("x", "foo")); 86 | add(new Pair<>("y", "bar")); 87 | } 88 | }}, new String[]{"foo"}), 89 | Arguments.of(1, new int[]{1}, "y", new List[]{new ArrayList>() { 90 | { 91 | add(new Pair<>("x", "foo")); 92 | add(new Pair<>("y", "bar")); 93 | } 94 | }}, new String[]{"bar"}), 95 | Arguments.of(1, new int[]{1}, "z", new List[]{new ArrayList>() { 96 | { 97 | add(new Pair<>("x", "foo")); 98 | add(new Pair<>("y", "bar")); 99 | } 100 | }}, new String[]{}), 101 | Arguments.of(1, new int[]{1}, "x", new List[]{new ArrayList>() { 102 | { 103 | add(new Pair<>("x", "foo")); 104 | add(new Pair<>("x", "bar")); 105 | } 106 | }}, new String[]{"foo", "bar"}), 107 | Arguments.of(1, new int[]{1}, "x", new List[]{new ArrayList>() { 108 | { 109 | add(new Pair<>("x", "foo")); 110 | add(new Pair<>("x", "bar")); 111 | add(new Pair<>("y", "baz")); 112 | } 113 | }}, new String[]{"foo", "bar"}), 114 | 115 | Arguments.of(1, new int[]{1}, new String("x"), new List[]{new ArrayList>() { 116 | { 117 | add(new Pair<>("x", "foo")); 118 | add(new Pair<>("x", "bar")); 119 | add(new Pair<>("y", "baz")); 120 | } 121 | }}, new String[]{"foo", "bar"}) 122 | 123 | ); 124 | 125 | } 126 | 127 | @ParameterizedTest 128 | @MethodSource 129 | public void testFindAll(int minSize, int[] a, String k, List>[] table, String[] expect) 130 | throws Exception { 131 | Hashtable hashtable = new Hashtable<>(minSize, a); 132 | 133 | ModuloHelper mH = (i, divisor) -> i % divisor; 134 | 135 | Field tableField = hashtable.getClass().getDeclaredField("table"); 136 | tableField.setAccessible(true); 137 | tableField.set(hashtable, table); 138 | 139 | List result = hashtable.findAll(k, mH); 140 | 141 | assertEquals(Arrays.asList(expect), result); 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /w02/test/gad/logging/loggingTest.java: -------------------------------------------------------------------------------- 1 | package gad.logging; 2 | 3 | import gad.binarysearch.BinSea; 4 | import gad.binarysearch.Interval; 5 | import org.junit.jupiter.params.ParameterizedTest; 6 | import org.junit.jupiter.params.provider.Arguments; 7 | import org.junit.jupiter.params.provider.MethodSource; 8 | 9 | import java.util.stream.Stream; 10 | 11 | public class loggingTest { 12 | 13 | // ---- SimpleSearch ---- // 14 | private static Stream testLoggingSimpleSearch() { 15 | return Stream.of( 16 | // normal 17 | Arguments.of(new int[]{1, 2, 3, 4}, 2, new int[][]{{1}}), 18 | Arguments.of(new int[]{6, 23, 45, 67, 89, 90, 100}, 90, new int[][]{{3, 5}}), 19 | // values not in array 20 | Arguments.of(new int[]{1, 2, 3, 4}, 5, new int[][]{{1, 2}, {1, 2, 3}}), 21 | Arguments.of(new int[]{1, 2}, 0, new int[][]{{0}}), 22 | // duplicates in array 23 | Arguments.of(new int[]{1, 2, 2, 2, 6, 7, 8, 9, 10}, 2, new int[][]{{4, 1}}), 24 | Arguments.of(new int[]{1, 2, 2, 2, 6, 7, 8, 9, 10}, 6, new int[][]{{4}})); 25 | } 26 | 27 | @ParameterizedTest 28 | @MethodSource 29 | public void testLoggingSimpleSearch(int[] sortedData, int value, int[][] possibleSolutions) { 30 | TestResultImplementation result = new TestResultImplementation(possibleSolutions); 31 | BinSea.search(sortedData, value, result); 32 | result.testLogging(possibleSolutions); 33 | } 34 | 35 | // ---- BoundSearch ---- // 36 | private static Stream testLoggingBoundSearch() { 37 | return Stream.of( 38 | // normal 39 | Arguments.of(new int[]{1, 2, 3, 4}, 1, true, new int[][]{{1}, {1, 0}}), 40 | Arguments.of(new int[]{1, 2, 3, 4, 5, 6, 7, 8}, 2, false, new int[][]{{3, 1}}), 41 | // values not in array 42 | Arguments.of(new int[]{1, 2, 2, 2, 6, 7, 8, 9, 10}, -1, true, new int[][]{{4, 1}, {4, 1, 0}}), 43 | Arguments.of(new int[]{1, 2, 2, 4, 4, 4, 4, 8, 8, 27, 89}, 3, false, 44 | new int[][]{{5, 2, 3}}), 45 | // duplicates in array 46 | Arguments.of(new int[]{1, 2, 2, 4, 4, 4, 4, 8, 8, 27, 89}, 4, true, new int[][]{{5}}), 47 | Arguments.of(new int[]{1, 2, 2, 4, 4, 4, 4, 8, 8, 27, 89}, 4, false, 48 | new int[][]{{5}}), 49 | // random values 50 | Arguments.of(new int[]{1, 4, 4, 4, 5, 5, 5, 5, 8, 10, 23}, 6, false, 51 | new int[][]{{5, 8, 6}, {5, 8, 6, 7}}), 52 | Arguments.of(new int[]{1, 4, 4, 4, 5, 5, 5, 5, 8}, 9, true, new int[][]{{4, 6, 7}, {4, 6, 7, 8}}), 53 | Arguments.of(new int[]{1, 2, 2, 4, 4, 4, 4, 8, 8, 27, 89}, 25, true, 54 | new int[][]{{5, 8, 9}})); 55 | } 56 | 57 | @ParameterizedTest 58 | @MethodSource 59 | public void testLoggingBoundSearch(int[] sortedData, int value, boolean lowerBound, int[][] possibleSolutions) { 60 | TestResultImplementation result = new TestResultImplementation(possibleSolutions); 61 | BinSea.search(sortedData, value, lowerBound, result); 62 | result.testLogging(possibleSolutions); 63 | } 64 | 65 | // ---- IntervalSearch ---- // 66 | private static Stream testLoggingIntervalSearch() { 67 | return Stream.of( 68 | // random 69 | Arguments.of( 70 | new int[]{1, 4, 4, 4, 5, 5, 5, 5, 8}, 71 | Interval.NonEmptyInterval.fromArrayIndices(1, 4), 72 | new int[][]{{4, 1}, {4, 1, 0}}, 73 | new int[][]{{4, 1}}), 74 | Arguments.of( 75 | new int[]{1, 4, 4, 4, 5, 5, 5, 5, 8}, 76 | Interval.NonEmptyInterval.fromArrayIndices(2, 5), 77 | new int[][]{{4, 1}, {4, 1, 0}}, 78 | new int[][]{{4}}), 79 | Arguments.of( 80 | new int[]{1, 4, 4, 4, 5, 5, 5, 5, 8, 10, 23}, 81 | Interval.NonEmptyInterval.fromArrayIndices(3, 9), 82 | new int[][]{{5, 2, 0}, {5, 2, 0, 1}}, 83 | new int[][]{{5, 8, 9}}), 84 | Arguments.of( 85 | new int[]{1, 4, 4, 4, 5, 5, 5, 5, 8, 10, 23}, 86 | Interval.NonEmptyInterval.fromArrayIndices(8, 8), 87 | new int[][]{{5, 8}}, 88 | new int[][]{{5, 8}}), 89 | // empty 90 | Arguments.of( 91 | new int[]{1, 4, 4, 4, 5, 5, 5, 5, 8}, 92 | Interval.NonEmptyInterval.fromArrayIndices(6, 7), 93 | new int[][]{{4, 6, 7}, {4, 6, 7, 8}}, 94 | new int[][]{{4, 6, 7}, {4, 6, 7, 8}}), 95 | Arguments.of( 96 | new int[]{1, 4, 4, 4, 5, 5, 5, 5, 8, 2325}, 97 | Interval.NonEmptyInterval.fromArrayIndices(235, 2311), 98 | new int[][]{{4, 7, 8}, {4, 7, 8, 9}}, 99 | new int[][]{{4, 7, 8}, {4, 7, 8, 9}}), 100 | // only lower logging 101 | Arguments.of( 102 | new int[]{1, 2, 3}, 103 | Interval.NonEmptyInterval.fromArrayIndices(4, 5), 104 | new int[][]{{1}, {1, 2}}, 105 | new int[][]{{}}), 106 | Arguments.of( 107 | new int[]{1, 4, 4, 4, 5, 5, 5, 5, 8}, 108 | Interval.NonEmptyInterval.fromArrayIndices(10, 16), 109 | new int[][]{{4, 6, 7}, {4, 6, 7, 8}}, 110 | new int[][]{{}})); 111 | } 112 | 113 | @ParameterizedTest 114 | @MethodSource 115 | public void testLoggingIntervalSearch(int[] sortedData, Interval.NonEmptyInterval interval, 116 | int[][] expectLowerPossibilities, int[][] expectHigherPossibilities) { 117 | TestResultImplementation resultLower = new TestResultImplementation(expectLowerPossibilities); 118 | TestResultImplementation resultHigher = new TestResultImplementation(expectHigherPossibilities); 119 | BinSea.search(sortedData, interval, resultLower, resultHigher); 120 | resultLower.testLogging(expectLowerPossibilities, "lower "); 121 | resultHigher.testLogging(expectHigherPossibilities, "higher "); 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /w04/test/gad/simplesort/MedianPivotTest.java: -------------------------------------------------------------------------------- 1 | package gad.simplesort; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | 5 | import java.util.stream.Stream; 6 | 7 | import org.junit.jupiter.params.ParameterizedTest; 8 | import org.junit.jupiter.params.provider.Arguments; 9 | import org.junit.jupiter.params.provider.MethodSource; 10 | 11 | public class MedianPivotTest { 12 | 13 | @ParameterizedTest 14 | @MethodSource({"artemisBeispiele", "numberOfConsideredElementsEqualToLength", "numberOfConsideredElementsSmaller", "numberOfConsideredElementsBigger", "deviatingFromAndTo"}) 15 | void medianPivotTest(int[] numbers, int expected, int numberOfConsideredElements, int from, int to) { 16 | PivotFinder medianPivotFinder = PivotFinder.getMedianPivotFront(numberOfConsideredElements); 17 | assertEquals(expected, medianPivotFinder.findPivot(numbers, from, to)); 18 | } 19 | 20 | private static Stream artemisBeispiele() { 21 | return Stream.of( 22 | Arguments.of( 23 | new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, 24 | 1, 25 | 3, 26 | 0, 27 | 9 28 | ), 29 | Arguments.of( 30 | new int[]{9, 1, 8, 5, 2, 3, 5, 1, 0, 7}, 31 | 3, 32 | 5, 33 | 0, 34 | 9 35 | ), 36 | Arguments.of( 37 | new int[]{9, 1, 8, 5, 2, 3, 5, 1, 0, 7}, 38 | 3, 39 | 5, 40 | 2, 41 | 8 42 | ) 43 | ); 44 | } 45 | 46 | private static Stream numberOfConsideredElementsEqualToLength() { 47 | return Stream.of( 48 | Arguments.of( 49 | new int[]{1, 5, 5, 5, 5, 5, 5, 7, 8}, 50 | 1, 51 | 9, 52 | 0, 53 | 8 54 | ), 55 | Arguments.of( 56 | new int[]{1, 2, 3, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 9}, 57 | 5, 58 | 16, 59 | 0, 60 | 15 61 | ), 62 | Arguments.of( 63 | new int[]{2, 4, 1, 9, 10}, 64 | 1, 65 | 5, 66 | 0, 67 | 4 68 | ), 69 | Arguments.of( 70 | new int[]{2, 4, 1, 9, 10, 11, 12, 13, 14, 15, 26, 37, 45}, 71 | 6, 72 | 13, 73 | 0, 74 | 12 75 | ), 76 | Arguments.of( 77 | new int[]{44, 23, 2, 4, 1, 99, 9, 10, 11, 12, 13, 14, 15, 26, 37, 45}, 78 | 10, 79 | 16, 80 | 0, 81 | 15 82 | ), 83 | Arguments.of( 84 | new int[]{11, 21, 30, 4, 15, 1, 26, 13, 2, 29, 12, 10, 17, 22, 25, 24, 18, 28, 6, 14, 16, 19, 3, 5, 7, 27, 8, 9, 20, 23, 0}, 85 | 4, 86 | 31, 87 | 0, 88 | 30 89 | ) 90 | ); 91 | } 92 | 93 | private static Stream numberOfConsideredElementsSmaller() { 94 | return Stream.of( 95 | Arguments.of( 96 | new int[]{2, 4, 1, 9, 10}, 97 | 0, 98 | 3, 99 | 0, 100 | 5 101 | ), 102 | Arguments.of( 103 | new int[]{2, 4, 1, 9, 10, 11, 12, 13, 14, 15, 26, 37, 45}, 104 | 1, 105 | 5, 106 | 0, 107 | 12 108 | ), 109 | Arguments.of( 110 | new int[]{44, 23, 2, 4, 1, 99, 9, 10, 11, 12, 13, 14, 15, 26, 37, 45}, 111 | 6, 112 | 7, 113 | 0, 114 | 15 115 | ), 116 | Arguments.of( 117 | new int[]{11, 21, 30, 4, 15, 1, 26, 13, 2, 29, 12, 10, 17, 22, 25, 24, 18, 28, 6, 14, 16, 19, 3, 5, 7, 27, 8, 9, 20, 23, 0}, 118 | 10, 119 | 12, 120 | 0, 121 | 30 122 | ), 123 | Arguments.of( 124 | new int[]{44, 23, 2, 4, 1, 99, 9, 10, 11, 12, 13, 14, 15, 26, 37, 45}, 125 | 0, 126 | 16 + Integer.MAX_VALUE, 127 | 0, 128 | 15 129 | ), 130 | Arguments.of( 131 | new int[]{11, 21, 30, 4, 15, 1, 26, 13, 2, 29, 12, 10, 17, 22, 25, 24, 18, 28, 6, 14, 16, 19, 3, 5, 7, 27, 8, 9, 20, 23, 0}, 132 | 0, 133 | -31, 134 | 0, 135 | 30 136 | ) 137 | ); 138 | } 139 | 140 | private static Stream numberOfConsideredElementsBigger() { 141 | return Stream.of( 142 | Arguments.of( 143 | new int[]{2, 4, 1, 9, 10}, 144 | 1, 145 | 6, 146 | 0, 147 | 4 148 | ), 149 | Arguments.of( 150 | new int[]{2, 4, 1, 9, 10, 11, 12, 13, 14, 15, 26, 37, 45}, 151 | 6, 152 | 14, 153 | 0, 154 | 12 155 | ) 156 | ); 157 | } 158 | 159 | private static Stream deviatingFromAndTo() { 160 | return Stream.of( 161 | Arguments.of( 162 | new int[]{2, 4, 1, 9, 10}, 163 | 0, 164 | 5, 165 | 0, 166 | 3 167 | ), 168 | Arguments.of( 169 | new int[]{2, 4, 1, 9, 10, 11, 12, 13, 14, 15, 26, 37, 45}, 170 | 5, 171 | 13, 172 | 0, 173 | 10 174 | ), 175 | Arguments.of( 176 | new int[]{44, 23, 2, 4, 1, 99, 9, 10, 11, 12, 13, 14, 15, 26, 37, 45}, 177 | 10, 178 | 17, 179 | 0, 180 | 17 181 | ), 182 | Arguments.of( 183 | new int[]{11, 21, 30, 4, 15, 1, 26, 13, 2, 29, 12, 10, 17, 22, 25, 24, 18, 28, 6, 14, 16, 19, 3, 5, 7, 27, 8, 9, 20, 23, 0}, 184 | 19, 185 | 31, 186 | 3, 187 | 30 188 | ), 189 | Arguments.of( 190 | new int[]{2, 4, 1, 9, 10}, 191 | 1, 192 | 5, 193 | 1, 194 | 3 195 | ), 196 | Arguments.of( 197 | new int[]{2, 4, 1, 9, 10, 11, 12, 13, 14, 15, 26, 37, 45}, 198 | 11, 199 | 13, 200 | 10, 201 | 12 202 | ), 203 | Arguments.of( 204 | new int[]{44, 23, 2, 4, 1, 99, 9, 10, 11, 12, 13, 14, 15, 26, 37, 45}, 205 | 7, 206 | 5, 207 | 4, 208 | 12 209 | ), 210 | Arguments.of( 211 | new int[]{11, 21, 30, 4, 15, 1, 26, 13, 2, 29, 12, 10, 17, 22, 25, 24, 18, 28, 6, 14, 16, 19, 3, 5, 7, 27, 8, 9, 20, 23, 0}, 212 | 16, 213 | 8, 214 | 9, 215 | 23 216 | ) 217 | ); 218 | } 219 | } -------------------------------------------------------------------------------- /w04/test/gad/simplesort/DoubleMedianPivotTest.java: -------------------------------------------------------------------------------- 1 | package gad.simplesort; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 4 | 5 | import java.util.stream.Stream; 6 | 7 | import org.junit.jupiter.api.DisplayName; 8 | import org.junit.jupiter.params.ParameterizedTest; 9 | import org.junit.jupiter.params.provider.Arguments; 10 | import org.junit.jupiter.params.provider.MethodSource; 11 | 12 | public class DoubleMedianPivotTest { 13 | 14 | @ParameterizedTest 15 | @MethodSource("deviatingNumbersOfConsideredElements") 16 | @DisplayName("Based on Artemis") 17 | void doubleMedianPivotTest(int[] numbers, int[] expected, int numbersOfConsideredElements, int from, int to) { 18 | DualPivotFinder dualPivotFinder = DualPivotFinder.getMedianPivotFront(numbersOfConsideredElements); 19 | assertArrayEquals(expected, dualPivotFinder.findPivot(numbers, from, to)); 20 | } 21 | 22 | @ParameterizedTest 23 | @MethodSource({"notRequire", "hard"}) 24 | @DisplayName("General Tests not necessary") 25 | void doubleMedianPivotTest2(int[] numbers, int[] expected, int numbersOfConsideredElements, int from, int to) { 26 | DualPivotFinder dualPivotFinder = DualPivotFinder.getMedianPivotFront(numbersOfConsideredElements); 27 | assertArrayEquals(expected, dualPivotFinder.findPivot(numbers, from, to)); 28 | } 29 | 30 | 31 | 32 | private static Stream deviatingNumbersOfConsideredElements() { 33 | return Stream.of( 34 | Arguments.of( 35 | new int[] { 4, 9, 1, 10, 2 }, 36 | new int[] { 1, 4 }, 37 | 5, 38 | 0, 39 | 4), 40 | Arguments.of( 41 | new int[] { 4, 9, 1, 10, 2, 5, 99, 23, 3, 35, 6 }, 42 | new int[] { 1, 2 }, 43 | 5, 44 | 1, 45 | 4), 46 | Arguments.of( 47 | new int[] { 4, 9, 1, 10, 2 }, 48 | new int[] { 1, 4 }, 49 | 5, 50 | 0, 51 | 4), 52 | Arguments.of( 53 | new int[] { 4, 9, 1, 10, 2 }, 54 | new int[] { 1, 4 }, 55 | 5, 56 | 0, 57 | 4)); 58 | } 59 | 60 | private static Stream notRequire() 61 | { 62 | return Stream.of( 63 | Arguments.of( 64 | new int[] { 4, 9, 1, 10, 2, 5, 99, 23, 3, 35, 6 }, 65 | new int[] { 0, 3 }, 66 | 11, 67 | 0, 68 | 10), 69 | Arguments.of( 70 | new int[] { 8, 42, 10, 75, 29, 77, 38, 57, 24 }, 71 | new int[] { 1, 8 }, 72 | 9, 73 | 0, 74 | 8), 75 | Arguments.of( 76 | new int[] { 24, 8, 76, 10, 75, 29, 77, 38, 57 }, 77 | new int[] { 0, 8 }, 78 | 9, 79 | 0, 80 | 8), 81 | Arguments.of( 82 | new int[] { 11, 21, 30, 4, 15, 1, 26, 13, 2, 29, 12, 10, 17, 22, 25, 24, 83 | 18, 28, 6, 14, 16, 19, 3, 5, 7, 27, 8, 9, 20, 23, 0 }, 84 | new int[] { 27, 28 }, 85 | 31, 86 | 0, 87 | 30), 88 | Arguments.of( 89 | new int[] { 5, 6, 5, 6, 55, 6, 5, 6, 55, 6, 5, 6, 5 }, 90 | new int[] { 0, 1 }, 91 | 13, 92 | 0, 93 | 12), 94 | Arguments.of( 95 | new int[] { 2, 4, 8, 8, 8, 12, 14, 26, 26, 28, 30, 30, 30, 30, 34, 36, 96 | 36 }, 97 | new int[] { 5, 10 }, 98 | 17, 99 | 0, 100 | 16), 101 | Arguments.of( 102 | new int[] { 5, 6, 7, 44, 44, 44, 44, 44, 44, 44, 44 }, 103 | new int[] { 3, 4 }, 104 | 11, 105 | 0, 106 | 10), 107 | Arguments.of( 108 | new int[] { 16, 15, 14, 12, 12, 12, 11, 10, 10, 10, 10, 4, 2 }, 109 | new int[] { 3, 7 }, 110 | 13, 111 | 0, 112 | 12), 113 | Arguments.of( 114 | new int[] { 16, 15, 14, 12, 12, 12, 10, 10, 10, 10, 4, 2 }, 115 | new int[] { 3, 6 }, 116 | 12, 117 | 0, 118 | 11), 119 | Arguments.of( 120 | new int[] { 10, 10, 10, 15, 10, 20, 10, 10 }, 121 | new int[] { 0, 1 }, 122 | 8, 123 | 0, 124 | 7), 125 | Arguments.of( 126 | new int[] { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, 127 | new int[] { 0, 1 }, 128 | 10, 129 | 0, 130 | 9), 131 | Arguments.of( 132 | new int[] { 11, 21, 30, 4, 15, 1, 26, 13, 2, 29, 12, 10, 17, 22, 25, 24, 133 | 18, 28, 6, 14, 16, 19, 3, 5, 7, 27, 8, 9, 20, 23, 0 }, 134 | new int[] { 1, 3 }, 135 | 31, 136 | 0, 137 | 9), 138 | Arguments.of( 139 | new int[] { 11, 21, 30, 4, 15, 1, 26, 13, 2, 29, 12, 10, 17, 22, 25, 24, 140 | 18, 28, 6, 14, 16, 19, 3, 5, 7, 27, 8, 9, 20, 23, 0 }, 141 | new int[] { 0, 1 }, 142 | 31, 143 | 0, 144 | 2), 145 | Arguments.of( 146 | new int[] { 11, 21, 30, 4, 15, 1, 26, 13, 2, 29, 12, 10, 17, 22, 25, 24, 147 | 18, 28, 6, 14, 16, 19, 3, 5, 7, 27, 8, 9, 20, 23, 0 }, 148 | new int[] { 10, 15 }, 149 | 31, 150 | 5, 151 | 15), 152 | Arguments.of( 153 | new int[] { 11, 21, 30, 4, 15, 1, 26, 13, 2, 29, 12, 10, 17, 22, 25, 24, 154 | 18, 28, 6, 14, 16, 19, 3, 5, 7, 27, 8, 9, 20, 23, 0 }, 155 | new int[] { 3, 4 }, 156 | 6, 157 | 2, 158 | 30), 159 | Arguments.of( 160 | new int[] { 11, 21, 30, 4, 15, 1, 26, 13, 2, 29, 12, 10, 17, 22, 25, 24, 161 | 18, 28, 6, 14, 16, 19, 3, 5, 7, 27, 8, 9, 20, 23, 0 }, 162 | new int[] { 4, 8 }, 163 | 7, 164 | 4, 165 | 30), 166 | Arguments.of( 167 | new int[] { 11, 21, 30, 4, 15, 1, 26, 13, 2, 29, 12, 10, 17, 22, 25, 24, 168 | 18, 28, 6, 14, 16, 19, 3, 5, 7, 27, 8, 9, 20, 23, 0 }, 169 | new int[] { 10, 13 }, 170 | 8, 171 | 6, 172 | 30) 173 | ); 174 | } 175 | /** 176 | *
177 | * This Testcase is a bitch. It's nearly impossible to pass it. 178 | *
179 | *
180 | * Kudos to you if you can pass this testcase. It took me all night getting the 181 | * algorithm right. 182 | *
183 | */ 184 | private static Stream hard() { 185 | return Stream.of( 186 | Arguments.of( 187 | new int[] { 5, 6, 5, 6, 55, 6, 5, 6, 5, 6, 55, 6, 5, 6, 5, 6, 55 }, 188 | new int[] { 0, 1 }, 189 | 17, 190 | 0, 191 | 16), 192 | Arguments.of( 193 | new int[] { 44, 44, 44, 5, 6, 44, 44, 7, 44, 44, 44 }, 194 | new int[] { 0, 1 }, 195 | 11, 196 | 0, 197 | 10)); 198 | } 199 | } -------------------------------------------------------------------------------- /w03/test/gad/dynamicarray/UnitTest.java: -------------------------------------------------------------------------------- 1 | package gad.dynamicarray; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 4 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | import static org.junit.jupiter.api.Assertions.assertThrows; 7 | import static org.junit.jupiter.api.Assertions.fail; 8 | 9 | import java.lang.reflect.Field; 10 | import java.util.Arrays; 11 | 12 | import org.junit.jupiter.api.Disabled; 13 | import org.junit.jupiter.api.DisplayName; 14 | import org.junit.jupiter.api.RepeatedTest; 15 | import org.junit.jupiter.api.RepetitionInfo; 16 | 17 | import org.junit.jupiter.api.Test; 18 | 19 | public class UnitTest { 20 | // 21 | // 22 | // 23 | 24 | private static TDA Arr(int growthFactor, int maxOverhead) { 25 | return new TDA(growthFactor, maxOverhead); 26 | } 27 | 28 | private static TDS Stck(int growthFactor, int maxOverhead) { 29 | return new TDS(growthFactor, maxOverhead); 30 | } 31 | 32 | private static TDQ Queue(int growthFactor, int maxOverhead) { 33 | return new TDQ(growthFactor, maxOverhead); 34 | } 35 | 36 | private static TDSQ StkQu(int growthFactor, int maxOverhead) { 37 | return new TDSQ(growthFactor, maxOverhead); 38 | } 39 | 40 | private static int[] Ints(int... args) { 41 | return args; 42 | } 43 | 44 | private static Interval.EmptyInterval Ival() { 45 | return Interval.EmptyInterval.getEmptyInterval(); 46 | } 47 | 48 | private static Interval.NonEmptyInterval Ival(int from, int to) { 49 | return new Interval.NonEmptyInterval(from, to); 50 | } 51 | 52 | // Test Dynamic Array Test Result 53 | private static class TDATR implements Result { 54 | public int[] array; 55 | 56 | public void logArray(int[] array) { 57 | this.array = array; 58 | } 59 | } 60 | 61 | // Test Dynamic Array 62 | private static class TDA extends DynamicArray { 63 | public TDA(int growthFactor, int maxOverhead) { 64 | super(growthFactor, maxOverhead); 65 | } 66 | 67 | /** 68 | * args = ( [index, value], ... ) 69 | */ 70 | public TDA sets(int... args) { 71 | for (int i = 0; i < args.length; i += 2) { 72 | super.set(args[i], args[i + 1]); 73 | } 74 | return this; 75 | } 76 | 77 | public TDA nest(Interval i, int m) { 78 | super.reportUsage(i, m); 79 | return this; 80 | } 81 | 82 | public TDA nest(Interval i, int m, Interval expect) { 83 | assertEquals(expect, super.reportUsage(i, m)); 84 | return this; 85 | } 86 | 87 | public TDA test(int[] expected) { 88 | assertArrayEquals(expected, getArray()); 89 | return this; 90 | } 91 | 92 | public int[] getArray() { 93 | var t = new TDATR(); 94 | this.reportArray(t); 95 | return t.array; 96 | } 97 | 98 | // public String getArrayString() { 99 | // var t = new TDATR(); 100 | // this.reportArray(t); 101 | // return Arrays.toString(t.array); 102 | // } 103 | } 104 | 105 | private static class TestComponent { 106 | public Data data; 107 | public TDA array; 108 | 109 | // Test Dynamic Array 110 | public TestComponent(int growthFactor, int maxOverhead) { 111 | array = new TDA(growthFactor, maxOverhead); 112 | } 113 | 114 | public void setChildArray() { 115 | try { 116 | Field f1 = data.getClass().getDeclaredField("array"); 117 | f1.setAccessible(true); 118 | f1.set(data, array); 119 | } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) { 120 | fail(e.getMessage()); 121 | } 122 | } 123 | 124 | public TestComponent test(int[] expected) { 125 | assertArrayEquals(expected, getArray(), String.format("%nexpected:\t%s%ngot:\t%s%n", 126 | Arrays.toString(expected), Arrays.toString(getArray()))); 127 | return this; 128 | } 129 | 130 | public int[] getArray() { 131 | try { 132 | Field f1 = DynamicStack.class.getDeclaredField("array"); 133 | f1.setAccessible(true); 134 | var k = (TDA) f1.get(data); 135 | return k.getArray(); 136 | } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { 137 | throw new RuntimeException(e); 138 | } 139 | } 140 | } 141 | 142 | // Test Dynamic Stack 143 | private static class TDS extends TestComponent { 144 | public TDS(int growthFactor, int maxOverhead) { 145 | super(growthFactor, maxOverhead); 146 | data = new DynamicStack(growthFactor, maxOverhead, new TDATR()); 147 | setChildArray(); 148 | } 149 | 150 | public TDS pushes(int... args) { 151 | for (int i = 0; i < args.length; i++) { 152 | data.pushBack(args[i]); 153 | } 154 | return this; 155 | } 156 | 157 | public TDS popCount(int count) { 158 | for (int i = 0; i < count; i++) { 159 | data.popBack(); 160 | } 161 | return this; 162 | } 163 | 164 | public TDS pops(int... args) { 165 | for (int i = 0; i < args.length; i++) { 166 | assertEquals(args[i], data.popBack()); 167 | } 168 | return this; 169 | } 170 | 171 | @Override 172 | public TDS test(int[] expected) { 173 | return (TDS) super.test(expected); 174 | } 175 | } 176 | 177 | // Test Dynamic (Ring) Queue 178 | private static class TDQ extends TestComponent { 179 | // Test Dynamic Array 180 | public TDQ(int growthFactor, int maxOverhead) { 181 | super(growthFactor, maxOverhead); 182 | data = new RingQueue(growthFactor, maxOverhead, new TDATR()); 183 | 184 | try { 185 | Field f1 = RingQueue.class.getDeclaredField("array"); 186 | f1.setAccessible(true); 187 | f1.set(data, new TDA(growthFactor, maxOverhead)); 188 | } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) { 189 | fail(e.getMessage()); 190 | } 191 | } 192 | 193 | public TDQ pushes(int... args) { 194 | for (int i = 0; i < args.length; i++) { 195 | data.pushBack(args[i]); 196 | } 197 | return this; 198 | } 199 | 200 | public TDQ popCount(int count) { 201 | for (int i = 0; i < count; i++) { 202 | data.popFront(); 203 | } 204 | return this; 205 | } 206 | 207 | public TDQ pops(int... args) { 208 | for (int i = 0; i < args.length; i++) { 209 | assertEquals(args[i], data.popFront()); 210 | } 211 | return this; 212 | } 213 | 214 | @Override 215 | public TDQ test(int[] expected) { 216 | return (TDQ) super.test(expected); 217 | } 218 | 219 | @Override 220 | public int[] getArray() { 221 | try { 222 | // System.out.println(Arrays.toString(DynamicStack.class.getDeclaredFields())); 223 | Field f1 = RingQueue.class.getDeclaredField("array"); 224 | f1.setAccessible(true); 225 | var k = (TDA) f1.get(data); 226 | return k.getArray(); 227 | } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { 228 | throw new RuntimeException(e); 229 | } 230 | } 231 | } 232 | 233 | // Test Dynamic (Stacky) Queue 234 | private static class TDSQ extends StackyQueue { 235 | public TDS s1; 236 | public TDS s2; 237 | 238 | public TDSQ(int growthFactor, int maxOverhead) { 239 | super(growthFactor, maxOverhead, new TDATR(), new TDATR()); 240 | 241 | s1 = new TDS(growthFactor, maxOverhead); 242 | s2 = new TDS(growthFactor, maxOverhead); 243 | 244 | try { 245 | Field f1 = StackyQueue.class.getDeclaredField("first"); 246 | f1.setAccessible(true); 247 | f1.set(this, s1.data); 248 | } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { 249 | throw new RuntimeException(e); 250 | } 251 | 252 | try { 253 | Field f1 = StackyQueue.class.getDeclaredField("second"); 254 | f1.setAccessible(true); 255 | f1.set(this, s2.data); 256 | } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { 257 | throw new RuntimeException(e); 258 | } 259 | } 260 | 261 | public TDSQ pushes(int... args) { 262 | for (int i = 0; i < args.length; i++) { 263 | pushBack(args[i]); 264 | } 265 | return this; 266 | } 267 | 268 | public TDSQ popCount(int count) { 269 | for (int i = 0; i < count; i++) { 270 | popFront(); 271 | } 272 | return this; 273 | } 274 | 275 | public TDSQ pops(int... args) { 276 | for (int i = 0; i < args.length; i++) { 277 | assertEquals(args[i], popFront()); 278 | } 279 | return this; 280 | } 281 | 282 | public TDSQ test(int[] expected, int[] expected2) { 283 | s1.test(expected); 284 | s2.test(expected2); 285 | return this; 286 | } 287 | } 288 | 289 | // 290 | // 291 | // 292 | 293 | // 294 | // 295 | // 296 | 297 | @Test 298 | @DisplayName("Artemis Array Initialization") 299 | public void artemis1() { 300 | Arr(3, 4) 301 | .nest(Ival(), 1, Ival()) 302 | .sets(2, 1, 0, 3) 303 | .test(Ints(3, 0, 1)); 304 | } 305 | 306 | @Test 307 | public void artemis2() { 308 | Arr(3, 4) 309 | .nest(Ival(), 1) 310 | .sets(2, 1, 0, 3) 311 | .nest(Ival(1, 2), 4, Ival(0, 1)) 312 | .test(Ints(0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)); 313 | } 314 | 315 | @Test 316 | @Disabled 317 | public void artemis3() { 318 | Arr(3, 4) 319 | .nest(Ival(), 1) 320 | .sets(2, 1, 0, 3) 321 | .nest(Ival(3, 6), 4, Ival(3, 6)) 322 | .test(Ints(0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)); 323 | } 324 | 325 | @Test 326 | public void artemis4() { 327 | Arr(3, 4) 328 | .nest(Ival(), 1) 329 | .sets(2, 1, 0, 3) 330 | .nest(Ival(1, 2), 4) 331 | .nest(Ival(3, 6), 4) 332 | .nest(Ival(1, 1), 1, Ival(0, 0)) 333 | .test(Ints(1, 0, 0)); 334 | } 335 | 336 | @Test 337 | @Disabled 338 | public void artemis5() { 339 | Arr(3, 4) 340 | .nest(Ival(), 1) 341 | .sets(0, 1) 342 | .nest(Ival(2, 0), 4, Ival()) 343 | .test(Ints(0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)); 344 | } 345 | 346 | @Test 347 | public void artemis6() { 348 | Arr(3, 4) 349 | .nest(Ival(), 1) 350 | .sets(0, 1) 351 | .nest(Ival(2, 0), 4) 352 | .nest(Ival(5, 1), 9, Ival(5, 1)) 353 | .test(Ints(0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)); 354 | } 355 | 356 | @Test 357 | public void artemis7() { 358 | Arr(3, 4) 359 | .nest(Ival(), 1) 360 | .sets(0, 1) 361 | .nest(Ival(2, 0), 4) 362 | .nest(Ival(), 0, Ival()) 363 | .test(Ints()); 364 | } 365 | 366 | @Test 367 | public void jTest() { 368 | var arr = Arr(5, 7) 369 | .nest(Ival(), 100); 370 | assertEquals(500, arr.getArray().length); 371 | for (int i = 0; i < 500; i++) { 372 | arr.getArray()[i] = i; 373 | } 374 | var arr2 = arr.nest(Ival(10, 20), 50); 375 | assertEquals(250, arr2.getArray().length); 376 | for (int i = 0; i < 10; i++) { 377 | assertEquals(10 + i, arr2.getArray()[i]); 378 | } 379 | } 380 | 381 | @Test 382 | @DisplayName("Illegal Arguments in Constructor Test") 383 | public void arrayConstructor() { 384 | assertThrows(IllegalArgumentException.class, () -> Stck(5, 4)); 385 | assertThrows(IllegalArgumentException.class, () -> Stck(10, 9)); 386 | assertThrows(IllegalArgumentException.class, () -> Stck(100, 99)); 387 | assertThrows(IllegalArgumentException.class, () -> Stck(101, 100)); 388 | assertDoesNotThrow(() -> Stck(9, 10)); 389 | assertDoesNotThrow(() -> Stck(99, 100)); 390 | assertDoesNotThrow(() -> Stck(100, 101)); 391 | assertDoesNotThrow(() -> Stck(101, 102)); 392 | assertThrows(IllegalArgumentException.class, () -> Stck(-1, 2)); 393 | assertDoesNotThrow(() -> Stck(1, 2)); 394 | } 395 | 396 | @Test 397 | public void dynamic1() { 398 | Stck(3, 4) 399 | .test(Ints()) 400 | .pushes(1, 2, 3) 401 | .test(Ints(1, 2, 3)) 402 | .pops(3, 2) 403 | .test(Ints(1, 2, 3)); 404 | } 405 | 406 | @Test 407 | public void dynamic2() { 408 | Stck(3, 4) 409 | .pushes(1, 2, 3, 4) 410 | .test(Ints(1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0)) 411 | .pops(4) 412 | .test(Ints(1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0)) 413 | .pushes(5) 414 | .test(Ints(1, 2, 3, 5, 0, 0, 0, 0, 0, 0, 0, 0)); 415 | } 416 | 417 | @Test 418 | public void dynamic3() { 419 | Stck(3, 4) 420 | .pushes(1, 2, 3, 4) 421 | .pops(4) 422 | .pushes(5) 423 | .pops(5, 3) 424 | .test(Ints(1, 2, 0, 0, 0, 0)); 425 | } 426 | 427 | @Test 428 | public void dynamicSize() { 429 | var arr = Stck(3, 4).pushes(1, 2, 3, 4, 5); 430 | assertEquals(5, arr.data.size()); 431 | } 432 | 433 | @Test 434 | public void dynamicEmpty() { 435 | assertEquals(0, Stck(3, 4).getArray().length); 436 | for (int i = 0; i < 50; i += 10) { 437 | var s = Stck(3, 4) 438 | .pushes(new int[i]) 439 | .pops(new int[i]) 440 | .test(Ints()); 441 | } 442 | } 443 | 444 | @Test 445 | public void queue1() { 446 | Queue(3, 4) 447 | .test(Ints()) 448 | .pushes(1, 2, 3) 449 | .test(Ints(1, 2, 3)) 450 | .pushes(4) 451 | .test(Ints(1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0)); 452 | } 453 | 454 | @Test 455 | public void queue2() { 456 | Queue(3, 4) 457 | .pushes(1, 2, 3, 4, 5) 458 | .pops(1) 459 | .test(Ints(1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0)); 460 | } 461 | 462 | @Test 463 | public void queue3() { 464 | Queue(3, 4) 465 | .pushes(1, 2, 3, 4) 466 | .pops(1, 2) 467 | .test(Ints(3, 4, 0, 0, 0, 0)); 468 | } 469 | 470 | @Test 471 | public void queue4() { 472 | Queue(3, 4) 473 | .pushes(1, 2, 3, 4) 474 | .pops(1, 2, 3) 475 | .test(Ints(4, 0, 0)); 476 | } 477 | 478 | @Test 479 | public void queue5() { 480 | Queue(3, 4) 481 | .pushes(1, 2, 3, 4) 482 | .popCount(3) 483 | .pushes(5, 6) 484 | .test(Ints(4, 5, 6)); 485 | } 486 | 487 | @Test 488 | public void queue6() { 489 | Queue(3, 4) 490 | .pushes(1, 2, 3, 4) 491 | .popCount(3) 492 | .pushes(5, 6) 493 | .pops(4) 494 | .test(Ints(4, 5, 6)); 495 | } 496 | 497 | @Test 498 | public void queue7() { 499 | Queue(3, 4) 500 | .pushes(1, 2, 3, 4) 501 | .popCount(3) 502 | .pushes(5, 6) 503 | .pops(4) 504 | .pushes(7) 505 | .test(Ints(7, 5, 6)); 506 | } 507 | 508 | @Test 509 | public void queue8() { 510 | Queue(3, 4) 511 | .pushes(1, 2, 3, 4) 512 | .popCount(3) 513 | .pushes(5, 6) 514 | .pops(4) 515 | .pushes(7, 8) 516 | .test(Ints(5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0)); 517 | } 518 | 519 | @Test 520 | public void queuePlus() { 521 | Queue(3, 4) 522 | .pushes(1, 2, 3, 4) 523 | .popCount(3) 524 | .pushes(5, 6) 525 | .pops(4) 526 | .pushes(7, 8, 1, 3, 3, 7, 6, 9, 4, 2) 527 | .test(Ints(5, 6, 7, 8, 1, 3, 3, 7, 6, 9, 4, 2)); 528 | } 529 | 530 | @Test 531 | public void stackyQueue1() { 532 | StkQu(3, 4) 533 | .test(Ints(), Ints()); 534 | } 535 | 536 | @Test 537 | public void stackyQueue2() { 538 | StkQu(3, 4) 539 | .pushes(1, 2, 3) 540 | .test(Ints(1, 2, 3), Ints()) 541 | .pushes(4) 542 | .test(Ints(1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0), Ints()); 543 | } 544 | 545 | @Test 546 | public void stackyQueue3() { 547 | StkQu(3, 4) 548 | .pushes(1, 2, 3, 4) 549 | .pops(1) 550 | .test(Ints(), Ints(4, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0)) 551 | .pops(2) 552 | .test(Ints(), Ints(4, 3, 0, 0, 0, 0)) 553 | .pops(3) 554 | .test(Ints(), Ints(4, 0, 0)); 555 | } 556 | 557 | @Test 558 | public void stackyQueue4() { 559 | StkQu(3, 4) 560 | .pushes(1, 2, 3, 4) 561 | .popCount(3) 562 | .pushes(5, 6) 563 | .test(Ints(5, 6, 0), Ints(4, 0, 0)); 564 | } 565 | 566 | @Test 567 | public void stackyQueue5() { 568 | StkQu(3, 4) 569 | .pushes(1, 2, 3, 4) 570 | .popCount(3) 571 | .pushes(5, 6) 572 | .pops(4) 573 | .test(Ints(5, 6, 0), Ints()) 574 | .pops(5) 575 | .test(Ints(), Ints(6, 5, 0)); 576 | } 577 | 578 | // 579 | // 580 | // 581 | 582 | // 583 | // 584 | // 585 | 586 | // 587 | // 588 | // 589 | 590 | // 591 | // 592 | // 593 | 594 | @Test 595 | public void testIntervals() { 596 | var d = Arr(150, 477); 597 | 598 | assertEquals(Ival(), d.reportUsage(Ival(), 10)); 599 | assertArrayEquals(new int[1500], d.getArray()); 600 | 601 | assertEquals(Ival(0, 10), d.reportUsage(Ival(0, 10), 10)); 602 | 603 | assertEquals(Ival(0, 10), d.reportUsage(Ival(0, 10), 100)); 604 | assertArrayEquals(new int[1500], d.getArray()); 605 | 606 | assertEquals(Ival(0, 10), d.reportUsage(Ival(0, 10), 200)); 607 | assertArrayEquals(new int[1500], d.getArray()); 608 | 609 | assertEquals(Ival(0, 10), d.reportUsage(Ival(0, 10), 2000)); 610 | assertArrayEquals(new int[300000], d.getArray()); 611 | 612 | assertEquals(Ival(0, 1), d.reportUsage(Ival(0, 1), 500)); 613 | assertArrayEquals(new int[75000], d.getArray()); 614 | 615 | assertEquals(Ival(74950, 50), d.reportUsage(Ival(74950, 50), 500)); 616 | assertArrayEquals(new int[75000], d.getArray()); 617 | 618 | assertEquals(Ival(0, 100), d.reportUsage(Ival(74950, 50), 100)); 619 | assertArrayEquals(new int[15000], d.getArray()); 620 | } 621 | 622 | // 623 | // 624 | // 625 | } 626 | --------------------------------------------------------------------------------