1 package org.apache.lucene.store;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 import org.apache.lucene.util.LuceneTestCase;
22 import org.apache.lucene.util._TestUtil;
24 import org.junit.Test;
26 public class TestCopyBytes extends LuceneTestCase {
28 private byte value(int idx) {
29 return (byte) ((idx%256) * (1+(idx/256)));
34 public void testCopyBytes() throws Exception {
35 int num = atLeast(10);
36 for(int iter=0;iter<num;iter++) {
37 Directory dir = newDirectory();
39 System.out.println("TEST: iter=" + iter + " dir=" + dir);
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);
49 bytes[byteUpto++] = value(upto);
51 if (byteUpto == bytes.length) {
52 out.writeBytes(bytes, 0, bytes.length);
57 out.writeBytes(bytes, 0, byteUpto);
58 assertEquals(size, out.getFilePointer());
60 assertEquals(size, dir.fileLength("test"));
62 // copy from test -> test2
63 final IndexInput in = dir.openInput("test");
65 out = dir.createOutput("test2");
69 if (random.nextBoolean()) {
70 out.writeByte(in.readByte());
73 final int chunk = Math.min(_TestUtil.nextInt(random, 1, bytes.length), size-upto);
74 out.copyBytes(in, chunk);
78 assertEquals(size, upto);
83 IndexInput in2 = dir.openInput("test2");
86 if (random.nextBoolean()) {
87 final byte v = in2.readByte();
88 assertEquals(value(upto), v);
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]);
101 dir.deleteFile("test");
102 dir.deleteFile("test2");