@@ -5,21 +5,114 @@ describe('Stacks: Multistack', function () {
55 var multiStack ;
66
77 beforeEach ( function ( ) {
8- multiStack = new MultiStack ( ) ;
8+ multiStack = new MultiStack ( 6 ) ;
99 } ) ;
1010
11- it ( 'should add 300 hundred element to each stack and retrive the data' , function ( ) {
12- for ( let stackIndex = 0 ; stackIndex < 3 ; stackIndex ++ ) {
13- for ( let data = 0 ; data < 300 ; data ++ ) {
14- multiStack . push ( stackIndex , data ) ;
15- }
16- }
17-
18- for ( let data = 299 ; data > - 1 ; data -- ) {
19- for ( let stackIndex = 0 ; stackIndex < 3 ; stackIndex ++ ) {
20- var actual = multiStack . pop ( stackIndex ) ;
21- expect ( actual ) . to . equal ( data , `Expected stack #${ stackIndex } data to be ${ data } but was ${ actual } ` ) ;
22- }
23- }
11+ describe ( '.push' , function ( ) {
12+ it ( 'should add an element on stack 0' , function ( ) {
13+ multiStack . push ( 0 , 'a' ) ;
14+ expect ( multiStack . array ) . to . eql ( [ 'a' ] ) ;
15+ } ) ;
16+
17+ it ( 'should add an element on stack 0, 1, 2' , function ( ) {
18+ multiStack . push ( 0 , 'a' ) ;
19+ multiStack . push ( 1 , 'b' ) ;
20+ multiStack . push ( 2 , 'c' ) ;
21+
22+ expect ( multiStack . array [ 0 ] ) . to . equal ( 'a' ) ;
23+ expect ( multiStack . array [ 1 ] ) . to . be . an ( 'undefined' ) ;
24+ expect ( multiStack . array [ 2 ] ) . to . equal ( 'b' ) ;
25+ expect ( multiStack . array [ 3 ] ) . to . be . an ( 'undefined' ) ;
26+ expect ( multiStack . array [ 4 ] ) . to . equal ( 'c' ) ;
27+ } ) ;
28+
29+ it ( 'should overflow when adding too much elements' , function ( ) {
30+ multiStack . push ( 0 , 'a' ) ;
31+ multiStack . push ( 0 , 'b' ) ;
32+ function overflow ( ) { multiStack . push ( 0 , 'c' ) ; }
33+ expect ( overflow ) . to . throw ( Error ) ;
34+ expect ( overflow ) . to . throw ( / o v e r f l o w / gi) ;
35+ } ) ;
36+ } ) ;
37+
38+ describe ( '.isEmpty' , function ( ) {
39+ it ( 'should start empty' , function ( ) {
40+ expect ( multiStack . isEmpty ( 1 ) ) . to . equal ( true ) ;
41+ } ) ;
42+
43+ it ( 'should not be empty after adding an element' , function ( ) {
44+ multiStack . push ( 1 , 'b' ) ;
45+ expect ( multiStack . isEmpty ( 1 ) ) . to . equal ( false ) ;
46+ } ) ;
47+ } ) ;
48+
49+ describe ( '.getStack' , function ( ) {
50+ it ( 'should throw error if there is no index' , function ( ) {
51+ expect ( multiStack . getStack ) . to . throw ( Error ) ;
52+ } ) ;
53+
54+ it ( 'should throw error if index is 3' , function ( ) {
55+ function actual ( ) { multiStack . getStack ( 3 ) }
56+ expect ( actual ) . to . throw ( Error ) ;
57+ } ) ;
58+
59+ it ( 'should NOT throw error if index is 2' , function ( ) {
60+ function actual ( ) { multiStack . getStack ( 2 ) }
61+ expect ( actual ) . not . to . throw ( Error ) ;
62+ } ) ;
63+
64+ it ( 'should NOT throw error if index is 0' , function ( ) {
65+ function actual ( ) { multiStack . getStack ( 0 ) }
66+ expect ( actual ) . not . to . throw ( Error ) ;
67+ } ) ;
68+
69+ it ( 'should throw error if index is -1' , function ( ) {
70+ function actual ( ) { multiStack . getStack ( - 1 ) }
71+ expect ( actual ) . to . throw ( Error ) ;
72+ } ) ;
73+ } ) ;
74+
75+ describe ( '.pop' , function ( ) {
76+ it ( 'should pop last element' , function ( ) {
77+ multiStack . push ( 0 , 'a' ) ;
78+ expect ( multiStack . pop ( 0 ) ) . to . equal ( 'a' ) ;
79+ } ) ;
80+
81+ it ( 'should return elements on FIFO' , function ( ) {
82+ multiStack . push ( 0 , 'a' ) ;
83+ multiStack . push ( 0 , 'b' ) ;
84+ expect ( multiStack . pop ( 0 ) ) . to . equal ( 'b' ) ;
85+ expect ( multiStack . pop ( 0 ) ) . to . equal ( 'a' ) ;
86+ } ) ;
87+
88+ it ( 'should not have a negative index on empty' , function ( ) {
89+ expect ( multiStack . pop ( 0 ) ) . to . equal ( undefined ) ;
90+ expect ( multiStack . getStack ( 0 ) . current ) . to . equal ( 0 ) ;
91+ } ) ;
92+
93+ it ( 'should return undefined when pop more than number of elements' , function ( ) {
94+ multiStack . push ( 0 , 'a' ) ;
95+ expect ( multiStack . pop ( 0 ) ) . to . equal ( 'a' ) ;
96+ expect ( multiStack . pop ( 0 ) ) . to . equal ( undefined ) ;
97+ expect ( multiStack . getStack ( 0 ) . current ) . to . equal ( 0 ) ;
98+ } ) ;
99+
100+ it ( 'should pop from multiple stacks' , function ( ) {
101+ multiStack . push ( 0 , 'a' ) ;
102+ multiStack . push ( 0 , 'b' ) ;
103+ multiStack . push ( 1 , 'a' ) ;
104+ multiStack . push ( 1 , 'b' ) ;
105+ expect ( multiStack . pop ( 0 ) ) . to . equal ( 'b' ) ;
106+ expect ( multiStack . pop ( 0 ) ) . to . equal ( 'a' ) ;
107+ expect ( multiStack . pop ( 1 ) ) . to . equal ( 'b' ) ;
108+ expect ( multiStack . pop ( 1 ) ) . to . equal ( 'a' ) ;
109+ } ) ;
110+ } ) ;
111+
112+ describe ( '.peek' , function ( ) {
113+ it ( 'should return last element' , function ( ) {
114+ multiStack . push ( 0 , 'a' ) ;
115+ expect ( multiStack . peek ( 0 ) ) . to . equal ( 'a' ) ;
116+ } ) ;
24117 } ) ;
25118} ) ;
0 commit comments