1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20 package org.apache.commons.compress.archivers.zip; 21 22 import static org.junit.Assert.*; 23 24 import org.junit.Test; 25 26 public class CircularBufferTest { 27 28 @Test 29 public void testPutAndGet() throws Exception { 30 final int size = 16; 31 final CircularBuffer buffer = new CircularBuffer(size); 32 for (int i = 0; i < size / 2; i++) { 33 buffer.put(i); 34 } 35 36 assertTrue("available", buffer.available()); 37 38 for (int i = 0; i < size / 2; i++) { 39 assertEquals("buffer[" + i + "]", i, buffer.get()); 40 } 41 42 assertEquals(-1, buffer.get()); 43 assertFalse("available", buffer.available()); 44 } 45 46 @Test 47 public void testCopy() throws Exception { 48 final CircularBuffer buffer = new CircularBuffer(16); 49 50 buffer.put(1); 51 buffer.put(2); 52 buffer.get(); 53 buffer.get(); 54 55 // copy uninitialized data 56 buffer.copy(6, 8); 57 58 for (int i = 2; i < 6; i++) { 59 assertEquals("buffer[" + i + "]", 0, buffer.get()); 60 } 61 assertEquals("buffer[" + 6 + "]", 1, buffer.get()); 62 assertEquals("buffer[" + 7 + "]", 2, buffer.get()); 63 assertEquals("buffer[" + 8 + "]", 0, buffer.get()); 64 assertEquals("buffer[" + 9 + "]", 0, buffer.get()); 65 66 for (int i = 10; i < 14; i++) { 67 buffer.put(i); 68 buffer.get(); 69 } 70 71 assertFalse("available", buffer.available()); 72 73 // copy data and wrap 74 buffer.copy(2, 8); 75 76 for (int i = 14; i < 18; i++) { 77 assertEquals("buffer[" + i + "]", i % 2 == 0 ? 12 : 13, buffer.get()); 78 } 79 } 80 } 81