From 7c904471d419af7a139a7da302fd86f25f00e5d1 Mon Sep 17 00:00:00 2001 From: Vera0712 Date: Sun, 17 Sep 2023 21:37:18 -0400 Subject: [PATCH] Fixed the bugs in java and add all the tests --- .../edu/cmu/cs/cs214/rec02/ArrayIntQueue.java | 12 ++++++-- .../edu/cmu/cs/cs214/rec02/IntQueueTest.java | 29 +++++++++++++++++-- TypeScript/package-lock.json | 21 +++++++++----- TypeScript/tests/queue.test.ts | 18 ++++++++++++ 4 files changed, 68 insertions(+), 12 deletions(-) diff --git a/Java/src/main/java/edu/cmu/cs/cs214/rec02/ArrayIntQueue.java b/Java/src/main/java/edu/cmu/cs/cs214/rec02/ArrayIntQueue.java index 69e0cba..c481cb1 100644 --- a/Java/src/main/java/edu/cmu/cs/cs214/rec02/ArrayIntQueue.java +++ b/Java/src/main/java/edu/cmu/cs/cs214/rec02/ArrayIntQueue.java @@ -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; @@ -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]; } @@ -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; diff --git a/Java/src/test/java/edu/cmu/cs/cs214/rec02/IntQueueTest.java b/Java/src/test/java/edu/cmu/cs/cs214/rec02/IntQueueTest.java index 9522b27..3cca3b7 100644 --- a/Java/src/test/java/edu/cmu/cs/cs214/rec02/IntQueueTest.java +++ b/Java/src/test/java/edu/cmu/cs/cs214/rec02/IntQueueTest.java @@ -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)); } @@ -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"); @@ -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()); + } } diff --git a/TypeScript/package-lock.json b/TypeScript/package-lock.json index 9b7bd50..dbc529a 100644 --- a/TypeScript/package-lock.json +++ b/TypeScript/package-lock.json @@ -8056,7 +8056,8 @@ "version": "5.3.2", "resolved": "https://registry.npmjs.org/acorn-jsx/-/acorn-jsx-5.3.2.tgz", "integrity": "sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==", - "dev": true + "dev": true, + "requires": {} }, "acorn-walk": { "version": "7.2.0", @@ -8827,13 +8828,15 @@ "version": "16.0.3", "resolved": "https://registry.npmjs.org/eslint-config-standard/-/eslint-config-standard-16.0.3.tgz", "integrity": "sha512-x4fmJL5hGqNJKGHSjnLdgA6U6h1YW/G2dW9fA+cyVur4SK6lyue8+UgNKWlZtUDTXvgKDD/Oa3GQjmB5kjtVvg==", - "dev": true + "dev": true, + "requires": {} }, "eslint-config-standard-jsx": { "version": "10.0.0", "resolved": "https://registry.npmjs.org/eslint-config-standard-jsx/-/eslint-config-standard-jsx-10.0.0.tgz", "integrity": "sha512-hLeA2f5e06W1xyr/93/QJulN/rLbUVUmqTlexv9PRKHFwEC9ffJcH2LvJhMoEqYQBEYafedgGZXH2W8NUpt5lA==", - "dev": true + "dev": true, + "requires": {} }, "eslint-config-standard-with-typescript": { "version": "19.0.1", @@ -8849,7 +8852,8 @@ "version": "14.1.1", "resolved": "https://registry.npmjs.org/eslint-config-standard/-/eslint-config-standard-14.1.1.tgz", "integrity": "sha512-Z9B+VR+JIXRxz21udPTL9HpFMyoMUEeX1G251EQ6e05WD9aPVtVBn09XUmZ259wCMlCDmYDSZG62Hhm+ZTJcUg==", - "dev": true + "dev": true, + "requires": {} } } }, @@ -9167,7 +9171,8 @@ "version": "4.1.0", "resolved": "https://registry.npmjs.org/eslint-plugin-standard/-/eslint-plugin-standard-4.1.0.tgz", "integrity": "sha512-ZL7+QRixjTR6/528YNGyDotyffm5OQst/sGxKDwGb9Uqs4In5Egi4+jbobhqJoyoCM6/7v/1A5fhQ7ScMtDjaQ==", - "dev": true + "dev": true, + "requires": {} }, "eslint-scope": { "version": "5.1.1", @@ -10178,7 +10183,8 @@ "version": "1.2.3", "resolved": "https://registry.npmjs.org/jest-pnp-resolver/-/jest-pnp-resolver-1.2.3.tgz", "integrity": "sha512-+3NpwQEnRoIBtx4fyhblQDPgJI0H1IEIkX7ShLUjPGA7TtUTvI1oiKi3SR4oBR0hQhQR80l4WAe5RrXBwWMA8w==", - "dev": true + "dev": true, + "requires": {} }, "jest-regex-util": { "version": "27.5.1", @@ -12031,7 +12037,8 @@ "version": "7.5.9", "resolved": "https://registry.npmjs.org/ws/-/ws-7.5.9.tgz", "integrity": "sha512-F+P9Jil7UiSKSkppIiD94dN07AwvFixvLIj1Og1Rl9GGMuNipJnV9JzjD6XuqmAeiswGvUmNLjr5cFuXwNS77Q==", - "dev": true + "dev": true, + "requires": {} }, "xdg-basedir": { "version": "4.0.0", diff --git a/TypeScript/tests/queue.test.ts b/TypeScript/tests/queue.test.ts index 4fe6b5e..2979b01 100644 --- a/TypeScript/tests/queue.test.ts +++ b/TypeScript/tests/queue.test.ts @@ -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) => {