Introduce struct type Coord to reduce tuples passing around.

This commit is contained in:
Sascha L. Teichmann
2014-08-18 21:33:58 +02:00
parent 066675896d
commit efe6c6abb8
2 changed files with 78 additions and 72 deletions

View File

@ -8,11 +8,11 @@ var data = []int16{
-150, -1, 0, 1, 88, 524, 527, 549,
1783, 1817, 1826, 2028, 2032}
func allData(f func(int16, int16, int16)) {
func allData(f func(Coord)) {
for _, z := range data {
for _, y := range data {
for _, x := range data {
f(x, y, z)
f(Coord{X: x, Y: y, Z: z})
}
}
}
@ -22,84 +22,79 @@ func checkEncodeDecode(
desc string,
join KeyJoiner,
encode KeyEncoder, decode KeyDecoder,
x, y, z int16, t *testing.T) {
c Coord, t *testing.T) {
k1 := join(x, y, z)
k1 := join(c)
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)
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 (%d, %d, %d) %s\n",
desc, x, y, z, err)
t.Errorf("%s: Failed to decode %s %s\n", desc, c, err)
return
}
if k1 != k2 {
t.Errorf("%s: Expected %d got %d for (%d, %d, %d) %b\n",
desc, k1, k2, x, y, z)
t.Errorf("%s: Expected %d got %d for %s\n", desc, k1, k2, c)
}
}
func TestEncodeDecode(t *testing.T) {
allData(func(x, y, z int16) {
allData(func(c Coord) {
checkEncodeDecode(
"Big endian",
XYZToInterleaved,
CoordToInterleaved,
EncodeToBigEndian, DecodeFromBigEndian,
x, y, z, t)
c, t)
})
allData(func(x, y, z int16) {
allData(func(c Coord) {
checkEncodeDecode(
"String",
XYZToInterleaved,
CoordToInterleaved,
EncodeStringToBytes, DecodeStringFromBytes,
x, y, z, t)
c, t)
})
}
func checkJoinSplit(
desc string,
join KeyJoiner, split KeySplitter,
x1, y1, z1 int16, t *testing.T) {
c Coord, 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)
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(x, y, z int16) {
allData(func(c Coord) {
checkJoinSplit(
"P2XYZ(XYZ2P(xyz))",
XYZToPlain, PlainToXYZ,
x, y, z, t)
"P2C(C2P(xyz))",
CoordToPlain, PlainToCoord,
c, t)
})
allData(func(x, y, z int16) {
allData(func(c Coord) {
checkJoinSplit(
"I2XYZ(XYZ2I(xyz))",
XYZToInterleaved, InterleavedToXYZ,
x, y, z, t)
"I2C(C2I(xyz))",
CoordToInterleaved, InterleavedToCoord,
c, t)
})
}
func checkTransformer(
desc string, joiner KeyJoiner,
transform KeyTransformer,
x, y, z int16, t *testing.T) {
c Coord, t *testing.T) {
k1 := joiner(x, y, z)
k1 := joiner(c)
k2 := transform(k1)
if k2 != k1 {
t.Errorf("%s: Expected %v got %v for (%d, %d, %d)\n",
desc, k1, k2, x, y, z)
t.Errorf("%s: Expected %v got %v for %s\n", desc, k1, k2, c)
}
}
@ -114,25 +109,25 @@ func compose(transforms ...KeyTransformer) KeyTransformer {
func TestTransforms(t *testing.T) {
// Mainly to check the test itself.
allData(func(x, y, z int16) {
allData(func(c Coord) {
checkTransformer(
"plain",
XYZToPlain,
CoordToPlain,
compose(),
x, y, z, t)
c, t)
})
allData(func(x, y, z int16) {
allData(func(c Coord) {
checkTransformer(
"I2P(P2I(plain))",
XYZToPlain,
CoordToPlain,
compose(TransformPlainToInterleaved, TransformInterleavedPlain),
x, y, z, t)
c, t)
})
allData(func(x, y, z int16) {
allData(func(c Coord) {
checkTransformer(
"P2I(I2P(interleaved))",
XYZToInterleaved,
CoordToInterleaved,
compose(TransformInterleavedPlain, TransformPlainToInterleaved),
x, y, z, t)
c, t)
})
}