mirror of
https://bitbucket.org/s_l_teichmann/mtsatellite
synced 2024-11-08 11:10:27 +01:00
200 lines
4.0 KiB
Go
200 lines
4.0 KiB
Go
// Copyright 2014 by Sascha L. Teichmann
|
|
// Use of this source code is governed by the MIT license
|
|
// that can be found in the LICENSE file.
|
|
|
|
package common
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand"
|
|
"testing"
|
|
)
|
|
|
|
var data = []int16{
|
|
-2045, -1850, -1811, -1629, -1104,
|
|
-967, -725, -646, -329, -212,
|
|
-150, -1, 0, 1, 88, 524, 527, 549,
|
|
1783, 1817, 1826, 2028, 2032}
|
|
|
|
func allData(f func(Coord)) {
|
|
for _, z := range data {
|
|
for _, y := range data {
|
|
for _, x := range data {
|
|
f(Coord{X: x, Y: y, Z: z})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func checkEncodeDecode(
|
|
desc string,
|
|
join KeyJoiner,
|
|
encode KeyEncoder, decode KeyDecoder,
|
|
c Coord, t *testing.T) {
|
|
|
|
k1 := join(c)
|
|
var err error
|
|
var b []byte
|
|
if b, err = encode(k1); err != nil {
|
|
t.Errorf("%s: Failed to encode %s %s\n", desc, c, err)
|
|
return
|
|
}
|
|
var k2 int64
|
|
if k2, err = decode(b); err != nil {
|
|
t.Errorf("%s: Failed to decode %s %s\n", desc, c, err)
|
|
return
|
|
}
|
|
|
|
if k1 != k2 {
|
|
t.Errorf("%s: Expected %d got %d for %s\n", desc, k1, k2, c)
|
|
}
|
|
}
|
|
|
|
func TestEncodeDecode(t *testing.T) {
|
|
allData(func(c Coord) {
|
|
checkEncodeDecode(
|
|
"Big endian - interleaved",
|
|
CoordToInterleaved,
|
|
EncodeToBigEndian, DecodeFromBigEndian,
|
|
c, t)
|
|
})
|
|
allData(func(c Coord) {
|
|
checkEncodeDecode(
|
|
"String - interleaved",
|
|
CoordToInterleaved,
|
|
EncodeStringToBytes, DecodeStringFromBytes,
|
|
c, t)
|
|
})
|
|
allData(func(c Coord) {
|
|
checkEncodeDecode(
|
|
"Big endian - plain",
|
|
CoordToPlain,
|
|
EncodeToBigEndian, DecodeFromBigEndian,
|
|
c, t)
|
|
})
|
|
allData(func(c Coord) {
|
|
checkEncodeDecode(
|
|
"String - plain",
|
|
CoordToPlain,
|
|
EncodeStringToBytes, DecodeStringFromBytes,
|
|
c, t)
|
|
})
|
|
}
|
|
|
|
func checkJoinSplit(
|
|
desc string,
|
|
join KeyJoiner, split KeySplitter,
|
|
c Coord, t *testing.T) {
|
|
|
|
k := join(c)
|
|
s := split(k)
|
|
if !s.Equals(c) {
|
|
t.Errorf("%s: Expected %s got %s %b\n", desc, c, s, k)
|
|
}
|
|
}
|
|
|
|
func TestJoinSplit(t *testing.T) {
|
|
allData(func(c Coord) {
|
|
checkJoinSplit(
|
|
"P2C(C2P(xyz))",
|
|
CoordToPlain, PlainToCoord,
|
|
c, t)
|
|
})
|
|
allData(func(c Coord) {
|
|
checkJoinSplit(
|
|
"I2C(C2I(xyz))",
|
|
CoordToInterleaved, InterleavedToCoord,
|
|
c, t)
|
|
})
|
|
}
|
|
|
|
func checkTransformer(
|
|
desc string, joiner KeyJoiner,
|
|
transform KeyTransformer,
|
|
c Coord, t *testing.T) {
|
|
|
|
k1 := joiner(c)
|
|
k2 := transform(k1)
|
|
if k2 != k1 {
|
|
t.Errorf("%s: Expected %v got %v for %s\n", desc, k1, k2, c)
|
|
}
|
|
}
|
|
|
|
func compose(transforms ...KeyTransformer) KeyTransformer {
|
|
return func(x int64) int64 {
|
|
for _, transform := range transforms {
|
|
x = transform(x)
|
|
}
|
|
return x
|
|
}
|
|
}
|
|
|
|
func TestTransforms(t *testing.T) {
|
|
// Mainly to check the test itself.
|
|
allData(func(c Coord) {
|
|
checkTransformer(
|
|
"plain",
|
|
CoordToPlain,
|
|
compose(),
|
|
c, t)
|
|
})
|
|
allData(func(c Coord) {
|
|
checkTransformer(
|
|
"I2P(P2I(plain))",
|
|
CoordToPlain,
|
|
compose(TransformPlainToInterleaved, TransformInterleavedToPlain),
|
|
c, t)
|
|
})
|
|
allData(func(c Coord) {
|
|
checkTransformer(
|
|
"P2I(I2P(interleaved))",
|
|
CoordToInterleaved,
|
|
compose(TransformInterleavedToPlain, TransformPlainToInterleaved),
|
|
c, t)
|
|
})
|
|
}
|
|
|
|
func TestBigMin(t *testing.T) {
|
|
const tries = 1
|
|
errors, success := 0, 0
|
|
for i := 0; i < tries; i++ {
|
|
x1 := rand.Intn(4000) - 2000
|
|
y1 := rand.Intn(4000) - 2000
|
|
z1 := rand.Intn(4000) - 2000
|
|
w := rand.Intn(20) + 1
|
|
h := rand.Intn(20) + 1
|
|
d := rand.Intn(20) + 1
|
|
x2 := x1 + w
|
|
y2 := y1 + h
|
|
z2 := z1 + d
|
|
|
|
c1 := Coord{X: int16(x1), Y: int16(y1), Z: int16(z1)}
|
|
c2 := Coord{X: int16(x2), Y: int16(y2), Z: int16(z2)}
|
|
cub := Cuboid{P1: c1, P2: c2}
|
|
fmt.Printf("Cuboid: %s\n", cub)
|
|
|
|
zmin := CoordToInterleaved(c1)
|
|
zmax := CoordToInterleaved(c2)
|
|
|
|
for zcode := zmin + 1; zcode < zmax; zcode++ {
|
|
c3 := InterleavedToCoord(zcode)
|
|
if cub.Contains(c3) {
|
|
continue
|
|
}
|
|
nbm := NaiveBigMin(zmin, zmax, zcode)
|
|
cbm := BigMin(zmin, zmax, zcode)
|
|
//fmt.Printf("cbm: %d\n", cbm)
|
|
//fmt.Printf("nbm: %d\n", nbm)
|
|
if nbm != cbm {
|
|
errors++
|
|
} else {
|
|
success++
|
|
}
|
|
}
|
|
}
|
|
if errors > 0 {
|
|
t.Errorf("BigMin: %d errors out of %d (%f)\n",
|
|
errors, errors+success, float64(errors)/float64(errors+success))
|
|
}
|
|
}
|