001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tctest;
005:
006: import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
007:
008: import com.tc.exception.TCRuntimeException;
009: import com.tc.object.config.ConfigVisitor;
010: import com.tc.object.config.DSOClientConfigHelper;
011: import com.tc.object.config.TransparencyClassSpec;
012: import com.tc.object.config.spec.CyclicBarrierSpec;
013: import com.tc.simulator.app.ApplicationConfig;
014: import com.tc.simulator.listener.ListenerProvider;
015: import com.tctest.runner.AbstractTransparentApp;
016:
017: import java.util.ArrayList;
018: import java.util.Arrays;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Random;
022:
023: public class SimplePrimitiveArrayTestApp extends AbstractTransparentApp {
024:
025: private ArrayRoot root;
026: private CyclicBarrier barrier;
027:
028: public static void visitL1DSOConfig(ConfigVisitor visitor,
029: DSOClientConfigHelper config) {
030: String testClass = SimplePrimitiveArrayTestApp.class.getName();
031: TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
032: String methodExpression = "* " + testClass + "*.*(..)";
033: config.addWriteAutolock(methodExpression);
034: spec.addRoot("root", "the-data-root-yo");
035: spec.addRoot("barrier", "barrier");
036: config.addIncludePattern(ArrayRoot.class.getName());
037: new CyclicBarrierSpec().visit(visitor, config);
038: }
039:
040: public SimplePrimitiveArrayTestApp(String appId,
041: ApplicationConfig cfg, ListenerProvider listenerProvider) {
042: super (appId, cfg, listenerProvider);
043: barrier = new CyclicBarrier(getParticipantCount());
044: }
045:
046: public void run() {
047: try {
048: int length = new Random().nextInt(100);
049: root = new ArrayRoot(getParticipantCount(), length);
050: Double[] original = root.get();
051:
052: ArrayRoot.validateWithEquals(1, original);
053: ArrayRoot.validate(1, original);
054: barrier.barrier();
055:
056: ArrayRoot.modify(2, original);
057: barrier.barrier();
058:
059: root.validateWithEquals(2);
060: root.validate(2);
061: barrier.barrier();
062:
063: Double[] toCopyFrom = new Double[original.length];
064: for (int i = 0; i < toCopyFrom.length; i++) {
065: toCopyFrom[i] = new Double(3);
066: }
067:
068: synchronized (original) {
069: System.arraycopy(toCopyFrom, 0, original, 0,
070: toCopyFrom.length);
071: }
072:
073: barrier.barrier();
074: root.validateWithEquals(3);
075: root.validate(3);
076:
077: } catch (InterruptedException e) {
078: throw new TCRuntimeException(e);
079: }
080:
081: }
082:
083: private static final class ArrayRoot {
084: private final List arrays;
085: private int index;
086:
087: public ArrayRoot(int count, int length) {
088: arrays = new ArrayList();
089: for (int i = 0; i < count; i++) {
090: Double[] sub = new Double[length];
091: arrays.add(sub);
092: for (int j = 0; j < sub.length; j++) {
093: sub[j] = new Double(1);
094: }
095: }
096: }
097:
098: public int size() {
099: return arrays.size();
100: }
101:
102: public synchronized Double[] get() {
103: return (Double[]) arrays.get(index++);
104: }
105:
106: public static void modify(double newValue, Double[] array) {
107: synchronized (array) {
108: for (int i = 0; i < array.length; i++) {
109: array[i] = new Double(newValue);
110: }
111: }
112: }
113:
114: public synchronized void validate(double expectedValue) {
115: for (Iterator i = arrays.iterator(); i.hasNext();) {
116: validate(expectedValue, (Double[]) i.next());
117: }
118: }
119:
120: public synchronized void validateWithEquals(double expectedValue) {
121: for (Iterator i = arrays.iterator(); i.hasNext();) {
122: validateWithEquals(expectedValue, (Double[]) i.next());
123: }
124: }
125:
126: public static void validate(double expectedValue, Double[] array) {
127: synchronized (array) {
128: for (int i = 0; i < array.length; i++) {
129: double value = array[i].doubleValue();
130: if (expectedValue != value) {
131: throw new RuntimeException("Expected "
132: + expectedValue + " but was " + value);
133: }
134: }
135: }
136: }
137:
138: private static void validateWithEquals(double expectedValue,
139: Double[] array) {
140: Double[] compare = new Double[array.length];
141: for (int i = 0; i < compare.length; i++) {
142: compare[i] = new Double(expectedValue);
143: }
144: if (!Arrays.equals(compare, array))
145: throw new RuntimeException("Arrays aren't equal!");
146: }
147:
148: }
149:
150: }
|