View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache.store;
18  
19  import java.util.Random;
20  
21  import junit.framework.TestCase;
22  
23  import org.junit.Test;
24  
25  /***
26   * Test logic for initialCapacity for MemoryStore
27   *
28   * @author Abhishek Sanoujam
29   */
30  public class InitialCapacityTest extends TestCase {
31  
32      private static final Random random = new Random(System.currentTimeMillis());
33  
34      /***
35       * Test the initial capacity logic
36       */
37      @Test
38      public void testInitialCapacity() {
39          float loadFactor = MemoryStore.DEFAULT_LOAD_FACTOR;
40          // test positive sizes
41          for (int i = 0; i < 1000; i++) {
42              int n = random.nextInt(Integer.MAX_VALUE);
43              assertTrue(n >= 0);
44              doTestInitialCapacity((int) Math.ceil(n / loadFactor), loadFactor, n);
45          }
46          // test negative sizes
47          for (int i = 0; i < 1000; i++) {
48              int n = random.nextInt(Integer.MAX_VALUE) * -1;
49              assertTrue(n <= 0);
50              // initialCapacity is zero for negative maximumSizes
51              doTestInitialCapacity(0, loadFactor, n);
52          }
53          // test 0
54          doTestInitialCapacity(0, loadFactor, 0);
55  
56          // test Integer.MAX_VALUE
57          doTestInitialCapacity(Integer.MAX_VALUE, loadFactor, Integer.MAX_VALUE);
58      }
59  
60      private void doTestInitialCapacity(int expectedValue, float loadFactor, int maximumSizeGoal) {
61          assertEquals(expectedValue, MemoryStore.getInitialCapacityForLoadFactor(maximumSizeGoal, loadFactor));
62      }
63  
64  }