github.com/iainanderson83/datastructures@v0.0.4-0.20191103204413-889e20b53bcf/stack/stack_test.go (about) 1 package stack 2 3 import "testing" 4 5 func TestArrStack(t *testing.T) { 6 tests := map[string]struct { 7 in string 8 balanced bool 9 }{ 10 "SingleType": { 11 "(()())", 12 true, 13 }, 14 "Unbalanced": { 15 "(((())(", 16 false, 17 }, 18 "Unbalanced2": { 19 "((())))", 20 false, 21 }, 22 "MultipleTypes": { 23 "({[]})", 24 true, 25 }, 26 "MultipleTypesUnbalanced": { 27 "((({}]))", 28 false, 29 }, 30 "Empty": { 31 "", 32 true, 33 }, 34 "UnknownChars": { 35 "this (is a) sentence {kinda}", 36 true, 37 }, 38 } 39 40 for name, test := range tests { 41 t.Run(name, func(t *testing.T) { 42 var s ArrStack 43 for _, c := range test.in { 44 switch c { 45 case '(', '{', '[': 46 s.Push(c) 47 case ')', '}', ']': 48 v := s.Pop() 49 if v == nil { 50 if !test.balanced { 51 return 52 } 53 t.Fatal("expected balanced") 54 } 55 56 switch v { 57 case ')': 58 if c != '(' && test.balanced { 59 t.Fatal("expected balanced") 60 } 61 case '}': 62 if c != '{' && test.balanced { 63 t.Fatal("expected balanced") 64 } 65 case ']': 66 if c != '[' && test.balanced { 67 t.Fatal("expected balanced") 68 } 69 default: 70 } 71 default: 72 } 73 } 74 75 if s.Peek() != nil && test.balanced { 76 t.Fatal("expected balanced") 77 } 78 }) 79 } 80 }