clone

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

feat/elo-difficulty
puzzletea / games / shikaku / benchmark_test.go
benchmark_test.go 2.1K
1package shikaku23import (4 "math/rand/v2"5 "testing"6)78func skipBenchmarkInShortMode(b *testing.B) {9 b.Helper()10 if testing.Short() {11 b.Skip("skipping benchmark in short mode")12 }13}1415func benchmarkShikakuModes() []ShikakuMode {16 return []ShikakuMode{17 NewMode("Mini 5x5", "benchmark", 5, 5, 5),18 NewMode("Easy 7x7", "benchmark", 7, 7, 8),19 NewMode("Medium 8x8", "benchmark", 8, 8, 12),20 NewMode("Hard 10x10", "benchmark", 10, 10, 15),21 NewMode("Expert 12x12", "benchmark", 12, 12, 20),22 }23}2425func makeUncoveredGenerationGrid(width, height int) [][]int {26 g := make([][]int, height)27 for y := range height {28 g[y] = make([]int, width)29 for x := range width {30 g[y][x] = -131 }32 }33 return g34}3536func BenchmarkGeneratePuzzleBySize(b *testing.B) {37 skipBenchmarkInShortMode(b)3839 for modeIndex, mode := range benchmarkShikakuModes() {40 b.Run(mode.Title(), func(b *testing.B) {41 b.ReportAllocs()42 for i := 0; i < b.N; i++ {43 var (44 puzzle Puzzle45 err error46 )47 for attempt := range 3 {48 rng := rand.New(rand.NewPCG(uint64(i+1), uint64(modeIndex*10+attempt+1)))49 puzzle, err = GeneratePuzzleSeeded(mode.Width, mode.Height, mode.MaxRectSize, rng)50 if err == nil {51 break52 }53 }54 if err != nil {55 b.Fatalf("GeneratePuzzleSeeded failed: %v", err)56 }57 if puzzle.Width != mode.Width || puzzle.Height != mode.Height {58 b.Fatalf("wrong dimensions: got %dx%d, want %dx%d", puzzle.Width, puzzle.Height, mode.Width, mode.Height)59 }60 }61 })62 }63}6465func BenchmarkPartitionGrid(b *testing.B) {66 skipBenchmarkInShortMode(b)6768 for modeIndex, mode := range benchmarkShikakuModes() {69 b.Run(mode.Title(), func(b *testing.B) {70 b.ReportAllocs()71 for i := 0; i < b.N; i++ {72 rng := rand.New(rand.NewPCG(uint64(i+1), uint64(modeIndex+100)))73 grid := makeUncoveredGenerationGrid(mode.Width, mode.Height)74 partitions := make([]genRect, 0, mode.Width*mode.Height/2)75 ok := partitionGrid(grid, mode.Width, mode.Height, mode.MaxRectSize, &partitions, rng)76 if !ok {77 b.Fatalf("partitionGrid failed at iteration %d", i)78 }79 }80 })81 }82}