BoundedFIFOTestCase xref

    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
    32  import junit.framework.TestCase;
    33  import junit.framework.TestSuite;
    34  import junit.framework.Test;
    35
    36
    37
    38
    39  /**
    40     Unit test the {@link BoundedFIFO}.
    41     @author Ceki G&uuml;lc&uuml;
    42     @since 0.9.1 */
    43  public class BoundedFIFOTestCase extends TestCase {
    44    static Logger cat = Logger.getLogger("x");
    45
    46    static int MAX = 1000;
    47
    48    static LoggingEvent[] e = new LoggingEvent[MAX];
    49
    50    {
    51      for (int i = 0; i < MAX; i++) {
    52        e[i] =  new LoggingEvent("", cat, Level.DEBUG, "e"+i, null);
    53      }
    54    }
    55
    56
    57    public BoundedFIFOTestCase(String name) {
    58      super(name);
    59    }
    60
    61
    62    public
    63    void setUp() {
    64
    65    }
    66
    67
    68    /**
    69       Pattern: +++++..-----..
    70     */
    71    public
    72    void test1() {
    73      for(int size = 1; size <= 128; size *=2) {
    74        BoundedFIFO bf = new BoundedFIFO(size);
    75
    76        assertEquals(bf.getMaxSize(), size);
    77        assertNull(bf.get());
    78
    79        int i;
    80        int j;
    81        int k;
    82
    83        for(i = 1; i < 2*size; i++) {
    84  	for(j = 0; j < i; j++) {
    85  	  //System.out.println("Putting "+e[j]);
    86  	  bf.put(e[j]); assertEquals(bf.length(), j < size ?  j+1 : size);
    87  	}
    88  	int max = size < j ? size : j;
    89  	j--;
    90  	for(k = 0; k <= j; k++) {
    91  	  //System.out.println("max="+max+", j="+j+", k="+k);
    92  	  assertEquals(bf.length(), max - k > 0 ? max - k : 0);
    93  	  Object r = bf.get();
    94  	  //System.out.println("Got "+r);
    95  	  if(k >= size)
    96  	    assertNull(r);
    97  	  else
    98  	    assertEquals(r, e[k]);
    99  	}
    100       }
    101       //System.out.println("Passed size="+size);
    102     }
    103   }
    104
    105
    106   /**
    107      Pattern: ++++--++--++
    108    */
    109   public
    110   void test2() {
    111     int size = 3;
    112     BoundedFIFO bf = new BoundedFIFO(size);
    113
    114     bf.put(e[0]);
    115     assertEquals(bf.get(), e[0]);
    116     assertNull(bf.get());
    117
    118     bf.put(e[1]); assertEquals(bf.length(), 1);
    119     bf.put(e[2]); assertEquals(bf.length(), 2);
    120     bf.put(e[3]); assertEquals(bf.length(), 3);
    121     assertEquals(bf.get(), e[1]); assertEquals(bf.length(), 2);
    122     assertEquals(bf.get(), e[2]); assertEquals(bf.length(), 1);
    123     assertEquals(bf.get(), e[3]); assertEquals(bf.length(), 0);
    124     assertNull(bf.get()); assertEquals(bf.length(), 0);
    125   }
    126
    127   int min(int a, int b) {
    128     return a < b ? a : b;
    129   }
    130
    131
    132   /**
    133      Pattern ++++++++++++++++++++ (insert only);
    134    */
    135   public
    136   void testResize1() {
    137     int size = 10;
    138
    139     for(int n = 1; n < size*2; n++) {
    140       for(int i = 0; i < size*2; i++) {
    141
    142         BoundedFIFO bf = new BoundedFIFO(size);
    143         for(int f = 0; f < i; f++) {
    144           bf.put(e[f]);
    145         }
    146
    147         bf.resize(n);
    148         int expectedSize = min(n, min(i, size));
    149         assertEquals(bf.length(), expectedSize);
    150         for(int c = 0; c < expectedSize; c++) {
    151           assertEquals(bf.get(), e[c]);
    152         }
    153       }
    154     }
    155   }
    156
    157
    158
    159   /**
    160      Pattern ++...+ --...-
    161    */
    162   public
    163   void testResize2() {
    164     int size = 10;
    165
    166     for(int n = 1; n < size*2; n++) {
    167       for(int i = 0; i < size*2; i++) {
    168 	for(int d = 0; d < min(i,size); d++) {
    169
    170 	  BoundedFIFO bf = new BoundedFIFO(size);
    171 	  for(int p = 0; p < i; p++) {
    172 	    bf.put(e[p]);
    173 	  }
    174
    175 	  for(int g = 0; g < d; g++) {
    176 	    bf.get();
    177 	  }
    178
    179 	  // x = the number of elems in
    180 	  int x = bf.length();
    181
    182 	  bf.resize(n);
    183
    184 	  int expectedSize = min(n, x);
    185 	  assertEquals(bf.length(), expectedSize);
    186
    187 	  for(int c = 0; c < expectedSize; c++) {
    188 	    assertEquals(bf.get(), e[c+d]);
    189 	  }
    190 	  assertNull(bf.get());
    191 	}
    192       }
    193     }
    194   }
    195
    196
    197   /**
    198      Pattern: i inserts, d deletes, r inserts
    199    */
    200   public
    201   void testResize3() {
    202     int size = 10;
    203
    204     for(int n = 1; n < size*2; n++) {
    205       for(int i = 0; i < size; i++) {
    206 	for(int d = 0; d < i; d++) {
    207 	  for(int r = 0; r < d; r++) {
    208
    209 	    BoundedFIFO bf = new BoundedFIFO(size);
    210 	    for(int p0 = 0; p0 < i; p0++)
    211 	      bf.put(e[p0]);
    212
    213 	    for(int g = 0; g < d; g++)
    214 	      bf.get();
    215 	    for(int p1 = 0; p1 < r; p1++)
    216 	      bf.put(e[i+p1]);
    217
    218
    219
    220 	    int x =  bf.length();
    221
    222 	    bf.resize(n);
    223
    224
    225 	    int expectedSize = min(n, x);
    226 	    assertEquals(bf.length(), expectedSize);
    227
    228 	    for(int c = 0; c < expectedSize; c++) {
    229 	      assertEquals(bf.get(), e[c+d]);
    230 	    }
    231 	    //assertNull(bf.get());
    232 	  }
    233 	}
    234       }
    235     }
    236   }
    237
    238
    239   public
    240   static
    241   Test suite() {
    242     TestSuite suite = new TestSuite();
    243     suite.addTest(new BoundedFIFOTestCase("test1"));
    244     suite.addTest(new BoundedFIFOTestCase("test2"));
    245     suite.addTest(new BoundedFIFOTestCase("testResize1"));
    246     suite.addTest(new BoundedFIFOTestCase("testResize2"));
    247     suite.addTest(new BoundedFIFOTestCase("testResize3"));
    248     return suite;
    249   }
    250 }