Skip to content

Commit 9bc5b95

Browse files
Karthikeyan SubbarajCalamarBicefalo
authored andcommitted
Unit tests and DequeBasedSynchronizedStack added up
1 parent 7dbca34 commit 9bc5b95

3 files changed

Lines changed: 200 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.thread_safe_lifo;
2+
3+
import java.util.ArrayDeque;
4+
5+
/**
6+
* Deque Based Stack implementation.
7+
*/
8+
public class DequeBasedSynchronizedStack<T> {
9+
10+
// Internal Deque which gets decorated for synchronization.
11+
private ArrayDeque<T> dequeStore = new ArrayDeque<>();
12+
13+
public DequeBasedSynchronizedStack(int initialCapacity) {
14+
this.dequeStore = new ArrayDeque<>(initialCapacity);
15+
}
16+
17+
public DequeBasedSynchronizedStack() {
18+
19+
}
20+
21+
public synchronized T pop() {
22+
return this.dequeStore.pop();
23+
}
24+
25+
public synchronized void push(T element) {
26+
this.dequeStore.push(element);
27+
}
28+
29+
public synchronized T peek() {
30+
return this.dequeStore.peek();
31+
}
32+
33+
public synchronized int size() {
34+
return this.dequeStore.size();
35+
}
36+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.baeldung.stack_tests;
2+
3+
import com.baeldung.thread_safe_lifo.DequeBasedSynchronizedStack;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
7+
import java.util.ArrayDeque;
8+
import java.util.concurrent.ConcurrentLinkedDeque;
9+
10+
import static java.util.stream.IntStream.range;
11+
12+
/**
13+
* Correctness tests for Stack in multi threaded environment.
14+
*/
15+
public class MultithreadingCorrectnessStackTests {
16+
17+
@Test
18+
public void test_multithreading_correctness_with_synchronized_deque() {
19+
20+
DequeBasedSynchronizedStack<Integer> deque = new DequeBasedSynchronizedStack<>();
21+
22+
// Serial execution of push on ConcurrentLinkedQueue will always result in correct execution.
23+
range(1, 10000).forEach(value -> deque.push(value));
24+
25+
int sum = 0;
26+
while(deque.peek() != null) {
27+
sum += deque.pop();
28+
}
29+
30+
Assert.assertEquals(49995000, sum);
31+
32+
// Parallel execution of push on ConcurrentLinkedQueue will always result in correct execution.
33+
range(1, 10000).parallel().forEach(value -> deque.push(value));
34+
35+
sum = 0;
36+
while(deque.peek() != null) {
37+
sum += deque.pop();
38+
}
39+
40+
Assert.assertEquals(49995000, sum);
41+
}
42+
43+
@Test
44+
public void test_multithreading_correctness_with_concurrent_linked_queue() {
45+
46+
ConcurrentLinkedDeque<Integer> deque = new ConcurrentLinkedDeque<>();
47+
48+
// Serial execution of push on ConcurrentLinkedQueue will always result in correct execution.
49+
range(1, 10000).forEach(value -> deque.push(value));
50+
51+
int sum = 0;
52+
while(deque.peek() != null) {
53+
sum += deque.pop();
54+
}
55+
56+
Assert.assertEquals(49995000, sum);
57+
58+
// Parallel execution of push on ConcurrentLinkedQueue will always result in correct execution.
59+
range(1, 10000).parallel().forEach(value -> deque.push(value));
60+
61+
sum = 0;
62+
while(deque.peek() != null) {
63+
sum += deque.pop();
64+
}
65+
66+
Assert.assertEquals(49995000, sum);
67+
}
68+
69+
@Test
70+
public void test_multithreading_correctness_with_array_deque() {
71+
72+
ArrayDeque<Integer> deque = new ArrayDeque<>();
73+
74+
// Serial execution of push on ArrayDeque will always result in correct execution.
75+
range(1, 10000).forEach(value -> deque.push(value));
76+
77+
int sum = 0;
78+
while(deque.peek() != null) {
79+
sum += deque.pop();
80+
}
81+
82+
Assert.assertEquals(49995000, sum);
83+
84+
// Parallel execution of push on ArrayDeque will not result in correct execution.
85+
range(1, 10000).parallel().forEach(value -> deque.push(value));
86+
87+
sum = 0;
88+
while(deque.peek() != null) {
89+
sum += deque.pop();
90+
}
91+
92+
// This shouldn't happen.
93+
if(sum == 49995000) {
94+
System.out.println("Something wrong in the environment, Please try some big value and check");
95+
// To safe-guard build without test failures.
96+
return;
97+
}
98+
99+
Assert.assertNotEquals(49995000, sum);
100+
}
101+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.baeldung.stack_tests;
2+
3+
import com.baeldung.thread_safe_lifo.DequeBasedSynchronizedStack;
4+
import com.google.common.collect.Streams;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
import java.util.ArrayDeque;
9+
import java.util.Collection;
10+
import java.util.Collections;
11+
import java.util.Stack;
12+
import java.util.concurrent.ConcurrentLinkedDeque;
13+
import java.util.stream.Stream;
14+
15+
import static java.util.stream.IntStream.range;
16+
17+
/**
18+
* These tests are to understand the Stack implementation in Java Collections.
19+
*/
20+
public class StackTests {
21+
22+
@Test
23+
public void test_basic_with_stack() {
24+
Stack<String> namesStack = new Stack<>();
25+
26+
namesStack.push("Bill Gates");
27+
namesStack.push("Elon Musk");
28+
29+
Assert.assertEquals("Elon Musk", namesStack.peek());
30+
Assert.assertEquals("Elon Musk", namesStack.pop());
31+
Assert.assertEquals("Bill Gates", namesStack.pop());
32+
33+
Assert.assertEquals(0, namesStack.size());
34+
}
35+
36+
@Test
37+
public void test_basic_with_synchronized_deque() {
38+
DequeBasedSynchronizedStack<String> namesStack = new DequeBasedSynchronizedStack<>();
39+
40+
namesStack.push("Bill Gates");
41+
namesStack.push("Elon Musk");
42+
43+
Assert.assertEquals("Elon Musk", namesStack.peek());
44+
Assert.assertEquals("Elon Musk", namesStack.pop());
45+
Assert.assertEquals("Bill Gates", namesStack.pop());
46+
47+
Assert.assertEquals(0, namesStack.size());
48+
}
49+
50+
@Test
51+
public void test_basic_with_concurrent_linked_queue() {
52+
ConcurrentLinkedDeque<String> namesStack = new ConcurrentLinkedDeque<>();
53+
54+
namesStack.push("Bill Gates");
55+
namesStack.push("Elon Musk");
56+
57+
Assert.assertEquals("Elon Musk", namesStack.peek());
58+
Assert.assertEquals("Elon Musk", namesStack.pop());
59+
Assert.assertEquals("Bill Gates", namesStack.pop());
60+
61+
Assert.assertEquals(0, namesStack.size());
62+
}
63+
}

0 commit comments

Comments
 (0)