提交集合模块的测试类内容

This commit is contained in:
YuCheng Hu 2022-05-03 11:47:46 -04:00
parent 6a949efd05
commit f8b57e996a
35 changed files with 1880 additions and 0 deletions

View File

@ -0,0 +1,16 @@
## Core Java Collections
This module contains articles about Java collections
### Relevant Articles:
- [Introduction to the Java ArrayDeque](https://www.baeldung.com/java-array-deque)
- [An Introduction to Java.util.Hashtable Class](https://www.baeldung.com/java-hash-table)
- [Thread Safe LIFO Data Structure Implementations](https://www.baeldung.com/java-lifo-thread-safe)
- [Time Complexity of Java Collections](https://www.baeldung.com/java-collections-complexity)
- [A Guide to EnumMap](https://www.baeldung.com/java-enum-map)
- [A Guide to Iterator in Java](https://www.baeldung.com/java-iterator)
- [Defining a Char Stack in Java](https://www.baeldung.com/java-char-stack)
- [Guide to the Java Queue Interface](https://www.baeldung.com/java-queue)
- [An Introduction to Synchronized Java Collections](https://www.baeldung.com/java-synchronized-collections)
- [Convert an Array of Primitives to a List](https://www.baeldung.com/java-primitive-array-to-list)
- More articles: [[next -->]](/core-java-modules/core-java-collections-2)

View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-collections</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-collections</name>
<packaging>jar</packaging>
<parent>
<groupId>com.ossez.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.2-SNAPSHOT</version>
<relativePath></relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh-core.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh-generator.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,37 @@
package com.ossez.charstack;
import java.util.Iterator;
import java.util.LinkedList;
public class CharStack {
private LinkedList<Character> items;
public CharStack() {
this.items = new LinkedList<Character>();
}
public void push(Character item) {
items.push(item);
}
public Character peek() {
return items.getFirst();
}
public Character pop() {
Iterator<Character> iter = items.iterator();
Character item = iter.next();
if (item != null) {
iter.remove();
return item;
}
return null;
}
public int size() {
return items.size();
}
}

View File

@ -0,0 +1,48 @@
package com.ossez.charstack;
public class CharStackWithArray {
private char[] elements;
private int size;
public CharStackWithArray() {
size = 0;
elements = new char[4];
}
public int size() {
return size;
}
public char peek() {
if (size == 0) {
throw new EmptyStackException();
}
return elements[size - 1];
}
public char pop() {
if (size == 0) {
throw new EmptyStackException();
}
return elements[--size];
}
public void push(char item) {
ensureCapacity(size + 1);
elements[size] = item;
size++;
}
private void ensureCapacity(int newSize) {
char newBiggerArray[];
if (elements.length < newSize) {
newBiggerArray = new char[elements.length * 2];
System.arraycopy(elements, 0, newBiggerArray, 0, size);
elements = newBiggerArray;
}
}
}

View File

@ -0,0 +1,9 @@
package com.ossez.charstack;
public class EmptyStackException extends RuntimeException {
public EmptyStackException() {
super("Stack is empty");
}
}

View File

@ -0,0 +1,49 @@
package com.ossez.collections.convertarrayprimitives;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.apache.commons.lang3.ArrayUtils;
import com.google.common.primitives.Ints;
public class ConvertPrimitivesArrayToList {
public static void failConvert() {
int[] input = new int[]{1,2,3,4};
// List<Integer> inputAsList = Arrays.asList(input);
}
public static List<Integer> iterateConvert(int[] input) {
List<Integer> output = new ArrayList<Integer>();
for (int value : input) {
output.add(value);
}
return output;
}
public static List<Integer> streamConvert(int[] input) {
List<Integer> output = Arrays.stream(input).boxed().collect(Collectors.toList());
return output;
}
public static List<Integer> streamConvertIntStream(int[] input) {
List<Integer> output = IntStream.of(input).boxed().collect(Collectors.toList());
return output;
}
public static List<Integer> guavaConvert(int[] input) {
List<Integer> output = Ints.asList(input);
return output;
}
public static List<Integer> apacheCommonConvert(int[] input) {
Integer[] outputBoxed = ArrayUtils.toObject(input);
List<Integer> output = Arrays.asList(outputBoxed);
return output;
}
}

View File

@ -0,0 +1,28 @@
package com.ossez.hashtable;
public class Word {
private String name;
public Word(String name) {
this.name = name;
}
public String getName() {
return name;
}
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof Word))
return false;
Word word = (Word) o;
return word.getName().equals(this.name) ? true : false;
}
public int hashCode() {
return name.hashCode();
}
}

View File

@ -0,0 +1,39 @@
package com.ossez.iteratorguide;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class IteratorGuide {
public static void main(String[] args) {
List<String> items = new ArrayList<>();
items.add("ONE");
items.add("TWO");
items.add("THREE");
Iterator<String> iter = items.iterator();
while (iter.hasNext()) {
String next = iter.next();
System.out.println(next);
iter.remove();
}
ListIterator<String> listIterator = items.listIterator();
while(listIterator.hasNext()) {
String nextWithIndex = items.get(listIterator.nextIndex());
String next = listIterator.next();
if( "ONE".equals(next)) {
listIterator.set("SWAPPED");
}
}
listIterator.add("FOUR");
while(listIterator.hasPrevious()) {
String previousWithIndex = items.get(listIterator.previousIndex());
String previous = listIterator.previous();
System.out.println(previous);
}
listIterator.forEachRemaining(e -> {
System.out.println(e);
});
}
}

View File

@ -0,0 +1,78 @@
package com.ossez.performance;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Warmup(iterations = 10)
public class CopyOnWriteBenchmark {
@State(Scope.Thread)
public static class MyState {
CopyOnWriteArrayList<Employee> employeeList = new CopyOnWriteArrayList<>();
long iterations = 100000;
Employee employee = new Employee(100L, "Harry");
int employeeIndex = -1;
@Setup(Level.Trial)
public void setUp() {
for (long i = 0; i < iterations; i++) {
employeeList.add(new Employee(i, "John"));
}
employeeList.add(employee);
employeeIndex = employeeList.indexOf(employee);
}
}
@Benchmark
public void testAdd(CopyOnWriteBenchmark.MyState state) {
state.employeeList.add(new Employee(state.iterations + 1, "John"));
}
@Benchmark
public void testAddAt(CopyOnWriteBenchmark.MyState state) {
state.employeeList.add((int) (state.iterations), new Employee(state.iterations, "John"));
}
@Benchmark
public boolean testContains(CopyOnWriteBenchmark.MyState state) {
return state.employeeList.contains(state.employee);
}
@Benchmark
public int testIndexOf(CopyOnWriteBenchmark.MyState state) {
return state.employeeList.indexOf(state.employee);
}
@Benchmark
public Employee testGet(CopyOnWriteBenchmark.MyState state) {
return state.employeeList.get(state.employeeIndex);
}
@Benchmark
public boolean testRemove(CopyOnWriteBenchmark.MyState state) {
return state.employeeList.remove(state.employee);
}
public static void main(String[] args) throws Exception {
Options options = new OptionsBuilder()
.include(CopyOnWriteBenchmark.class.getSimpleName()).threads(1)
.forks(1).shouldFailOnError(true)
.shouldDoGC(true)
.jvmArgs("-server").build();
new Runner(options).run();
}
}

View File

@ -0,0 +1,55 @@
package com.ossez.performance;
public class Employee {
private Long id;
private String name;
public Employee(Long id, String name) {
this.name = name;
this.id = id;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
if (!id.equals(employee.id)) return false;
return name.equals(employee.name);
}
@Override
public int hashCode() {
int result = id.hashCode();
result = 31 * result + name.hashCode();
return result;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}

View File

@ -0,0 +1,73 @@
package com.ossez.performance;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.*;
import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Warmup(iterations = 10)
public class HashMapBenchmark {
@State(Scope.Thread)
public static class MyState {
Map<Long, Employee> employeeMap = new HashMap<>();
//LinkedHashMap<Long, Employee> employeeMap = new LinkedHashMap<>();
//IdentityHashMap<Long, Employee> employeeMap = new IdentityHashMap<>();
//WeakHashMap<Long, Employee> employeeMap = new WeakHashMap<>();
//ConcurrentHashMap<Long, Employee> employeeMap = new ConcurrentHashMap<>();
//ConcurrentSkipListMap<Long, Employee> employeeMap = new ConcurrentSkipListMap <>();
// TreeMap
long iterations = 100000;
Employee employee = new Employee(100L, "Harry");
int employeeIndex = -1;
@Setup(Level.Trial)
public void setUp() {
for (long i = 0; i < iterations; i++) {
employeeMap.put(i, new Employee(i, "John"));
}
//employeeMap.put(iterations, employee);
}
}
@Benchmark
public Employee testGet(HashMapBenchmark.MyState state) {
return state.employeeMap.get(state.iterations);
}
@Benchmark
public Employee testRemove(HashMapBenchmark.MyState state) {
return state.employeeMap.remove(state.iterations);
}
@Benchmark
public Employee testPut(HashMapBenchmark.MyState state) {
return state.employeeMap.put(state.employee.getId(), state.employee);
}
@Benchmark
public Boolean testContainsKey(HashMapBenchmark.MyState state) {
return state.employeeMap.containsKey(state.employee.getId());
}
public static void main(String[] args) throws Exception {
Options options = new OptionsBuilder()
.include(HashMapBenchmark.class.getSimpleName()).threads(1)
.forks(1).shouldFailOnError(true)
.shouldDoGC(true)
.jvmArgs("-server").build();
new Runner(options).run();
}
}

View File

@ -0,0 +1,62 @@
package com.ossez.performance;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.LinkedHashSet;
import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Warmup(iterations = 10)
public class SetBenchMark {
@State(Scope.Thread)
public static class MyState {
//Set<Employee> employeeSet = new HashSet<>();
LinkedHashSet<Employee> employeeSet = new LinkedHashSet<>();
//ConcurrentSkipListSet<Employee> employeeSet = new ConcurrentSkipListSet <>();
// TreeSet 
long iterations = 1000;
Employee employee = new Employee(100L, "Harry");
@Setup(Level.Trial)
public void setUp() {
for (long i = 0; i < iterations; i++) {
employeeSet.add(new Employee(i, "John"));
}
//employeeSet.add(employee);
}
}
@Benchmark
public boolean testAdd(SetBenchMark.MyState state) {
return state.employeeSet.add(state.employee);
}
@Benchmark
public Boolean testContains(SetBenchMark.MyState state) {
return state.employeeSet.contains(state.employee);
}
@Benchmark
public boolean testRemove(SetBenchMark.MyState state) {
return state.employeeSet.remove(state.employee);
}
public static void main(String[] args) throws Exception {
Options options = new OptionsBuilder()
.include(SetBenchMark.class.getSimpleName()).threads(1)
.forks(1).shouldFailOnError(true)
.shouldDoGC(true)
.jvmArgs("-server").build();
new Runner(options).run();
}
}

View File

@ -0,0 +1,48 @@
package com.ossez.queueInterface;
import java.util.AbstractQueue;
import java.util.Iterator;
import java.util.LinkedList;
public class CustomBaeldungQueue<T> extends AbstractQueue<T> {
private LinkedList<T> elements;
public CustomBaeldungQueue() {
this.elements = new LinkedList<T>();
}
@Override
public Iterator<T> iterator() {
return elements.iterator();
}
@Override
public int size() {
return elements.size();
}
@Override
public boolean offer(T t) {
if(t == null) return false;
elements.add(t);
return true;
}
@Override
public T poll() {
Iterator<T> iter = elements.iterator();
T t = iter.next();
if(t != null){
iter.remove();
return t;
}
return null;
}
@Override
public T peek() {
return elements.getFirst();
}
}

View File

@ -0,0 +1,18 @@
package com.ossez.synchronizedcollections;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;
public class Application {
private static final Logger LOGGER = Logger.getLogger(Application.class.getName());
public static void main(String[] args) throws InterruptedException {
List<Integer> syncCollection = Collections.synchronizedList(Arrays.asList(1, 2, 3, 4, 5, 6));
synchronized (syncCollection) {
syncCollection.forEach((e) -> {LOGGER.info(e.toString());});
}
}
}

View File

@ -0,0 +1,36 @@
package com.ossez.thread_safe_lifo;
import java.util.ArrayDeque;
/**
* Deque Based Stack implementation.
*/
public class DequeBasedSynchronizedStack<T> {
// Internal Deque which gets decorated for synchronization.
private ArrayDeque<T> dequeStore = new ArrayDeque<>();
public DequeBasedSynchronizedStack(int initialCapacity) {
this.dequeStore = new ArrayDeque<>(initialCapacity);
}
public DequeBasedSynchronizedStack() {
}
public synchronized T pop() {
return this.dequeStore.pop();
}
public synchronized void push(T element) {
this.dequeStore.push(element);
}
public synchronized T peek() {
return this.dequeStore.peek();
}
public synchronized int size() {
return this.dequeStore.size();
}
}

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -0,0 +1,50 @@
package com.ossez.arraydeque;
import java.util.ArrayDeque;
import java.util.Deque;
import static org.junit.Assert.*;
import org.junit.Test;
public class ArrayDequeUnitTest {
@Test
public void whenOffer_addsAtLast() {
final Deque<String> deque = new ArrayDeque<>();
deque.offer("first");
deque.offer("second");
assertEquals("second", deque.getLast());
}
@Test
public void whenPoll_removesFirst() {
final Deque<String> deque = new ArrayDeque<>();
deque.offer("first");
deque.offer("second");
assertEquals("first", deque.poll());
}
@Test
public void whenPush_addsAtFirst() {
final Deque<String> deque = new ArrayDeque<>();
deque.push("first");
deque.push("second");
assertEquals("second", deque.getFirst());
}
@Test
public void whenPop_removesLast() {
final Deque<String> deque = new ArrayDeque<>();
deque.push("first");
deque.push("second");
assertEquals("second", deque.pop());
}
}

View File

@ -0,0 +1,50 @@
package com.ossez.charstack;
import static org.junit.Assert.assertEquals;
import org.junit.jupiter.api.Test;
public class CharStackUnitTest {
@Test
public void whenCharStackIsCreated_thenItHasSize0() {
CharStack charStack = new CharStack();
assertEquals(0, charStack.size());
}
@Test
public void givenEmptyCharStack_whenElementIsPushed_thenStackSizeisIncreased() {
CharStack charStack = new CharStack();
charStack.push('A');
assertEquals(1, charStack.size());
}
@Test
public void givenCharStack_whenElementIsPoppedFromStack_thenElementIsRemovedAndSizeChanges() {
CharStack charStack = new CharStack();
charStack.push('A');
char element = charStack.pop();
assertEquals('A', element);
assertEquals(0, charStack.size());
}
@Test
public void givenCharStack_whenElementIsPeeked_thenElementIsNotRemovedAndSizeDoesNotChange() {
CharStack charStack = new CharStack();
charStack.push('A');
char element = charStack.peek();
assertEquals('A', element);
assertEquals(1, charStack.size());
}
}

View File

@ -0,0 +1,53 @@
package com.ossez.charstack;
import static org.junit.Assert.assertEquals;
import java.util.Stack;
import org.junit.jupiter.api.Test;
public class CharStackUsingJavaUnitTest {
@Test
public void whenCharStackIsCreated_thenItHasSize0() {
Stack<Character> charStack = new Stack<>();
assertEquals(0, charStack.size());
}
@Test
public void givenEmptyCharStack_whenElementIsPushed_thenStackSizeisIncreased() {
Stack<Character> charStack = new Stack<>();
charStack.push('A');
assertEquals(1, charStack.size());
}
@Test
public void givenCharStack_whenElementIsPoppedFromStack_thenElementIsRemovedAndSizeChanges() {
Stack<Character> charStack = new Stack<>();
charStack.push('A');
char element = charStack.pop();
assertEquals('A', element);
assertEquals(0, charStack.size());
}
@Test
public void givenCharStack_whenElementIsPeeked_thenElementIsNotRemovedAndSizeDoesNotChange() {
Stack<Character> charStack = new Stack<>();
charStack.push('A');
char element = charStack.peek();
assertEquals('A', element);
assertEquals(1, charStack.size());
}
}

View File

@ -0,0 +1,65 @@
package com.ossez.charstack;
import static org.junit.Assert.assertEquals;
import org.junit.jupiter.api.Test;
public class CharStackWithArrayUnitTest {
@Test
public void whenCharStackIsCreated_thenItHasSize0() {
CharStackWithArray charStack = new CharStackWithArray();
assertEquals(0, charStack.size());
}
@Test
public void givenEmptyCharStack_whenElementIsPushed_thenStackSizeisIncreased() {
CharStackWithArray charStack = new CharStackWithArray();
charStack.push('A');
assertEquals(1, charStack.size());
}
@Test
public void givenEmptyCharStack_when5ElementIsPushed_thenStackSizeis() {
CharStackWithArray charStack = new CharStackWithArray();
charStack.push('A');
charStack.push('B');
charStack.push('C');
charStack.push('D');
charStack.push('E');
assertEquals(5, charStack.size());
}
@Test
public void givenCharStack_whenElementIsPoppedFromStack_thenElementIsRemovedAndSizeChanges() {
CharStackWithArray charStack = new CharStackWithArray();
charStack.push('A');
char element = charStack.pop();
assertEquals('A', element);
assertEquals(0, charStack.size());
}
@Test
public void givenCharStack_whenElementIsPeeked_thenElementIsNotRemovedAndSizeDoesNotChange() {
CharStackWithArray charStack = new CharStackWithArray();
charStack.push('A');
char element = charStack.peek();
assertEquals('A', element);
assertEquals(1, charStack.size());
}
}

View File

@ -0,0 +1,35 @@
package com.ossez.collections.convertarrayprimitives;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import org.junit.Test;
public class ConvertPrimitivesArrayToListUnitTest {
@Test
public void givenArrayWithPrimitives_whenIterativeConvert_thenArrayGetsConverted() {
assertEquals(Arrays.asList(1,2,3,4), ConvertPrimitivesArrayToList.iterateConvert(new int[]{1,2,3,4}));
}
@Test
public void givenArrayWithPrimitives_whenStreamConvert_thenArrayGetsConverted() {
assertEquals(Arrays.asList(1,2,3,4), ConvertPrimitivesArrayToList.streamConvert(new int[]{1,2,3,4}));
}
@Test
public void givenArrayWithPrimitives_whenIntStreamConvert_thenArrayGetsConverted() {
assertEquals(Arrays.asList(1,2,3,4), ConvertPrimitivesArrayToList.streamConvertIntStream(new int[]{1,2,3,4}));
}
@Test
public void givenArrayWithPrimitives_whenGuavaConvert_thenArrayGetsConverted() {
assertEquals(Arrays.asList(1,2,3,4), ConvertPrimitivesArrayToList.guavaConvert(new int[]{1,2,3,4}));
}
@Test
public void givenArrayWithPrimitives_whenApacheCommonConvert_thenArrayGetsConverted() {
assertEquals(Arrays.asList(1,2,3,4), ConvertPrimitivesArrayToList.apacheCommonConvert(new int[]{1,2,3,4}));
}
}

View File

@ -0,0 +1,13 @@
package com.ossez.enummap;
/**
* This enum is used for benchmarking, therefore has many values.
*/
public enum DummyEnum {
CCC_000,
CCC_001,CCC_002,CCC_003,CCC_004,CCC_005,CCC_006,CCC_007,CCC_008,CCC_009,CCC_010,
CCC_011,CCC_012,CCC_013,CCC_014,CCC_015,CCC_016,CCC_017,CCC_018,CCC_019,CCC_020,
CCC_021,CCC_022,CCC_023,CCC_024,CCC_025,CCC_026,CCC_027,CCC_028,CCC_029,CCC_030,
CCC_031,CCC_032,CCC_033,CCC_034,CCC_035,CCC_036,CCC_037,CCC_038,CCC_039,CCC_040,
CCC_041,CCC_042,CCC_043,CCC_044,CCC_045,CCC_046,CCC_047,CCC_048,CCC_049,
}

View File

@ -0,0 +1,119 @@
package com.ossez.enummap;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.*;
import java.util.concurrent.TimeUnit;
@BenchmarkMode({ Mode.AverageTime })
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 5)
@Measurement(iterations = 5)
public class EnumMapBenchmarkLiveTest {
@State(Scope.Thread)
public static class BenchmarkState {
EnumMap<DummyEnum, String> enumMap = new EnumMap<>(DummyEnum.class);
HashMap<DummyEnum, String> hashMap = new HashMap<>();
TreeMap<DummyEnum, String> treeMap = new TreeMap<>();
int len = DummyEnum.values().length;
Random random = new Random();
int randomIndex;
@Setup(Level.Trial)
public void setUp() {
DummyEnum[] values = DummyEnum.values();
for (int i = 0; i < len; i++) {
enumMap.put(values[i], values[i].toString());
hashMap.put(values[i], values[i].toString());
treeMap.put(values[i], values[i].toString());
}
}
@Setup(Level.Invocation)
public void additionalSetup() {
randomIndex = random.nextInt(len);
}
}
@Benchmark
public int benchmark01_EnumMapPut(BenchmarkState s) {
s.enumMap.put(DummyEnum.values()[s.randomIndex], DummyEnum.values()[s.randomIndex].toString());
return ++s.randomIndex;
}
@Benchmark
public int benchmark01_HashMapPut(BenchmarkState s) {
s.hashMap.put(DummyEnum.values()[s.randomIndex], DummyEnum.values()[s.randomIndex].toString());
return ++s.randomIndex;
}
@Benchmark
public int benchmark01_TreeMapPut(BenchmarkState s) {
s.treeMap.put(DummyEnum.values()[s.randomIndex], DummyEnum.values()[s.randomIndex].toString());
return ++s.randomIndex;
}
@Benchmark
public int benchmark02_EnumMapGet(BenchmarkState s) {
s.enumMap.get(DummyEnum.values()[s.randomIndex]);
return ++s.randomIndex;
}
@Benchmark
public int benchmark02_HashMapGet(BenchmarkState s) {
s.hashMap.get(DummyEnum.values()[s.randomIndex]);
return ++s.randomIndex;
}
@Benchmark
public int benchmark02_TreeMapGet(BenchmarkState s) {
s.treeMap.get(DummyEnum.values()[s.randomIndex]);
return ++s.randomIndex;
}
@Benchmark
public int benchmark03_EnumMapContainsKey(BenchmarkState s) {
s.enumMap.containsKey(DummyEnum.values()[s.randomIndex]);
return ++s.randomIndex;
}
@Benchmark
public int benchmark03_HashMapContainsKey(BenchmarkState s) {
s.hashMap.containsKey(DummyEnum.values()[s.randomIndex]);
return ++s.randomIndex;
}
@Benchmark
public int benchmark03_TreeMapContainsKey(BenchmarkState s) {
s.treeMap.containsKey(DummyEnum.values()[s.randomIndex]);
return ++s.randomIndex;
}
@Benchmark
public int benchmark04_EnumMapContainsValue(BenchmarkState s) {
s.enumMap.containsValue(DummyEnum.values()[s.randomIndex].toString());
return ++s.randomIndex;
}
@Benchmark
public int benchmark04_HashMapContainsValue(BenchmarkState s) {
s.hashMap.containsValue(DummyEnum.values()[s.randomIndex].toString());
return ++s.randomIndex;
}
@Benchmark
public int benchmark04_TreeMapContainsValue(BenchmarkState s) {
s.treeMap.containsValue(DummyEnum.values()[s.randomIndex].toString());
return ++s.randomIndex;
}
public static void main(String[] args) throws Exception {
Options options = new OptionsBuilder().include(EnumMapBenchmarkLiveTest.class.getSimpleName()).threads(1).forks(0).shouldFailOnError(true).shouldDoGC(false).jvmArgs("-server").build();
new Runner(options).run();
}
}

View File

@ -0,0 +1,144 @@
package com.ossez.enummap;
import org.junit.Test;
import java.util.*;
import java.util.concurrent.TimeUnit;
import static java.util.AbstractMap.SimpleEntry;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
public class EnumMapUnitTest {
public enum DayOfWeek {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
@Test
public void whenContructedWithEnumType_ThenOnlyAcceptThatAsKey() {
Map dayMap = new EnumMap<>(DayOfWeek.class);
assertThatCode(
() -> dayMap.put(TimeUnit.NANOSECONDS, "NANOSECONDS"))
.isInstanceOf(ClassCastException.class);
}
@Test
public void whenConstructedWithEnumMap_ThenSameKeyTypeAndInitialMappings() {
EnumMap<DayOfWeek, String> activityMap = new EnumMap<>(DayOfWeek.class);
activityMap.put(DayOfWeek.MONDAY, "Soccer");
activityMap.put(DayOfWeek.TUESDAY, "Basketball");
EnumMap<DayOfWeek, String> activityMapCopy = new EnumMap<>(activityMap);
assertThat(activityMapCopy.size()).isEqualTo(2);
assertThat(activityMapCopy.get(DayOfWeek.MONDAY))
.isEqualTo("Soccer");
assertThat(activityMapCopy.get(DayOfWeek.TUESDAY))
.isEqualTo("Basketball");
}
@Test
public void givenEmptyMap_whenConstructedWithMap_ThenException() {
HashMap ordinaryMap = new HashMap();
assertThatCode(() -> new EnumMap(ordinaryMap))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Specified map is empty");
}
@Test
public void givenMapWithEntries_whenConstructedWithMap_ThenSucceed() {
HashMap<DayOfWeek, String> ordinaryMap = new HashMap<>();
ordinaryMap.put(DayOfWeek.MONDAY, "Soccer");
ordinaryMap.put(DayOfWeek.TUESDAY, "Basketball");
EnumMap<DayOfWeek, String> enumMap = new EnumMap<>(ordinaryMap);
assertThat(enumMap.size()).isEqualTo(2);
assertThat(enumMap.get(DayOfWeek.MONDAY)).isEqualTo("Soccer");
assertThat(enumMap.get(DayOfWeek.TUESDAY)).isEqualTo("Basketball");
}
@Test
public void givenMapWithMultiTypeEntries_whenConstructedWithMap_ThenException() {
HashMap<Enum, String> ordinaryMap = new HashMap<>();
ordinaryMap.put(DayOfWeek.MONDAY, "Soccer");
ordinaryMap.put(TimeUnit.MILLISECONDS, "Other enum type");
assertThatCode(() -> new EnumMap(ordinaryMap))
.isInstanceOf(ClassCastException.class);
}
@Test
public void whenPut_thenGet() {
Map<DayOfWeek, String> activityMap = new EnumMap(DayOfWeek.class);
activityMap.put(DayOfWeek.WEDNESDAY, "Hiking");
activityMap.put(DayOfWeek.THURSDAY, null);
assertThat(activityMap.get(DayOfWeek.WEDNESDAY)).isEqualTo("Hiking");
assertThat(activityMap.get(DayOfWeek.THURSDAY)).isNull();
}
@Test
public void givenMapping_whenContains_thenTrue() {
EnumMap<DayOfWeek, String> activityMap = new EnumMap(DayOfWeek.class);
assertThat(activityMap.containsKey(DayOfWeek.WEDNESDAY)).isFalse();
assertThat(activityMap.containsValue("Hiking")).isFalse();
activityMap.put(DayOfWeek.WEDNESDAY, "Hiking");
assertThat(activityMap.containsKey(DayOfWeek.WEDNESDAY)).isTrue();
assertThat(activityMap.containsValue("Hiking")).isTrue();
assertThat(activityMap.containsKey(DayOfWeek.SATURDAY)).isFalse();
assertThat(activityMap.containsValue(null)).isFalse();
activityMap.put(DayOfWeek.SATURDAY, null);
assertThat(activityMap.containsKey(DayOfWeek.SATURDAY)).isTrue();
assertThat(activityMap.containsValue(null)).isTrue();
}
@Test
public void whenRemove_thenRemoved() {
EnumMap<DayOfWeek, String> activityMap = new EnumMap(DayOfWeek.class);
activityMap.put(DayOfWeek.MONDAY, "Soccer");
assertThat(activityMap.remove(DayOfWeek.MONDAY)).isEqualTo("Soccer");
assertThat(activityMap.containsKey(DayOfWeek.MONDAY)).isFalse();
activityMap.put(DayOfWeek.MONDAY, "Soccer");
assertThat(activityMap.remove(DayOfWeek.MONDAY, "Hiking")).isEqualTo(false);
assertThat(activityMap.remove(DayOfWeek.MONDAY, "Soccer")).isEqualTo(true);
}
@Test
public void whenSubView_thenSubViewOrdered() {
EnumMap<DayOfWeek, String> activityMap = new EnumMap(DayOfWeek.class);
activityMap.put(DayOfWeek.THURSDAY, "Karate");
activityMap.put(DayOfWeek.WEDNESDAY, "Hiking");
activityMap.put(DayOfWeek.MONDAY, "Soccer");
Collection<String> values = activityMap.values();
assertThat(values).containsExactly("Soccer", "Hiking", "Karate");
Set<DayOfWeek> keys = activityMap.keySet();
assertThat(keys)
.containsExactly(DayOfWeek.MONDAY, DayOfWeek.WEDNESDAY,DayOfWeek.THURSDAY);
assertThat(activityMap.entrySet())
.containsExactly(
new SimpleEntry(DayOfWeek.MONDAY, "Soccer"),
new SimpleEntry(DayOfWeek.WEDNESDAY, "Hiking"),
new SimpleEntry(DayOfWeek.THURSDAY, "Karate"));
}
@Test
public void givenSubView_whenChange_thenReflected() {
EnumMap<DayOfWeek, String> activityMap = new EnumMap(DayOfWeek.class);
activityMap.put(DayOfWeek.THURSDAY, "Karate");
activityMap.put(DayOfWeek.WEDNESDAY, "Hiking");
activityMap.put(DayOfWeek.MONDAY, "Soccer");
Collection<String> values = activityMap.values();
assertThat(values).containsExactly("Soccer", "Hiking", "Karate");
activityMap.put(DayOfWeek.TUESDAY, "Basketball");
assertThat(values)
.containsExactly("Soccer", "Basketball", "Hiking", "Karate");
values.remove("Hiking");
assertThat(activityMap.containsKey(DayOfWeek.WEDNESDAY)).isFalse();
assertThat(activityMap.size()).isEqualTo(3);
}
}

View File

@ -0,0 +1,274 @@
package com.ossez.hashtable;
import java.util.ConcurrentModificationException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;
import org.junit.Test;
public class HashtableUnitTest {
@Test
public void whenPutAndGet_thenReturnsValue() {
Hashtable<Word, String> table = new Hashtable<Word, String>();
Word word = new Word("cat");
table.put(word, "an animal");
String definition = table.get(word);
assertEquals("an animal", definition);
definition = table.remove(word);
assertEquals("an animal", definition);
}
@Test
public void whenThesameInstanceOfKey_thenReturnsValue() {
Hashtable<Word, String> table = new Hashtable<Word, String>();
Word word = new Word("cat");
table.put(word, "an animal");
String extracted = table.get(word);
assertEquals("an animal", extracted);
}
@Test
public void whenEqualsOverridden_thenReturnsValue() {
Hashtable<Word, String> table = new Hashtable<Word, String>();
Word word = new Word("cat");
table.put(word, "an animal");
String extracted = table.get(new Word("cat"));
assertEquals("an animal", extracted);
}
@Test(expected = NullPointerException.class)
public void whenNullKey_thenException() {
Hashtable<Word, String> table = new Hashtable<Word, String>();
table.put(null, "an animal");
}
@Test(expected = ConcurrentModificationException.class)
public void whenIterate_thenFailFast() {
Hashtable<Word, String> table = new Hashtable<Word, String>();
table.put(new Word("cat"), "an animal");
table.put(new Word("dog"), "another animal");
Iterator<Word> it = table.keySet().iterator();
System.out.println("iterator created");
table.remove(new Word("dog"));
System.out.println("element removed");
while (it.hasNext()) {
Word key = it.next();
System.out.println(table.get(key));
}
}
@Test
public void whenEnumerate_thenNotFailFast() {
Hashtable<Word, String> table = new Hashtable<Word, String>();
table.put(new Word("1"), "one");
table.put(new Word("2"), "two");
table.put(new Word("3"), "three");
table.put(new Word("4"), "four");
table.put(new Word("5"), "five");
table.put(new Word("6"), "six");
table.put(new Word("7"), "seven");
table.put(new Word("8"), "eight");
Enumeration<Word> enumKey = table.keys();
System.out.println("Enumeration created");
table.remove(new Word("1"));
System.out.println("element removed");
while (enumKey.hasMoreElements()) {
Word key = enumKey.nextElement();
System.out.println(table.get(key));
}
}
@Test
public void whenAddElements_thenIterationOrderUnpredicable() {
Hashtable<Word, String> table = new Hashtable<Word, String>();
table.put(new Word("1"), "one");
table.put(new Word("2"), "two");
table.put(new Word("3"), "three");
table.put(new Word("4"), "four");
table.put(new Word("5"), "five");
table.put(new Word("6"), "six");
table.put(new Word("7"), "seven");
table.put(new Word("8"), "eight");
Iterator<Map.Entry<Word, String>> it = table.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Word, String> entry = it.next();
System.out.println(entry.getValue());
}
}
@Test
public void whenGetOrDefault_thenDefaultGot() {
Hashtable<Word, String> table = new Hashtable<Word, String>();
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
Word key = new Word("dog");
String definition;
// old way
/* if (table.containsKey(key)) {
definition = table.get(key);
} else {
definition = "not found";
}*/
// new way
definition = table.getOrDefault(key, "not found");
assertThat(definition, is("not found"));
}
@Test
public void whenPutifAbsent_thenNotRewritten() {
Hashtable<Word, String> table = new Hashtable<Word, String>();
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
String definition = "an animal";
// old way
/* if (!table.containsKey(new Word("cat"))) {
table.put(new Word("cat"), definition);
}*/
// new way
table.putIfAbsent(new Word("cat"), definition);
assertThat(table.get(new Word("cat")), is("a small domesticated carnivorous mammal"));
}
@Test
public void whenRemovePair_thenCheckKeyAndValue() {
Hashtable<Word, String> table = new Hashtable<Word, String>();
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
// old way
/* if (table.get(new Word("cat")).equals("an animal")) {
table.remove(new Word("cat"));
}*/
// new way
boolean result = table.remove(new Word("cat"), "an animal");
assertThat(result, is(false));
}
@Test
public void whenReplacePair_thenValueChecked() {
Hashtable<Word, String> table = new Hashtable<Word, String>();
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
String definition = "an animal";
// old way
/* if (table.containsKey(new Word("cat")) && table.get(new Word("cat")).equals("a small domesticated carnivorous mammal")) {
table.put(new Word("cat"), definition);
}*/
// new way
table.replace(new Word("cat"), "a small domesticated carnivorous mammal", definition);
assertThat(table.get(new Word("cat")), is("an animal"));
}
@Test
public void whenKeyIsAbsent_thenNotRewritten() {
Hashtable<Word, String> table = new Hashtable<Word, String>();
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
// old way
/* if (!table.containsKey(cat)) {
String definition = "an animal";// calculate
table.put(new Word("cat"), definition);
}
*/
// new way
table.computeIfAbsent(new Word("cat"), key -> "an animal");
assertThat(table.get(new Word("cat")), is("a small domesticated carnivorous mammal"));
}
@Test
public void whenKeyIsPresent_thenComputeIfPresent() {
Hashtable<Word, String> table = new Hashtable<Word, String>();
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
Word cat = new Word("cat");
// old way
/* if (table.containsKey(cat)) {
String concatination = cat.getName() + " - " + table.get(cat);
table.put(cat, concatination);
}*/
// new way
table.computeIfPresent(cat, (key, value) -> key.getName() + " - " + value);
assertThat(table.get(cat), is("cat - a small domesticated carnivorous mammal"));
}
@Test
public void whenCompute_thenForAllKeys() {
Hashtable<String, Integer> table = new Hashtable<String, Integer>();
String[] animals = { "cat", "dog", "dog", "cat", "bird", "mouse", "mouse" };
for (String animal : animals) {
table.compute(animal, (key, value) -> (value == null ? 1 : value + 1));
}
assertThat(table.values(), hasItems(2, 2, 2, 1));
}
@Test
public void whenInsteadOfCompute_thenMerge() {
Hashtable<String, Integer> table = new Hashtable<String, Integer>();
String[] animals = { "cat", "dog", "dog", "cat", "bird", "mouse", "mouse" };
for (String animal : animals) {
table.merge(animal, 1, (oldValue, value) -> (oldValue + value));
}
assertThat(table.values(), hasItems(2, 2, 2, 1));
}
@Test
public void whenForeach_thenIterate() {
Hashtable<Word, String> table = new Hashtable<Word, String>();
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
table.put(new Word("dog"), "another animal");
table.forEach((k, v) -> System.out.println(k.getName() + " - " + v)
);
}
@Test
public void whenReplaceall_thenNoIterationNeeded() {
Hashtable<Word, String> table = new Hashtable<Word, String>();
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
table.put(new Word("dog"), "another animal");
table.replaceAll((k, v) -> k.getName() + " - " + v);
assertThat(table.values(), hasItems("cat - a small domesticated carnivorous mammal", "dog - another animal"));
}
}

View File

@ -0,0 +1,30 @@
package com.ossez.queueInterface;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
public class CustomBaeldungQueueUnitTest {
private CustomBaeldungQueue<Integer> customQueue;
@Before
public void setUp() throws Exception {
customQueue = new CustomBaeldungQueue<>();
}
@Test
public void givenQueueWithTwoElements_whenElementsRetrieved_checkRetrievalCorrect() {
customQueue.add(7);
customQueue.add(5);
int first = customQueue.poll();
int second = customQueue.poll();
assertEquals(7, first);
assertEquals(5, second);
}
}

View File

@ -0,0 +1,53 @@
package com.ossez.queueInterface;
import org.junit.Before;
import org.junit.Test;
import java.util.PriorityQueue;
import static org.junit.Assert.assertEquals;
public class PriorityQueueUnitTest {
@Test
public void givenIntegerQueue_whenIntegersOutOfOrder_checkRetrievalOrderIsNatural() {
PriorityQueue<Integer> integerQueue = new PriorityQueue<>();
integerQueue.add(9);
integerQueue.add(2);
integerQueue.add(4);
int first = integerQueue.poll();
int second = integerQueue.poll();
int third = integerQueue.poll();
assertEquals(2, first);
assertEquals(4, second);
assertEquals(9, third);
}
@Test
public void givenStringQueue_whenStringsAddedOutOfNaturalOrder_checkRetrievalOrderNatural() {
PriorityQueue<String> stringQueue = new PriorityQueue<>();
stringQueue.add("banana");
stringQueue.add("apple");
stringQueue.add("cherry");
String first = stringQueue.poll();
String second = stringQueue.poll();
String third = stringQueue.poll();
assertEquals("apple", first);
assertEquals("banana", second);
assertEquals("cherry", third);
}
}

View File

@ -0,0 +1,101 @@
package com.ossez.stack_tests;
import com.ossez.thread_safe_lifo.DequeBasedSynchronizedStack;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayDeque;
import java.util.concurrent.ConcurrentLinkedDeque;
import static java.util.stream.IntStream.range;
/**
* Correctness tests for Stack in multi threaded environment.
*/
public class MultithreadingCorrectnessStackUnitTest {
@Test
public void givenSynchronizedDeque_whenExecutedParallel_thenWorkRight() {
DequeBasedSynchronizedStack<Integer> deque = new DequeBasedSynchronizedStack<>();
// Serial execution of push on ConcurrentLinkedQueue will always result in correct execution.
range(1, 10000).forEach(value -> deque.push(value));
int sum = 0;
while(deque.peek() != null) {
sum += deque.pop();
}
Assert.assertEquals(49995000, sum);
// Parallel execution of push on ConcurrentLinkedQueue will always result in correct execution.
range(1, 10000).parallel().forEach(value -> deque.push(value));
sum = 0;
while(deque.peek() != null) {
sum += deque.pop();
}
Assert.assertEquals(49995000, sum);
}
@Test
public void givenConcurrentLinkedQueue_whenExecutedParallel_thenWorkRight() {
ConcurrentLinkedDeque<Integer> deque = new ConcurrentLinkedDeque<>();
// Serial execution of push on ConcurrentLinkedQueue will always result in correct execution.
range(1, 10000).forEach(value -> deque.push(value));
int sum = 0;
while(deque.peek() != null) {
sum += deque.pop();
}
Assert.assertEquals(49995000, sum);
// Parallel execution of push on ConcurrentLinkedQueue will always result in correct execution.
range(1, 10000).parallel().forEach(value -> deque.push(value));
sum = 0;
while(deque.peek() != null) {
sum += deque.pop();
}
Assert.assertEquals(49995000, sum);
}
@Test
public void givenArrayDeque_whenExecutedParallel_thenShouldFail() {
ArrayDeque<Integer> deque = new ArrayDeque<>();
// Serial execution of push on ArrayDeque will always result in correct execution.
range(1, 10000).forEach(value -> deque.push(value));
int sum = 0;
while(deque.peek() != null) {
sum += deque.pop();
}
Assert.assertEquals(49995000, sum);
// Parallel execution of push on ArrayDeque will not result in correct execution.
range(1, 10000).parallel().forEach(value -> deque.push(value));
sum = 0;
while(deque.peek() != null) {
sum += deque.pop();
}
// This shouldn't happen.
if(sum == 49995000) {
System.out.println("Something wrong in the environment, Please try some big value and check");
// To safe-guard build without test failures.
return;
}
Assert.assertNotEquals(49995000, sum);
}
}

View File

@ -0,0 +1,56 @@
package com.ossez.stack_tests;
import com.ossez.thread_safe_lifo.DequeBasedSynchronizedStack;
import org.junit.Assert;
import org.junit.Test;
import java.util.Stack;
import java.util.concurrent.ConcurrentLinkedDeque;
/**
* These tests are to understand the Stack implementation in Java Collections.
*/
public class StackUnitTest {
@Test
public void givenStack_whenPushPopPeek_thenWorkRight() {
Stack<String> namesStack = new Stack<>();
namesStack.push("Bill Gates");
namesStack.push("Elon Musk");
Assert.assertEquals("Elon Musk", namesStack.peek());
Assert.assertEquals("Elon Musk", namesStack.pop());
Assert.assertEquals("Bill Gates", namesStack.pop());
Assert.assertEquals(0, namesStack.size());
}
@Test
public void givenSynchronizedDeque_whenPushPopPeek_thenWorkRight() {
DequeBasedSynchronizedStack<String> namesStack = new DequeBasedSynchronizedStack<>();
namesStack.push("Bill Gates");
namesStack.push("Elon Musk");
Assert.assertEquals("Elon Musk", namesStack.peek());
Assert.assertEquals("Elon Musk", namesStack.pop());
Assert.assertEquals("Bill Gates", namesStack.pop());
Assert.assertEquals(0, namesStack.size());
}
@Test
public void givenConcurrentLinkedDeque_whenPushPopPeek_thenWorkRight() {
ConcurrentLinkedDeque<String> namesStack = new ConcurrentLinkedDeque<>();
namesStack.push("Bill Gates");
namesStack.push("Elon Musk");
Assert.assertEquals("Elon Musk", namesStack.peek());
Assert.assertEquals("Elon Musk", namesStack.pop());
Assert.assertEquals("Bill Gates", namesStack.pop());
Assert.assertEquals(0, namesStack.size());
}
}

View File

@ -0,0 +1,28 @@
package com.ossez.synchronizedcollections;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class SynchronizedCollectionUnitTest {
@Test
public void givenSynchronizedCollection_whenTwoThreadsAddElements_thenCorrectCollectionSize() throws InterruptedException {
Collection<Integer> syncCollection = Collections.synchronizedCollection(new ArrayList<>());
Runnable listOperations = () -> {
syncCollection.addAll(Arrays.asList(1, 2, 3, 4, 5, 6));
};
Thread thread1 = new Thread(listOperations);
Thread thread2 = new Thread(listOperations);
thread1.start();
thread2.start();
thread1.join();
thread2.join();
assertThat(syncCollection.size()).isEqualTo(12);
}
}

View File

@ -0,0 +1,51 @@
package com.ossez.synchronizedcollections;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.Test;
import static org.assertj.core.api.Assertions.*;
public class SynchronizedListUnitTest {
@Test
public void givenSynchronizedList_whenTwoThreadsAddElements_thenCorrectListSize() throws InterruptedException {
List<Integer> syncList = Collections.synchronizedList(new ArrayList<>());
Runnable listOperations = () -> {
syncList.addAll(Arrays.asList(1, 2, 3, 4, 5, 6));
};
Thread thread1 = new Thread(listOperations);
Thread thread2 = new Thread(listOperations);
thread1.start();
thread2.start();
thread1.join();
thread2.join();
assertThat(syncList.size()).isEqualTo(12);
}
@Test
public void givenStringList_whenTwoThreadsIterateOnSynchronizedList_thenCorrectResult() throws InterruptedException {
List<String> syncCollection = Collections.synchronizedList(Arrays.asList("a", "b", "c"));
List<String> uppercasedCollection = new ArrayList<>();
Runnable listOperations = () -> {
synchronized (syncCollection) {
syncCollection.forEach((e) -> {
uppercasedCollection.add(e.toUpperCase());
});
}
};
Thread thread1 = new Thread(listOperations);
Thread thread2 = new Thread(listOperations);
thread1.start();
thread2.start();
thread1.join();
thread2.join();
assertThat(uppercasedCollection.get(0)).isEqualTo("A");
}
}

View File

@ -0,0 +1,30 @@
package com.ossez.synchronizedcollections;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import static org.assertj.core.api.Assertions.*;
public class SynchronizedMapUnitTest {
@Test
public void givenSynchronizedMap_whenTwoThreadsAddElements_thenCorrectMapSize() throws InterruptedException {
Map<Integer, String> syncMap = Collections.synchronizedMap(new HashMap<>());
Runnable mapOperations = () -> {
syncMap.put(1, "one");
syncMap.put(2, "two");
syncMap.put(3, "three");
};
Thread thread1 = new Thread(mapOperations);
Thread thread2 = new Thread(mapOperations);
thread1.start();
thread2.start();
thread1.join();
thread2.join();
assertThat(syncMap.size()).isEqualTo(3);
}
}

View File

@ -0,0 +1,26 @@
package com.ossez.synchronizedcollections;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.junit.Test;
import static org.assertj.core.api.Assertions.*;
public class SynchronizedSetUnitTest {
@Test
public void givenSynchronizedSet_whenTwoThreadsAddElements_thenCorrectSetSize() throws InterruptedException {
Set<Integer> syncSet = Collections.synchronizedSet(new HashSet<>());
Runnable setOperations = () -> {syncSet.addAll(Arrays.asList(1, 2, 3, 4, 5, 6));};
Thread thread1 = new Thread(setOperations);
Thread thread2 = new Thread(setOperations);
thread1.start();
thread2.start();
thread1.join();
thread2.join();
assertThat(syncSet.size()).isEqualTo(6);
}
}

View File

@ -0,0 +1,29 @@
package com.ossez.synchronizedcollections;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class SynchronizedSortedMapUnitTest {
@Test
public void givenSynchronizedSorteMap_whenTwoThreadsAddElements_thenCorrectSortedMapSize() throws InterruptedException {
Map<Integer, String> syncSortedMap = Collections.synchronizedSortedMap(new TreeMap<>());
Runnable sortedMapOperations = () -> {
syncSortedMap.put(1, "One");
syncSortedMap.put(2, "Two");
syncSortedMap.put(3, "Three");
};
Thread thread1 = new Thread(sortedMapOperations);
Thread thread2 = new Thread(sortedMapOperations);
thread1.start();
thread2.start();
thread1.join();
thread2.join();
assertThat(syncSortedMap.size()).isEqualTo(3);
}
}

View File

@ -0,0 +1,28 @@
package com.ossez.synchronizedcollections;
import java.util.Arrays;
import java.util.Collections;
import java.util.SortedSet;
import java.util.TreeSet;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class SynchronizedSortedSetUnitTest {
@Test
public void givenSynchronizedSortedSet_whenTwoThreadsAddElements_thenCorrectSortedSetSize() throws InterruptedException {
SortedSet<Integer> syncSortedSet = Collections.synchronizedSortedSet(new TreeSet<>());
Runnable sortedSetOperations = () -> {syncSortedSet.addAll(Arrays.asList(1, 2, 3, 4, 5, 6));};
sortedSetOperations.run();
sortedSetOperations.run();
Thread thread1 = new Thread(sortedSetOperations);
Thread thread2 = new Thread(sortedSetOperations);
thread1.start();
thread2.start();
thread1.join();
thread2.join();
assertThat(syncSortedSet.size()).isEqualTo(6);
}
}