View Javadoc
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.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertFalse;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import org.junit.jupiter.api.Test;
27  
28  public class CircularBufferTest {
29  
30      @Test
31      public void testCopy() {
32          final CircularBuffer buffer = new CircularBuffer(16);
33  
34          buffer.put(1);
35          buffer.put(2);
36          buffer.get();
37          buffer.get();
38  
39          // copy uninitialized data
40          buffer.copy(6, 8);
41  
42          for (int i = 2; i < 6; i++) {
43              assertEquals(0, buffer.get(), "buffer[" + i + "]");
44          }
45          assertEquals(1, buffer.get(), "buffer[" + 6 + "]");
46          assertEquals(2, buffer.get(), "buffer[" + 7 + "]");
47          assertEquals(0, buffer.get(), "buffer[" + 8 + "]");
48          assertEquals(0, buffer.get(), "buffer[" + 9 + "]");
49  
50          for (int i = 10; i < 14; i++) {
51              buffer.put(i);
52              buffer.get();
53          }
54  
55          assertFalse(buffer.available(), "available");
56  
57          // copy data and wrap
58          buffer.copy(2, 8);
59  
60          for (int i = 14; i < 18; i++) {
61              assertEquals(i % 2 == 0 ? 12 : 13, buffer.get(), "buffer[" + i + "]");
62          }
63      }
64  
65      @Test
66      public void testPutAndGet() {
67          final int size = 16;
68          final CircularBuffer buffer = new CircularBuffer(size);
69          for (int i = 0; i < size / 2; i++) {
70              buffer.put(i);
71          }
72  
73          assertTrue(buffer.available(), "available");
74  
75          for (int i = 0; i < size / 2; i++) {
76              assertEquals(i, buffer.get(), "buffer[" + i + "]");
77          }
78  
79          assertEquals(-1, buffer.get());
80          assertFalse(buffer.available(), "available");
81      }
82  }