Skip to content

Commit c1f2556

Browse files
Fixed TODO to improve unordered string comparision
1 parent 8843b43 commit c1f2556

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

nondex-test/src/test/java/edu/illinois/nondex/core/AbstractCollectionTest.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ a copy of this software and associated documentation files (the
2929

3030
package edu.illinois.nondex.core;
3131

32+
import java.util.Arrays;
33+
3234
import edu.illinois.nondex.shuffling.ControlNondeterminism;
3335

3436
import org.junit.Assert;
@@ -56,16 +58,22 @@ protected void assertParameterized(T ds, Object derived, String str) {
5658
}
5759
}
5860

59-
protected void assertEqualstUnordered(String msg, String expected, String actual) {
60-
Assert.assertEquals(msg + ": " + expected + " =/= " + actual, expected.length(), actual.length());
61-
String trimmed = expected.substring(1, expected.length() - 1);
61+
private String[] formatString(String msg) {
62+
String trimmed = msg.substring(1, msg.length() - 1);
6263
String[] elems = trimmed.split(",");
63-
// TODO(gyori): fix and make this more robust. It does not check duplicates, substrings, etc.
6464
for (int i = 0; i < elems.length; i++) {
6565
elems[i] = elems[i].trim();
66-
Assert.assertTrue(msg + ": " + trimmed + " =/= " + actual, actual.contains(elems[i]));
6766
}
67+
return elems;
68+
}
6869

70+
protected void assertEqualstUnordered(String msg, String expected, String actual) {
71+
String trimmed = expected.substring(1, expected.length() - 1);
72+
String[] actualTokenized = this.formatString(actual);
73+
String[] expectedTokenized = this.formatString(expected);
6974

75+
Arrays.sort(actualTokenized);
76+
Arrays.sort(expectedTokenized);
77+
Assert.assertArrayEquals(msg + ": " + trimmed + " =/= " + actual, expectedTokenized, actualTokenized);
7078
}
7179
}

nondex-test/src/test/java/edu/illinois/nondex/core/HashMapTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ public void testEntrySet() {
148148
"{0=0, 1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9}", entrySet.toString());
149149
}
150150

151+
@Test(expected = AssertionError.class)
152+
public void testAssertEqualsUnorderedFailsWithDuplicates() {
153+
this.assertEqualstUnordered("the strings are not a permutation of each other",
154+
"{0=0, 0=0, 1=1, 2=2, 3=3}", "{0=0, 1=1, 2=2, 3=3}");
155+
}
156+
151157
@Test
152158
public void testEntrySetParametrized() {
153159
Map<Integer, Integer> map = this.createResizedDS();

0 commit comments

Comments
 (0)