package planet import ( "math" "testing" ) // boxDown is what the two overview heightmaps are made with, and a planet's two sides rarely share an // integer factor, so it has to be right at a ratio that does not divide. func TestBoxDownAveragesAndKeepsTheMean(t *testing.T) { const w, h = 12, 7 src := make([]float32, w*h) sum := 0.0 for i := range src { src[i] = float32(i%5) + float32(i/w) sum += float64(src[i]) } want := sum / float64(len(src)) for _, d := range [][2]int{{6, 7}, {4, 3}, {5, 3}, {12, 7}, {1, 1}} { out := boxDown(src, w, h, d[0], d[1]) if len(out) != d[0]*d[1] { t.Fatalf("%dx%d: got %d values", d[0], d[1], len(out)) } got := 0.0 for _, v := range out { got += float64(v) } got /= float64(len(out)) // Buckets do not all hold the same number of cells at a ratio that does not divide, so the mean of // the means drifts a little; what must not happen is a bucket left empty or a value invented. if math.Abs(got-want) > 0.35 { t.Errorf("%dx%d: mean %.3f, source mean %.3f", d[0], d[1], got, want) } lo, hi := math.Inf(1), math.Inf(-1) for _, v := range out { lo = math.Min(lo, float64(v)) hi = math.Max(hi, float64(v)) } if lo < 0 || hi > 11 { t.Errorf("%dx%d: range %.2f..%.2f is outside the source's 0..10", d[0], d[1], lo, hi) } } } func TestBoxDownIsIdentityAtTheSameSize(t *testing.T) { src := []float32{1, 2, 3, 4, 5, 6} out := boxDown(src, 3, 2, 3, 2) for i := range src { if out[i] != src[i] { t.Fatalf("cell %d: %v, want %v", i, out[i], src[i]) } } }