View Javadoc

1   /**
2    * Copyright (c) 2012, University of Konstanz, Distributed Systems Group
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    * * Redistributions of source code must retain the above copyright
8    * notice, this list of conditions and the following disclaimer.
9    * * Redistributions in binary form must reproduce the above copyright
10   * notice, this list of conditions and the following disclaimer in the
11   * documentation and/or other materials provided with the distribution.
12   * * Neither the name of the University of Konstanz nor the
13   * names of its contributors may be used to endorse or promote products
14   * derived from this software without specific prior written permission.
15   * 
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19   * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20   * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26   */
27  /**
28   * 
29   */
30  package org.perfidix.example;
31  
32  import static org.junit.Assert.assertEquals;
33  
34  import java.util.Random;
35  import java.util.Stack;
36  
37  import org.junit.BeforeClass;
38  import org.junit.Test;
39  import org.perfidix.annotation.BeforeBenchClass;
40  import org.perfidix.annotation.BenchClass;
41  
42  @BenchClass(runs = 100)
43  public class FastIntStackTest {
44  
45      private static int[] data = new int[15];
46  
47      @BeforeBenchClass
48      @BeforeClass
49      public static void beforeClass() {
50          for (int i = 0; i < data.length; i++) {
51              data[i] = new Random().nextInt();
52          }
53      }
54  
55      @Test
56      public void myStackTest() {
57          final FastIntStack myStack = new FastIntStack();
58          for (int i = 0; i < data.length; i++) {
59              myStack.push(data[i]);
60          }
61          for (int i = data.length - 1; i > 0; i--) {
62              assertEquals(data[i], myStack.pop());
63          }
64      }
65  
66      @Test
67      public void normalStackTest() {
68          final Stack<Integer> normalStack = new Stack<Integer>();
69          for (int i = 0; i < data.length; i++) {
70              normalStack.push(data[i]);
71          }
72          for (int i = data.length - 1; i > 0; i--) {
73              assertEquals(data[i], normalStack.pop().intValue());
74          }
75      }
76  
77      final class FastIntStack {
78  
79          private int[] stack;
80  
81          private int size;
82  
83          FastIntStack() {
84              stack = new int[32];
85              size = 0;
86          }
87  
88          final void push(final int element) {
89              if (stack.length == size) {
90                  int[] biggerStack = new int[stack.length << 1];
91                  System.arraycopy(stack, 0, biggerStack, 0, stack.length);
92                  stack = biggerStack;
93              }
94              stack[size++] = element;
95          }
96  
97          final int peek() {
98              return stack[size - 1];
99          }
100 
101         final int get(final int position) {
102             return stack[position];
103         }
104 
105         final int pop() {
106             return stack[--size];
107         }
108 
109         final void clear() {
110             size = 0;
111         }
112 
113         final int size() {
114             return size;
115         }
116 
117     }
118 }