monoidlaws

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 12, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package monoidlaws provides reusable test helpers that verify a monoid.Monoid[V] satisfies the algebraic laws (associativity and identity) required for safe use in Murmur pipelines.

Implementing a custom monoid? Wire up a sample generator and call:

monoidlaws.TestMonoid(t, m, gen, monoidlaws.WithSamples(64))

where `gen` is a func(int) V that returns the i-th sample. Optionally pass WithEqual to override the default equality check (useful for monoids whose internal representation has don't-care bits — encoded sketches, etc).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TestMonoid

func TestMonoid[V any](t *testing.T, m monoid.Monoid[V], gen func(i int) V, opts ...Option[V])

TestMonoid asserts the three monoid laws hold for m given a sample generator:

  1. Left identity: Combine(Identity(), x) == x
  2. Right identity: Combine(x, Identity()) == x
  3. Associativity: Combine(Combine(a, b), c) == Combine(a, Combine(b, c))

gen(i) returns the i-th sample; the helper draws cfg.samples samples, then exercises identity at each one and associativity over a representative subset of triples (cubic blow-up is the reason samples defaults to 32).

Failures are reported via t.Errorf with the offending input, so a single broken monoid produces a focused diagnostic rather than a wall of "FAIL".

Types

type Option

type Option[V any] func(*config[V])

Option configures TestMonoid.

func WithEqual

func WithEqual[V any](eq func(a, b V) bool) Option[V]

WithEqual overrides the equality check. Default is reflect.DeepEqual. Use this for monoids whose Combine result is not bitwise-stable (e.g. floating point) — supply a tolerance-based comparator.

func WithSamples

func WithSamples[V any](n int) Option[V]

WithSamples sets how many sample values are drawn from the generator. Default 32. Bigger values explore more triples for the associativity check; cubic in samples.

Jump to

Keyboard shortcuts

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