Skip to content

Commit

Permalink
Fixed TODO to improve unordered string comparision
Browse files Browse the repository at this point in the history
  • Loading branch information
kavvya-ramarathnam committed Oct 19, 2023
1 parent 8843b43 commit d01c6b6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,16 +58,25 @@ 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[] formatString(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 assertEqualstUnordered(String msg, String expected, String actual) {
String[] actualTokenized = this.formatString(actual);
String[] expectedTokenized = this.formatString(expected);

Arrays.sort(actualTokenized);
Arrays.sort(expectedTokenized);
if (Arrays.equals(actualTokenized, expectedTokenized)) {
Assert.assertTrue(msg + ": " + expectedTokenized + "=/= " + actualTokenized, true);
} else {
Assert.fail(msg + ": " + expectedTokenized + "=/= " + actualTokenized);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,17 @@ public void testEntrySet() {
"{0=0, 1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9}", entrySet.toString());
}

@Test
public void testAssertEqualsUnorderedFailsWithDuplicates() {
try {
this.assertEqualstUnordered("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}");
} catch (AssertionError assertionError) {
return;
}
Assert.fail("The test should have failed but didn't");
}

@Test
public void testEntrySetParametrized() {
Map<Integer, Integer> map = this.createResizedDS();
Expand Down

0 comments on commit d01c6b6

Please sign in to comment.