Skip to content

Commit b17b73c

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

3 files changed

Lines changed: 29 additions & 15 deletions

File tree

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

Lines changed: 14 additions & 6 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;
@@ -46,7 +48,7 @@ protected void assertParameterized(T ds, Object derived, String str) {
4648
case FULL:
4749
String tempStr = derived.toString();
4850
Assert.assertNotEquals("FULL is improperly running", str, tempStr);
49-
this.assertEqualstUnordered("Does not match permutation", str, tempStr);
51+
this.assertEqualsUnordered("Does not match permutation", str, tempStr);
5052
break;
5153
case ONE:
5254
Assert.assertEquals("ONE is improperly running", str, derived.toString());
@@ -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[] trimAndSplitStrings(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 assertEqualsUnordered(String msg, String expected, String actual) {
71+
String trimmed = expected.substring(1, expected.length() - 1);
72+
String[] actualTokenized = this.trimAndSplitStrings(actual);
73+
String[] expectedTokenized = this.trimAndSplitStrings(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/ConcurrentHashMapTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public void testKeySet() {
9898

9999
Assert.assertNotEquals("You are likely running an unchanged JVM",
100100
"{0, 2, 3, 4, 5, 6, 7, 8, 9}", keySet.toString());
101-
this.assertEqualstUnordered("The strings are not a permutation of each other",
101+
this.assertEqualsUnordered("The strings are not a permutation of each other",
102102
"{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}", keySet.toString());
103103

104104
}
@@ -127,7 +127,7 @@ public void testValuesParameterized() {
127127

128128
Assert.assertNotEquals("You are likely running an unchanged JVM",
129129
"{0, 2, 3, 4, 5, 6, 7, 8, 9}", values.toString());
130-
this.assertEqualstUnordered("The strings are not a permutation of each other",
130+
this.assertEqualsUnordered("The strings are not a permutation of each other",
131131
"{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}", values.toString());
132132

133133
String str = values.toString();
@@ -141,7 +141,7 @@ public void testEntrySet() {
141141

142142
Assert.assertNotEquals("You are likely running an unchanged JVM",
143143
"{0=0, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9}", entrySet.toString());
144-
this.assertEqualstUnordered("The strings are not a permutation of each other",
144+
this.assertEqualsUnordered("The strings are not a permutation of each other",
145145
"{0=0, 1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9}", entrySet.toString());
146146
}
147147

@@ -161,7 +161,7 @@ public void testElements() {
161161

162162
Assert.assertNotEquals("You are likely running an unchanged JVM",
163163
"{0, 2, 3, 4, 5, 6, 7, 8, 9}", enumerated.toString());
164-
this.assertEqualstUnordered("The collection does not containt the elements asserted",
164+
this.assertEqualsUnordered("The collection does not containt the elements asserted",
165165
"{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}", enumerated.toString());
166166
}
167167
}

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void smokeTest() {
8989

9090
String str = map.toString();
9191
Assert.assertNotEquals("You are not running FULL nondex", str, map.toString());
92-
this.assertEqualstUnordered("The strings are not a permutation of each other", str, map.toString());
92+
this.assertEqualsUnordered("The strings are not a permutation of each other", str, map.toString());
9393
}
9494

9595
@Test
@@ -99,7 +99,7 @@ public void testKeySet() {
9999

100100
Assert.assertNotEquals("You are likely running an unchanged JVM",
101101
"{0, 2, 3, 4, 5, 6, 7, 8, 9}", keySet.toString());
102-
this.assertEqualstUnordered("The strings are not a permuation of each other",
102+
this.assertEqualsUnordered("The strings are not a permuation of each other",
103103
"{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}", keySet.toString());
104104

105105
}
@@ -119,7 +119,7 @@ public void testValues() {
119119

120120
Assert.assertNotEquals("You are likely running an unchanged JVM",
121121
"{0, 2, 3, 4, 5, 6, 7, 8, 9}", values.toString());
122-
this.assertEqualstUnordered("The strings are not a permutation of each other",
122+
this.assertEqualsUnordered("The strings are not a permutation of each other",
123123
"{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}", values.toString());
124124
}
125125

@@ -130,7 +130,7 @@ public void testValuesParametrized() {
130130

131131
Assert.assertNotEquals("You are likely running an unchanged JVM",
132132
"{0, 2, 3, 4, 5, 6, 7, 8, 9}", values.toString());
133-
this.assertEqualstUnordered("The strings are not a permuation of each other",
133+
this.assertEqualsUnordered("The strings are not a permuation of each other",
134134
"{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}", values.toString());
135135

136136
String str = values.toString();
@@ -144,10 +144,16 @@ public void testEntrySet() {
144144

145145
Assert.assertNotEquals("You are likely running an unchanged JVM",
146146
"{0=0, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9}", entrySet.toString());
147-
this.assertEqualstUnordered("The strings are not a permutation of each other",
147+
this.assertEqualsUnordered("The strings are not a permutation of each other",
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.assertEqualsUnordered("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)