add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / backwards / src / test / org / apache / lucene / store / TestCopyBytes.java
1 package org.apache.lucene.store;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20
21 import org.apache.lucene.util.LuceneTestCase;
22 import org.apache.lucene.util._TestUtil;
23
24 import org.junit.Test;
25
26 public class TestCopyBytes extends LuceneTestCase {
27
28   private byte value(int idx) {
29     return (byte) ((idx%256) * (1+(idx/256)));
30   }
31
32
33   @Test
34   public void testCopyBytes() throws Exception {
35     int num = atLeast(10);
36     for(int iter=0;iter<num;iter++) {
37       Directory dir = newDirectory();
38       if (VERBOSE) {
39         System.out.println("TEST: iter=" + iter + " dir=" + dir);
40       }
41
42       // make random file
43       IndexOutput out = dir.createOutput("test");
44       byte[] bytes = new byte[_TestUtil.nextInt(random, 1, 77777)];
45       final int size = _TestUtil.nextInt(random, 1, 1777777);
46       int upto = 0;
47       int byteUpto = 0;
48       while(upto < size) {
49         bytes[byteUpto++] = value(upto);
50         upto++;
51         if (byteUpto == bytes.length) {
52           out.writeBytes(bytes, 0, bytes.length);
53           byteUpto = 0;
54         }
55       }
56
57       out.writeBytes(bytes, 0, byteUpto);
58       assertEquals(size, out.getFilePointer());
59       out.close();
60       assertEquals(size, dir.fileLength("test"));
61
62       // copy from test -> test2
63       final IndexInput in = dir.openInput("test");
64
65       out = dir.createOutput("test2");
66
67       upto = 0;
68       while(upto < size) {
69         if (random.nextBoolean()) {
70           out.writeByte(in.readByte());
71           upto++;
72         } else {
73           final int chunk = Math.min(_TestUtil.nextInt(random, 1, bytes.length), size-upto);
74           out.copyBytes(in, chunk);
75           upto += chunk;
76         }
77       }
78       assertEquals(size, upto);
79       out.close();
80       in.close();
81
82       // verify
83       IndexInput in2 = dir.openInput("test2");
84       upto = 0;
85       while(upto < size) {
86         if (random.nextBoolean()) {
87           final byte v = in2.readByte();
88           assertEquals(value(upto), v);
89           upto++;
90         } else {
91           final int limit = Math.min(_TestUtil.nextInt(random, 1, bytes.length), size-upto);
92           in2.readBytes(bytes, 0, limit);
93           for(int byteIdx=0;byteIdx<limit;byteIdx++) {
94             assertEquals(value(upto), bytes[byteIdx]);
95             upto++;
96           }
97         }
98       }
99       in2.close();
100
101       dir.deleteFile("test");
102       dir.deleteFile("test2");
103       
104       dir.close();
105     }
106   }
107 }