Skip to content

Commit f72a133

Browse files
committed
Add TextTests
1 parent aa0a829 commit f72a133

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.common.text;
11+
12+
import org.elasticsearch.common.bytes.BytesArray;
13+
import org.elasticsearch.test.ESTestCase;
14+
15+
import java.nio.charset.StandardCharsets;
16+
17+
public class TextTests extends ESTestCase {
18+
public void testConvertToBytes() {
19+
String value = randomUnicodeOfLength(randomInt(128));
20+
var encoded = new BytesArray(value);
21+
22+
var text = new Text(value);
23+
assertTrue(text.hasString());
24+
assertFalse(text.hasBytes());
25+
26+
assertEquals(value, text.string());
27+
assertEquals(encoded, text.bytes());
28+
29+
assertTrue(text.hasString());
30+
assertTrue(text.hasBytes());
31+
32+
// Ensure the conversion didn't mess up subsequent calls
33+
assertEquals(value, text.string());
34+
assertEquals(encoded, text.bytes());
35+
36+
assertSame(text.bytes(), text.bytes());
37+
}
38+
39+
public void testConvertToString() {
40+
String value = randomUnicodeOfLength(randomInt(128));
41+
var encoded = new BytesArray(value);
42+
43+
var text = new Text(encoded);
44+
assertFalse(text.hasString());
45+
assertTrue(text.hasBytes());
46+
47+
assertEquals(value, text.string());
48+
assertEquals(encoded, text.bytes());
49+
50+
assertFalse(text.hasString());
51+
assertTrue(text.hasBytes());
52+
53+
// Ensure the conversion didn't mess up subsequent calls
54+
assertEquals(value, text.string());
55+
assertEquals(encoded, text.bytes());
56+
57+
assertSame(encoded, text.bytes());
58+
}
59+
60+
public void testEquals() {
61+
String value = randomUnicodeOfLength(randomInt(128));
62+
var encoded = new BytesArray(value);
63+
64+
{
65+
var text1 = new Text(value);
66+
var text2 = new Text(value);
67+
assertTrue(text1.equals(text2));
68+
}
69+
70+
{
71+
var text1 = new Text(value);
72+
var text2 = new Text(encoded);
73+
assertTrue(text1.equals(text2));
74+
}
75+
76+
{
77+
var text1 = new Text(encoded);
78+
var text2 = new Text(encoded);
79+
assertTrue(text1.equals(text2));
80+
}
81+
}
82+
83+
public void testCompareTo() {
84+
String value1 = randomUnicodeOfLength(randomInt(128));
85+
var encoded1 = new BytesArray(value1);
86+
87+
{
88+
var text1 = new Text(value1);
89+
var text2 = new Text(value1);
90+
assertEquals(0, text1.compareTo(text2));
91+
}
92+
93+
{
94+
var text1 = new Text(value1);
95+
var text2 = new Text(encoded1);
96+
assertEquals(0, text1.compareTo(text2));
97+
}
98+
99+
{
100+
var text1 = new Text(encoded1);
101+
var text2 = new Text(encoded1);
102+
assertEquals(0, text1.compareTo(text2));
103+
}
104+
105+
String value2 = randomUnicodeOfLength(randomInt(128));
106+
var encoded2 = new BytesArray(value2);
107+
108+
int compSign = (int) Math.signum(encoded1.compareTo(encoded2));
109+
110+
{
111+
var text1 = new Text(value1);
112+
var text2 = new Text(value2);
113+
assertEquals(compSign, (int) Math.signum(text1.compareTo(text2)));
114+
}
115+
116+
{
117+
var text1 = new Text(value1);
118+
var text2 = new Text(encoded2);
119+
assertEquals(compSign, (int) Math.signum(text1.compareTo(text2)));
120+
}
121+
122+
{
123+
var text1 = new Text(encoded1);
124+
var text2 = new Text(value2);
125+
assertEquals(compSign, (int) Math.signum(text1.compareTo(text2)));
126+
}
127+
128+
{
129+
var text1 = new Text(encoded1);
130+
var text2 = new Text(encoded2);
131+
assertEquals(compSign, (int) Math.signum(text1.compareTo(text2)));
132+
}
133+
}
134+
135+
}

0 commit comments

Comments
 (0)