mtsatellite/common/coords_test.go

134 lines
2.5 KiB
Go
Raw Normal View History

package common
import "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",
CoordToInterleaved,
EncodeToBigEndian, DecodeFromBigEndian,
c, t)
})
allData(func(c Coord) {
checkEncodeDecode(
"String",
CoordToInterleaved,
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, TransformInterleavedPlain),
c, t)
})
allData(func(c Coord) {
checkTransformer(
"P2I(I2P(interleaved))",
CoordToInterleaved,
compose(TransformInterleavedPlain, TransformPlainToInterleaved),
c, t)
})
}