From b17b73cfd44a7207a24214ab4dc5e4bddc3656c7 Mon Sep 17 00:00:00 2001 From: kavvya97 Date: Thu, 19 Oct 2023 13:31:17 -0500 Subject: [PATCH] Fixed TODO to improve unordered string comparision --- .../nondex/core/AbstractCollectionTest.java | 20 +++++++++++++------ .../nondex/core/ConcurrentHashMapTest.java | 8 ++++---- .../edu/illinois/nondex/core/HashMapTest.java | 16 ++++++++++----- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/nondex-test/src/test/java/edu/illinois/nondex/core/AbstractCollectionTest.java b/nondex-test/src/test/java/edu/illinois/nondex/core/AbstractCollectionTest.java index 028f849d..cbcde748 100644 --- a/nondex-test/src/test/java/edu/illinois/nondex/core/AbstractCollectionTest.java +++ b/nondex-test/src/test/java/edu/illinois/nondex/core/AbstractCollectionTest.java @@ -29,6 +29,8 @@ a copy of this software and associated documentation files (the package edu.illinois.nondex.core; +import java.util.Arrays; + import edu.illinois.nondex.shuffling.ControlNondeterminism; import org.junit.Assert; @@ -46,7 +48,7 @@ protected void assertParameterized(T ds, Object derived, String str) { case FULL: String tempStr = derived.toString(); Assert.assertNotEquals("FULL is improperly running", str, tempStr); - this.assertEqualstUnordered("Does not match permutation", str, tempStr); + this.assertEqualsUnordered("Does not match permutation", str, tempStr); break; case ONE: Assert.assertEquals("ONE is improperly running", str, derived.toString()); @@ -56,16 +58,22 @@ protected void assertParameterized(T ds, Object derived, String str) { } } - protected void assertEqualstUnordered(String msg, String expected, String actual) { - Assert.assertEquals(msg + ": " + expected + " =/= " + actual, expected.length(), actual.length()); - String trimmed = expected.substring(1, expected.length() - 1); + private String[] trimAndSplitStrings(String msg) { + String trimmed = msg.substring(1, msg.length() - 1); String[] elems = trimmed.split(","); - // TODO(gyori): fix and make this more robust. It does not check duplicates, substrings, etc. for (int i = 0; i < elems.length; i++) { elems[i] = elems[i].trim(); - Assert.assertTrue(msg + ": " + trimmed + " =/= " + actual, actual.contains(elems[i])); } + return elems; + } + protected void assertEqualsUnordered(String msg, String expected, String actual) { + String trimmed = expected.substring(1, expected.length() - 1); + String[] actualTokenized = this.trimAndSplitStrings(actual); + String[] expectedTokenized = this.trimAndSplitStrings(expected); + Arrays.sort(actualTokenized); + Arrays.sort(expectedTokenized); + Assert.assertArrayEquals(msg + ": " + trimmed + " =/= " + actual, expectedTokenized, actualTokenized); } } diff --git a/nondex-test/src/test/java/edu/illinois/nondex/core/ConcurrentHashMapTest.java b/nondex-test/src/test/java/edu/illinois/nondex/core/ConcurrentHashMapTest.java index 3644e686..6cfd103e 100644 --- a/nondex-test/src/test/java/edu/illinois/nondex/core/ConcurrentHashMapTest.java +++ b/nondex-test/src/test/java/edu/illinois/nondex/core/ConcurrentHashMapTest.java @@ -98,7 +98,7 @@ public void testKeySet() { Assert.assertNotEquals("You are likely running an unchanged JVM", "{0, 2, 3, 4, 5, 6, 7, 8, 9}", keySet.toString()); - this.assertEqualstUnordered("The strings are not a permutation of each other", + this.assertEqualsUnordered("The strings are not a permutation of each other", "{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}", keySet.toString()); } @@ -127,7 +127,7 @@ public void testValuesParameterized() { Assert.assertNotEquals("You are likely running an unchanged JVM", "{0, 2, 3, 4, 5, 6, 7, 8, 9}", values.toString()); - this.assertEqualstUnordered("The strings are not a permutation of each other", + this.assertEqualsUnordered("The strings are not a permutation of each other", "{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}", values.toString()); String str = values.toString(); @@ -141,7 +141,7 @@ public void testEntrySet() { Assert.assertNotEquals("You are likely running an unchanged JVM", "{0=0, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9}", entrySet.toString()); - this.assertEqualstUnordered("The strings are not a permutation of each other", + this.assertEqualsUnordered("The strings are not a permutation of each other", "{0=0, 1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9}", entrySet.toString()); } @@ -161,7 +161,7 @@ public void testElements() { Assert.assertNotEquals("You are likely running an unchanged JVM", "{0, 2, 3, 4, 5, 6, 7, 8, 9}", enumerated.toString()); - this.assertEqualstUnordered("The collection does not containt the elements asserted", + this.assertEqualsUnordered("The collection does not containt the elements asserted", "{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}", enumerated.toString()); } } diff --git a/nondex-test/src/test/java/edu/illinois/nondex/core/HashMapTest.java b/nondex-test/src/test/java/edu/illinois/nondex/core/HashMapTest.java index 9fc689d0..e091574c 100644 --- a/nondex-test/src/test/java/edu/illinois/nondex/core/HashMapTest.java +++ b/nondex-test/src/test/java/edu/illinois/nondex/core/HashMapTest.java @@ -89,7 +89,7 @@ public void smokeTest() { String str = map.toString(); Assert.assertNotEquals("You are not running FULL nondex", str, map.toString()); - this.assertEqualstUnordered("The strings are not a permutation of each other", str, map.toString()); + this.assertEqualsUnordered("The strings are not a permutation of each other", str, map.toString()); } @Test @@ -99,7 +99,7 @@ public void testKeySet() { Assert.assertNotEquals("You are likely running an unchanged JVM", "{0, 2, 3, 4, 5, 6, 7, 8, 9}", keySet.toString()); - this.assertEqualstUnordered("The strings are not a permuation of each other", + this.assertEqualsUnordered("The strings are not a permuation of each other", "{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}", keySet.toString()); } @@ -119,7 +119,7 @@ public void testValues() { Assert.assertNotEquals("You are likely running an unchanged JVM", "{0, 2, 3, 4, 5, 6, 7, 8, 9}", values.toString()); - this.assertEqualstUnordered("The strings are not a permutation of each other", + this.assertEqualsUnordered("The strings are not a permutation of each other", "{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}", values.toString()); } @@ -130,7 +130,7 @@ public void testValuesParametrized() { Assert.assertNotEquals("You are likely running an unchanged JVM", "{0, 2, 3, 4, 5, 6, 7, 8, 9}", values.toString()); - this.assertEqualstUnordered("The strings are not a permuation of each other", + this.assertEqualsUnordered("The strings are not a permuation of each other", "{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}", values.toString()); String str = values.toString(); @@ -144,10 +144,16 @@ public void testEntrySet() { Assert.assertNotEquals("You are likely running an unchanged JVM", "{0=0, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9}", entrySet.toString()); - this.assertEqualstUnordered("The strings are not a permutation of each other", + this.assertEqualsUnordered("The strings are not a permutation of each other", "{0=0, 1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9}", entrySet.toString()); } + @Test(expected = AssertionError.class) + public void testAssertEqualsUnorderedFailsWithDuplicates() { + this.assertEqualsUnordered("the strings are not a permutation of each other", + "{0=0, 0=0, 1=1, 2=2, 3=3}", "{0=0, 1=1, 2=2, 3=3}"); + } + @Test public void testEntrySetParametrized() { Map map = this.createResizedDS();