KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > SimplePrimitiveArrayTestApp


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.tctest;
5
6 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
7
8 import com.tc.exception.TCRuntimeException;
9 import com.tc.object.config.ConfigVisitor;
10 import com.tc.object.config.DSOClientConfigHelper;
11 import com.tc.object.config.TransparencyClassSpec;
12 import com.tc.object.config.spec.CyclicBarrierSpec;
13 import com.tc.simulator.app.ApplicationConfig;
14 import com.tc.simulator.listener.ListenerProvider;
15 import com.tctest.runner.AbstractTransparentApp;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.Arrays JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Random JavaDoc;
22
23 public class SimplePrimitiveArrayTestApp extends AbstractTransparentApp {
24
25   private ArrayRoot root;
26   private CyclicBarrier barrier;
27
28   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
29     String JavaDoc testClass = SimplePrimitiveArrayTestApp.class.getName();
30     TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
31     String JavaDoc methodExpression = "* " + testClass + "*.*(..)";
32     config.addWriteAutolock(methodExpression);
33     spec.addRoot("root", "the-data-root-yo");
34     spec.addRoot("barrier", "barrier");
35     config.addIncludePattern(ArrayRoot.class.getName());
36     new CyclicBarrierSpec().visit(visitor, config);
37   }
38
39   public SimplePrimitiveArrayTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
40     super(appId, cfg, listenerProvider);
41     barrier = new CyclicBarrier(getParticipantCount());
42   }
43
44   public void run() {
45     try {
46       int length = new Random JavaDoc().nextInt(100);
47       root = new ArrayRoot(getParticipantCount(), length);
48       Double JavaDoc[] original = root.get();
49
50       ArrayRoot.validateWithEquals(1, original);
51       ArrayRoot.validate(1, original);
52       barrier.barrier();
53
54       ArrayRoot.modify(2, original);
55       barrier.barrier();
56
57       root.validateWithEquals(2);
58       root.validate(2);
59       barrier.barrier();
60
61       Double JavaDoc[] toCopyFrom = new Double JavaDoc[original.length];
62       for (int i = 0; i < toCopyFrom.length; i++) {
63         toCopyFrom[i] = new Double JavaDoc(3);
64       }
65
66       synchronized (original) {
67         System.arraycopy(toCopyFrom, 0, original, 0, toCopyFrom.length);
68       }
69
70       barrier.barrier();
71       root.validateWithEquals(3);
72       root.validate(3);
73
74     } catch (InterruptedException JavaDoc e) {
75       throw new TCRuntimeException(e);
76     }
77
78   }
79
80   private static final class ArrayRoot {
81     private final List JavaDoc arrays;
82     private int index;
83
84     public ArrayRoot(int count, int length) {
85       arrays = new ArrayList JavaDoc();
86       for (int i = 0; i < count; i++) {
87         Double JavaDoc[] sub = new Double JavaDoc[length];
88         arrays.add(sub);
89         for (int j = 0; j < sub.length; j++) {
90           sub[j] = new Double JavaDoc(1);
91         }
92       }
93     }
94
95     public int size() {
96       return arrays.size();
97     }
98
99     public synchronized Double JavaDoc[] get() {
100       return (Double JavaDoc[]) arrays.get(index++);
101     }
102
103     public static void modify(double newValue, Double JavaDoc[] array) {
104       synchronized (array) {
105         for (int i = 0; i < array.length; i++) {
106           array[i] = new Double JavaDoc(newValue);
107         }
108       }
109     }
110
111     public synchronized void validate(double expectedValue) {
112       for (Iterator JavaDoc i = arrays.iterator(); i.hasNext();) {
113         validate(expectedValue, (Double JavaDoc[]) i.next());
114       }
115     }
116
117     public synchronized void validateWithEquals(double expectedValue) {
118       for (Iterator JavaDoc i = arrays.iterator(); i.hasNext();) {
119         validateWithEquals(expectedValue, (Double JavaDoc[]) i.next());
120       }
121     }
122
123     public static void validate(double expectedValue, Double JavaDoc[] array) {
124       synchronized (array) {
125         for (int i = 0; i < array.length; i++) {
126           double value = array[i].doubleValue();
127           if (expectedValue != value) { throw new RuntimeException JavaDoc("Expected " + expectedValue + " but was " + value); }
128         }
129       }
130     }
131
132     private static void validateWithEquals(double expectedValue, Double JavaDoc[] array) {
133       Double JavaDoc[] compare = new Double JavaDoc[array.length];
134       for (int i = 0; i < compare.length; i++) {
135         compare[i] = new Double JavaDoc(expectedValue);
136       }
137       if (!Arrays.equals(compare, array)) throw new RuntimeException JavaDoc("Arrays aren't equal!");
138     }
139
140   }
141
142 }
143
Popular Tags