pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / facet / src / test / org / apache / lucene / facet / taxonomy / writercache / cl2o / TestCharBlockArray.java
1 package org.apache.lucene.facet.taxonomy.writercache.cl2o;
2
3 import java.io.BufferedInputStream;
4 import java.io.BufferedOutputStream;
5 import java.io.File;
6 import java.io.FileInputStream;
7 import java.io.FileOutputStream;
8
9 import org.junit.Test;
10
11 import org.apache.lucene.util.LuceneTestCase;
12 import org.apache.lucene.facet.taxonomy.writercache.cl2o.CharBlockArray;
13
14 /**
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
21  *
22  *     http://www.apache.org/licenses/LICENSE-2.0
23  *
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.
29  */
30
31 public class TestCharBlockArray extends LuceneTestCase {
32
33   @Test public void testArray() throws Exception {
34     CharBlockArray array = new CharBlockArray();
35     StringBuilder builder = new StringBuilder();
36
37     final int n = 100 * 1000;
38
39     byte[] buffer = new byte[50];
40
41     for (int i = 0; i < n; i++) {
42       random.nextBytes(buffer);
43       int size = 1 + random.nextInt(50);
44
45       String s = new String(buffer, 0, size);
46       array.append(s);
47       builder.append(s);
48     }
49
50     for (int i = 0; i < n; i++) {
51       random.nextBytes(buffer);
52       int size = 1 + random.nextInt(50);
53
54       String s = new String(buffer, 0, size);
55       array.append((CharSequence)s);
56       builder.append(s);
57     }
58
59     for (int i = 0; i < n; i++) {
60       random.nextBytes(buffer);
61       int size = 1 + random.nextInt(50);
62
63       String s = new String(buffer, 0, size);
64       for (int j = 0; j < s.length(); j++) {
65         array.append(s.charAt(j));
66       }
67       builder.append(s);
68     }
69
70     assertEqualsInternal("GrowingCharArray<->StringBuilder mismatch.", builder, array);
71
72     File f = new File("GrowingCharArrayTest.tmp");
73     BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f));
74     array.flush(out);
75     out.flush();
76     out.close();
77
78     BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
79     array = CharBlockArray.open(in);
80     assertEqualsInternal("GrowingCharArray<->StringBuilder mismatch after flush/load.", builder, array);
81     in.close();
82     f.delete();
83   }
84
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));
89     }
90   }
91
92 }