KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > stats > counter > CounterTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.stats.counter;
5
6 import java.util.Random JavaDoc;
7
8 import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong;
9 import EDU.oswego.cs.dl.util.concurrent.SynchronizedRef;
10 import junit.framework.TestCase;
11
12 public class CounterTest extends TestCase {
13
14   public void testInitialValue() {
15     Counter counter = new CounterImpl();
16     assertEquals(0L, counter.getValue());
17
18     counter = new CounterImpl(42L);
19     assertEquals(42L, counter.getValue());
20   }
21
22   public void testConcurrency() throws InterruptedException JavaDoc {
23     final Counter counter = new CounterImpl();
24     final SynchronizedLong local = new SynchronizedLong(0L);
25     final SynchronizedRef error = new SynchronizedRef(null);
26
27     Thread JavaDoc[] threads = new Thread JavaDoc[10];
28     for (int i = 0; i < threads.length; i++) {
29       threads[i] = new Thread JavaDoc() {
30         public void run() {
31           try {
32             Random JavaDoc random = new Random JavaDoc(hashCode());
33             for (int n = 0; n < 100000; n++) {
34               int operation = random.nextInt(4);
35
36               switch (operation) {
37                 case 0: {
38                   local.decrement();
39                   counter.decrement();
40                   break;
41                 }
42                 case 1: {
43                   local.increment();
44                   counter.increment();
45                   break;
46                 }
47                 case 2: {
48                   long amount = random.nextLong();
49                   local.subtract(amount);
50                   counter.decrement(amount);
51                   break;
52                 }
53                 case 3: {
54                   long amount = random.nextLong();
55                   local.add(amount);
56                   counter.increment(amount);
57                   break;
58                 }
59                 default: {
60                   throw new RuntimeException JavaDoc("operation " + operation);
61                 }
62               }
63             }
64           } catch (Throwable JavaDoc t) {
65             t.printStackTrace();
66             error.set(t);
67           }
68         }
69       };
70       threads[i].start();
71     }
72
73     for (int i = 0; i < threads.length; i++) {
74       threads[i].join();
75     }
76
77     if (error.get() != null) {
78       fail(error.get().toString());
79     }
80
81     assertEquals(local.get(), counter.getValue());
82   }
83
84   
85   public void testMinMax() {
86     Counter counter = new CounterImpl();
87     assertEquals(0, counter.getMinValue());
88     assertEquals(0, counter.getMaxValue());
89     
90     counter.increment();
91     assertEquals(0, counter.getMinValue());
92     assertEquals(1, counter.getMaxValue());
93     
94     counter.decrement();
95     assertEquals(0, counter.getMinValue());
96     assertEquals(1, counter.getMaxValue());
97     counter.decrement();
98     assertEquals(-1, counter.getMinValue());
99     assertEquals(1, counter.getMaxValue());
100     
101     counter.increment(10);
102     assertEquals(-1, counter.getMinValue());
103     assertEquals(9, counter.getMaxValue());
104     
105     counter.decrement(20);
106     assertEquals(-11, counter.getMinValue());
107     assertEquals(9, counter.getMaxValue());
108     
109     counter.setValue(100);
110     assertEquals(-11, counter.getMinValue());
111     assertEquals(100, counter.getMaxValue());
112     
113     counter.setValue(-100);
114     assertEquals(-100, counter.getMinValue());
115     assertEquals(100, counter.getMaxValue());
116     
117     counter.getAndSet(5);
118     assertEquals(-100, counter.getMinValue());
119     assertEquals(100, counter.getMaxValue());
120   }
121   
122   
123   public void test() {
124     Counter counter = new CounterImpl();
125     assertEquals(0L, counter.getValue());
126
127     counter.decrement();
128     assertEquals(-1L, counter.getValue());
129
130     counter.increment();
131     assertEquals(0L, counter.getValue());
132
133     counter.decrement(10L);
134     assertEquals(-10L, counter.getValue());
135
136     counter.increment(10L);
137     assertEquals(0L, counter.getValue());
138
139     counter.increment(-10L);
140     assertEquals(-10L, counter.getValue());
141
142     counter.decrement(-10L);
143     assertEquals(0L, counter.getValue());
144
145     counter.setValue(Long.MAX_VALUE);
146     assertEquals(Long.MAX_VALUE, counter.getValue());
147
148     counter.setValue(Long.MIN_VALUE);
149     assertEquals(Long.MIN_VALUE, counter.getValue());
150
151     counter.setValue(0L);
152     assertEquals(0L, counter.getValue());
153
154     counter.increment();
155     counter.increment();
156     counter.increment();
157     long value = counter.getAndSet(42L);
158     assertEquals(3L, value);
159     assertEquals(42L, counter.getValue());
160   }
161
162 }
163
Popular Tags