// Copyright (c) 2026 Petr BalvĂ­n (https://petrbalvin.org) // SPDX-License-Identifier: BSD-3-Clause package verify import ( "testing" ) func TestFuzzResultString(t *testing.T) { t.Run("ok", func(t *testing.T) { r := FuzzResult{Func: "add", Iterations: 100, Matches: 100} s := r.String() if s != "add: 100/100 iterations match" { t.Errorf("String() = %q", s) } }) t.Run("mismatch", func(t *testing.T) { r := FuzzResult{Func: "mul", Iterations: 100, Matches: 95, Mismatches: 5, FirstFail: "iter 23"} s := r.String() if s != "mul: 95/100 match, 5 MISMATCH: iter 23" { t.Errorf("String() = %q", s) } }) t.Run("crash", func(t *testing.T) { r := FuzzResult{Func: "dec", Iterations: 100, Matches: 99, Mismatches: 1, FirstFail: "SIGSEGV", CrashInput: []byte{0x01, 0x02}} s := r.String() if s != "dec: 99/100 match, 1 MISMATCH: SIGSEGV\n input: 0102" { t.Errorf("String() = %q", s) } }) } func TestArrayLen(t *testing.T) { tests := []struct { typ string want int }{ {"*[32]uint16", 32}, {"*[16]int32", 16}, {"bad", 1}, {"*[]", 1}, {"*[0x]", 1}, } for _, tt := range tests { if got := arrayLen(tt.typ); got != tt.want { t.Errorf("arrayLen(%q) = %d, want %d", tt.typ, got, tt.want) } } } func TestElemSizeFor(t *testing.T) { tests := []struct { typ string want int }{ {"[]byte", 1}, {"[]uint8", 1}, {"[]int8", 1}, {"[]uint16", 2}, {"[]int16", 2}, {"[]uint32", 4}, {"[]int32", 4}, {"[]float32", 4}, {"[]uint64", 8}, {"[]int64", 8}, {"[]float64", 8}, {"[]unknown", 8}, } for _, tt := range tests { if got := elemSizeFor(tt.typ); got != tt.want { t.Errorf("elemSizeFor(%q) = %d, want %d", tt.typ, got, tt.want) } } } func TestEqualBytes(t *testing.T) { if !equalBytes([]byte{1, 2, 3}, []byte{1, 2, 3}) { t.Error("expected equal") } if equalBytes([]byte{1, 2}, []byte{1, 2, 3}) { t.Error("different length: expected not equal") } if equalBytes([]byte{1, 2, 3}, []byte{1, 2, 4}) { t.Error("different content: expected not equal") } } func TestParamsSize(t *testing.T) { sig := funcSig{ name: "test", params: []param{{name: "a", typ: "[]byte"}, {name: "b", typ: "int"}}, results: []param{{name: "n", typ: "int"}}, } if got := paramsSize(sig); got != 32 { t.Errorf("paramsSize = %d, want 32 (24 for slice + 8 for int)", got) } } func TestBlockCount(t *testing.T) { k := loadBasic(t) n, err := k.BlockCount("sum") if err != nil { t.Fatalf("BlockCount(sum): %v", err) } if n < 2 { t.Errorf("sum: expected at least 2 blocks, got %d", n) } } func TestFillBuffer(t *testing.T) { t.Run("zero", func(t *testing.T) { // fillBuffer("zero") is a no-op; relies on make already zeroing. buf := make([]byte, 16) fillBuffer(buf, "zero") for _, b := range buf { if b != 0 { t.Error("zero pattern: make should produce zeroed buffer") break } } }) t.Run("ones", func(t *testing.T) { buf := make([]byte, 16) fillBuffer(buf, "ones") for _, b := range buf { if b != 0xFF { t.Error("ones pattern should fill with 0xFF") break } } }) t.Run("seq", func(t *testing.T) { buf := make([]byte, 256) fillBuffer(buf, "seq") for i, b := range buf { if b != byte(i) { t.Errorf("seq[%d] = %d, want %d", i, b, i) break } } }) t.Run("hex", func(t *testing.T) { buf := make([]byte, 6) fillBuffer(buf, "deadbeef") want := []byte{0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD} for i, b := range buf { if b != want[i] { t.Errorf("hex[%d] = %02x, want %02x", i, b, want[i]) break } } }) } func TestFuzzFuncChecked(t *testing.T) { k := loadBasic(t) sig := funcSig{ name: "sum", params: []param{{name: "data", typ: "[]int64"}}, results: []param{{name: "r", typ: "int64"}}, } result := k.FuzzFuncChecked("sum", sig, 10, 0) if !result.OK() { t.Errorf("FuzzFuncChecked(sum): %s", result) } // Test with non-existent function; should report failure. result = k.FuzzFuncChecked("nope", sig, 10, 0) if result.OK() { t.Error("FuzzFuncChecked(nope): expected failure") } }