-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import Stack from 'Stack'; | ||
|
||
class StackSet { | ||
constructor(stackSize) { | ||
if (typeof stackSize !== 'number' || stackSize < 1) { | ||
throw new Error('stackSize must be at least 1.'); | ||
} | ||
|
||
this.stackSize = stackSize; | ||
} | ||
|
||
stacks = [new Stack()] | ||
|
||
isEmpty = () => !this.size() | ||
|
||
pop = () => { | ||
const last = this.stacks[this.stacks.length - 1]; | ||
const item = last.pop(); | ||
|
||
if (this.stacks.length > 1 && last.size() === 0) { | ||
this.stacks.pop(); | ||
} | ||
|
||
return item; | ||
} | ||
|
||
push = (item) => { | ||
if (this.stacks[this.stacks.length - 1].size() === this.stackSize) { | ||
this.stacks.push(new Stack()); | ||
} | ||
|
||
this.stacks[this.stacks.length - 1].push(item); | ||
} | ||
|
||
size = () => { | ||
let count = this.stacks[this.stacks.length - 1].size(); | ||
|
||
if (this.stacks.length > 1) { | ||
count += (this.stacks.length - 1) * this.stackSize; | ||
} | ||
|
||
return count; | ||
} | ||
} | ||
|
||
export default StackSet; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import StackSet from './StackSet'; | ||
|
||
let stack; | ||
|
||
describe('StackSet', () => { | ||
describe('with stackSize === 1', () => { | ||
beforeEach(() => { | ||
stack = new StackSet(1); | ||
stack.push(1); | ||
stack.push(2); | ||
stack.push(3); | ||
}); | ||
|
||
it('has the correct number of items', () => { | ||
expect(stack.size()).toBe(3); | ||
}); | ||
|
||
it('has the correct stack count', () => { | ||
expect(stack.stacks.length).toBe(3); | ||
}); | ||
|
||
describe('pop()', () => { | ||
it('returns the correct item', () => { | ||
expect(stack.pop()).toBe(3); | ||
expect(stack.pop()).toBe(2); | ||
expect(stack.pop()).toBe(1); | ||
}); | ||
|
||
it('decreases the stack size', () => { | ||
stack.pop(); | ||
expect(stack.size()).toBe(2); | ||
}); | ||
|
||
it('descreases the stack count', () => { | ||
stack.pop(); | ||
expect(stack.stacks.length).toBe(2); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('isEmpty()', () => { | ||
it('returns true bu default', () => { | ||
stack = new StackSet(5); | ||
expect(stack.isEmpty()).toBe(true); | ||
}); | ||
|
||
it('returns false when there are items', () => { | ||
stack.push(4); | ||
expect(stack.isEmpty()).toBe(false); | ||
}); | ||
}); | ||
|
||
const throwCases = [0, -999, '', {}, [], undefined, null]; | ||
|
||
for (let i = 0; i < throwCases.length; i++) { | ||
const input = throwCases[i]; | ||
|
||
it(`throws when initialized with ${JSON.stringify(input)}`, () => { | ||
const throws = () => { | ||
new StackSet(input); | ||
}; | ||
|
||
expect(throws).toThrow(); | ||
}); | ||
} | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default from './StackSet'; |