Fix example function names, stabilize tests, and relax floating-point comparisons

This commit is contained in:
Snider 2025-11-03 19:07:08 +00:00
parent 6247c3de83
commit 4ccccc97f5
3 changed files with 13 additions and 8 deletions

View file

@ -9,7 +9,11 @@ The format is based on Keep a Changelog and this project adheres to Semantic Ver
- pkg.go.dev Examples: `ExampleNewKDTreeFromDim_Insert`, `ExampleKDTree_TiesBehavior`, `ExampleKDTree_Radius_none`.
- Lint: enable `errcheck` in `.golangci.yml` with test-file exclusion to reduce noise.
- CI: enable module cache in `actions/setup-go` to speed up workflows.
- Docs: README cross-link to Performance page and add a concise “Choosing a metric” tip section.
### Fixed
- go vet failures in examples due to misnamed `Example*` functions; renamed to avoid referencing non-existent methods and identifiers.
- Stabilized `ExampleKDTree_Nearest` to avoid a tie case; adjusted query and expected output.
- Relaxed floating-point equality in `TestWeightedCosineDistance_Basics` to use an epsilon, avoiding spurious failures on some toolchains.
## [0.3.0] - 2025-11-03
### Added

View file

@ -40,9 +40,9 @@ func ExampleKDTree_Nearest() {
{ID: "y", Coords: []float64{2, 0}, Value: 2},
}
tr, _ := poindexter.NewKDTree(pts, poindexter.WithMetric(poindexter.EuclideanDistance{}))
p, d, ok := tr.Nearest([]float64{1, 0})
p, d, ok := tr.Nearest([]float64{1.4, 0})
fmt.Printf("ok=%v id=%s d=%.1f", ok, p.ID, d)
// Output: ok=true id=y d=1.0
// Output: ok=true id=y d=0.6
}
func ExampleKDTree_KNearest() {
@ -69,7 +69,7 @@ func ExampleKDTree_Radius() {
// Output: 2 a b
}
func ExampleKDTree_InsertDeleteByID() {
func ExampleKDTree_DeleteByID() {
pts := []poindexter.KDPoint[string]{
{ID: "A", Coords: []float64{0}, Value: "a"},
}
@ -204,7 +204,7 @@ func ExampleBuild4DWithStats() {
// Output: 4
}
func ExampleNewKDTreeFromDim_Insert() {
func ExampleNewKDTreeFromDim() {
// Construct an empty 2D tree, insert a point, then query.
tr, _ := poindexter.NewKDTreeFromDim[string](2)
tr.Insert(poindexter.KDPoint[string]{ID: "A", Coords: []float64{0.1, 0.2}, Value: "alpha"})
@ -213,7 +213,7 @@ func ExampleNewKDTreeFromDim_Insert() {
// Output: ok=true id=A dim=2 len=1
}
func ExampleKDTree_TiesBehavior() {
func Example_tiesBehavior() {
// Two points equidistant from the query; tie ordering is arbitrary,
// but distances are equal.
pts := []poindexter.KDPoint[int]{

View file

@ -47,8 +47,9 @@ func TestWeightedCosineDistance_Basics(t *testing.T) {
a := []float64{1, 0}
b := []float64{1, 0}
d := w.Distance(a, b)
if d != 0 {
t.Fatalf("expected 0, got %v", d)
// allow tiny floating-point noise
if d > 1e-12 {
t.Fatalf("expected ~0, got %v", d)
}
// orthogonal remains ~1 regardless of weights for these axes
b = []float64{0, 3}