    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17
    18  //
    19  // Log4j uses the JUnit framework for internal unit testing. JUnit
    20  // available from
    21  //
    22  //     http://www.junit.org
    23
    24
    25  package org.apache.log4j.helpers;
    26
    27  import org.apache.log4j.spi.LoggingEvent;
    28  import org.apache.log4j.Logger;
    29  import org.apache.log4j.Level;
    30
    31  import junit.framework.TestCase;
    32  import junit.framework.TestSuite;
    33  import junit.framework.Test;
    34
    35  /**
    36     Unit test the {@link CyclicBuffer}.
    37
    38     @author Ceki G&uuml;lc&uuml;
    39
    40  */
    41  public class CyclicBufferTestCase extends TestCase {
    42
    43    static Logger cat = Logger.getLogger("x");
    44
    45    static int MAX = 1000;
    46
    47    static LoggingEvent[] e = new LoggingEvent[MAX];
    48
    49    {
    50      for (int i = 0; i < MAX; i++) {
    51        e[i] =  new LoggingEvent("", cat, Level.DEBUG, "e"+i, null);
    52      }
    53    }
    54
    55
    56    public CyclicBufferTestCase(String name) {
    57      super(name);
    58    }
    59
    60
    61    public
    62    void setUp() {
    63
    64    }
    65
    66
    67    public
    68    void test0() {
    69      int size = 2;
    70
    71      CyclicBuffer cb = new CyclicBuffer(size);
    72      assertEquals(cb.getMaxSize(), size);
    73
    74      cb.add(e[0]);
    75      assertEquals(cb.length(), 1);
    76      assertEquals(cb.get(), e[0]); assertEquals(cb.length(), 0);
    77      assertNull(cb.get()); assertEquals(cb.length(), 0);
    78
    79
    80      cb = new CyclicBuffer(size);
    81      cb.add(e[0]);
    82      cb.add(e[1]);
    83      assertEquals(cb.length(), 2);
    84      assertEquals(cb.get(), e[0]); assertEquals(cb.length(), 1);
    85      assertEquals(cb.get(), e[1]); assertEquals(cb.length(), 0);
    86      assertNull(cb.get()); assertEquals(cb.length(), 0);
    87
    88
    89    }
    90
    91    /**
    92       Test a buffer of size 1,2,4,8,..,128
    93     */
    94    public
    95    void test1() {
    96      for(int bufSize = 1; bufSize <= 128; bufSize *=2)
    97        doTest1(bufSize);
    98    }
    99
    100   void doTest1(int size) {
    101     //System.out.println("Doing test with size = "+size);
    102     CyclicBuffer cb = new CyclicBuffer(size);
    103
    104     assertEquals(cb.getMaxSize(), size);
    105
    106     for(int i = -(size+10); i < (size+10); i++) {
    107       assertNull(cb.get(i));
    108     }
    109
    110     for(int i = 0; i < MAX; i++) {
    111       cb.add(e[i]);
    112       int limit = i < size-1 ? i : size-1;
    113
    114       //System.out.println("\nLimit is " + limit + ", i="+i);
    115
    116       for(int j = limit; j >= 0; j--) {
    117 	//System.out.println("i= "+i+", j="+j);
    118 	assertEquals(cb.get(j), e[i-(limit-j)]);
    119       }
    120       assertNull(cb.get(-1));
    121       assertNull(cb.get(limit+1));
    122     }
    123   }
    124
    125   public
    126   void testResize() {
    127     for(int isize = 1; isize <= 128; isize *=2) {
    128       doTestResize(isize, isize/2+1, isize/2+1);
    129       doTestResize(isize, isize/2+1, isize+10);
    130       doTestResize(isize, isize+10, isize/2+1);
    131       doTestResize(isize, isize+10, isize+10);
    132     }
    133   }
    134
    135   void doTestResize(int initialSize, int numberOfAdds, int newSize) {
    136     //System.out.println("initialSize = "+initialSize+", numberOfAdds="
    137     //	       +numberOfAdds+", newSize="+newSize);
    138     CyclicBuffer cb = new CyclicBuffer(initialSize);
    139     for(int i = 0; i < numberOfAdds; i++) {
    140       cb.add(e[i]);
    141     }
    142     cb.resize(newSize);
    143
    144     int offset = numberOfAdds - initialSize;
    145     if(offset< 0)
    146       offset = 0;
    147
    148     int len = newSize < numberOfAdds ? newSize : numberOfAdds;
    149     len = len < initialSize ? len : initialSize;
    150     //System.out.println("Len = "+len+", offset="+offset);
    151     for(int j = 0; j < len; j++) {
    152       assertEquals(cb.get(j), e[offset+j]);
    153     }
    154
    155   }
    156
    157
    158   public
    159   static
    160   Test suite() {
    161     TestSuite suite = new TestSuite();
    162     suite.addTest(new CyclicBufferTestCase("test0"));
    163     suite.addTest(new CyclicBufferTestCase("test1"));
    164     suite.addTest(new CyclicBufferTestCase("testResize"));
    165     return suite;
    166   }
    167 }