Poindexter/examples/kdtree_2d_ping_hop/example_test.go
Claude 848a5e8036
Some checks failed
CI / build-test-gonum (push) Failing after 6m4s
Deploy Documentation / deploy (push) Failing after 9s
Tests / test (push) Failing after 3s
Release / Goreleaser publish (push) Failing after 3s
CI / build-test-wasm (push) Failing after 8m41s
chore: migrate module path from github.com to forge.lthn.ai
Move module declaration and all internal imports from
github.com/Snider/Poindexter to forge.lthn.ai/Snider/Poindexter.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-22 21:34:52 +00:00

49 lines
1 KiB
Go

package main
import (
"testing"
poindexter "forge.lthn.ai/Snider/Poindexter"
)
type peer2 struct {
ID string
PingMS float64
Hops float64
}
func TestExample2D(t *testing.T) {
peers := []peer2{
{ID: "A", PingMS: 22, Hops: 3},
{ID: "B", PingMS: 34, Hops: 2},
{ID: "C", PingMS: 15, Hops: 4},
{ID: "D", PingMS: 55, Hops: 1},
{ID: "E", PingMS: 18, Hops: 2},
}
weights := [2]float64{1.0, 1.0}
invert := [2]bool{false, false}
pts, err := poindexter.Build2D(
peers,
func(p peer2) string { return p.ID },
func(p peer2) float64 { return p.PingMS },
func(p peer2) float64 { return p.Hops },
weights, invert,
)
if err != nil {
t.Fatalf("Build2D err: %v", err)
}
tr, err := poindexter.NewKDTree(pts, poindexter.WithMetric(poindexter.ManhattanDistance{}))
if err != nil {
t.Fatalf("NewKDTree err: %v", err)
}
best, d, ok := tr.Nearest([]float64{0, 0.3})
if !ok {
t.Fatalf("no nearest")
}
if best.ID == "" {
t.Fatalf("unexpected empty ID")
}
if d < 0 {
t.Fatalf("negative distance: %v", d)
}
}