core-java-collections-3 模块添加到仓库列表中
This commit is contained in:
+182
@@ -0,0 +1,182 @@
|
||||
package com.ossez.collections.bitset;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.openjdk.jol.info.ClassLayout;
|
||||
import org.openjdk.jol.info.GraphLayout;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.BitSet;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class BitSetUnitTest {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(BitSetUnitTest.class);
|
||||
|
||||
@Test
|
||||
public void givenBoolArray_whenMemoryLayout_thenConsumeMoreThanOneBit() {
|
||||
boolean[] bits = new boolean[1024 * 1024];
|
||||
|
||||
LOG.debug(ClassLayout.parseInstance(bits).toPrintable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBitSet_whenMemoryLayout_thenConsumeOneBitPerFlag() {
|
||||
BitSet bitSet = new BitSet(1024 * 1024);
|
||||
|
||||
LOG.debug(GraphLayout.parseInstance(bitSet).toPrintable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBitSet_whenSetting_thenShouldBeTrue() {
|
||||
BitSet bitSet = new BitSet();
|
||||
|
||||
bitSet.set(10);
|
||||
assertThat(bitSet.get(10)).isTrue();
|
||||
|
||||
bitSet.set(20, 30);
|
||||
for (int i = 20; i <= 29; i++) {
|
||||
assertThat(bitSet.get(i)).isTrue();
|
||||
}
|
||||
assertThat(bitSet.get(30)).isFalse();
|
||||
|
||||
bitSet.set(10, false);
|
||||
assertThat(bitSet.get(10)).isFalse();
|
||||
|
||||
bitSet.set(20, 30, false);
|
||||
for (int i = 20; i <= 30; i++) {
|
||||
assertThat(bitSet.get(i)).isFalse();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBitSet_whenClearing_thenShouldBeFalse() {
|
||||
BitSet bitSet = new BitSet();
|
||||
bitSet.set(42);
|
||||
assertThat(bitSet.get(42)).isTrue();
|
||||
|
||||
bitSet.clear(42);
|
||||
assertThat(bitSet.get(42)).isFalse();
|
||||
|
||||
bitSet.set(10, 20);
|
||||
for (int i = 10; i < 20; i++) {
|
||||
assertThat(bitSet.get(i)).isTrue();
|
||||
}
|
||||
|
||||
bitSet.clear(10, 20);
|
||||
for (int i = 10; i < 20; i++) {
|
||||
assertThat(bitSet.get(i)).isFalse();
|
||||
}
|
||||
|
||||
bitSet.set(10, 20);
|
||||
bitSet.clear();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
assertThat(bitSet.get(i)).isFalse();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBitSet_whenGettingElements_thenShouldReturnRequestedBits() {
|
||||
BitSet bitSet = new BitSet();
|
||||
bitSet.set(42);
|
||||
|
||||
assertThat(bitSet.get(42)).isTrue();
|
||||
assertThat(bitSet.get(43)).isFalse();
|
||||
|
||||
bitSet.set(10, 20);
|
||||
BitSet newBitSet = bitSet.get(10, 20);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
assertThat(newBitSet.get(i)).isTrue();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBitSet_whenFlip_thenTogglesTrueToFalseAndViceVersa() {
|
||||
BitSet bitSet = new BitSet();
|
||||
bitSet.set(42);
|
||||
bitSet.flip(42);
|
||||
assertThat(bitSet.get(42)).isFalse();
|
||||
|
||||
bitSet.flip(12);
|
||||
assertThat(bitSet.get(12)).isTrue();
|
||||
|
||||
bitSet.flip(30, 40);
|
||||
for (int i = 30; i < 40; i++) {
|
||||
assertThat(bitSet.get(i)).isTrue();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBitSet_whenGettingTheSize_thenReturnsTheSize() {
|
||||
BitSet defaultBitSet = new BitSet();
|
||||
assertThat(defaultBitSet.size()).isEqualTo(64);
|
||||
|
||||
BitSet bitSet = new BitSet(1024);
|
||||
assertThat(bitSet.size()).isEqualTo(1024);
|
||||
|
||||
assertThat(bitSet.cardinality()).isEqualTo(0);
|
||||
bitSet.set(10, 30);
|
||||
assertThat(bitSet.cardinality()).isEqualTo(30 - 10);
|
||||
|
||||
assertThat(bitSet.length()).isEqualTo(30);
|
||||
bitSet.set(100);
|
||||
assertThat(bitSet.length()).isEqualTo(101);
|
||||
|
||||
assertThat(bitSet.isEmpty()).isFalse();
|
||||
bitSet.clear();
|
||||
assertThat(bitSet.isEmpty()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBitSet_whenSetOperations_thenShouldReturnAnotherBitSet() {
|
||||
BitSet first = new BitSet();
|
||||
first.set(5, 10);
|
||||
|
||||
BitSet second = new BitSet();
|
||||
second.set(7, 15);
|
||||
|
||||
assertThat(first.intersects(second)).isTrue();
|
||||
|
||||
first.and(second);
|
||||
assertThat(first.get(7)).isTrue();
|
||||
assertThat(first.get(8)).isTrue();
|
||||
assertThat(first.get(9)).isTrue();
|
||||
assertThat(first.get(10)).isFalse();
|
||||
|
||||
first.clear();
|
||||
first.set(5, 10);
|
||||
|
||||
first.xor(second);
|
||||
for (int i = 5; i < 7; i++) {
|
||||
assertThat(first.get(i)).isTrue();
|
||||
}
|
||||
for (int i = 10; i < 15; i++) {
|
||||
assertThat(first.get(i)).isTrue();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBitSet_whenStream_thenStreamsAllSetBits() {
|
||||
BitSet bitSet = new BitSet();
|
||||
bitSet.set(15, 25);
|
||||
|
||||
bitSet.stream().forEach(bit -> LOG.debug(String.valueOf(bit)));
|
||||
assertThat(bitSet.stream().count()).isEqualTo(10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBitSet_whenNextOrPrev_thenReturnsTheNextOrPrevClearOrSetBit() {
|
||||
BitSet bitSet = new BitSet();
|
||||
bitSet.set(15, 25);
|
||||
|
||||
assertThat(bitSet.nextSetBit(13)).isEqualTo(15);
|
||||
assertThat(bitSet.nextSetBit(25)).isEqualTo(-1);
|
||||
|
||||
assertThat(bitSet.nextClearBit(23)).isEqualTo(25);
|
||||
|
||||
assertThat(bitSet.previousClearBit(24)).isEqualTo(14);
|
||||
assertThat(bitSet.previousSetBit(29)).isEqualTo(24);
|
||||
assertThat(bitSet.previousSetBit(14)).isEqualTo(-1);
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package com.ossez.collections.clearvsremoveall;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Unit tests demonstrating differences between ArrayList#clear() and ArrayList#removeAll()
|
||||
*/
|
||||
class ClearVsRemoveAllUnitTest {
|
||||
|
||||
/*
|
||||
* Tests
|
||||
*/
|
||||
@Test
|
||||
void givenArrayListWithElements_whenClear_thenListBecomesEmpty() {
|
||||
Collection<Integer> collection = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
|
||||
|
||||
collection.clear();
|
||||
|
||||
assertTrue(collection.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoArrayListsWithCommonElements_whenRemoveAll_thenFirstListMissElementsFromSecondList() {
|
||||
Collection<Integer> firstCollection = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
|
||||
Collection<Integer> secondCollection = new ArrayList<>(Arrays.asList(3, 4, 5, 6, 7));
|
||||
|
||||
firstCollection.removeAll(secondCollection);
|
||||
|
||||
assertEquals(Arrays.asList(1, 2), firstCollection);
|
||||
assertEquals(Arrays.asList(3, 4, 5, 6, 7), secondCollection);
|
||||
}
|
||||
|
||||
}
|
||||
+98
@@ -0,0 +1,98 @@
|
||||
package com.ossez.collections.hashmapvshashtable;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
public class HashmapVsHashtableDifferenceUnitTest {
|
||||
|
||||
// null values
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenHashtable_whenAddNullKey_thenNullPointerExceptionThrown() {
|
||||
Hashtable<String, String> table = new Hashtable<String, String>();
|
||||
table.put(null, "value");
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenHashtable_whenAddNullValue_thenNullPointerExceptionThrown() {
|
||||
Hashtable<String, String> table = new Hashtable<String, String>();
|
||||
table.put("key", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHashmap_whenAddNullKeyAndValue_thenObjectAdded() {
|
||||
HashMap<String, String> map = new HashMap<String, String>();
|
||||
map.put(null, "value");
|
||||
map.put("key1", null);
|
||||
map.put("key2", null);
|
||||
|
||||
assertEquals(3, map.size());
|
||||
}
|
||||
|
||||
// fail-fast iterator
|
||||
@Test(expected = ConcurrentModificationException.class)
|
||||
public void givenHashmap_whenModifyUnderlyingCollection_thenConcurrentModificationExceptionThrown() {
|
||||
HashMap<String, String> map = new HashMap<String, String>();
|
||||
map.put("key1", "value1");
|
||||
map.put("key2", "value2");
|
||||
map.put("key3", "value3");
|
||||
|
||||
Iterator<String> iterator = map.keySet().iterator();
|
||||
while(iterator.hasNext()){
|
||||
iterator.next();
|
||||
map.put("key4", "value4");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHashtable_whenModifyUnderlyingCollection_thenItHasNoEffectOnIteratedCollection() {
|
||||
Hashtable<String, String> table = new Hashtable<String, String>();
|
||||
table.put("key1", "value1");
|
||||
table.put("key2", "value2");
|
||||
|
||||
List<String> keysSelected = Lists.newArrayList();
|
||||
Enumeration<String> keys = table.keys();
|
||||
while (keys.hasMoreElements()) {
|
||||
String key = keys.nextElement();
|
||||
keysSelected.add(key);
|
||||
|
||||
if (key.equals("key1")) {
|
||||
table.put("key3", "value3");
|
||||
}
|
||||
}
|
||||
|
||||
assertEquals(2, keysSelected.size());
|
||||
}
|
||||
|
||||
// synchronized map
|
||||
@Test
|
||||
public void givenHashmap_thenCreateSynchronizedMap() {
|
||||
HashMap<String, String> map = new HashMap<String, String>();
|
||||
map.put("key1", "value1");
|
||||
map.put("key2", "value2");
|
||||
map.put("key3", "value3");
|
||||
|
||||
Set<Entry<String, String>> set = map.entrySet();
|
||||
synchronized (map) {
|
||||
Iterator<Entry<String, String>> it = set.iterator();
|
||||
while(it.hasNext()) {
|
||||
Map.Entry<String, String> elem = (Map.Entry<String, String>)it.next();
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, String> syncMap = Collections.synchronizedMap(map);
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.ossez.collections.hashset;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class HashSetUnitTest {
|
||||
|
||||
@Test
|
||||
void whenRemoveAllFromHashset_thenRemovesAllElementsFromHashsetThatArePresentInCollection() {
|
||||
Set<Integer> set = new HashSet<>();
|
||||
Collection<Integer> collection = new ArrayList<>();
|
||||
set.add(1);
|
||||
set.add(2);
|
||||
set.add(3);
|
||||
set.add(4);
|
||||
collection.add(1);
|
||||
collection.add(3);
|
||||
|
||||
set.removeAll(collection);
|
||||
|
||||
assertEquals(2, set.size());
|
||||
Integer[] actualElements = new Integer[set.size()];
|
||||
Integer[] expectedElements = new Integer[] { 2, 4 };
|
||||
assertArrayEquals(expectedElements, set.toArray(actualElements));
|
||||
}
|
||||
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package com.ossez.collections.iterators;
|
||||
|
||||
import static com.ossez.collections.collections.iterators.Iterators.failFast1;
|
||||
import static com.ossez.collections.collections.iterators.Iterators.failFast2;
|
||||
import static com.ossez.collections.collections.iterators.Iterators.failSafe1;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
import java.util.ConcurrentModificationException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Source code https://github.com/eugenp/tutorials
|
||||
*
|
||||
* @author Santosh Thakur
|
||||
*/
|
||||
|
||||
public class IteratorsUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenFailFast_ThenThrowsException() {
|
||||
assertThatThrownBy(() -> {
|
||||
failFast1();
|
||||
}).isInstanceOf(ConcurrentModificationException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFailFast_ThenThrowsExceptionInSecondIteration() {
|
||||
assertThatThrownBy(() -> {
|
||||
failFast2();
|
||||
}).isInstanceOf(ConcurrentModificationException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFailSafe_ThenDoesNotThrowException() {
|
||||
assertThat(failSafe1()).isGreaterThanOrEqualTo(0);
|
||||
}
|
||||
|
||||
}
|
||||
+146
@@ -0,0 +1,146 @@
|
||||
package com.ossez.collections.mapfirstpair;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
|
||||
import java.util.AbstractMap;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class MapFirstPairUnitTest {
|
||||
|
||||
private Map.Entry<Integer, String> getFirstPairUsingIterator(Map<Integer, String> map) {
|
||||
if (map == null || map.size() == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Iterator<Map.Entry<Integer, String>> iterator = map.entrySet()
|
||||
.iterator();
|
||||
|
||||
if (iterator.hasNext()) {
|
||||
return iterator.next();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private Map.Entry<Integer, String> getFirstPairUsingStream(Map<Integer, String> map) {
|
||||
if (map == null || map.size() == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Set<Map.Entry<Integer, String>> entrySet = map.entrySet();
|
||||
|
||||
return entrySet.stream()
|
||||
.findFirst()
|
||||
.get();
|
||||
}
|
||||
|
||||
private Map<Integer, String> populateMapValues(Map<Integer, String> map) {
|
||||
if (map != null) {
|
||||
map.put(5, "A");
|
||||
map.put(1, "B");
|
||||
map.put(2, "C");
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingIteratorForHashMap_thenFirstPairWhichWasNotInsertedFirst() {
|
||||
Map<Integer, String> hashMap = new HashMap<>();
|
||||
hashMap = populateMapValues(hashMap);
|
||||
|
||||
Map.Entry<Integer, String> actualValue = getFirstPairUsingIterator(hashMap);
|
||||
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(1, "B");
|
||||
Map.Entry<Integer, String> pairInsertedFirst = new AbstractMap.SimpleEntry<Integer, String>(5, "A");
|
||||
|
||||
assertEquals(expectedValue, actualValue);
|
||||
assertNotEquals(pairInsertedFirst, actualValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingStreamForHashMap_thenFirstPairWhichWasNotInsertedFirst() {
|
||||
Map<Integer, String> hashMap = new HashMap<>();
|
||||
hashMap = populateMapValues(hashMap);
|
||||
Map.Entry<Integer, String> actualValue = getFirstPairUsingStream(hashMap);
|
||||
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(1, "B");
|
||||
Map.Entry<Integer, String> pairInsertedFirst = new AbstractMap.SimpleEntry<Integer, String>(5, "A");
|
||||
|
||||
assertEquals(expectedValue, actualValue);
|
||||
assertNotEquals(pairInsertedFirst, actualValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingIteratorForLinkedHashMap_thenFirstPairWhichWasInsertedFirst() {
|
||||
Map<Integer, String> linkedHashMap = new LinkedHashMap<>();
|
||||
linkedHashMap = populateMapValues(linkedHashMap);
|
||||
Map.Entry<Integer, String> actualValue = getFirstPairUsingIterator(linkedHashMap);
|
||||
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(5, "A");
|
||||
|
||||
assertEquals(expectedValue, actualValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingStreamForLinkedHashMap_thenFirstPairWhichWasInsertedFirst() {
|
||||
Map<Integer, String> linkedHashMap = new LinkedHashMap<>();
|
||||
linkedHashMap = populateMapValues(linkedHashMap);
|
||||
|
||||
Map.Entry<Integer, String> actualValue = getFirstPairUsingStream(linkedHashMap);
|
||||
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(5, "A");
|
||||
|
||||
assertEquals(expectedValue, actualValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAddedAnElementInHashMap_thenFirstPairChangedUsingIterator() {
|
||||
Map<Integer, String> hashMap = new HashMap<>();
|
||||
hashMap = populateMapValues(hashMap);
|
||||
|
||||
hashMap.put(0, "D");
|
||||
Map.Entry<Integer, String> actualValue = getFirstPairUsingIterator(hashMap);
|
||||
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(0, "D");
|
||||
|
||||
assertEquals(expectedValue, actualValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAddedAnElementInHashMap_thenFirstPairChangedUsingStream() {
|
||||
Map<Integer, String> hashMap = new HashMap<>();
|
||||
hashMap = populateMapValues(hashMap);
|
||||
|
||||
hashMap.put(0, "D");
|
||||
Map.Entry<Integer, String> actualValue = getFirstPairUsingStream(hashMap);
|
||||
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(0, "D");
|
||||
|
||||
assertEquals(expectedValue, actualValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAddedAnElementInLinkedHashMap_thenFirstPairRemainUnchangedUsingIterator() {
|
||||
Map<Integer, String> linkedHashMap = new LinkedHashMap<>();
|
||||
linkedHashMap = populateMapValues(linkedHashMap);
|
||||
|
||||
linkedHashMap.put(0, "D");
|
||||
Map.Entry<Integer, String> actualValue = getFirstPairUsingIterator(linkedHashMap);
|
||||
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(5, "A");
|
||||
|
||||
assertEquals(expectedValue, actualValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAddedAnElementInLinkedHashMap_thenFirstPairRemainUnchangedUsingStream() {
|
||||
Map<Integer, String> linkedHashMap = new LinkedHashMap<>();
|
||||
linkedHashMap = populateMapValues(linkedHashMap);
|
||||
|
||||
linkedHashMap.put(0, "D");
|
||||
Map.Entry<Integer, String> actualValue = getFirstPairUsingStream(linkedHashMap);
|
||||
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(5, "A");
|
||||
|
||||
assertEquals(expectedValue, actualValue);
|
||||
}
|
||||
}
|
||||
+183
@@ -0,0 +1,183 @@
|
||||
package com.ossez.collections.stack;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.Stack;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
public class StackUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenStackIsCreated_thenItHasSizeZero() {
|
||||
Stack<Integer> intStack = new Stack<>();
|
||||
|
||||
assertEquals(0, intStack.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenElementIsPushed_thenStackSizeIsIncreased() {
|
||||
Stack<Integer> intStack = new Stack<>();
|
||||
intStack.push(1);
|
||||
|
||||
assertEquals(1, intStack.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMultipleElementsArePushed_thenStackSizeIsIncreased() {
|
||||
Stack<Integer> intStack = new Stack<>();
|
||||
List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
|
||||
|
||||
boolean result = intStack.addAll(intList);
|
||||
|
||||
assertTrue(result);
|
||||
assertEquals(7, intList.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenElementIsPoppedFromStack_thenElementIsRemovedAndSizeChanges() {
|
||||
Stack<Integer> intStack = new Stack<>();
|
||||
intStack.push(5);
|
||||
|
||||
Integer element = intStack.pop();
|
||||
|
||||
assertEquals(Integer.valueOf(5), element);
|
||||
assertTrue(intStack.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenElementIsPeeked_thenElementIsNotRemovedAndSizeDoesNotChange() {
|
||||
Stack<Integer> intStack = new Stack<>();
|
||||
intStack.push(5);
|
||||
|
||||
Integer element = intStack.peek();
|
||||
|
||||
assertEquals(Integer.valueOf(5), element);
|
||||
assertEquals(1, intStack.search(5));
|
||||
assertEquals(1, intStack.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenElementIsOnStack_thenSearchReturnsItsDistanceFromTheTop() {
|
||||
Stack<Integer> intStack = new Stack<>();
|
||||
intStack.push(5);
|
||||
intStack.push(8);
|
||||
|
||||
assertEquals(2, intStack.search(5));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenElementIsOnStack_thenIndexOfReturnsItsIndex() {
|
||||
Stack<Integer> intStack = new Stack<>();
|
||||
intStack.push(5);
|
||||
|
||||
int indexOf = intStack.indexOf(5);
|
||||
|
||||
assertEquals(0, indexOf);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMultipleElementsAreOnStack_thenIndexOfReturnsLastElementIndex() {
|
||||
Stack<Integer> intStack = new Stack<>();
|
||||
intStack.push(5);
|
||||
intStack.push(5);
|
||||
intStack.push(5);
|
||||
|
||||
int lastIndexOf = intStack.lastIndexOf(5);
|
||||
|
||||
assertEquals(2, lastIndexOf);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRemoveElementIsInvoked_thenElementIsRemoved() {
|
||||
Stack<Integer> intStack = new Stack<>();
|
||||
intStack.push(5);
|
||||
intStack.push(5);
|
||||
|
||||
intStack.removeElement(5);
|
||||
|
||||
assertEquals(1, intStack.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRemoveElementAtIsInvoked_thenElementIsRemoved() {
|
||||
Stack<Integer> intStack = new Stack<>();
|
||||
intStack.push(5);
|
||||
intStack.push(7);
|
||||
|
||||
intStack.removeElementAt(1);
|
||||
|
||||
assertEquals(-1, intStack.search(7));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRemoveAllElementsIsInvoked_thenAllElementsAreRemoved() {
|
||||
Stack<Integer> intStack = new Stack<>();
|
||||
intStack.push(5);
|
||||
intStack.push(7);
|
||||
|
||||
intStack.removeAllElements();
|
||||
|
||||
assertTrue(intStack.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenElementsOnStack_whenRemoveAllIsInvoked_thenAllElementsFromCollectionAreRemoved() {
|
||||
Stack<Integer> intStack = new Stack<>();
|
||||
List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
|
||||
intStack.addAll(intList);
|
||||
intStack.add(500);
|
||||
|
||||
intStack.removeAll(intList);
|
||||
|
||||
assertEquals(1, intStack.size());
|
||||
assertEquals(1, intStack.search(500));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRemoveIfIsInvoked_thenAllElementsSatysfyingConditionAreRemoved() {
|
||||
Stack<Integer> intStack = new Stack<>();
|
||||
List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
|
||||
intStack.addAll(intList);
|
||||
|
||||
intStack.removeIf(element -> element < 6);
|
||||
|
||||
assertEquals(2, intStack.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAnotherStackCreatedWhileTraversingStack_thenStacksAreEqual() {
|
||||
Stack<Integer> intStack = new Stack<>();
|
||||
List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
|
||||
intStack.addAll(intList);
|
||||
|
||||
ListIterator<Integer> it = intStack.listIterator();
|
||||
|
||||
Stack<Integer> result = new Stack<>();
|
||||
while(it.hasNext()) {
|
||||
result.push(it.next());
|
||||
}
|
||||
|
||||
assertThat(result, equalTo(intStack));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStackIsFiltered_allElementsNotSatisfyingFilterConditionAreDiscarded() {
|
||||
Stack<Integer> intStack = new Stack<>();
|
||||
List<Integer> inputIntList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 9, 10);
|
||||
intStack.addAll(inputIntList);
|
||||
|
||||
List<Integer> filtered = intStack
|
||||
.stream()
|
||||
.filter(element -> element <= 3)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertEquals(3, filtered.size());
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
Reference in New Issue
Block a user