Skip to content

Commit 4a0ddcd

Browse files
committed
Add randomized test to TextTests
1 parent 0af1611 commit 4a0ddcd

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

libs/x-content/src/test/java/org/elasticsearch/xcontent/TextTests.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,28 @@ public void testCompareTo() {
163163
}
164164
}
165165

166+
public void testRandomized() {
167+
int stringLength = randomInt(128);
168+
String value = randomUnicodeOfLength(stringLength);
169+
byte[] encodedArr = value.getBytes(StandardCharsets.UTF_8);
170+
var encoded = new XContentString.UTF8Bytes(encodedArr);
171+
172+
Text text = switch (randomInt(2)) {
173+
case 0 -> new Text(value);
174+
case 1 -> new Text(encoded);
175+
default -> new Text(encoded, stringLength);
176+
};
177+
178+
for (int i = 0; i < 20; i++) {
179+
switch (randomInt(5)) {
180+
case 0 -> assertEquals(encoded, text.bytes());
181+
case 1 -> assertSame(text.bytes(), text.bytes());
182+
case 2 -> assertEquals(value, text.string());
183+
case 3 -> assertEquals(value, text.toString());
184+
case 4 -> assertEquals(stringLength, text.stringLength());
185+
case 5 -> assertEquals(new Text(value), text);
186+
}
187+
}
188+
}
189+
166190
}

0 commit comments

Comments
 (0)