Skip to content

Commit 4c4d1a7

Browse files
committed
UtilTest.java: make sure that most generated strings have a common prefix
1 parent b740bcc commit 4c4d1a7

File tree

1 file changed

+36
-5
lines changed
  • firebase-firestore/src/test/java/com/google/firebase/firestore/util

1 file changed

+36
-5
lines changed

firebase-firestore/src/test/java/com/google/firebase/firestore/util/UtilTest.java

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,17 @@ private void validateDiffCollection(List<String> before, List<String> after) {
9393
@Test
9494
public void compareUtf8StringsShouldReturnCorrectValue() {
9595
ArrayList<String> errors = new ArrayList<>();
96-
int seed = new Random().nextInt();
96+
int seed = new Random().nextInt(Integer.MAX_VALUE);
9797
int passCount = 0;
98-
StringGenerator stringGenerator = new StringGenerator(seed);
98+
StringGenerator stringGenerator = new StringGenerator(29750468);
99+
StringPairGenerator stringPairGenerator = new StringPairGenerator(stringGenerator);
99100
for (int i = 0; i < 1_000_000 && errors.size() < 10; i++) {
100-
String s1 = stringGenerator.next();
101-
String s2 = stringGenerator.next();
101+
final String s1, s2;
102+
{
103+
StringPairGenerator.StringPair stringPair = stringPairGenerator.next();
104+
s1 = stringPair.s1;
105+
s2 = stringPair.s2;
106+
}
102107

103108
int actual = Util.compareUtf8Strings(s1, s2);
104109

@@ -140,6 +145,31 @@ public void compareUtf8StringsShouldReturnCorrectValue() {
140145
}
141146
}
142147

148+
private static class StringPairGenerator {
149+
150+
private final StringGenerator stringGenerator;
151+
152+
public StringPairGenerator(StringGenerator stringGenerator) {
153+
this.stringGenerator = stringGenerator;
154+
}
155+
156+
public StringPair next() {
157+
String prefix = stringGenerator.next();
158+
String s1 = prefix + stringGenerator.next();
159+
String s2 = prefix + stringGenerator.next();
160+
return new StringPair(s1, s2);
161+
}
162+
163+
public static class StringPair {
164+
public final String s1, s2;
165+
166+
public StringPair(String s1, String s2) {
167+
this.s1 = s1;
168+
this.s2 = s2;
169+
}
170+
}
171+
}
172+
143173
private static class StringGenerator {
144174

145175
private static final float DEFAULT_SURROGATE_PAIR_PROBABILITY = 0.33f;
@@ -214,7 +244,8 @@ public String next() {
214244
final int length = rnd.nextInt(maxLength + 1);
215245
final StringBuilder sb = new StringBuilder();
216246
while (sb.length() < length) {
217-
sb.appendCodePoint(nextCodePoint());
247+
int codePoint = nextCodePoint();
248+
sb.appendCodePoint(codePoint);
218249
}
219250
return sb.toString();
220251
}

0 commit comments

Comments
 (0)