mirror of
https://bitbucket.org/s_l_teichmann/mtsatellite
synced 2024-11-08 11:10:27 +01:00
139 lines
2.7 KiB
Go
139 lines
2.7 KiB
Go
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(int16, int16, int16)) {
|
|
for _, z := range data {
|
|
for _, y := range data {
|
|
for _, x := range data {
|
|
f(x, y, z)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func checkEncodeDecode(
|
|
desc string,
|
|
join KeyJoiner,
|
|
encode KeyEncoder, decode KeyDecoder,
|
|
x, y, z int16, t *testing.T) {
|
|
|
|
k1 := join(x, y, z)
|
|
var err error
|
|
var b []byte
|
|
if b, err = encode(k1); err != nil {
|
|
t.Errorf("%s: Failed to encode (%d, %d, %d) %s\n",
|
|
desc, x, y, z, err)
|
|
return
|
|
}
|
|
var k2 int64
|
|
if k2, err = decode(b); err != nil {
|
|
t.Errorf("%s: Failed to decode (%d, %d, %d) %s\n",
|
|
desc, x, y, z, err)
|
|
return
|
|
}
|
|
|
|
if k1 != k2 {
|
|
t.Errorf("%s: Expected %d got %d for (%d, %d, %d) %b\n",
|
|
desc, k1, k2, x, y, z)
|
|
}
|
|
}
|
|
|
|
func TestEncodeDecode(t *testing.T) {
|
|
allData(func(x, y, z int16) {
|
|
checkEncodeDecode(
|
|
"Big endian",
|
|
XYZToInterleaved,
|
|
EncodeToBigEndian, DecodeFromBigEndian,
|
|
x, y, z, t)
|
|
})
|
|
allData(func(x, y, z int16) {
|
|
checkEncodeDecode(
|
|
"String",
|
|
XYZToInterleaved,
|
|
EncodeStringToBytes, DecodeStringFromBytes,
|
|
x, y, z, t)
|
|
})
|
|
}
|
|
|
|
func checkJoinSplit(
|
|
desc string,
|
|
join KeyJoiner, split KeySplitter,
|
|
x1, y1, z1 int16, t *testing.T) {
|
|
|
|
k1 := join(x1, y1, z1)
|
|
x2, y2, z2 := split(k1)
|
|
if x1 != x2 || y1 != y2 || z1 != z2 {
|
|
t.Errorf("%s: Expected (%d, %d, %d) got (%d, %d, %d) %b\n",
|
|
desc, x1, y1, z1, x2, y2, z2, k1)
|
|
}
|
|
}
|
|
|
|
func TestJoinSplit(t *testing.T) {
|
|
allData(func(x, y, z int16) {
|
|
checkJoinSplit(
|
|
"P2XYZ(XYZ2P(xyz))",
|
|
XYZToPlain, PlainToXYZ,
|
|
x, y, z, t)
|
|
})
|
|
allData(func(x, y, z int16) {
|
|
checkJoinSplit(
|
|
"I2XYZ(XYZ2I(xyz))",
|
|
XYZToInterleaved, InterleavedToXYZ,
|
|
x, y, z, t)
|
|
})
|
|
}
|
|
|
|
func checkTransformer(
|
|
desc string, joiner KeyJoiner,
|
|
transform KeyTransformer,
|
|
x, y, z int16, t *testing.T) {
|
|
|
|
k1 := joiner(x, y, z)
|
|
k2 := transform(k1)
|
|
if k2 != k1 {
|
|
t.Errorf("%s: Expected %v got %v for (%d, %d, %d)\n",
|
|
desc, k1, k2, x, y, z)
|
|
}
|
|
}
|
|
|
|
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(x, y, z int16) {
|
|
checkTransformer(
|
|
"plain",
|
|
XYZToPlain,
|
|
compose(),
|
|
x, y, z, t)
|
|
})
|
|
allData(func(x, y, z int16) {
|
|
checkTransformer(
|
|
"I2P(P2I(plain))",
|
|
XYZToPlain,
|
|
compose(TransformPlainToInterleaved, TransformInterleavedPlain),
|
|
x, y, z, t)
|
|
})
|
|
allData(func(x, y, z int16) {
|
|
checkTransformer(
|
|
"P2I(I2P(interleaved))",
|
|
XYZToInterleaved,
|
|
compose(TransformInterleavedPlain, TransformPlainToInterleaved),
|
|
x, y, z, t)
|
|
})
|
|
}
|