photoprism/pkg/vector/stats_test.go
Michael Mayer 03129c9129 Vector: Reorganize package into topic-based files #4669
Split the catch-all values.go into one file per concept, each mirrored
by its test: distance.go, norm.go, stats.go, product.go, centroid.go,
plus the mean methods folded into mean.go and Copy/Dim/Sum into
vector.go. Remove values.go, values_test.go, and values_more_test.go so
functionality and tests live where developers expect them.

Hoist the two 512-dimensional face embeddings shared by the distance,
norm, and cosine tests into fixtures_test.go, removing the previous
triplication, and decompose the monolithic TestVector into per-concept
tests. Close pre-existing coverage gaps in the integer converters and
the GeometricMean/HarmonicMean method wrappers, bringing the package to
100% statement coverage. Pure code movement; no behavior change.
2026-05-30 09:44:03 +00:00

38 lines
967 B
Go

package vector
import (
"math"
"testing"
"github.com/stretchr/testify/assert"
)
func TestVector_Variance(t *testing.T) {
t.Run("Values", func(t *testing.T) {
assert.InDelta(t, 32.0/7.0, Vector{2, 4, 4, 4, 5, 5, 7, 9}.Variance(), 0.00001)
})
t.Run("Single", func(t *testing.T) {
assert.InDelta(t, 0.0, Vector{5}.Variance(), 0.00001)
})
t.Run("Empty", func(t *testing.T) {
assert.InDelta(t, 0.0, Vector{}.Variance(), 0.00001)
})
}
func TestVector_Sd(t *testing.T) {
assert.InDelta(t, math.Sqrt(32.0/7.0), Vector{2, 4, 4, 4, 5, 5, 7, 9}.Sd(), 0.00001)
assert.InDelta(t, 0.0, Vector{5}.Sd(), 0.00001)
}
func TestCor(t *testing.T) {
t.Run("PerfectPositive", func(t *testing.T) {
r, err := Cor(Vector{1, 2, 3}, Vector{2, 4, 6})
assert.NoError(t, err)
assert.InDelta(t, 1.0, r, 0.00001)
})
t.Run("LengthMismatch", func(t *testing.T) {
r, err := Cor(Vector{1, 2, 3}, Vector{1, 2})
assert.Error(t, err)
assert.True(t, math.IsNaN(r))
})
}