시리즈: Go
go
71 줄
· 업데이트 2026-04-18
queue_test.go
Go/part2-dsa/ds/queue/queue_test.go
package queue
import (
"reflect"
"testing"
)
func TestQueueBasic(t *testing.T) {
q := New[int](2)
if q.Len() != 0 {
t.Fatalf("len = %d", q.Len())
}
if _, ok := q.Pop(); ok {
t.Fatal("pop on empty should be !ok")
}
for i := 1; i <= 10; i++ {
q.Push(i)
}
if q.Len() != 10 {
t.Fatalf("len = %d", q.Len())
}
if v, _ := q.Peek(); v != 1 {
t.Fatalf("peek = %d", v)
}
want := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
if got := q.Snapshot(); !reflect.DeepEqual(got, want) {
t.Fatalf("snapshot = %v", got)
}
for _, w := range want {
got, ok := q.Pop()
if !ok || got != w {
t.Fatalf("pop = %d %v, want %d", got, ok, w)
}
}
if q.Len() != 0 {
t.Fatalf("expected empty, got %d", q.Len())
}
}
func TestQueueWrapAround(t *testing.T) {
q := New[int](4)
for i := 0; i < 3; i++ {
q.Push(i)
}
q.Pop()
q.Pop()
for i := 3; i < 7; i++ {
q.Push(i)
}
want := []int{2, 3, 4, 5, 6}
if got := q.Snapshot(); !reflect.DeepEqual(got, want) {
t.Fatalf("snapshot = %v, want %v", got, want)
}
}
func TestQueueGrowsLinear(t *testing.T) {
q := New[int](4)
for i := 0; i < 100; i++ {
q.Push(i)
}
for i := 0; i < 100; i++ {
got, ok := q.Pop()
if !ok || got != i {
t.Fatalf("at %d got %d %v", i, got, ok)
}
}
}
관련 글
Go
go
업데이트 2026-04-18
main.go
main.go — go source code from the Go learning materials (Go/Baasid/cmd/hashpw/main.go).
글 읽기 →
Go
go
업데이트 2026-04-18
main.go
main.go — go source code from the Go learning materials (Go/Baasid/cmd/server/main.go).
글 읽기 →
Go
go
업데이트 2026-04-18
docs.go
docs.go — go source code from the Go learning materials (Go/Baasid/docs/docs.go).
글 읽기 →
Go
go
업데이트 2026-04-18
jwt.go
jwt.go — go source code from the Go learning materials (Go/Baasid/internal/auth/jwt.go).
글 읽기 →
Go
go
업데이트 2026-04-18
password.go
password.go — go source code from the Go learning materials (Go/Baasid/internal/auth/password.go).
글 읽기 →
Go
go
업데이트 2026-04-18
password_test.go
password_test.go — go source code from the Go learning materials (Go/Baasid/internal/auth/password_test.go).
글 읽기 →