From 58f8009dfc8da1db4636b6f0894b8fbb77a57525 Mon Sep 17 00:00:00 2001 From: Brian Vosburgh Date: Tue, 9 Feb 2016 15:17:36 -0500 Subject: clean up Stack tests --- .../stack/CachingConcurrentStackTests.java | 157 +------------------ .../tests/internal/stack/ConcurrentStackTests.java | 157 +------------------ .../utility/tests/internal/stack/StackTests.java | 171 ++++++++++++++++++++- .../tests/internal/stack/StackToolsTests.java | 82 ++++++++++ .../internal/stack/SynchronizedStackTests.java | 5 + 5 files changed, 263 insertions(+), 309 deletions(-) (limited to 'common/tests') diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/stack/CachingConcurrentStackTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/stack/CachingConcurrentStackTests.java index 3e68f24b94..64672fbf82 100644 --- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/stack/CachingConcurrentStackTests.java +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/stack/CachingConcurrentStackTests.java @@ -9,13 +9,7 @@ ******************************************************************************/ package org.eclipse.jpt.common.utility.tests.internal.stack; -import java.util.Arrays; -import java.util.List; -import java.util.Vector; -import org.eclipse.jpt.common.utility.internal.ArrayTools; import org.eclipse.jpt.common.utility.internal.ObjectTools; -import org.eclipse.jpt.common.utility.internal.reference.SynchronizedBoolean; -import org.eclipse.jpt.common.utility.internal.stack.CachingConcurrentStack; import org.eclipse.jpt.common.utility.internal.stack.StackTools; import org.eclipse.jpt.common.utility.stack.Stack; @@ -32,154 +26,9 @@ public class CachingConcurrentStackTests return StackTools.cachingConcurrentStack(); } - public void testConcurrentAccess() throws InterruptedException { - CachingConcurrentStack stack = StackTools.cachingConcurrentStack(); - - int threadCount = 10; - int elementsPerThread = 100000; - SynchronizedBoolean startFlag = new SynchronizedBoolean(false); - - PushRunnable[] pushRunnables = new PushRunnable[threadCount]; - Thread[] pushThreads = new Thread[threadCount]; - for (int i = 0; i < threadCount; i++) { - PushRunnable pushRunnable = new PushRunnable(stack, (i * elementsPerThread), elementsPerThread, startFlag); - pushRunnables[i] = pushRunnable; - Thread pushThread = new Thread(pushRunnable); - pushThreads[i] = pushThread; - pushThread.start(); - } - - PopRunnable[] popRunnables = new PopRunnable[threadCount]; - Thread[] popThreads = new Thread[threadCount]; - for (int i = 0; i < threadCount; i++) { - PopRunnable popRunnable = new PopRunnable(stack, elementsPerThread, startFlag); - popRunnables[i] = popRunnable; - Thread popThread = new Thread(popRunnable); - popThreads[i] = popThread; - popThread.start(); - } - - startFlag.setTrue(); - for (int i = 0; i < threadCount; i++) { - pushThreads[i].join(); - assertTrue(pushRunnables[i].exceptions.isEmpty()); - } - for (int i = 0; i < threadCount; i++) { - popThreads[i].join(); - assertTrue(popRunnables[i].exceptions.isEmpty()); - } - - // if we get here, we have, at the least, popd as many elements as we pushd... - // ...now verify that all the popd elements are unique - // (i.e. none were lost or duplicated) - int totalCount = threadCount * elementsPerThread; - int uberMax = totalCount + 1; - int uberMaxThreadIndex = threadCount + 1; - int[] indexes = ArrayTools.fill(new int[threadCount], 0); - for (int i = 0; i < totalCount; i++) { - int min = uberMax; - int minThreadIndex = uberMaxThreadIndex; - for (int j = 0; j < threadCount; j++) { - int currentIndex = indexes[j]; - if (currentIndex < elementsPerThread) { - int current = popRunnables[j].elements[currentIndex].intValue(); - if (current < min) { - min = current; - minThreadIndex = j; - } - } - } - assertEquals(i, min); - indexes[minThreadIndex]++; - } - } - - public static class PushRunnable - implements Runnable - { - private final Stack stack; - private final int start; - private final int stop; - private final SynchronizedBoolean startFlag; - final List exceptions = new Vector<>(); - - public PushRunnable(Stack stack, int start, int count, SynchronizedBoolean startFlag) { - super(); - this.stack = stack; - this.start = start; - this.stop = start + count; - this.startFlag = startFlag; - } - - public void run() { - try { - this.run_(); - } catch (InterruptedException ex) { - this.exceptions.add(ex); - } - } - - private void run_() throws InterruptedException { - this.startFlag.waitUntilTrue(); - for (int i = this.start; i < this.stop; i++) { - this.stack.push(Integer.valueOf(i)); - if ((i % 20) == 0) { - Thread.sleep(0); - } - } - } - } - - public static class PopRunnable - implements Runnable - { - private final Stack stack; - private final int count; - final Integer[] elements; - private int elementsCount; - private final SynchronizedBoolean startFlag; - final List exceptions = new Vector<>(); - - public PopRunnable(Stack stack, int count, SynchronizedBoolean startFlag) { - super(); - this.stack = stack; - this.count = count; - this.elements = new Integer[count]; - this.elementsCount = 0; - this.startFlag = startFlag; - } - - public void run() { - try { - this.run_(); - } catch (InterruptedException ex) { - this.exceptions.add(ex); - } - } - - private void run_() throws InterruptedException { - this.startFlag.waitUntilTrue(); - int i = 0; - while (true) { - Integer element = this.stack.peek(); // fiddle with peek also - element = this.stack.pop(); - if (element != null) { - this.elements[this.elementsCount++] = element; - if (this.elementsCount == this.count) { - break; - } - } - if ((i % 20) == 0) { - Thread.sleep(0); - } - if (i == Integer.MAX_VALUE) { - i = 0; - } else { - i++; - } - } - Arrays.sort(this.elements); - } + @Override + Stack buildConcurrentStack() { + return StackTools.cachingConcurrentStack(); } public void testCache() { diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/stack/ConcurrentStackTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/stack/ConcurrentStackTests.java index 613517b73c..c218b33c11 100644 --- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/stack/ConcurrentStackTests.java +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/stack/ConcurrentStackTests.java @@ -9,13 +9,7 @@ ******************************************************************************/ package org.eclipse.jpt.common.utility.tests.internal.stack; -import java.util.Arrays; -import java.util.List; -import java.util.Vector; -import org.eclipse.jpt.common.utility.internal.ArrayTools; import org.eclipse.jpt.common.utility.internal.ObjectTools; -import org.eclipse.jpt.common.utility.internal.reference.SynchronizedBoolean; -import org.eclipse.jpt.common.utility.internal.stack.ConcurrentStack; import org.eclipse.jpt.common.utility.internal.stack.StackTools; import org.eclipse.jpt.common.utility.stack.Stack; @@ -32,154 +26,9 @@ public class ConcurrentStackTests return StackTools.concurrentStack(); } - public void testConcurrentAccess() throws InterruptedException { - ConcurrentStack stack = StackTools.concurrentStack(); - - int threadCount = 10; - int elementsPerThread = 100000; - SynchronizedBoolean startFlag = new SynchronizedBoolean(false); - - PushRunnable[] pushRunnables = new PushRunnable[threadCount]; - Thread[] pushThreads = new Thread[threadCount]; - for (int i = 0; i < threadCount; i++) { - PushRunnable pushRunnable = new PushRunnable(stack, (i * elementsPerThread), elementsPerThread, startFlag); - pushRunnables[i] = pushRunnable; - Thread pushThread = new Thread(pushRunnable); - pushThreads[i] = pushThread; - pushThread.start(); - } - - PopRunnable[] popRunnables = new PopRunnable[threadCount]; - Thread[] popThreads = new Thread[threadCount]; - for (int i = 0; i < threadCount; i++) { - PopRunnable popRunnable = new PopRunnable(stack, elementsPerThread, startFlag); - popRunnables[i] = popRunnable; - Thread popThread = new Thread(popRunnable); - popThreads[i] = popThread; - popThread.start(); - } - - startFlag.setTrue(); - for (int i = 0; i < threadCount; i++) { - pushThreads[i].join(); - assertTrue(pushRunnables[i].exceptions.isEmpty()); - } - for (int i = 0; i < threadCount; i++) { - popThreads[i].join(); - assertTrue(popRunnables[i].exceptions.isEmpty()); - } - - // if we get here, we have, at the least, popd as many elements as we pushd... - // ...now verify that all the popd elements are unique - // (i.e. none were lost or duplicated) - int totalCount = threadCount * elementsPerThread; - int uberMax = totalCount + 1; - int uberMaxThreadIndex = threadCount + 1; - int[] indexes = ArrayTools.fill(new int[threadCount], 0); - for (int i = 0; i < totalCount; i++) { - int min = uberMax; - int minThreadIndex = uberMaxThreadIndex; - for (int j = 0; j < threadCount; j++) { - int currentIndex = indexes[j]; - if (currentIndex < elementsPerThread) { - int current = popRunnables[j].elements[currentIndex].intValue(); - if (current < min) { - min = current; - minThreadIndex = j; - } - } - } - assertEquals(i, min); - indexes[minThreadIndex]++; - } - } - - public static class PushRunnable - implements Runnable - { - private final Stack stack; - private final int start; - private final int stop; - private final SynchronizedBoolean startFlag; - final List exceptions = new Vector<>(); - - public PushRunnable(Stack stack, int start, int count, SynchronizedBoolean startFlag) { - super(); - this.stack = stack; - this.start = start; - this.stop = start + count; - this.startFlag = startFlag; - } - - public void run() { - try { - this.run_(); - } catch (InterruptedException ex) { - this.exceptions.add(ex); - } - } - - private void run_() throws InterruptedException { - this.startFlag.waitUntilTrue(); - for (int i = this.start; i < this.stop; i++) { - this.stack.push(Integer.valueOf(i)); - if ((i % 20) == 0) { - Thread.sleep(0); - } - } - } - } - - public static class PopRunnable - implements Runnable - { - private final Stack stack; - private final int count; - final Integer[] elements; - private int elementsCount; - private final SynchronizedBoolean startFlag; - final List exceptions = new Vector<>(); - - public PopRunnable(Stack stack, int count, SynchronizedBoolean startFlag) { - super(); - this.stack = stack; - this.count = count; - this.elements = new Integer[count]; - this.elementsCount = 0; - this.startFlag = startFlag; - } - - public void run() { - try { - this.run_(); - } catch (InterruptedException ex) { - this.exceptions.add(ex); - } - } - - private void run_() throws InterruptedException { - this.startFlag.waitUntilTrue(); - int i = 0; - while (true) { - Integer element = this.stack.peek(); // fiddle with peek also - element = this.stack.pop(); - if (element != null) { - this.elements[this.elementsCount++] = element; - if (this.elementsCount == this.count) { - break; - } - } - if ((i % 20) == 0) { - Thread.sleep(0); - } - if (i == Integer.MAX_VALUE) { - i = 0; - } else { - i++; - } - } - Arrays.sort(this.elements); - } + @Override + Stack buildConcurrentStack() { + return StackTools.concurrentStack(); } public void testNodeToString() { diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/stack/StackTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/stack/StackTests.java index 54fc775ef1..9c2aecf0f8 100644 --- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/stack/StackTests.java +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/stack/StackTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2015 Oracle. All rights reserved. + * Copyright (c) 2007, 2016 Oracle. All rights reserved. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0, which accompanies this distribution * and is available at http://www.eclipse.org/legal/epl-v10.html. @@ -9,8 +9,13 @@ ******************************************************************************/ package org.eclipse.jpt.common.utility.tests.internal.stack; +import java.util.Arrays; import java.util.EmptyStackException; +import java.util.List; +import java.util.Vector; +import org.eclipse.jpt.common.utility.internal.ArrayTools; import org.eclipse.jpt.common.utility.internal.ObjectTools; +import org.eclipse.jpt.common.utility.internal.reference.SynchronizedBoolean; import org.eclipse.jpt.common.utility.stack.Stack; import org.eclipse.jpt.common.utility.tests.internal.MultiThreadedTestCase; import org.eclipse.jpt.common.utility.tests.internal.TestTools; @@ -166,4 +171,168 @@ public abstract class StackTests assertEquals("[third, second, first]", stack.toString()); } + + // ********** concurrency ********** + + Stack buildConcurrentStack() { + return null; + } + + public void testConcurrentAccess() throws InterruptedException { + Stack stack = this.buildConcurrentStack(); + if (stack == null) { + return; + } + + int threadCount = 10; + int elementsPerThread = 100000; + SynchronizedBoolean startFlag = new SynchronizedBoolean(false); + + PushRunnable[] pushRunnables = new PushRunnable[threadCount]; + Thread[] pushThreads = new Thread[threadCount]; + for (int i = 0; i < threadCount; i++) { + PushRunnable pushRunnable = new PushRunnable(stack, (i * elementsPerThread), elementsPerThread, startFlag); + pushRunnables[i] = pushRunnable; + Thread pushThread = new Thread(pushRunnable, "Push-" + i); + pushThreads[i] = pushThread; + pushThread.start(); + } + + PopRunnable[] popRunnables = new PopRunnable[threadCount]; + Thread[] popThreads = new Thread[threadCount]; + for (int i = 0; i < threadCount; i++) { + PopRunnable popRunnable = new PopRunnable(stack, elementsPerThread, startFlag); + popRunnables[i] = popRunnable; + Thread popThread = new Thread(popRunnable, "Pop-" + i); + popThreads[i] = popThread; + popThread.start(); + } + + startFlag.setTrue(); + for (int i = 0; i < threadCount; i++) { + pushThreads[i].join(); + assertTrue(pushRunnables[i].exceptions.isEmpty()); + } + for (int i = 0; i < threadCount; i++) { + popThreads[i].join(); + assertTrue(popRunnables[i].exceptions.isEmpty()); + } + + // if we get here, we have, at the least, popped as many elements as we pushed... + // ...now verify that all the popped elements are unique + // (i.e. none were lost or duplicated) + int totalCount = threadCount * elementsPerThread; + int uberMax = totalCount + 1; + int uberMaxThreadIndex = threadCount + 1; + int[] indexes = ArrayTools.fill(new int[threadCount], 0); + for (int i = 0; i < totalCount; i++) { + int min = uberMax; + int minThreadIndex = uberMaxThreadIndex; + for (int j = 0; j < threadCount; j++) { + int currentIndex = indexes[j]; + if (currentIndex < elementsPerThread) { + int current = popRunnables[j].elements[currentIndex].intValue(); + if (current < min) { + min = current; + minThreadIndex = j; + } + } + } + assertEquals(i, min); + indexes[minThreadIndex]++; + } + } + + public static class PushRunnable + implements Runnable + { + private final Stack stack; + private final int start; + private final int stop; + private final SynchronizedBoolean startFlag; + final List exceptions = new Vector<>(); + + public PushRunnable(Stack stack, int start, int count, SynchronizedBoolean startFlag) { + super(); + this.stack = stack; + this.start = start; + this.stop = start + count; + this.startFlag = startFlag; + } + + public void run() { + try { + this.run_(); + } catch (InterruptedException ex) { + this.exceptions.add(ex); + } + } + + private void run_() throws InterruptedException { + this.startFlag.waitUntilTrue(); + for (int i = this.start; i < this.stop; i++) { + this.stack.push(Integer.valueOf(i)); + if ((i % 20) == 0) { + Thread.sleep(0); + } + } + } + } + + public static class PopRunnable + implements Runnable + { + private final Stack stack; + private final int count; + final Integer[] elements; + private int elementsCount; + private final SynchronizedBoolean startFlag; + final List exceptions = new Vector<>(); + + public PopRunnable(Stack stack, int count, SynchronizedBoolean startFlag) { + super(); + this.stack = stack; + this.count = count; + this.elements = new Integer[count]; + this.elementsCount = 0; + this.startFlag = startFlag; + } + + public void run() { + try { + this.run_(); + } catch (InterruptedException ex) { + this.exceptions.add(ex); + } + } + + private void run_() throws InterruptedException { + this.startFlag.waitUntilTrue(); + int i = 0; + while (true) { + Integer element = null; + try { + element = this.stack.peek(); // fiddle with peek also + element = this.stack.pop(); + } catch (EmptyStackException ex) { + element = null; + } + if (element != null) { + this.elements[this.elementsCount++] = element; + if (this.elementsCount == this.count) { + break; + } + } + if ((i % 20) == 0) { + Thread.sleep(0); + } + if (i == Integer.MAX_VALUE) { + i = 0; + } else { + i++; + } + } + Arrays.sort(this.elements); + } + } } diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/stack/StackToolsTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/stack/StackToolsTests.java index e9167bf12c..e1e8e592b1 100644 --- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/stack/StackToolsTests.java +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/stack/StackToolsTests.java @@ -19,6 +19,8 @@ import org.eclipse.jpt.common.utility.internal.StringTools; import org.eclipse.jpt.common.utility.internal.queue.ArrayQueue; import org.eclipse.jpt.common.utility.internal.queue.QueueTools; import org.eclipse.jpt.common.utility.internal.stack.ArrayStack; +import org.eclipse.jpt.common.utility.internal.stack.CachingConcurrentStack; +import org.eclipse.jpt.common.utility.internal.stack.ConcurrentStack; import org.eclipse.jpt.common.utility.internal.stack.LinkedStack; import org.eclipse.jpt.common.utility.internal.stack.StackTools; import org.eclipse.jpt.common.utility.internal.stack.SynchronizedStack; @@ -406,6 +408,86 @@ public class StackToolsTests assertEquals("one", stack.pop()); } + // ********** concurrent stack ********** + + public void testConcurrentStack() { + ConcurrentStack d = StackTools.concurrentStack(); + assertTrue(d.isEmpty()); + } + + public void testConcurrentStackIterable() { + ArrayList iterable = new ArrayList<>(); + iterable.add("one"); + iterable.add("two"); + iterable.add("three"); + Stack stack = StackTools.concurrentStack(iterable); + assertEquals("three", stack.pop()); + assertEquals("two", stack.pop()); + assertEquals("one", stack.pop()); + } + + public void testConcurrentStackIterator() { + ArrayList iterable = new ArrayList<>(); + iterable.add("one"); + iterable.add("two"); + iterable.add("three"); + Stack stack = StackTools.concurrentStack(iterable.iterator()); + assertEquals("three", stack.pop()); + assertEquals("two", stack.pop()); + assertEquals("one", stack.pop()); + } + + public void testConcurrentStackArray() { + ArrayList iterable = new ArrayList<>(); + iterable.add("one"); + iterable.add("two"); + iterable.add("three"); + Stack stack = StackTools.concurrentStack(iterable.toArray(StringTools.EMPTY_STRING_ARRAY)); + assertEquals("three", stack.pop()); + assertEquals("two", stack.pop()); + assertEquals("one", stack.pop()); + } + + // ********** caching concurrent stack ********** + + public void testCachingConcurrentStack() { + CachingConcurrentStack d = StackTools.cachingConcurrentStack(); + assertTrue(d.isEmpty()); + } + + public void testCachingConcurrentStackIterable() { + ArrayList iterable = new ArrayList<>(); + iterable.add("one"); + iterable.add("two"); + iterable.add("three"); + Stack stack = StackTools.cachingConcurrentStack(iterable); + assertEquals("three", stack.pop()); + assertEquals("two", stack.pop()); + assertEquals("one", stack.pop()); + } + + public void testCachingConcurrentStackIterator() { + ArrayList iterable = new ArrayList<>(); + iterable.add("one"); + iterable.add("two"); + iterable.add("three"); + Stack stack = StackTools.cachingConcurrentStack(iterable.iterator()); + assertEquals("three", stack.pop()); + assertEquals("two", stack.pop()); + assertEquals("one", stack.pop()); + } + + public void testCachingConcurrentStackArray() { + ArrayList iterable = new ArrayList<>(); + iterable.add("one"); + iterable.add("two"); + iterable.add("three"); + Stack stack = StackTools.cachingConcurrentStack(iterable.toArray(StringTools.EMPTY_STRING_ARRAY)); + assertEquals("three", stack.pop()); + assertEquals("two", stack.pop()); + assertEquals("one", stack.pop()); + } + // ********** fixed-capacity array stack ********** public void testFixedCapacityArrayStackCollection() { diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/stack/SynchronizedStackTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/stack/SynchronizedStackTests.java index 2c51d3f4bc..deabb00f16 100644 --- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/stack/SynchronizedStackTests.java +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/stack/SynchronizedStackTests.java @@ -45,6 +45,11 @@ public class SynchronizedStackTests return StackTools.synchronizedStack(); } + @Override + Stack buildConcurrentStack() { + return StackTools.synchronizedStack(); + } + @Override public void testClone() { // synchronized stack is not cloneable -- cgit v1.2.3