forked from Hithomelabs/Princeton1
Adding tests and modifying roadmap
This commit is contained in:
parent
7499161725
commit
fdca30f50a
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/modules/module4/Pricenton1.module4.test.iml" filepath="$PROJECT_DIR$/.idea/modules/module4/Pricenton1.module4.test.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
8
.idea/modules/module4/Pricenton1.module4.test.iml
generated
Normal file
8
.idea/modules/module4/Pricenton1.module4.test.iml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module version="4">
|
||||
<component name="AdditionalModuleElements">
|
||||
<content url="file://$MODULE_DIR$/../../../module4/src/test" dumb="true">
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../../module4/src/test/java" isTestSource="true" />
|
||||
</content>
|
||||
</component>
|
||||
</module>
|
@ -1,10 +1,18 @@
|
||||
#Data Structures
|
||||
|
||||
1. Stacks:
|
||||
- Linear data structure
|
||||
- Can have various implementations
|
||||
- Should support (LIFO)
|
||||
- Primitive implementation (Using arrays of primitive data types)
|
||||
- Other implementations (Using linked lists)
|
||||
2. Queues:
|
||||
# Data Structures
|
||||
|
||||
1. Stacks:
|
||||
- [API](../module4/src/main/java/com/hithomelabs/princeton1/module4/Stack.java)
|
||||
- [Sample client](../clients/src/main/java/com/hithomelabs/clients/ArrayStackClient.java)
|
||||
- Linear data structure
|
||||
- Can have various implementations
|
||||
- Should support (LIFO)
|
||||
- [Primitive implementation](../module4/src/main/java/com/hithomelabs/princeton1/module4/ArrayStack.java) (Using arrays of primitive data types)
|
||||
- [Other implementations](../module4/src/main/java/com/hithomelabs/princeton1/module4/LinkedStack.java) (Using linked lists)
|
||||
2. Queues:
|
||||
- [API](../module4/src/main/java/com/hithomelabs/princeton1/module4/Queue.java)
|
||||
- [sample Client](../clients/src/main/java/com/hithomelabs/clients/LinkedQueueClient.java)
|
||||
- Linear data structure.
|
||||
- Can have various implementations.
|
||||
- Should support FIFO.
|
||||
- [Primitive implementation](../module4/src/main/java/com/hithomelabs/princeton1/module4/ArrayQueue.java) (Using array of primitive data types)
|
||||
- Other implementation ([using linked lists](../module4/src/main/java/com/hithomelabs/princeton1/module4/LinkedQueue.java))
|
@ -1,8 +1,9 @@
|
||||
package com.hithomelabs.princeton1.module4;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.Iterator;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public class ArrayQueue<E> extends Queue<E>{
|
||||
|
||||
// insertion from tail, removal from head
|
||||
|
@ -1,13 +1,13 @@
|
||||
package com.hithomelabs.princeton1.module4;
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.Iterator;
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
// Concrete implementation of stack using arrays
|
||||
// Creating a generic stack of type E
|
||||
public class ArrayStack<E> extends Stack<E> {
|
||||
|
||||
// Capacity and size are two variables, capacity determines total capacity of array, capacity defaults at 10
|
||||
// Everytime size == capacity, capacity = 2 * capacity
|
||||
// every time size == capacity, capacity = 2 * capacity
|
||||
private static final int DEFAULT_CAPACITY = 10;
|
||||
private int capacity;
|
||||
private int size;
|
||||
@ -18,7 +18,7 @@ public class ArrayStack<E> extends Stack<E> {
|
||||
arr = (E[]) new Object[this.capacity];
|
||||
}
|
||||
|
||||
// Constructor chaining, default constructor will call parametrised constructor with default initial capacity 10
|
||||
// Constructor chaining, default constructor will call parametrized constructor with default initial capacity 10
|
||||
public ArrayStack(){
|
||||
this(DEFAULT_CAPACITY);
|
||||
}
|
||||
@ -64,7 +64,7 @@ public class ArrayStack<E> extends Stack<E> {
|
||||
}
|
||||
|
||||
private void checkResize() {
|
||||
if (size < capacity / 4 && capacity >= 40){
|
||||
if (size < capacity / 4 && capacity >= 20){
|
||||
capacity = capacity / 2;
|
||||
changeCapacity(capacity);
|
||||
}
|
||||
|
@ -60,7 +60,7 @@ public class LinkedQueue<E> extends Queue<E>{
|
||||
Node current = head;
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return current != tail;
|
||||
return current != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -0,0 +1,76 @@
|
||||
package com.hithomelabs.princeton1.module4;
|
||||
|
||||
import java.util.Iterator;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class ArrayQueueTest {
|
||||
|
||||
private Queue<Integer> queue;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
queue = new ArrayQueue<Integer>();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
queue = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Empty queue must return size zero")
|
||||
public void testEmptyQueueSize(){
|
||||
assertEquals(0, queue.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Enqueuing must work as expected")
|
||||
public void testEnqueue(){
|
||||
queue.enqueue(1);
|
||||
queue.enqueue(2);
|
||||
assertEquals(2,queue.size());
|
||||
Iterator<Integer> iterator = queue.iterator();
|
||||
assertEquals(1, iterator.next());
|
||||
assertEquals(2, iterator.next());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Dequeueing must work as expected")
|
||||
public void testDequeue(){
|
||||
queue.enqueue(1);
|
||||
queue.enqueue(2);
|
||||
assertEquals(1, queue.dequeue());
|
||||
queue.enqueue(3);
|
||||
assertEquals(2, queue.size());
|
||||
assertEquals(2, queue.dequeue());
|
||||
assertEquals(3, queue.dequeue());
|
||||
assertEquals(0, queue.size());
|
||||
assertNull(queue.dequeue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Testing resizable array and iterator")
|
||||
public void testResize(){
|
||||
for(int i = 0; i < 21; i++)
|
||||
queue.enqueue(i);
|
||||
for(int i = 0; i < 13; i++){
|
||||
queue.dequeue();
|
||||
}
|
||||
assertEquals(13, queue.dequeue());
|
||||
assertEquals(7,queue.size());
|
||||
Integer[] arr = new Integer[queue.size()];
|
||||
Iterator<Integer> iterator = queue.iterator();
|
||||
int count = 0;
|
||||
while(iterator.hasNext()){
|
||||
arr[count++] = iterator.next();
|
||||
}
|
||||
assertArrayEquals(new Integer[]{14, 15, 16, 17, 18, 19, 20}, arr);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
package com.hithomelabs.princeton1.module4;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class ArrayStackTest {
|
||||
|
||||
private Stack<String> stack;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
stack = new ArrayStack<String>();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
stack = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Empty stack must return size zero")
|
||||
public void testEmptyStackSize(){
|
||||
assertEquals(0, stack.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Push must work as expected")
|
||||
public void testEnqueue(){
|
||||
stack.push("Hello");
|
||||
stack.push("World");
|
||||
assertEquals(2,stack.size());
|
||||
Iterator<String> iterator = stack.iterator();
|
||||
assertEquals("Hello", iterator.next());
|
||||
assertEquals("World", iterator.next());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Pop must work as expected")
|
||||
public void testDequeue(){
|
||||
stack.push("hello");
|
||||
stack.push("world");
|
||||
assertEquals("world", stack.pop());
|
||||
stack.push("I");
|
||||
assertEquals(2, stack.size());
|
||||
assertEquals("I", stack.pop());
|
||||
assertEquals("hello", stack.pop());
|
||||
assertEquals(0, stack.size());
|
||||
assertNull(stack.pop());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Testing resizable array and iterator")
|
||||
public void testResize(){
|
||||
stack.push("Hello");
|
||||
stack.push("world");
|
||||
stack.push("I");
|
||||
stack.push("am");
|
||||
stack.push("testing");
|
||||
stack.push("my");
|
||||
stack.push("implementation");
|
||||
stack.push("of");
|
||||
stack.push("Stacks");
|
||||
stack.push("Data");
|
||||
stack.push("Structure");
|
||||
assertEquals("Structure", stack.pop());
|
||||
int counter = 6;
|
||||
while(counter-- > 0)
|
||||
stack.pop();
|
||||
assertEquals(4,stack.size());
|
||||
String[] arr = new String[stack.size()];
|
||||
Iterator<String> iterator = stack.iterator();
|
||||
int count = 0;
|
||||
while(iterator.hasNext()){
|
||||
arr[count++] = iterator.next();
|
||||
}
|
||||
assertArrayEquals(new String[]{"Hello", "world", "I", "am"}, arr);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
package com.hithomelabs.princeton1.module4;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class LinkedQueueTest {
|
||||
|
||||
private Queue<Integer> queue;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
queue = new LinkedQueue<>();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
queue = null;
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@DisplayName("Empty queue must return size zero")
|
||||
public void testEmptyQueueSize(){
|
||||
assertEquals(0, queue.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Enqueuing must work as expected")
|
||||
public void testEnqueue(){
|
||||
queue.enqueue(1);
|
||||
queue.enqueue(2);
|
||||
assertEquals(2,queue.size());
|
||||
Iterator<Integer> iterator = queue.iterator();
|
||||
assertEquals(1, iterator.next());
|
||||
assertEquals(2, iterator.next());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Dequeueing must work as expected")
|
||||
public void testDequeue(){
|
||||
queue.enqueue(1);
|
||||
queue.enqueue(2);
|
||||
assertEquals(1, queue.dequeue());
|
||||
queue.enqueue(3);
|
||||
assertEquals(2, queue.size());
|
||||
assertEquals(2, queue.dequeue());
|
||||
assertEquals(3, queue.dequeue());
|
||||
assertEquals(0, queue.size());
|
||||
assertNull(queue.dequeue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Testing resizable array and iterator")
|
||||
public void testResize(){
|
||||
for(int i = 0; i < 21; i++)
|
||||
queue.enqueue(i);
|
||||
for(int i = 0; i < 13; i++){
|
||||
queue.dequeue();
|
||||
}
|
||||
assertEquals(13, queue.dequeue());
|
||||
assertEquals(7,queue.size());
|
||||
Integer[] arr = new Integer[queue.size()];
|
||||
Iterator<Integer> iterator = queue.iterator();
|
||||
int count = 0;
|
||||
while(iterator.hasNext()){
|
||||
arr[count++] = iterator.next();
|
||||
}
|
||||
assertArrayEquals(new Integer[]{14, 15, 16, 17, 18, 19, 20}, arr);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user