Skip to content

Commit c1e5a7a

Browse files
committed
test(fundamentals): add assertion to old algorithms
1 parent a1b2942 commit c1e5a7a

File tree

10 files changed

+117
-19
lines changed

10 files changed

+117
-19
lines changed

algorithms/fundamentals/bags/Bag.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ class Bag {
2121
}
2222
}
2323

24-
export default Bag
24+
module.exports = Bag
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const assert = require("assert")
2+
const Bag = require("./Bag.js")
3+
4+
const bag = new Bag()
5+
6+
bag.add(1)
7+
8+
assert.deepEqual(bag.size(), 1)
9+
10+
for(const item of bag) {
11+
assert.deepEqual(item, 1)
12+
}

algorithms/fundamentals/lists/LinkedList.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,4 @@ class LinkedList {
7979
}
8080
}
8181

82-
export default LinkedList
82+
module.exports = LinkedList
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const assert = require("assert")
2+
const LinkedList = require("./LinkedList.js")
3+
4+
const linkedList = new LinkedList()
5+
6+
linkedList.push(1)
7+
linkedList.push(2)
8+
9+
assert.deepEqual(linkedList.isEmpty(), false)
10+
assert.deepEqual(linkedList.size(), 2)
11+
assert.deepEqual(linkedList.pop(), 2)
12+
assert.deepEqual(linkedList.pop(), 1)
13+
assert.deepEqual(linkedList.isEmpty(), true)

algorithms/fundamentals/queues/FIFOQueue.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class FIFOQueue {
88
}
99

1010
enqueue(item) {
11-
this.#items.add(item)
11+
this.#items.push(item)
1212

1313
this.#itemsCount++
1414
}
@@ -48,4 +48,4 @@ class FIFOQueue {
4848
}
4949
}
5050

51-
export default FIFOQueue
51+
module.exports = FIFOQueue
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const assert = require("assert")
2+
const FIFOQueue = require("./FIFOQueue.js")
3+
4+
const fifoQueue = new FIFOQueue()
5+
6+
fifoQueue.enqueue(1)
7+
fifoQueue.enqueue(2)
8+
9+
assert.deepEqual(fifoQueue.isEmpty(), false)
10+
assert.deepEqual(fifoQueue.size(), 2)
11+
assert.deepEqual(fifoQueue.dequeue(), 1)
12+
assert.deepEqual(fifoQueue.dequeue(), 2)
13+
assert.deepEqual(fifoQueue.isEmpty(), true)
Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import PushdownStack from "./PushdownStack"
2-
3-
class FixedCapacityStack extends PushdownStack {
1+
class FixedCapacityStack {
42
#maxCapacity
3+
#items
4+
#itemsCount
55

66
constructor(maxCapacity) {
7-
super()
8-
97
this.#maxCapacity = maxCapacity
108

11-
this.items = new Array(this.#maxCapacity)
9+
this.#itemsCount = 0
10+
11+
this.#items = new Array(this.#maxCapacity)
1212
}
1313

1414
push(item) {
@@ -20,16 +20,50 @@ class FixedCapacityStack extends PushdownStack {
2020
)
2121
}
2222

23-
this.#items.add(item)
23+
this.#items.push(item)
2424

2525
this.#itemsCount++
2626
}
2727

28+
pop() {
29+
const isEmpty = this.isEmpty()
30+
31+
if (isEmpty) {
32+
throw new Error("This stack is empty!")
33+
}
34+
35+
const leastRecentItem = this.#items.pop()
36+
37+
this.#itemsCount--
38+
39+
return leastRecentItem
40+
}
41+
42+
size() {
43+
const size = this.#itemsCount
44+
45+
return size
46+
}
47+
48+
isEmpty() {
49+
const isEmpty = this.#itemsCount === 0
50+
51+
return isEmpty
52+
}
53+
2854
isFull() {
29-
const isFull = this.#maxCapacity >= this.size()
55+
const isFull = this.size() >= this.#maxCapacity
3056

3157
return isFull
3258
}
59+
60+
*[Symbol.iterator]() {
61+
for(let i = 0; i < this.size(); i++) {
62+
const item = this.pop()
63+
64+
yield item
65+
}
66+
}
3367
}
3468

35-
export default FixedCapacityStack
69+
module.exports = FixedCapacityStack
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const assert = require("assert")
2+
const FixedCapacityStack = require("./FixedCapacityStack.js")
3+
4+
const fixedCapacityStack = new FixedCapacityStack(2)
5+
6+
fixedCapacityStack.push(1)
7+
fixedCapacityStack.push(2)
8+
9+
assert.deepEqual(fixedCapacityStack.isEmpty(), false)
10+
assert.deepEqual(fixedCapacityStack.isFull(), true)
11+
assert.deepEqual(fixedCapacityStack.size(), 2)
12+
assert.throws(() => fixedCapacityStack.push(3))
13+
assert.deepEqual(fixedCapacityStack.pop(), 2)
14+
assert.deepEqual(fixedCapacityStack.pop(), 1)
15+
assert.throws(() => fixedCapacityStack.pop())
16+
assert.deepEqual(fixedCapacityStack.isEmpty(), true)

algorithms/fundamentals/stacks/PushdownStack.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class PushdownStack {
88
}
99

1010
push(item) {
11-
this.#items.add(item)
11+
this.#items.push(item)
1212

1313
this.#itemsCount++
1414
}
@@ -46,10 +46,6 @@ class PushdownStack {
4646
yield item
4747
}
4848
}
49-
50-
set items(items) {
51-
this.#items = items
52-
}
5349
}
5450

55-
export default PushdownStack
51+
module.exports = PushdownStack
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const assert = require("assert")
2+
const PushdownStack = require("./PushdownStack.js")
3+
4+
const pushdownStack = new PushdownStack()
5+
6+
pushdownStack.push(1)
7+
pushdownStack.push(2)
8+
9+
assert.deepEqual(pushdownStack.isEmpty(), false)
10+
assert.deepEqual(pushdownStack.size(), 2)
11+
assert.deepEqual(pushdownStack.pop(), 2)
12+
assert.deepEqual(pushdownStack.pop(), 1)
13+
assert.throws(() => pushdownStack.pop())
14+
assert.deepEqual(pushdownStack.isEmpty(), true)

0 commit comments

Comments
 (0)