1+ const expect = require ( 'chai' ) . expect ;
2+ const MinStack = require ( './min-stack' ) ;
3+
4+ describe ( 'Stacks: MinStack' , function ( ) {
5+ var minstack ;
6+
7+ beforeEach ( function ( ) {
8+ minstack = new MinStack ( ) ;
9+ } ) ;
10+
11+ describe ( '.push' , function ( ) {
12+ it ( 'should add an element to stack' , function ( ) {
13+ minstack . push ( 1 ) ;
14+ expect ( minstack . head . data ) . to . equal ( 1 ) ;
15+ } ) ;
16+
17+ it ( 'should add multiple elements to stack' , function ( ) {
18+ minstack . push ( 1 ) ;
19+ minstack . push ( 2 ) ;
20+ expect ( minstack . head . data ) . to . equal ( 2 ) ;
21+ } ) ;
22+ } ) ;
23+
24+ describe ( '.pop' , function ( ) {
25+ it ( 'should take the last element from the list' , function ( ) {
26+ minstack . push ( 1 ) ;
27+ expect ( minstack . pop ( ) ) . to . equal ( 1 ) ;
28+ } ) ;
29+
30+ it ( 'should remove element FIFO' , function ( ) {
31+ minstack . push ( 1 ) ;
32+ minstack . push ( 2 ) ;
33+ expect ( minstack . pop ( ) ) . to . equal ( 2 ) ;
34+ expect ( minstack . pop ( ) ) . to . equal ( 1 ) ;
35+ } ) ;
36+ } ) ;
37+
38+ describe ( '.min' , function ( ) {
39+ it ( 'should keep track of min' , function ( ) {
40+ minstack . push ( 1 ) ;
41+ expect ( minstack . min ( ) ) . to . equal ( 1 ) ;
42+ } ) ;
43+
44+ it ( 'should return min among other nodes' , function ( ) {
45+ minstack . push ( 10 ) ;
46+ minstack . push ( 15 ) ;
47+ expect ( minstack . min ( ) ) . to . equal ( 10 ) ;
48+ minstack . push ( 1 ) ;
49+ expect ( minstack . min ( ) ) . to . equal ( 1 ) ;
50+ } ) ;
51+
52+ it ( 'should return 2nd min if the min is removed' , function ( ) {
53+ minstack . push ( 10 ) ;
54+ minstack . push ( 1 ) ;
55+ minstack . pop ( ) ; // 1
56+ expect ( minstack . min ( ) ) . to . equal ( 10 ) ;
57+ } ) ;
58+ } ) ;
59+ } ) ;
0 commit comments