From 8a21b354f39f4f1cf796534e5bd46233fd4b3974 Mon Sep 17 00:00:00 2001 From: fdai7012 Date: Wed, 17 Jan 2024 16:20:05 +0100 Subject: [PATCH] Added Equals function to Vector2 --- src/main/java/pacmanGame/Vector2.java | 13 ++++++++ src/test/java/pacmanTests/Vector2Test.java | 36 ++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 src/test/java/pacmanTests/Vector2Test.java diff --git a/src/main/java/pacmanGame/Vector2.java b/src/main/java/pacmanGame/Vector2.java index 5f7811d..ba12fdd 100644 --- a/src/main/java/pacmanGame/Vector2.java +++ b/src/main/java/pacmanGame/Vector2.java @@ -8,4 +8,17 @@ public class Vector2 { this.x = x; this.y = y; } + + @Override + public boolean equals(Object o) { + + if (o == this) { + return true; + } + if (!(o instanceof Vector2)) { + return false; + } + Vector2 b = (Vector2) o; + return x == b.x && y == b.y; + } } diff --git a/src/test/java/pacmanTests/Vector2Test.java b/src/test/java/pacmanTests/Vector2Test.java new file mode 100644 index 0000000..5e57424 --- /dev/null +++ b/src/test/java/pacmanTests/Vector2Test.java @@ -0,0 +1,36 @@ +package pacmanTests; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +import pacmanGame.*; + + +class Vector2Test { + + @Test + void Vector2_sameValues_areEqual() { + // arrange + Vector2 a = new Vector2(2, 2); + Vector2 b = new Vector2(2, 2); + boolean expected = true; + // act + boolean result = a.equals(b); + // assert + assertThat(result).isEqualTo(expected); + } + + @Test + void Vector2_differentValues_areNotEqual() { + // arrange + Vector2 a = new Vector2(2, 2); + Vector2 b = new Vector2(2, 5); + boolean expected = false; + // act + boolean result = a.equals(b); + // assert + assertThat(result).isEqualTo(expected); + } +}