testutils

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 6, 2026 License: LGPL-3.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Seed uint64 //nolint:gochecknoglobals // intentionally global for test reproducibility

Functions

func NewRand

func NewRand(t *testing.T) *rand.Rand

func Pick

func Pick[T any](g *Gen, slice []T) T

Pick returns an element from the slice.

func RandMapKey

func RandMapKey[K comparable, V any](r *rand.Rand, m map[K]V) K

RandMapKey returns a random key from a map. Panics if the map is empty.

func RandWeightedOp

func RandWeightedOp[T WeightedOp](r *rand.Rand, ops []T) T

RandWeightedOp returns a random operation from a slice, using each op's value as its weight.

func Shuffle

func Shuffle[T any](g *Gen, slice []T)

Shuffle shuffles the slice in-place, exhaustively generating all permutations.

Types

type AnotherEvent

type AnotherEvent struct {
	Data string
}

func (AnotherEvent) Name

func (AnotherEvent) Name() string

type ComponentA

type ComponentA struct {
	X, Y, Z float64
}

func (ComponentA) Name

func (ComponentA) Name() string

type ComponentB

type ComponentB struct {
	ID      uint64
	Label   string
	Enabled bool
}

func (ComponentB) Name

func (ComponentB) Name() string

type ComponentC

type ComponentC struct {
	Values  [8]int32
	Counter uint16
}

func (ComponentC) Name

func (ComponentC) Name() string

type Gen

type Gen struct {
	// contains filtered or unexported fields
}

Gen is an exhaustive generator that iterates through all possible combinations of values.

The implementation is tricky, refer to the post for details.

On each iteration of `while (!g.done())` loop, Gen generates a sequence of numbers. Internally, it remembers this sequence together with bounds the user requested:

value: 3 1 4 4 bound: 5 4 4 4

To advance to the next iteration, Gen finds the smallest sequence of values which is larger than the current one, but still satisfies all the bounds. "Smallest" means that Gen tries to increment the rightmost number.

In the above example, the last two "4"s already match the bound, so we can't increment them. However, we can increment the second number, "1", to get 3 2 4 4. This isn’t the smallest sequence though, 3 2 0 0 is be smaller. So, after incrementing the rightmost number possible, we zero the rest.

See: <https://matklad.github.io/2021/11/07/generate-all-the-things.html>

func NewGen

func NewGen() *Gen

NewGen creates a new exhaustive generator.

func (*Gen) Bool

func (g *Gen) Bool() bool

Bool returns an exhaustive boolean value.

func (*Gen) Done

func (g *Gen) Done() bool

Done returns true when all combinations have been exhausted.

func (*Gen) Index

func (g *Gen) Index(length int) int

Index returns a valid index into a slice of the given length.

func (*Gen) Intn

func (g *Gen) Intn(bound int) int

Intn returns an int in range [0, bound] (inclusive).

func (*Gen) Range

func (g *Gen) Range(minVal, maxVal int) int

Range returns an int in range [minVal, maxVal] (inclusive).

type SimpleCommand

type SimpleCommand struct {
	Value int
}

func (SimpleCommand) Name

func (SimpleCommand) Name() string

type SimpleComponent

type SimpleComponent struct {
	Value int
}

func (SimpleComponent) Name

func (SimpleComponent) Name() string

type SimpleEvent

type SimpleEvent struct {
	Value int
}

func (SimpleEvent) Name

func (SimpleEvent) Name() string

type SimpleSystemEvent

type SimpleSystemEvent struct {
	Value int
}

func (SimpleSystemEvent) Name

func (SimpleSystemEvent) Name() string

type SystemEventA

type SystemEventA struct {
	X, Y, Z float64
}

func (SystemEventA) Name

func (SystemEventA) Name() string

type SystemEventB

type SystemEventB struct {
	ID      uint64
	Label   string
	Enabled bool
}

func (SystemEventB) Name

func (SystemEventB) Name() string

type SystemEventC

type SystemEventC struct {
	Values  [8]int32
	Counter uint16
}

func (SystemEventC) Name

func (SystemEventC) Name() string

type WeightedOp

type WeightedOp interface {
	~uint8 | ~uint16 | ~uint32 | ~int
}

WeightedOp is a constraint for operation types that use their value as the weight.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL