clone

fifteen puzzle games for the terminal, with daily puzzles, weekly ladders and xp, built on bubble tea

refactor/root-tui-model
puzzletea / games / takuzu / benchmark_test.go
benchmark_test.go 1.7K
1package takuzu23import (4 "fmt"5 "math/rand/v2"6 "testing"7)89func skipBenchmarkInShortMode(b *testing.B) {10 b.Helper()11 if testing.Short() {12 b.Skip("skipping benchmark in short mode")13 }14}1516type takuzuBenchMode struct {17 size int18 prefilled float6419}2021var takuzuBenchModes = []takuzuBenchMode{22 {size: 8, prefilled: 0.40},23 {size: 10, prefilled: 0.32},24 {size: 12, prefilled: 0.30},25 {size: 14, prefilled: 0.28},26}2728func BenchmarkGeneratePuzzleSizes(b *testing.B) {29 skipBenchmarkInShortMode(b)3031 for _, mode := range takuzuBenchModes {32 name := fmt.Sprintf("%dx%d", mode.size, mode.size)33 b.Run(name, func(b *testing.B) {34 rng := rand.New(rand.NewPCG(uint64(mode.size), uint64(mode.size*17+3)))35 b.ReportAllocs()36 b.ResetTimer()3738 for range b.N {39 complete := generateCompleteSeeded(mode.size, rng)40 puzzle, provided := generatePuzzleSeeded(complete, mode.size, mode.prefilled, rng)41 if len(puzzle) != mode.size || len(provided) != mode.size {42 b.Fatalf("unexpected puzzle dimensions for size %d", mode.size)43 }44 }45 })46 }47}4849func BenchmarkCountSolutionsSizes(b *testing.B) {50 skipBenchmarkInShortMode(b)5152 for _, mode := range takuzuBenchModes {53 name := fmt.Sprintf("%dx%d", mode.size, mode.size)54 b.Run(name, func(b *testing.B) {55 rng := rand.New(rand.NewPCG(uint64(mode.size*101), uint64(mode.size*37+11)))56 complete := generateCompleteSeeded(mode.size, rng)57 puzzle, _ := generatePuzzleSeeded(complete, mode.size, mode.prefilled, rng)5859 b.ReportAllocs()60 b.ResetTimer()6162 for range b.N {63 count := countSolutions(puzzle, mode.size, 2)64 if count != 1 {65 b.Fatalf("countSolutions returned %d, want 1", count)66 }67 }68 })69 }70}