From 85790ef294e7f8c98b92345e8c3fb13ec1a1edf3 Mon Sep 17 00:00:00 2001 From: lipian <lipian@cs.washington.edu> Date: Tue, 14 Apr 2020 16:19:32 -0400 Subject: [PATCH] Adding equals to item for better testing abilities --- src/cse332/datastructures/containers/Item.java | 16 ++++++++++++++++ .../gitlab/ckpt1/NGramToNextChoicesMapTests.java | 16 ++-------------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/cse332/datastructures/containers/Item.java b/src/cse332/datastructures/containers/Item.java index 83d6867..a6fc7fc 100644 --- a/src/cse332/datastructures/containers/Item.java +++ b/src/cse332/datastructures/containers/Item.java @@ -1,5 +1,7 @@ package cse332.datastructures.containers; +import java.util.Objects; + /** * Simple class to hold a piece of data and its value. * @@ -40,4 +42,18 @@ public class Item<K, V> { public String toString() { return this.key + "=" + this.value + ""; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Item<?, ?> item = (Item<?, ?>) o; + return Objects.equals(key, item.key) && + Objects.equals(value, item.value); + } + + @Override + public int hashCode() { + return Objects.hash(key, value); + } } diff --git a/src/tests/gitlab/ckpt1/NGramToNextChoicesMapTests.java b/src/tests/gitlab/ckpt1/NGramToNextChoicesMapTests.java index 3ef3d98..a82e790 100644 --- a/src/tests/gitlab/ckpt1/NGramToNextChoicesMapTests.java +++ b/src/tests/gitlab/ckpt1/NGramToNextChoicesMapTests.java @@ -16,14 +16,8 @@ import org.junit.Test; import static org.junit.Assert.*; public class NGramToNextChoicesMapTests { - private Supplier<Dictionary<NGram, Dictionary<AlphabeticString, Integer>>> newOuter = - () -> new BinarySearchTree(); - - private Supplier<Dictionary<AlphabeticString, Integer>> newInner = - () -> new BinarySearchTree(); - private NGramToNextChoicesMap init() { - return new NGramToNextChoicesMap(newOuter, newInner); + return new NGramToNextChoicesMap(BinarySearchTree::new, BinarySearchTree::new); } @Test(timeout = 3000) @@ -172,13 +166,7 @@ public class NGramToNextChoicesMapTests { Arrays.sort(results, Comparator.comparing(r -> r.key)); Item<String, Integer>[] expected = answers.get(ngram); // checks for correct number of unique words after - assertEquals(expected.length, results.length); - for (int j = 0; j < expected.length; j++) { - // checks if correct word after via sorted words - assertEquals(expected[j].key, results[j].key); - // checks if correct count for given word after - assertEquals(expected[j].value, results[j].value); - } + assertArrayEquals(expected, results); } } } -- GitLab