1 package org.apache.lucene.util;
3 import java.util.Arrays;
6 * Licensed to the Apache Software Foundation (ASF) under one or more
7 * contributor license agreements. See the NOTICE file distributed with
8 * this work for additional information regarding copyright ownership.
9 * The ASF licenses this file to You under the Apache License, Version 2.0
10 * (the "License"); you may not use this file except in compliance with
11 * the License. You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
22 public class TestCharsRef extends LuceneTestCase {
23 public void testUTF16InUTF8Order() {
24 final int numStrings = atLeast(1000);
25 BytesRef utf8[] = new BytesRef[numStrings];
26 CharsRef utf16[] = new CharsRef[numStrings];
28 for (int i = 0; i < numStrings; i++) {
29 String s = _TestUtil.randomUnicodeString(random);
30 utf8[i] = new BytesRef(s);
31 utf16[i] = new CharsRef(s);
35 Arrays.sort(utf16, CharsRef.getUTF16SortedAsUTF8Comparator());
37 for (int i = 0; i < numStrings; i++) {
38 assertEquals(utf8[i].utf8ToString(), utf16[i].toString());
42 public void testAppend() {
43 CharsRef ref = new CharsRef();
44 StringBuilder builder = new StringBuilder();
45 int numStrings = atLeast(10);
46 for (int i = 0; i < numStrings; i++) {
47 char[] charArray = _TestUtil.randomRealisticUnicodeString(random, 1, 100).toCharArray();
48 int offset = random.nextInt(charArray.length);
49 int length = charArray.length - offset;
50 builder.append(charArray, offset, length);
51 ref.append(charArray, offset, length);
54 assertEquals(builder.toString(), ref.toString());
57 public void testCopy() {
58 int numIters = atLeast(10);
59 for (int i = 0; i < numIters; i++) {
60 CharsRef ref = new CharsRef();
61 char[] charArray = _TestUtil.randomRealisticUnicodeString(random, 1, 100).toCharArray();
62 int offset = random.nextInt(charArray.length);
63 int length = charArray.length - offset;
64 String str = new String(charArray, offset, length);
65 ref.copy(charArray, offset, length);
66 assertEquals(str, ref.toString());