-
Notifications
You must be signed in to change notification settings - Fork 587
/
CodePointAdapterTest.java
433 lines (383 loc) · 18.6 KB
/
CodePointAdapterTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
* Copyright (c) 2018 Goldman Sachs.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.eclipse.collections.impl.string.immutable;
import java.util.stream.Collectors;
import org.eclipse.collections.api.IntIterable;
import org.eclipse.collections.api.list.primitive.ImmutableIntList;
import org.eclipse.collections.impl.block.factory.primitive.IntPredicates;
import org.eclipse.collections.impl.list.immutable.primitive.AbstractImmutableIntListTestCase;
import org.eclipse.collections.impl.test.Verify;
import org.junit.Assert;
import org.junit.Test;
public class CodePointAdapterTest extends AbstractImmutableIntListTestCase
{
private static final String UNICODE_STRING = "\u3042\uD840\uDC00\u3044\uD840\uDC03\u3046\uD83D\uDE09";
@Override
protected ImmutableIntList classUnderTest()
{
return CodePointAdapter.from(1, 2, 3);
}
@Override
protected ImmutableIntList newWith(int... elements)
{
return CodePointAdapter.from(elements);
}
@SuppressWarnings("StringBufferReplaceableByString")
@Test
public void stringBuilder()
{
CodePointAdapter adapt = CodePointAdapter.adapt(UNICODE_STRING);
Assert.assertEquals(UNICODE_STRING, new StringBuilder(adapt).toString());
}
@Test
public void subSequence()
{
CodePointAdapter adapt = CodePointAdapter.adapt(UNICODE_STRING);
CharSequence sequence = adapt.subSequence(1, 3);
Assert.assertEquals(UNICODE_STRING.subSequence(1, 3), sequence);
}
@Override
@Test
public void testEquals()
{
super.testEquals();
ImmutableIntList list1 = this.newWith(1, 2, 3, 4);
ImmutableIntList list2 = this.newWith(4, 3, 2, 1);
Assert.assertNotEquals(list1, list2);
Assert.assertEquals(CodePointAdapter.adapt(UNICODE_STRING), CodePointAdapter.adapt(UNICODE_STRING));
Assert.assertNotEquals(CodePointAdapter.adapt("\u3042\uD840\uDC00\u3044\uD840\uDC03\u3046"), CodePointAdapter.adapt(UNICODE_STRING));
Assert.assertEquals(CodePointAdapter.adapt("ABC"), CodePointAdapter.adapt("ABC"));
Assert.assertNotEquals(CodePointAdapter.adapt("123"), CodePointAdapter.adapt("ABC"));
Verify.assertEqualsAndHashCode(CodePointAdapter.adapt("ABC"), CodePointList.from("ABC"));
Verify.assertEqualsAndHashCode(CodePointAdapter.adapt(UNICODE_STRING), CodePointList.from(UNICODE_STRING));
Assert.assertNotEquals(CodePointList.from("123"), CodePointAdapter.adapt("ABC"));
Assert.assertNotEquals(CodePointAdapter.adapt("ABC"), CodePointList.from("123"));
Assert.assertNotEquals(CodePointList.from("ABCD"), CodePointAdapter.adapt("ABC"));
Assert.assertNotEquals(CodePointAdapter.adapt("ABC"), CodePointList.from("ABCD"));
Assert.assertNotEquals(CodePointList.from("ABC"), CodePointAdapter.adapt("ABCD"));
Assert.assertNotEquals(CodePointAdapter.adapt("ABCD"), CodePointList.from("ABC"));
}
@Override
@Test
public void max()
{
Assert.assertEquals(9L, this.newWith(1, 2, 9).max());
Assert.assertEquals(32L, this.newWith(1, 0, 9, 30, 31, 32).max());
Assert.assertEquals(32L, this.newWith(0, 9, 30, 31, 32).max());
Assert.assertEquals(31L, this.newWith(31, 0, 30).max());
Assert.assertEquals(39L, this.newWith(32, 39, 35).max());
Assert.assertEquals(this.classUnderTest().size(), this.classUnderTest().max());
}
@Override
@Test
public void min()
{
Assert.assertEquals(1L, this.newWith(1, 2, 9).min());
Assert.assertEquals(0L, this.newWith(1, 0, 9, 30, 31, 32).min());
Assert.assertEquals(31L, this.newWith(31, 32, 33).min());
Assert.assertEquals(32L, this.newWith(32, 39, 35).min());
Assert.assertEquals(1L, this.classUnderTest().min());
}
@Override
@Test
public void allSatisfy()
{
Assert.assertFalse(this.newWith(1, 0, 2).allSatisfy(IntPredicates.greaterThan(0)));
Assert.assertTrue(this.newWith(1, 2, 3).allSatisfy(IntPredicates.greaterThan(0)));
Assert.assertFalse(this.newWith(1, 0, 31, 32).allSatisfy(IntPredicates.greaterThan(0)));
Assert.assertFalse(this.newWith(1, 0, 31, 32).allSatisfy(IntPredicates.greaterThan(0)));
Assert.assertTrue(this.newWith(1, 2, 31, 32).allSatisfy(IntPredicates.greaterThan(0)));
Assert.assertFalse(this.newWith(32).allSatisfy(IntPredicates.equal(33)));
IntIterable iterable = this.newWith(0, 1, 2);
Assert.assertFalse(iterable.allSatisfy(value -> 3 < value));
Assert.assertTrue(iterable.allSatisfy(IntPredicates.lessThan(3)));
IntIterable iterable1 = this.classUnderTest();
int size = iterable1.size();
Assert.assertEquals(size == 0, iterable1.allSatisfy(IntPredicates.greaterThan(3)));
Assert.assertEquals(size < 3, iterable1.allSatisfy(IntPredicates.lessThan(3)));
}
@Override
@Test
public void anySatisfy()
{
Assert.assertTrue(this.newWith(1, 2).anySatisfy(IntPredicates.greaterThan(0)));
Assert.assertFalse(this.newWith(1, 2).anySatisfy(IntPredicates.equal(0)));
Assert.assertTrue(this.newWith(31, 32).anySatisfy(IntPredicates.greaterThan(0)));
Assert.assertTrue(this.newWith(2, 31, 32).anySatisfy(IntPredicates.greaterThan(0)));
Assert.assertFalse(this.newWith(1, 31, 32).anySatisfy(IntPredicates.equal(0)));
Assert.assertTrue(this.newWith(32).anySatisfy(IntPredicates.greaterThan(0)));
IntIterable iterable = this.newWith(0, 1, 2);
Assert.assertTrue(iterable.anySatisfy(value -> value < 3));
Assert.assertFalse(iterable.anySatisfy(IntPredicates.greaterThan(3)));
IntIterable iterable1 = this.classUnderTest();
int size = iterable1.size();
Assert.assertEquals(size > 3, iterable1.anySatisfy(IntPredicates.greaterThan(3)));
Assert.assertEquals(size != 0, iterable1.anySatisfy(IntPredicates.lessThan(3)));
}
@Override
@Test
public void testToString()
{
StringBuilder expectedString = new StringBuilder();
int size = this.classUnderTest().size();
for (int each = 0; each < size; each++)
{
expectedString.appendCodePoint(each + 1);
}
Assert.assertEquals(expectedString.toString(), this.classUnderTest().toString());
}
@Override
@Test
public void makeString()
{
ImmutableIntList list = this.classUnderTest();
StringBuilder expectedString = new StringBuilder("");
StringBuilder expectedString1 = new StringBuilder("");
int size = list.size();
for (int each = 0; each < size; each++)
{
expectedString.appendCodePoint(each + 1);
expectedString1.appendCodePoint(each + 1);
expectedString.append(each == size - 1 ? "" : ", ");
expectedString1.append(each == size - 1 ? "" : "/");
}
Assert.assertEquals(expectedString.toString(), list.makeString());
Assert.assertEquals(expectedString1.toString(), list.makeString("/"));
}
@Override
@Test
public void appendString()
{
StringBuilder expectedString = new StringBuilder("");
StringBuilder expectedString1 = new StringBuilder("");
int size = this.classUnderTest().size();
for (int each = 0; each < size; each++)
{
expectedString.appendCodePoint(each + 1);
expectedString1.appendCodePoint(each + 1);
expectedString.append(each == size - 1 ? "" : ", ");
expectedString1.append(each == size - 1 ? "" : "/");
}
ImmutableIntList list = this.classUnderTest();
StringBuilder appendable2 = new StringBuilder();
list.appendString(appendable2);
Assert.assertEquals(expectedString.toString(), appendable2.toString());
StringBuilder appendable3 = new StringBuilder();
list.appendString(appendable3, "/");
Assert.assertEquals(expectedString1.toString(), appendable3.toString());
}
@SuppressWarnings("StringBufferMayBeStringBuilder")
@Test
public void appendStringStringBuffer()
{
StringBuffer expectedString = new StringBuffer("");
StringBuffer expectedString1 = new StringBuffer("");
int size = this.classUnderTest().size();
for (int each = 0; each < size; each++)
{
expectedString.appendCodePoint(each + 1);
expectedString1.appendCodePoint(each + 1);
expectedString.append(each == size - 1 ? "" : ", ");
expectedString1.append(each == size - 1 ? "" : "/");
}
ImmutableIntList list = this.classUnderTest();
StringBuffer appendable2 = new StringBuffer();
list.appendString(appendable2);
Assert.assertEquals(expectedString.toString(), appendable2.toString());
StringBuffer appendable3 = new StringBuffer();
list.appendString(appendable3, "/");
Assert.assertEquals(expectedString1.toString(), appendable3.toString());
StringBuffer appendable4 = new StringBuffer();
this.classUnderTest().appendString(appendable4, "", "", "");
Assert.assertEquals(this.classUnderTest().toString(), appendable4.toString());
}
@Test
public void appendStringAppendable()
{
StringBuilder expectedString = new StringBuilder();
StringBuilder expectedString1 = new StringBuilder();
int size = this.classUnderTest().size();
for (int each = 0; each < size; each++)
{
expectedString.appendCodePoint(each + 1);
expectedString1.appendCodePoint(each + 1);
expectedString.append(each == size - 1 ? "" : ", ");
expectedString1.append(each == size - 1 ? "" : "/");
}
ImmutableIntList list = this.classUnderTest();
SBAppendable appendable2 = new SBAppendable();
list.appendString(appendable2);
Assert.assertEquals(expectedString.toString(), appendable2.toString());
SBAppendable appendable3 = new SBAppendable();
list.appendString(appendable3, "/");
Assert.assertEquals(expectedString1.toString(), appendable3.toString());
}
@Test
public void collectCodePointUnicode()
{
Assert.assertEquals(
UNICODE_STRING.codePoints().boxed().collect(Collectors.toList()),
CodePointAdapter.adapt(UNICODE_STRING).collect(i -> i));
Assert.assertEquals(
UNICODE_STRING.codePoints().boxed().collect(Collectors.toList()),
CodePointAdapter.adapt(UNICODE_STRING).collect(i -> i));
}
@Test
public void selectCodePointUnicode()
{
String string = CodePointAdapter.adapt(UNICODE_STRING).select(Character::isBmpCodePoint).toString();
Assert.assertEquals("\u3042\u3044\u3046", string);
}
@Test
public void allSatisfyUnicode()
{
Assert.assertTrue(CodePointAdapter.adapt("\u3042\u3044\u3046").allSatisfy(Character::isBmpCodePoint));
Assert.assertFalse(CodePointAdapter.adapt("\uD840\uDC00\uD840\uDC03\uD83D\uDE09").allSatisfy(Character::isBmpCodePoint));
}
@Test
public void anySatisfyUnicode()
{
Assert.assertTrue(CodePointAdapter.adapt("\u3042\u3044\u3046").anySatisfy(Character::isBmpCodePoint));
Assert.assertFalse(CodePointAdapter.adapt("\uD840\uDC00\uD840\uDC03\uD83D\uDE09").anySatisfy(Character::isBmpCodePoint));
}
@Test
public void noneSatisfyUnicode()
{
Assert.assertFalse(CodePointAdapter.adapt("\u3042\u3044\u3046").noneSatisfy(Character::isBmpCodePoint));
Assert.assertTrue(CodePointAdapter.adapt("\uD840\uDC00\uD840\uDC03\uD83D\uDE09").noneSatisfy(Character::isBmpCodePoint));
}
@Test
public void forEachUnicode()
{
StringBuilder builder = new StringBuilder();
CodePointAdapter.adapt(UNICODE_STRING).forEach(builder::appendCodePoint);
Assert.assertEquals(UNICODE_STRING, builder.toString());
}
@Test
public void asReversedForEachUnicode()
{
StringBuilder builder = new StringBuilder();
CodePointAdapter.adapt(UNICODE_STRING).asReversed().forEach(builder::appendCodePoint);
Assert.assertEquals("\uD83D\uDE09\u3046\uD840\uDC03\u3044\uD840\uDC00\u3042", builder.toString());
StringBuilder builder2 = new StringBuilder();
CodePointAdapter.adapt("\uD840\uDC00\u3042\uD840\uDC03\u3044\uD83D\uDE09\u3046").asReversed().forEach(builder2::appendCodePoint);
Assert.assertEquals("\u3046\uD83D\uDE09\u3044\uD840\uDC03\u3042\uD840\uDC00", builder2.toString());
CodePointAdapter.adapt("").asReversed().forEach((int codePoint) -> Assert.fail());
}
@Test
public void asReversedForEachInvalidUnicode()
{
StringBuilder builder = new StringBuilder();
CodePointAdapter.adapt("\u3042\uDC00\uD840\u3044\uDC03\uD840\u3046\uDE09\uD83D").asReversed().forEach(builder::appendCodePoint);
Assert.assertEquals("\uD83D\uDE09\u3046\uD840\uDC03\u3044\uD840\uDC00\u3042", builder.toString());
StringBuilder builder2 = new StringBuilder();
CodePointAdapter.adapt("\u3042\uD840\u3044\uD840\u3046\uD840").asReversed().forEach(builder2::appendCodePoint);
Assert.assertEquals("\uD840\u3046\uD840\u3044\uD840\u3042", builder2.toString());
StringBuilder builder3 = new StringBuilder();
CodePointAdapter.adapt("\u3042\uDC00\u3044\uDC03\u3046\uDC06").asReversed().forEach(builder3::appendCodePoint);
Assert.assertEquals("\uDC06\u3046\uDC03\u3044\uDC00\u3042", builder3.toString());
CodePointAdapter.adapt("").asReversed().forEach((int codePoint) -> Assert.fail());
}
@Test
public void toReversedForEachUnicode()
{
StringBuilder builder = new StringBuilder();
CodePointAdapter.adapt(UNICODE_STRING).toReversed().forEach(builder::appendCodePoint);
Assert.assertEquals("\uD83D\uDE09\u3046\uD840\uDC03\u3044\uD840\uDC00\u3042", builder.toString());
StringBuilder builder2 = new StringBuilder();
CodePointAdapter.adapt("\uD840\uDC00\u3042\uD840\uDC03\u3044\uD83D\uDE09\u3046").toReversed().forEach(builder2::appendCodePoint);
Assert.assertEquals("\u3046\uD83D\uDE09\u3044\uD840\uDC03\u3042\uD840\uDC00", builder2.toString());
CodePointAdapter.adapt("").toReversed().forEach((int codePoint) -> Assert.fail());
}
@Test
public void toReversedForEachInvalidUnicode()
{
StringBuilder builder = new StringBuilder();
CodePointAdapter.adapt("\u3042\uDC00\uD840\u3044\uDC03\uD840\u3046\uDE09\uD83D").toReversed().forEach(builder::appendCodePoint);
Assert.assertEquals("\uD83D\uDE09\u3046\uD840\uDC03\u3044\uD840\uDC00\u3042", builder.toString());
StringBuilder builder2 = new StringBuilder();
CodePointAdapter.adapt("\u3042\uD840\u3044\uD840\u3046\uD840").toReversed().forEach(builder2::appendCodePoint);
Assert.assertEquals("\uD840\u3046\uD840\u3044\uD840\u3042", builder2.toString());
StringBuilder builder3 = new StringBuilder();
CodePointAdapter.adapt("\u3042\uDC00\u3044\uDC03\u3046\uDC06").toReversed().forEach(builder3::appendCodePoint);
Assert.assertEquals("\uDC06\u3046\uDC03\u3044\uDC00\u3042", builder3.toString());
CodePointAdapter.adapt("").toReversed().forEach((int codePoint) -> Assert.fail());
}
@Test
public void distinctUnicode()
{
Assert.assertEquals(
"\uD840\uDC00\uD840\uDC03\uD83D\uDE09",
CodePointAdapter.adapt("\uD840\uDC00\uD840\uDC03\uD83D\uDE09\uD840\uDC00\uD840\uDC03\uD83D\uDE09").distinct().toString());
}
@Test
public void newWithUnicode()
{
CodePointAdapter codePointAdapter = CodePointAdapter.adapt("");
CodePointAdapter collection = codePointAdapter.newWith(12354);
CodePointAdapter collection0 = codePointAdapter.newWith(12354).newWith(131072);
CodePointAdapter collection1 = codePointAdapter.newWith(12354).newWith(131072).newWith(12356);
CodePointAdapter collection2 = codePointAdapter.newWith(12354).newWith(131072).newWith(12356).newWith(131075);
CodePointAdapter collection3 = codePointAdapter.newWith(12354).newWith(131072).newWith(12356).newWith(131075).newWith(12358);
this.assertSizeAndContains(codePointAdapter);
this.assertSizeAndContains(collection, 12354);
this.assertSizeAndContains(collection0, 12354, 131072);
this.assertSizeAndContains(collection1, 12354, 131072, 12356);
this.assertSizeAndContains(collection2, 12354, 131072, 12356, 131075);
this.assertSizeAndContains(collection3, 12354, 131072, 12356, 131075, 12358);
}
@Test
public void newWithoutUnicode()
{
CodePointAdapter collection0 = CodePointAdapter.adapt("\u3042\uD840\uDC00\u3044\uD840\uDC03\u3046");
CodePointAdapter collection1 = collection0.newWithout(12358);
CodePointAdapter collection2 = collection1.newWithout(131075);
CodePointAdapter collection3 = collection2.newWithout(12356);
CodePointAdapter collection4 = collection3.newWithout(131072);
CodePointAdapter collection5 = collection4.newWithout(12354);
CodePointAdapter collection6 = collection5.newWithout(131078);
this.assertSizeAndContains(collection6);
this.assertSizeAndContains(collection5);
this.assertSizeAndContains(collection4, 12354);
this.assertSizeAndContains(collection3, 12354, 131072);
this.assertSizeAndContains(collection2, 12354, 131072, 12356);
this.assertSizeAndContains(collection1, 12354, 131072, 12356, 131075);
}
@Override
public void toReversed()
{
super.toReversed();
Assert.assertEquals("cba", CodePointAdapter.adapt("abc").toReversed().toString());
}
private static class SBAppendable implements Appendable
{
private final StringBuilder builder = new StringBuilder();
@Override
public Appendable append(char c)
{
return this.builder.append(c);
}
@Override
public Appendable append(CharSequence csq)
{
return this.builder.append(csq);
}
@Override
public Appendable append(CharSequence csq, int start, int end)
{
return this.builder.append(csq, start, end);
}
@Override
public String toString()
{
return this.builder.toString();
}
}
}