Various improvements.

This commit is contained in:
2026-02-21 00:34:57 +01:00
parent 8670c509e8
commit 09a1c8dd26
6 changed files with 160 additions and 11 deletions

View File

@@ -2,6 +2,7 @@ package serial_test
import (
"errors"
"fmt"
"testing"
"testing/quick"
@@ -243,7 +244,30 @@ func TestRandomCodeFailsWhenAllIndicesInUse(t *testing.T) {
_, _, err := serial.RandomCode(func(candidate uint32) bool {
return true
})
if !errors.Is(err, serial.ErrRandomGenerationFailed) {
t.Fatalf("expected random generation failed error, got: %v", err)
if !errors.Is(err, serial.ErrNoAvailableIndex) {
t.Fatalf("expected no available index error, got: %v", err)
}
}
func TestRandomCodeWithOptionsMaxAttempts(t *testing.T) {
_, _, err := serial.RandomCodeWithOptions(serial.RandomCodeOptions{
MaxAttempts: 1,
IsInUse: func(candidate uint32) bool {
return true
},
})
if !errors.Is(err, serial.ErrNoAvailableIndex) {
t.Fatalf("expected no available index error, got: %v", err)
}
}
func TestRandomCodeWithOptionsRandomSourceFailure(t *testing.T) {
_, _, err := serial.RandomCodeWithOptions(serial.RandomCodeOptions{
RandomIndex: func(max uint32) (uint32, error) {
return 0, fmt.Errorf("rng down")
},
})
if !errors.Is(err, serial.ErrRandomSourceFailed) {
t.Fatalf("expected random source failed error, got: %v", err)
}
}