Skip to content

Commit d01c6b6

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

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

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

Lines changed: 16 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,25 @@ 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[] actualTokenized = this.formatString(actual);
72+
String[] expectedTokenized = this.formatString(expected);
6973

74+
Arrays.sort(actualTokenized);
75+
Arrays.sort(expectedTokenized);
76+
if (Arrays.equals(actualTokenized, expectedTokenized)) {
77+
Assert.assertTrue(msg + ": " + expectedTokenized + "=/= " + actualTokenized, true);
78+
} else {
79+
Assert.fail(msg + ": " + expectedTokenized + "=/= " + actualTokenized);
80+
}
7081
}
7182
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,17 @@ 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
152+
public void testAssertEqualsUnorderedFailsWithDuplicates() {
153+
try {
154+
this.assertEqualstUnordered("the strings are not a permutation of each other",
155+
"{0=0, 0=0, 1=1, 2=2, 3=3}", "{0=0, 1=1, 2=2, 3=3}");
156+
} catch (AssertionError assertionError) {
157+
return;
158+
}
159+
Assert.fail("The test should have failed but didn't");
160+
}
161+
151162
@Test
152163
public void testEntrySetParametrized() {
153164
Map<Integer, Integer> map = this.createResizedDS();

0 commit comments

Comments
 (0)