From 93c8a4ed6fedbb23d59e238b92698a85c43e85d6 Mon Sep 17 00:00:00 2001 From: Mario Benov Date: Wed, 21 Feb 2024 11:58:49 +0200 Subject: [PATCH] 11b - unit testing --- .../stream-api-exercise/.idea/.gitignore | 3 + .../stream-api-exercise/.idea/encodings.xml | 4 + .../stream-api-exercise/.idea/misc.xml | 6 + .../stream-api-exercise/.idea/modules.xml | 8 ++ .../stream-api-exercise/.idea/vcs.xml | 6 + .../stream-api-exercise/numbers_100.txt | 100 ++++++++++++++ .../stream-api-exercise/src/Main.java | 109 +++++++++++++++ .../stream-api-exercise.iml | 11 ++ .../junit-intro/.idea/.gitignore | 3 + .../junit-intro/.idea/compiler.xml | 13 ++ .../junit-intro/.idea/encodings.xml | 7 + .../junit-intro/.idea/jarRepositories.xml | 20 +++ .../junit-intro/.idea/misc.xml | 14 ++ .../junit-intro/.idea/uiDesigner.xml | 124 ++++++++++++++++++ .../junit-intro/.idea/vcs.xml | 6 + .../junit-intro/pom.xml | 10 +- .../junit-intro/src/main/java/Foo.java | 7 + .../junit-intro/src/test/java/FooTest.java | 29 ++++ .../mockito/.idea/.gitignore | 3 + .../mockito/.idea/compiler.xml | 13 ++ .../mockito/.idea/encodings.xml | 7 + .../mockito/.idea/jarRepositories.xml | 20 +++ .../2024-02-12-mockito/mockito/.idea/misc.xml | 14 ++ .../2024-02-12-mockito/mockito/.idea/vcs.xml | 6 + .../11b/2024-02-12-mockito/mockito/pom.xml | 22 +++- .../mockito/src/main/java/Figure.java | 49 +++++++ .../mockito/src/main/java/Game.java | 74 +++++++++++ .../mockito/src/main/java/King.java | 18 +++ .../mockito/src/main/java/Main.java | 28 ++++ .../mockito/src/main/java/Rook.java | 10 ++ .../mockito/src/test/java/FigureTest.java | 73 +++++++++++ .../junit-3/.idea/.gitignore | 3 + .../junit-3/.idea/compiler.xml | 13 ++ .../junit-3/.idea/encodings.xml | 7 + .../junit-3/.idea/jarRepositories.xml | 20 +++ .../2024-02-19-junit-3/junit-3/.idea/misc.xml | 14 ++ .../2024-02-19-junit-3/junit-3/.idea/vcs.xml | 6 + .../11b/2024-02-19-junit-3/junit-3/pom.xml | 17 +++ .../java/org/elsys/oop/ConsolePlayer.java | 24 ++++ .../src/main/java/org/elsys/oop/Main.java | 4 + .../main/java/org/elsys/oop/PlayerBase.java | 15 +++ .../src/main/java/org/elsys/oop/Renderer.java | 5 + .../java/org/elsys/oop/TicTacToeGame.java | 34 +++++ 43 files changed, 977 insertions(+), 2 deletions(-) create mode 100644 materials/2023-2024/11b/2024-01-22-stream-api-exercise/stream-api-exercise/.idea/.gitignore create mode 100644 materials/2023-2024/11b/2024-01-22-stream-api-exercise/stream-api-exercise/.idea/encodings.xml create mode 100644 materials/2023-2024/11b/2024-01-22-stream-api-exercise/stream-api-exercise/.idea/misc.xml create mode 100644 materials/2023-2024/11b/2024-01-22-stream-api-exercise/stream-api-exercise/.idea/modules.xml create mode 100644 materials/2023-2024/11b/2024-01-22-stream-api-exercise/stream-api-exercise/.idea/vcs.xml create mode 100644 materials/2023-2024/11b/2024-01-22-stream-api-exercise/stream-api-exercise/numbers_100.txt create mode 100644 materials/2023-2024/11b/2024-01-22-stream-api-exercise/stream-api-exercise/src/Main.java create mode 100644 materials/2023-2024/11b/2024-01-22-stream-api-exercise/stream-api-exercise/stream-api-exercise.iml create mode 100644 materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/.idea/.gitignore create mode 100644 materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/.idea/compiler.xml create mode 100644 materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/.idea/encodings.xml create mode 100644 materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/.idea/jarRepositories.xml create mode 100644 materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/.idea/misc.xml create mode 100644 materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/.idea/uiDesigner.xml create mode 100644 materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/.idea/vcs.xml create mode 100644 materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/src/main/java/Foo.java create mode 100644 materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/src/test/java/FooTest.java create mode 100644 materials/2023-2024/11b/2024-02-12-mockito/mockito/.idea/.gitignore create mode 100644 materials/2023-2024/11b/2024-02-12-mockito/mockito/.idea/compiler.xml create mode 100644 materials/2023-2024/11b/2024-02-12-mockito/mockito/.idea/encodings.xml create mode 100644 materials/2023-2024/11b/2024-02-12-mockito/mockito/.idea/jarRepositories.xml create mode 100644 materials/2023-2024/11b/2024-02-12-mockito/mockito/.idea/misc.xml create mode 100644 materials/2023-2024/11b/2024-02-12-mockito/mockito/.idea/vcs.xml create mode 100644 materials/2023-2024/11b/2024-02-12-mockito/mockito/src/main/java/Figure.java create mode 100644 materials/2023-2024/11b/2024-02-12-mockito/mockito/src/main/java/Game.java create mode 100644 materials/2023-2024/11b/2024-02-12-mockito/mockito/src/main/java/King.java create mode 100644 materials/2023-2024/11b/2024-02-12-mockito/mockito/src/main/java/Main.java create mode 100644 materials/2023-2024/11b/2024-02-12-mockito/mockito/src/main/java/Rook.java create mode 100644 materials/2023-2024/11b/2024-02-12-mockito/mockito/src/test/java/FigureTest.java create mode 100644 materials/2023-2024/11b/2024-02-19-junit-3/junit-3/.idea/.gitignore create mode 100644 materials/2023-2024/11b/2024-02-19-junit-3/junit-3/.idea/compiler.xml create mode 100644 materials/2023-2024/11b/2024-02-19-junit-3/junit-3/.idea/encodings.xml create mode 100644 materials/2023-2024/11b/2024-02-19-junit-3/junit-3/.idea/jarRepositories.xml create mode 100644 materials/2023-2024/11b/2024-02-19-junit-3/junit-3/.idea/misc.xml create mode 100644 materials/2023-2024/11b/2024-02-19-junit-3/junit-3/.idea/vcs.xml create mode 100644 materials/2023-2024/11b/2024-02-19-junit-3/junit-3/pom.xml create mode 100644 materials/2023-2024/11b/2024-02-19-junit-3/junit-3/src/main/java/org/elsys/oop/ConsolePlayer.java create mode 100644 materials/2023-2024/11b/2024-02-19-junit-3/junit-3/src/main/java/org/elsys/oop/Main.java create mode 100644 materials/2023-2024/11b/2024-02-19-junit-3/junit-3/src/main/java/org/elsys/oop/PlayerBase.java create mode 100644 materials/2023-2024/11b/2024-02-19-junit-3/junit-3/src/main/java/org/elsys/oop/Renderer.java create mode 100644 materials/2023-2024/11b/2024-02-19-junit-3/junit-3/src/main/java/org/elsys/oop/TicTacToeGame.java diff --git a/materials/2023-2024/11b/2024-01-22-stream-api-exercise/stream-api-exercise/.idea/.gitignore b/materials/2023-2024/11b/2024-01-22-stream-api-exercise/stream-api-exercise/.idea/.gitignore new file mode 100644 index 00000000..eaf91e2a --- /dev/null +++ b/materials/2023-2024/11b/2024-01-22-stream-api-exercise/stream-api-exercise/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/materials/2023-2024/11b/2024-01-22-stream-api-exercise/stream-api-exercise/.idea/encodings.xml b/materials/2023-2024/11b/2024-01-22-stream-api-exercise/stream-api-exercise/.idea/encodings.xml new file mode 100644 index 00000000..15a15b21 --- /dev/null +++ b/materials/2023-2024/11b/2024-01-22-stream-api-exercise/stream-api-exercise/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-01-22-stream-api-exercise/stream-api-exercise/.idea/misc.xml b/materials/2023-2024/11b/2024-01-22-stream-api-exercise/stream-api-exercise/.idea/misc.xml new file mode 100644 index 00000000..f5dd03ae --- /dev/null +++ b/materials/2023-2024/11b/2024-01-22-stream-api-exercise/stream-api-exercise/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-01-22-stream-api-exercise/stream-api-exercise/.idea/modules.xml b/materials/2023-2024/11b/2024-01-22-stream-api-exercise/stream-api-exercise/.idea/modules.xml new file mode 100644 index 00000000..813d22f6 --- /dev/null +++ b/materials/2023-2024/11b/2024-01-22-stream-api-exercise/stream-api-exercise/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-01-22-stream-api-exercise/stream-api-exercise/.idea/vcs.xml b/materials/2023-2024/11b/2024-01-22-stream-api-exercise/stream-api-exercise/.idea/vcs.xml new file mode 100644 index 00000000..a5ef44da --- /dev/null +++ b/materials/2023-2024/11b/2024-01-22-stream-api-exercise/stream-api-exercise/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-01-22-stream-api-exercise/stream-api-exercise/numbers_100.txt b/materials/2023-2024/11b/2024-01-22-stream-api-exercise/stream-api-exercise/numbers_100.txt new file mode 100644 index 00000000..1a876b56 --- /dev/null +++ b/materials/2023-2024/11b/2024-01-22-stream-api-exercise/stream-api-exercise/numbers_100.txt @@ -0,0 +1,100 @@ +486840 +6192 +417711 +752513 +52188 +213615 +418759 +303453 +925423 +490451 +260528 +219552 +23805 +761211 +289456 +657985 +410085 +70199 +329645 +444526 +926798 +612062 +848442 +194546 +222419 +288136 +14891 +747982 +638130 +857684 +154761 +347939 +907444 +909565 +906307 +34485 +519166 +351237 +988660 +949645 +856264 +944758 +960141 +487251 +559148 +517883 +342850 +731529 +962402 +640698 +564935 +41575 +241498 +165806 +292533 +384482 +898113 +542642 +317876 +450545 +10935 +761514 +814698 +836503 +116300 +559929 +889368 +411540 +923995 +819078 +750632 +374216 +132176 +522465 +256377 +580968 +554690 +914008 +611472 +661092 +918637 +778039 +520728 +578117 +588666 +803825 +11968 +379092 +821084 +918402 +493908 +302527 +931779 +468983 +197620 +800223 +622806 +104486 +689730 +545937 \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-01-22-stream-api-exercise/stream-api-exercise/src/Main.java b/materials/2023-2024/11b/2024-01-22-stream-api-exercise/stream-api-exercise/src/Main.java new file mode 100644 index 00000000..7e85f01a --- /dev/null +++ b/materials/2023-2024/11b/2024-01-22-stream-api-exercise/stream-api-exercise/src/Main.java @@ -0,0 +1,109 @@ +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.lang.reflect.Array; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.*; +import java.util.stream.Collectors; + +public class Main { + public static void main(String[] args) { + List numbers = new LinkedList<>(); + try { + numbers = Files.readAllLines(Path.of("numbers_100.txt")) + .stream() + .map(Integer::parseInt) +// .mapToInt(Integer::parseInt) + .toList(); + } catch (IOException e) { + throw new RuntimeException(e); + } +// try { +// BufferedReader fr = new BufferedReader( +// new FileReader("numbers_100.txt") +// ); +// String line; +// while((line = fr.readLine()) != null) { +// numbers.add(Integer.getInteger(line)); +// } +// fr.close(); +// } catch (FileNotFoundException e) { +// throw new RuntimeException(e); +// } catch (IOException e) { +// throw new RuntimeException(e); +// } + + + System.out.println("Max: " + numbers + .stream() + .max(Comparator.comparingInt(a -> a)) + ); + System.out.println("Max: " + numbers + .stream() + .sorted((a, b) -> b - a) + .findFirst() + ); + + System.out.println("5 largest: " + numbers + .stream() + .sorted((a, b) -> b - a) + .limit(5) + .sorted((a, b) -> a - b) + .toList() + ); + + System.out.println("5 largest: " + numbers + .stream() + .sorted(Comparator.comparingInt(a -> a)) + .skip(numbers.size() - 5) + .toList() + ); + + System.out.println("sum of digits of min: " + numbers + .stream() + .sorted() + .limit(1) +// .peek(a -> System.out.println(a)) + .map(a -> a.toString().split("")) + .flatMap(Arrays::stream) + .map(Integer::parseInt) + .reduce(0, Integer::sum) +// .flatMapToInt(a -> Arrays.stream(a).map(Integer::parseInt)) +// .toList() + ); + + System.out.println(numbers + .stream() + .filter(a -> a % 5 == 0) + .mapToInt(a -> a) + .sum() + ); + + System.out.println(numbers + .stream() + .collect(Collectors.groupingBy( + a -> a % 2 == 0 ? "even" : "odd" + )) + ); + System.out.println(numbers + .stream() + +// U reduce(U identity, +// BiFunction accumulator, +// BinaryOperator combiner); + .reduce( + new HashMap>(), + (map, val) -> { + String key = val % 2 == 0 ? "even" : "odd"; + + if(map.) + + return map; + }, + (a, b) -> + ) + ); + } +} diff --git a/materials/2023-2024/11b/2024-01-22-stream-api-exercise/stream-api-exercise/stream-api-exercise.iml b/materials/2023-2024/11b/2024-01-22-stream-api-exercise/stream-api-exercise/stream-api-exercise.iml new file mode 100644 index 00000000..9465dd86 --- /dev/null +++ b/materials/2023-2024/11b/2024-01-22-stream-api-exercise/stream-api-exercise/stream-api-exercise.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/.idea/.gitignore b/materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/.idea/.gitignore new file mode 100644 index 00000000..eaf91e2a --- /dev/null +++ b/materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/.idea/compiler.xml b/materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/.idea/compiler.xml new file mode 100644 index 00000000..15a2b79a --- /dev/null +++ b/materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/.idea/encodings.xml b/materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/.idea/encodings.xml new file mode 100644 index 00000000..aa00ffab --- /dev/null +++ b/materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/.idea/jarRepositories.xml b/materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/.idea/jarRepositories.xml new file mode 100644 index 00000000..a468a999 --- /dev/null +++ b/materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/.idea/misc.xml b/materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/.idea/misc.xml new file mode 100644 index 00000000..bf4a108d --- /dev/null +++ b/materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/.idea/uiDesigner.xml b/materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/.idea/uiDesigner.xml new file mode 100644 index 00000000..6d50cd4d --- /dev/null +++ b/materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/.idea/vcs.xml b/materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/.idea/vcs.xml new file mode 100644 index 00000000..a5ef44da --- /dev/null +++ b/materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/pom.xml b/materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/pom.xml index 26129e11..bf05717a 100644 --- a/materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/pom.xml +++ b/materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/pom.xml @@ -13,5 +13,13 @@ 19 UTF-8 - + + + org.junit.jupiter + junit-jupiter + RELEASE + test + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/src/main/java/Foo.java b/materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/src/main/java/Foo.java new file mode 100644 index 00000000..13ffec7c --- /dev/null +++ b/materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/src/main/java/Foo.java @@ -0,0 +1,7 @@ +public class Foo { + public int add(int a, int b){ + return a+b; + } + + +} diff --git a/materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/src/test/java/FooTest.java b/materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/src/test/java/FooTest.java new file mode 100644 index 00000000..d6ed5fbb --- /dev/null +++ b/materials/2023-2024/11b/2024-01-26-junit-intro/junit-intro/src/test/java/FooTest.java @@ -0,0 +1,29 @@ +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class FooTest { + + @Test + void addPositive() { + + Foo add1 = new Foo(); + int a = 5; + int b = 3; + int result = add1.add(a,b); + + assertEquals(8, result); + + } + @Test + void addNegative() { + + Foo add1 = new Foo(); + int a = -1; + int b = -2; + int result = add1.add(a,b); + + assertEquals(-3, result); + + } +} \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-02-12-mockito/mockito/.idea/.gitignore b/materials/2023-2024/11b/2024-02-12-mockito/mockito/.idea/.gitignore new file mode 100644 index 00000000..eaf91e2a --- /dev/null +++ b/materials/2023-2024/11b/2024-02-12-mockito/mockito/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/materials/2023-2024/11b/2024-02-12-mockito/mockito/.idea/compiler.xml b/materials/2023-2024/11b/2024-02-12-mockito/mockito/.idea/compiler.xml new file mode 100644 index 00000000..d06e7d19 --- /dev/null +++ b/materials/2023-2024/11b/2024-02-12-mockito/mockito/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-02-12-mockito/mockito/.idea/encodings.xml b/materials/2023-2024/11b/2024-02-12-mockito/mockito/.idea/encodings.xml new file mode 100644 index 00000000..aa00ffab --- /dev/null +++ b/materials/2023-2024/11b/2024-02-12-mockito/mockito/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-02-12-mockito/mockito/.idea/jarRepositories.xml b/materials/2023-2024/11b/2024-02-12-mockito/mockito/.idea/jarRepositories.xml new file mode 100644 index 00000000..a468a999 --- /dev/null +++ b/materials/2023-2024/11b/2024-02-12-mockito/mockito/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-02-12-mockito/mockito/.idea/misc.xml b/materials/2023-2024/11b/2024-02-12-mockito/mockito/.idea/misc.xml new file mode 100644 index 00000000..bf4a108d --- /dev/null +++ b/materials/2023-2024/11b/2024-02-12-mockito/mockito/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-02-12-mockito/mockito/.idea/vcs.xml b/materials/2023-2024/11b/2024-02-12-mockito/mockito/.idea/vcs.xml new file mode 100644 index 00000000..a5ef44da --- /dev/null +++ b/materials/2023-2024/11b/2024-02-12-mockito/mockito/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-02-12-mockito/mockito/pom.xml b/materials/2023-2024/11b/2024-02-12-mockito/mockito/pom.xml index 69393922..e5b41a19 100644 --- a/materials/2023-2024/11b/2024-02-12-mockito/mockito/pom.xml +++ b/materials/2023-2024/11b/2024-02-12-mockito/mockito/pom.xml @@ -13,5 +13,25 @@ 19 UTF-8 - + + + org.junit.jupiter + junit-jupiter-api + 5.10.2 + test + + + org.mockito + mockito-core + 5.2.0 + test + + + org.mockito + mockito-junit-jupiter + 5.1.1 + test + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-02-12-mockito/mockito/src/main/java/Figure.java b/materials/2023-2024/11b/2024-02-12-mockito/mockito/src/main/java/Figure.java new file mode 100644 index 00000000..32a7801f --- /dev/null +++ b/materials/2023-2024/11b/2024-02-12-mockito/mockito/src/main/java/Figure.java @@ -0,0 +1,49 @@ +public abstract class Figure { + protected Game game; + protected String owner; + protected Game.Pos pos; + +// private char[] display; + private char display; + + public Figure(Game game, String owner, Game.Pos pos, char[] display) { + this.game = game; + this.owner = owner; + this.pos = pos; +// this.display = display; + this.display = owner.equals("white") ? display[0] : display[1]; + } + + public void move(String pos) { + Game.Pos newPos = Game.stringToPos(pos); +// 1. validate pos is in board + if(newPos.row < 0 || newPos.col < 0) + throw new IllegalArgumentException("Invalid position"); + if(newPos.row > 7 || newPos.col > 7) + throw new IllegalArgumentException("Invalid position"); +// 2. validate with canMove + if(!canMove(pos)) + throw new IllegalArgumentException("Cannot move to position"); +// 3. validate field is empty or owner by oponent + Figure targetCell = game.getAt(pos); + if(targetCell != null && targetCell.owner == owner) + throw new IllegalArgumentException("Cannot take own piece"); +// 4. board[pos] = this + game.setAt(newPos.toString(), this); +// 5. board[this.pos] = null + game.setAt(this.pos.toString(), null); +// 6. this.pos = pos + this.pos = newPos; + } + + protected abstract boolean canMove(String pos); + + @Override + public String toString() { + return "" + display; +// if(owner == "white") +// return "" + display[0]; +// else +// return "" + display[1]; + } +} diff --git a/materials/2023-2024/11b/2024-02-12-mockito/mockito/src/main/java/Game.java b/materials/2023-2024/11b/2024-02-12-mockito/mockito/src/main/java/Game.java new file mode 100644 index 00000000..2d605897 --- /dev/null +++ b/materials/2023-2024/11b/2024-02-12-mockito/mockito/src/main/java/Game.java @@ -0,0 +1,74 @@ +public class Game { + private Figure[][] board; + + private King whiteKing = null; + private King blackKing = null; + + public Game() { + board = new Figure[8][8]; + + board[0][0] = new Rook(this, "white", new Pos(0, 0)); + board[3][0] = whiteKing = new King(this, "white", new Pos(0, 3)); + + board[0][7] = new Rook(this, "black", new Pos(7, 0)); + board[3][7] = blackKing = new King(this, "black", new Pos(7, 3)); + } + + public Figure getAt(String pos) { + Pos newPos = stringToPos(pos); + return board[newPos.row][newPos.col]; + } + + public void setAt(String pos, Figure fig) { + Pos newPos = stringToPos(pos); + board[newPos.row][newPos.col] = fig; + } + + public void moveFigure(String start, String end) { + Figure fig = getAt(start); + fig.move(end); + } + + public void printBoard() { + for(int row = 0; row < 8; row++) { + for(int col = 0; col < 8; col++) { + Figure fig = getAt(new Pos(row, col).toString()); + if(fig == null) System.out.print('-'); + else System.out.print(fig); + } + System.out.println(""); + } + } + + public void checkForWinner() { + if(whiteKing.isMate()) { + System.out.println("Black wins"); + } else if(blackKing.isMate()) { + System.out.println("White wins"); + } + } + + public static class Pos { + public int row; + public int col; + + public Pos(int row, int col) { + this.row = row; + this.col = col; + } + + @Override + public String toString() { + int r = 'A' + row; + int c = '1' + col; + return Character.toString(r) + Character.toString(c); + } + } + + public static Pos stringToPos(String pos) { + int col = pos.charAt(0) - 'A'; + int row = pos.charAt(1) - '1'; + + return new Pos(row, col); + } +} diff --git a/materials/2023-2024/11b/2024-02-12-mockito/mockito/src/main/java/King.java b/materials/2023-2024/11b/2024-02-12-mockito/mockito/src/main/java/King.java new file mode 100644 index 00000000..a251da32 --- /dev/null +++ b/materials/2023-2024/11b/2024-02-12-mockito/mockito/src/main/java/King.java @@ -0,0 +1,18 @@ +public class King extends Figure { + public King(Game game, String owner, Game.Pos pos) { + super(game, owner, pos, new char[] {'K', 'k'}); + } + + @Override + protected boolean canMove(String pos) { + return false; + } + + public boolean isCheck() { + return false; + } + + public boolean isMate() { + return false; + } +} diff --git a/materials/2023-2024/11b/2024-02-12-mockito/mockito/src/main/java/Main.java b/materials/2023-2024/11b/2024-02-12-mockito/mockito/src/main/java/Main.java new file mode 100644 index 00000000..1b87d8ae --- /dev/null +++ b/materials/2023-2024/11b/2024-02-12-mockito/mockito/src/main/java/Main.java @@ -0,0 +1,28 @@ +public class Main { + public static void main(String[] args) { +// String[][] arr = new String[10][5]; +//// for(int i = 0; i < 5; i++) { +//// for(int j = 0; j < 10; j++) { +//// arr[i][j] = i + "-" + j; +//// } +//// } +// for(int x = 0; x < 10; x++) { +// for(int y = 0; y < 5; y++) { +// arr[x][y] = x + "-" + y; +// } +// } +// +// for(int row = 0; row < 5; row++) { +// for(int column = 0; column < 10; column++) { +// System.out.print(arr[row][column] + " "); +// } +// System.out.println(""); +// } + + Game g = new Game(); + g.printBoard(); + + g.moveFigure("A1", "A4"); + g.printBoard(); + } +} diff --git a/materials/2023-2024/11b/2024-02-12-mockito/mockito/src/main/java/Rook.java b/materials/2023-2024/11b/2024-02-12-mockito/mockito/src/main/java/Rook.java new file mode 100644 index 00000000..cf6d715d --- /dev/null +++ b/materials/2023-2024/11b/2024-02-12-mockito/mockito/src/main/java/Rook.java @@ -0,0 +1,10 @@ +public class Rook extends Figure { + public Rook(Game game, String owner, Game.Pos pos) { + super(game, owner, pos, new char[] {'R', 'r'}); + } + + @Override + protected boolean canMove(String pos) { + return true; + } +} diff --git a/materials/2023-2024/11b/2024-02-12-mockito/mockito/src/test/java/FigureTest.java b/materials/2023-2024/11b/2024-02-12-mockito/mockito/src/test/java/FigureTest.java new file mode 100644 index 00000000..5f0b9327 --- /dev/null +++ b/materials/2023-2024/11b/2024-02-12-mockito/mockito/src/test/java/FigureTest.java @@ -0,0 +1,73 @@ +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class FigureTest { + private Figure testFig = null; + + @Mock + public Game mockedGame; + + @BeforeEach + void setup() { + testFig = createFigure("white", false); + } + + @Test + void moveInvalidPos() { + assertThrows( + IllegalArgumentException.class, + () -> testFig.move("-2"), + "Negative position" + ); + assertThrows( + IllegalArgumentException.class, + () -> testFig.move("K2"), + "Position too big" + ); + assertThrows( + IllegalArgumentException.class, + () -> testFig.move("A2"), + "Cannot move to position" + ); + } + + @Test + void moveTargetPos() { + when(mockedGame.getAt("A2")).thenReturn(null); +// when(mockedGame.getAt("A3")).thenReturn((Figure) new Object()); + + testFig = createFigure("white", true); + testFig.move("A2"); + + verify(mockedGame).getAt("A2"); + verify(mockedGame).setAt("A2", testFig); + verify(mockedGame).setAt("C5", null); + } + + @Test + void testToString() { + assertEquals("w", testFig.toString()); + + testFig = createFigure("black", false); + assertEquals("b", testFig.toString()); + } + + private Figure createFigure(String owner, boolean canMove) { + return new Figure( + mockedGame, owner, new Game.Pos(2, 4), new char[]{'w', 'b'} + ) { + @Override + protected boolean canMove(String pos) { + return canMove; + } + }; + } +} \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/.idea/.gitignore b/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/.idea/.gitignore new file mode 100644 index 00000000..eaf91e2a --- /dev/null +++ b/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/.idea/compiler.xml b/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/.idea/compiler.xml new file mode 100644 index 00000000..8ab4286c --- /dev/null +++ b/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/.idea/encodings.xml b/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/.idea/encodings.xml new file mode 100644 index 00000000..aa00ffab --- /dev/null +++ b/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/.idea/jarRepositories.xml b/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/.idea/jarRepositories.xml new file mode 100644 index 00000000..a468a999 --- /dev/null +++ b/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/.idea/misc.xml b/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/.idea/misc.xml new file mode 100644 index 00000000..bf4a108d --- /dev/null +++ b/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/.idea/vcs.xml b/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/.idea/vcs.xml new file mode 100644 index 00000000..a5ef44da --- /dev/null +++ b/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/pom.xml b/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/pom.xml new file mode 100644 index 00000000..c493c0d7 --- /dev/null +++ b/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + org.example + junit-3 + 1.0-SNAPSHOT + + + 19 + 19 + UTF-8 + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/src/main/java/org/elsys/oop/ConsolePlayer.java b/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/src/main/java/org/elsys/oop/ConsolePlayer.java new file mode 100644 index 00000000..22cdf7e5 --- /dev/null +++ b/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/src/main/java/org/elsys/oop/ConsolePlayer.java @@ -0,0 +1,24 @@ +package org.elsys.oop; + +import java.util.Scanner; + +public class ConsolePlayer extends PlayerBase { + private final Scanner sc; + + public ConsolePlayer(String name) { + super(name); + + sc = new Scanner(System.in); + } + + @Override + public int[] getMove(int[][] board) { + int x = sc.nextInt(); + int y = sc.nextInt(); + +// TODO: Validate coords +// TODO: Loop input until coords are valid + + return new int[]{x, y}; + } +} diff --git a/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/src/main/java/org/elsys/oop/Main.java b/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/src/main/java/org/elsys/oop/Main.java new file mode 100644 index 00000000..7fa07bbd --- /dev/null +++ b/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/src/main/java/org/elsys/oop/Main.java @@ -0,0 +1,4 @@ +package org.elsys.oop; + +public class Main { +} diff --git a/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/src/main/java/org/elsys/oop/PlayerBase.java b/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/src/main/java/org/elsys/oop/PlayerBase.java new file mode 100644 index 00000000..e1c1f63b --- /dev/null +++ b/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/src/main/java/org/elsys/oop/PlayerBase.java @@ -0,0 +1,15 @@ +package org.elsys.oop; + +public abstract class PlayerBase { + private final String name; + + public PlayerBase(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public abstract int[] getMove(int[][] board); +} diff --git a/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/src/main/java/org/elsys/oop/Renderer.java b/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/src/main/java/org/elsys/oop/Renderer.java new file mode 100644 index 00000000..d1cf9f94 --- /dev/null +++ b/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/src/main/java/org/elsys/oop/Renderer.java @@ -0,0 +1,5 @@ +package org.elsys.oop; + +public abstract class Renderer { + public abstract void renderBoard(int[][] board); +} diff --git a/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/src/main/java/org/elsys/oop/TicTacToeGame.java b/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/src/main/java/org/elsys/oop/TicTacToeGame.java new file mode 100644 index 00000000..ba7c6000 --- /dev/null +++ b/materials/2023-2024/11b/2024-02-19-junit-3/junit-3/src/main/java/org/elsys/oop/TicTacToeGame.java @@ -0,0 +1,34 @@ +package org.elsys.oop; + +import java.util.LinkedList; +import java.util.List; + +public class TicTacToeGame { + private int[][] board = new int[3][3]; + + private PlayerBase currentPlayer; + + public TicTacToeGame(PlayerBase p1, PlayerBase p2) { + players = new LinkedList<>(); + players.add(p1); + players.add(p2); + + currentPlayer = p1; + } + + private List players; + + private boolean hasFinished() { + return false; + } + + public void start() { + while(!hasFinished()) { + int[] playerMove = currentPlayer.getMove(board); +// updateBoard(playerMove, currentPlayer); +// renderer.renderBoard(board); + +// currentPlayer = nextPlayer; + } + } +}