Series: Go
go
64 lines
· Updated 2026-04-18
stack_test.go
Go/part2-dsa/ds/stack/stack_test.go
package stack
import "testing"
func TestStackBasic(t *testing.T) {
s := New[int](0)
if s.Len() != 0 {
t.Fatalf("len = %d", s.Len())
}
if _, ok := s.Pop(); ok {
t.Fatal("pop on empty should be !ok")
}
s.Push(1)
s.Push(2)
s.Push(3)
if s.Len() != 3 {
t.Fatalf("len = %d", s.Len())
}
if v, _ := s.Peek(); v != 3 {
t.Fatalf("peek = %d", v)
}
for _, want := range []int{3, 2, 1} {
got, ok := s.Pop()
if !ok || got != want {
t.Fatalf("pop = %d %v, want %d", got, ok, want)
}
}
if s.Len() != 0 {
t.Fatalf("expected empty, got len=%d", s.Len())
}
}
func TestStackZeroValue(t *testing.T) {
var s Stack[string]
s.Push("a")
s.Push("b")
if v, _ := s.Pop(); v != "b" {
t.Errorf("got %q", v)
}
}
func TestBalancedBrackets(t *testing.T) {
cases := []struct {
in string
want bool
}{
{"", true},
{"()", true},
{"()[]{}", true},
{"([{}])", true},
{"(", false},
{")", false},
{"([)]", false},
{"hello (world [today])", true},
{"a(b]c", false},
}
for _, tc := range cases {
if got := BalancedBrackets(tc.in); got != tc.want {
t.Errorf("BalancedBrackets(%q) = %v, want %v", tc.in, got, tc.want)
}
}
}
Related articles
Go
go
Updated 2026-04-18
main.go
main.go — go source code from the Go learning materials (Go/Baasid/cmd/hashpw/main.go).
Read article →
Go
go
Updated 2026-04-18
main.go
main.go — go source code from the Go learning materials (Go/Baasid/cmd/server/main.go).
Read article →
Go
go
Updated 2026-04-18
docs.go
docs.go — go source code from the Go learning materials (Go/Baasid/docs/docs.go).
Read article →
Go
go
Updated 2026-04-18
jwt.go
jwt.go — go source code from the Go learning materials (Go/Baasid/internal/auth/jwt.go).
Read article →
Go
go
Updated 2026-04-18
password.go
password.go — go source code from the Go learning materials (Go/Baasid/internal/auth/password.go).
Read article →
Go
go
Updated 2026-04-18
password_test.go
password_test.go — go source code from the Go learning materials (Go/Baasid/internal/auth/password_test.go).
Read article →