1 package org.apache.lucene.facet.taxonomy.writercache.cl2o;
3 import java.io.BufferedInputStream;
4 import java.io.BufferedOutputStream;
6 import java.io.FileInputStream;
7 import java.io.FileOutputStream;
11 import org.apache.lucene.util.LuceneTestCase;
12 import org.apache.lucene.facet.taxonomy.writercache.cl2o.CharBlockArray;
15 * Licensed to the Apache Software Foundation (ASF) under one or more
16 * contributor license agreements. See the NOTICE file distributed with
17 * this work for additional information regarding copyright ownership.
18 * The ASF licenses this file to You under the Apache License, Version 2.0
19 * (the "License"); you may not use this file except in compliance with
20 * the License. You may obtain a copy of the License at
22 * http://www.apache.org/licenses/LICENSE-2.0
24 * Unless required by applicable law or agreed to in writing, software
25 * distributed under the License is distributed on an "AS IS" BASIS,
26 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27 * See the License for the specific language governing permissions and
28 * limitations under the License.
31 public class TestCharBlockArray extends LuceneTestCase {
33 @Test public void testArray() throws Exception {
34 CharBlockArray array = new CharBlockArray();
35 StringBuilder builder = new StringBuilder();
37 final int n = 100 * 1000;
39 byte[] buffer = new byte[50];
41 for (int i = 0; i < n; i++) {
42 random.nextBytes(buffer);
43 int size = 1 + random.nextInt(50);
45 String s = new String(buffer, 0, size);
50 for (int i = 0; i < n; i++) {
51 random.nextBytes(buffer);
52 int size = 1 + random.nextInt(50);
54 String s = new String(buffer, 0, size);
55 array.append((CharSequence)s);
59 for (int i = 0; i < n; i++) {
60 random.nextBytes(buffer);
61 int size = 1 + random.nextInt(50);
63 String s = new String(buffer, 0, size);
64 for (int j = 0; j < s.length(); j++) {
65 array.append(s.charAt(j));
70 assertEqualsInternal("GrowingCharArray<->StringBuilder mismatch.", builder, array);
72 File f = new File("GrowingCharArrayTest.tmp");
73 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f));
78 BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
79 array = CharBlockArray.open(in);
80 assertEqualsInternal("GrowingCharArray<->StringBuilder mismatch after flush/load.", builder, array);
85 private static void assertEqualsInternal(String msg, StringBuilder expected, CharBlockArray actual) {
86 assertEquals(msg, expected.length(), actual.length());
87 for (int i = 0; i < expected.length(); i++) {
88 assertEquals(msg, expected.charAt(i), actual.charAt(i));