Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the bugs in java and add all the tests #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Java/src/main/java/edu/cmu/cs/cs214/rec02/ArrayIntQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public Integer dequeue() {
return null;
}
Integer value = elementData[head];
// add here; don't have to
elementData[head] = 0;
head = (head + 1) % elementData.length;
size--;
return value;
Expand All @@ -73,11 +75,15 @@ public boolean enqueue(Integer value) {

/** {@inheritDoc} */
public boolean isEmpty() {
return size >= 0;
return size == 0;
}

/** {@inheritDoc} */
public Integer peek() {
// add here
if (isEmpty()) {
return null;
}
return elementData[head];
}

Expand All @@ -99,7 +105,9 @@ private void ensureCapacity() {
newData[i - head] = elementData[i];
}
for (int i = 0; i < head; i++) {
newData[head - i] = elementData[i];
// another bug here
newData[oldCapacity - head + i] = elementData[i];
//newData[head - i] = elementData[i];
}
elementData = newData;
head = 0;
Expand Down
29 changes: 26 additions & 3 deletions Java/src/test/java/edu/cmu/cs/cs214/rec02/IntQueueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public class IntQueueTest {
@Before
public void setUp() {
// comment/uncomment these lines to test each class
mQueue = new LinkedIntQueue();
// mQueue = new ArrayIntQueue();
//mQueue = new LinkedIntQueue();
mQueue = new ArrayIntQueue();

testList = new ArrayList<>(List.of(1, 2, 3));
}
Expand Down Expand Up @@ -80,6 +80,19 @@ public void testDequeue() {
}
}

@Test
public void testDequeueEmptyQueue() {
assertNull(mQueue.dequeue());
}

@Test
public void testClear() {
Integer input = 1;
mQueue.enqueue(input);
mQueue.clear();
assertTrue(mQueue.isEmpty());
}

@Test
public void testContent() throws IOException {
InputStream in = new FileInputStream("src/test/resources/data.txt");
Expand All @@ -100,5 +113,15 @@ public void testContent() throws IOException {
}
}


@Test
public void testEnqueueOutOfCapacity() {
mQueue.enqueue(1);
mQueue.dequeue();
for (int i = 1; i < 12; i++) {
mQueue.enqueue(i);
assertEquals((Integer)1, mQueue.peek());
assertEquals(i, mQueue.size());
}
assertEquals(11, mQueue.size());
}
}
21 changes: 14 additions & 7 deletions TypeScript/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions TypeScript/tests/queue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ test("test peek: queue with 2 element should peek the one that was most recently
expect(queue.peek()).toEqual(3)
})

test("test dequeue: dequeue in a newly created list should return null",() => {
expect(createQueue().dequeue()).toBeNull()
})

test("test dequeue: queue with 2 element should dequeue the one that was most recently added",() => {
const queue = createQueue()
queue.enqueue(2)
queue.enqueue(3)
expect(queue.dequeue()).toEqual(3)
})

test("test clear: a queue that was cleared should be empty",() => {
const queue = createQueue()
queue.enqueue(2)
queue.clear()
expect(queue.isEmpty()).toBeTruthy
})

let param = [5, 10, 1000000]
// parameterized test, apply to each value of the parameter
test.each(param)("test enqueue: enqueued number %d is correct", (nr) => {
Expand Down