apa102: Refactor tests (#251)

- Convert existing tests to table-driven
- Make reading []byte diffs easier by aligning lines
- Should be a no-op in terms of what's being tested and with what data
pull/1/head
Ben Lazarus 8 years ago committed by M-A
parent afdedaf59d
commit a948b6ab2e

@ -334,7 +334,7 @@ func TestDevEmpty(t *testing.T) {
t.Fatalf("%d %v", n, err) t.Fatalf("%d %v", n, err)
} }
if expected := []byte{0x0, 0x0, 0x0, 0x0, 0xFF}; !bytes.Equal(expected, buf.Bytes()) { if expected := []byte{0x0, 0x0, 0x0, 0x0, 0xFF}; !bytes.Equal(expected, buf.Bytes()) {
t.Fatalf("%#v != %#v", expected, buf.Bytes()) t.Fatalf("\ngot: %#v\nwant: %#v\n", buf.Bytes(), expected)
} }
if s := d.String(); s != "APA102{I:255, T:5000K, 0LEDs, recordraw}" { if s := d.String(); s != "APA102{I:255, T:5000K, 0LEDs, recordraw}" {
t.Fatal(s) t.Fatal(s)
@ -356,94 +356,120 @@ func TestDevLen(t *testing.T) {
t.Fatalf("%d %v", n, err) t.Fatalf("%d %v", n, err)
} }
if expected := []byte{}; !bytes.Equal(expected, buf.Bytes()) { if expected := []byte{}; !bytes.Equal(expected, buf.Bytes()) {
t.Fatalf("%#v != %#v", expected, buf.Bytes()) t.Fatalf("\ngot: %#v\nwant: %#v\n", buf.Bytes(), expected)
} }
} }
func TestDev(t *testing.T) { var writeTests = []struct {
buf := bytes.Buffer{} name string
colors := []color.NRGBA{ pixels []byte
{0xFF, 0xFF, 0xFF, 0x00}, want []byte
{0xFE, 0xFE, 0xFE, 0x00}, opts Opts
{0xF0, 0xF0, 0xF0, 0x00}, }{
{0x80, 0x80, 0x80, 0x00}, {
{0x80, 0x00, 0x00, 0x00}, name: "Temperature",
{0x00, 0x80, 0x00, 0x00}, pixels: ToRGB([]color.NRGBA{
{0x00, 0x00, 0x80, 0x00}, {0xFF, 0xFF, 0xFF, 0x00},
{0x00, 0x00, 0x10, 0x00}, {0xFE, 0xFE, 0xFE, 0x00},
{0x00, 0x00, 0x01, 0x00}, {0xF0, 0xF0, 0xF0, 0x00},
{0x00, 0x00, 0x00, 0x00}, {0x80, 0x80, 0x80, 0x00},
} {0x80, 0x00, 0x00, 0x00},
o := DefaultOpts {0x00, 0x80, 0x00, 0x00},
o.NumPixels = len(colors) {0x00, 0x00, 0x80, 0x00},
o.Temperature = 6500 {0x00, 0x00, 0x10, 0x00},
d, _ := New(spitest.NewRecordRaw(&buf), &o) {0x00, 0x00, 0x01, 0x00},
if n, err := d.Write(ToRGB(colors)); n != len(colors)*3 || err != nil { {0x00, 0x00, 0x00, 0x00},
t.Fatalf("%d %v", n, err) }),
} want: []byte{
expected := []byte{ 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFB, 0xFB, 0xFB,
0xFF, 0xFB, 0xFB, 0xFB, 0xFF, 0xC4, 0xC4, 0xC4,
0xFF, 0xC4, 0xC4, 0xC4, 0xE1, 0xF8, 0xF8, 0xF8,
0xE1, 0xF8, 0xF8, 0xF8, 0xE1, 0x00, 0x00, 0xF8,
0xE1, 0x00, 0x00, 0xF8, 0xE1, 0x00, 0xF8, 0x00,
0xE1, 0x00, 0xF8, 0x00, 0xE1, 0xF8, 0x00, 0x00,
0xE1, 0xF8, 0x00, 0x00, 0xE1, 0x10, 0x00, 0x00,
0xE1, 0x10, 0x00, 0x00, 0xE1, 0x01, 0x00, 0x00,
0xE1, 0x01, 0x00, 0x00, 0xE1, 0x00, 0x00, 0x00,
0xE1, 0x00, 0x00, 0x00, 0xFF,
0xFF, },
} opts: Opts{
if !bytes.Equal(expected, buf.Bytes()) { Intensity: 255,
t.Fatalf("%#v != %#v", expected, buf.Bytes()) Temperature: 6500,
} },
},
{
name: "Intensity",
pixels: ToRGB([]color.NRGBA{
{0xFF, 0xFF, 0xFF, 0x00},
{0xFE, 0xFE, 0xFE, 0x00},
{0xF0, 0xF0, 0xF0, 0x00},
{0x80, 0x80, 0x80, 0x00},
{0x80, 0x00, 0x00, 0x00},
{0x00, 0x80, 0x00, 0x00},
{0x00, 0x00, 0x80, 0x00},
{0x00, 0x00, 0x10, 0x00},
{0x00, 0x00, 0x01, 0x00},
{0x00, 0x00, 0x00, 0x00},
}),
want: []byte{
0x00, 0x00, 0x00, 0x00,
0xFF, 0x7F, 0x7F, 0x7F,
0xFF, 0x7D, 0x7D, 0x7D,
0xFF, 0x67, 0x67, 0x67,
0xE2, 0x9B, 0x9B, 0x9B,
0xE2, 0x00, 0x00, 0x9B,
0xE2, 0x00, 0x9B, 0x00,
0xE2, 0x9B, 0x00, 0x00,
0xE1, 0x10, 0x00, 0x00,
0xE1, 0x01, 0x00, 0x00,
0xE1, 0x00, 0x00, 0x00,
0xFF,
},
opts: Opts{
Intensity: 127,
Temperature: 6500,
},
},
{
name: "Intensity and temperature",
pixels: func() []byte {
var p []byte
for i := 0; i < 16*3; i++ {
p = append(p, uint8(i<<2))
}
return p
}(),
want: expectedi250t5000,
opts: Opts{
Intensity: 250,
Temperature: 5000,
},
},
} }
func TestDevColo(t *testing.T) { func TestWrites(t *testing.T) {
if (&Dev{}).ColorModel() != color.NRGBAModel { for _, tt := range writeTests {
t.Fail() buf := bytes.Buffer{}
tt.opts.NumPixels = len(tt.pixels) / 3
d, _ := New(spitest.NewRecordRaw(&buf), &tt.opts)
n, err := d.Write(tt.pixels)
if err != nil {
t.Fatal(err)
}
if n != len(tt.pixels) {
t.Fatalf("%s: Got %d bytes result, want %d", tt.name, n, len(tt.pixels)*3)
}
if !bytes.Equal(buf.Bytes(), tt.want) {
t.Fatalf("%s:\ngot: %#v\nwant: %#v\n", tt.name, buf.Bytes(), tt.want)
}
} }
} }
func TestDevIntensity(t *testing.T) { func TestDevColor(t *testing.T) {
buf := bytes.Buffer{} if (&Dev{}).ColorModel() != color.NRGBAModel {
colors := []color.NRGBA{ t.Fail()
{0xFF, 0xFF, 0xFF, 0x00},
{0xFE, 0xFE, 0xFE, 0x00},
{0xF0, 0xF0, 0xF0, 0x00},
{0x80, 0x80, 0x80, 0x00},
{0x80, 0x00, 0x00, 0x00},
{0x00, 0x80, 0x00, 0x00},
{0x00, 0x00, 0x80, 0x00},
{0x00, 0x00, 0x10, 0x00},
{0x00, 0x00, 0x01, 0x00},
{0x00, 0x00, 0x00, 0x00},
}
o := DefaultOpts
o.Intensity = 127
o.NumPixels = len(colors)
o.Temperature = 6500
d, _ := New(spitest.NewRecordRaw(&buf), &o)
if n, err := d.Write(ToRGB(colors)); n != len(colors)*3 || err != nil {
t.Fatalf("%d %v", n, err)
}
expected := []byte{
0x00, 0x00, 0x00, 0x00,
0xFF, 0x7F, 0x7F, 0x7F,
0xFF, 0x7D, 0x7D, 0x7D,
0xFF, 0x67, 0x67, 0x67,
0xE2, 0x9B, 0x9B, 0x9B,
0xE2, 0x00, 0x00, 0x9B,
0xE2, 0x00, 0x9B, 0x00,
0xE2, 0x9B, 0x00, 0x00,
0xE1, 0x10, 0x00, 0x00,
0xE1, 0x01, 0x00, 0x00,
0xE1, 0x00, 0x00, 0x00,
0xFF,
}
if !bytes.Equal(expected, buf.Bytes()) {
t.Fatalf("%#v != %#v", expected, buf.Bytes())
} }
} }
@ -465,7 +491,7 @@ func TestDevLong(t *testing.T) {
trailer[i] = 0xFF trailer[i] = 0xFF
} }
if !bytes.Equal(expected, buf.Bytes()) { if !bytes.Equal(expected, buf.Bytes()) {
t.Fatalf("%#v != %#v", expected, buf.Bytes()) t.Fatalf("\ngot: %#v\nwant: %#v\n", buf.Bytes(), expected)
} }
} }
@ -479,7 +505,7 @@ func TestDevWrite_Long(t *testing.T) {
} }
} }
// expectedi250t5000 is the expected output for 3 test cases. Each test case // expectedi250t5000 is the expected output for multiple test cases. Each test case
// use a completely different code path so make sure each code path results in // use a completely different code path so make sure each code path results in
// the exact same output. // the exact same output.
var expectedi250t5000 = []byte{ var expectedi250t5000 = []byte{
@ -501,89 +527,81 @@ var expectedi250t6500 = []byte{
0x3E, 0x38, 0x32, 0xFF, 0xFF, 0x3E, 0x38, 0x32, 0xFF, 0xFF,
} }
func TestDevTemperatureWarm(t *testing.T) { var drawTests = []struct {
buf := bytes.Buffer{} name string
pixels := [16 * 3]byte{} img image.Image
for i := range pixels { want []byte
// Test all intensity code paths. opts Opts
pixels[i] = uint8(i << 2) }{
} {
o := DefaultOpts name: "Draw NRGBA",
o.NumPixels = len(pixels) / 3 img: func() image.Image {
o.Intensity = 250 im := image.NewNRGBA(image.Rect(0, 0, 16, 1))
o.Temperature = 5000 for i := 0; i < 16; i++ {
d, _ := New(spitest.NewRecordRaw(&buf), &o) // Test all intensity code paths. Confirm that alpha is ignored.
if n, err := d.Write(pixels[:]); n != len(pixels) || err != nil { im.Pix[4*i] = uint8((3 * i) << 2)
t.Fatalf("%d %v", n, err) im.Pix[4*i+1] = uint8((3*i + 1) << 2)
} im.Pix[4*i+2] = uint8((3*i + 2) << 2)
if !bytes.Equal(expectedi250t5000, buf.Bytes()) { im.Pix[4*i+3] = 0
t.Fatalf("%#v != %#v", expectedi250t5000, buf.Bytes()) }
} return im
} }(),
want: expectedi250t5000,
func TestDrawNRGBA(t *testing.T) { opts: Opts{
img := image.NewNRGBA(image.Rect(0, 0, 16, 1)) NumPixels: 16,
for i := 0; i < 16; i++ { Intensity: 250,
// Test all intensity code paths. Confirm that alpha is ignored. Temperature: 5000,
img.Pix[4*i] = uint8((3 * i) << 2) },
img.Pix[4*i+1] = uint8((3*i + 1) << 2) },
img.Pix[4*i+2] = uint8((3*i + 2) << 2) {
img.Pix[4*i+3] = 0 name: "Draw NRGBA Wide",
} img: func() image.Image {
buf := bytes.Buffer{} im := image.NewNRGBA(image.Rect(0, 0, 17, 2))
o := DefaultOpts for x := 0; x < 16; x++ {
o.NumPixels = 16 // Test all intensity code paths. Confirm that alpha is ignored.
o.Intensity = 250 im.SetNRGBA(x, 0, color.NRGBA{uint8((3 * x) << 2), uint8((3*x + 1) << 2), uint8((3*x + 2) << 2), 0})
o.Temperature = 5000 }
d, _ := New(spitest.NewRecordRaw(&buf), &o) return im
if err := d.Draw(d.Bounds(), img, image.Point{}); err != nil { }(),
t.Fatal(err) want: expectedi250t6500,
} opts: Opts{
if !bytes.Equal(expectedi250t5000, buf.Bytes()) { NumPixels: 16,
t.Fatalf("%#v != %#v", expectedi250t5000, buf.Bytes()) Intensity: 250,
} Temperature: 6500,
} },
},
func TestDrawNRGBA_wide(t *testing.T) { {
img := image.NewNRGBA(image.Rect(0, 0, 17, 2)) name: "Draw RGBA",
for x := 0; x < 16; x++ { img: func() image.Image {
// Test all intensity code paths. Confirm that alpha is ignored. im := image.NewRGBA(image.Rect(0, 0, 16, 1))
img.SetNRGBA(x, 0, color.NRGBA{uint8((3 * x) << 2), uint8((3*x + 1) << 2), uint8((3*x + 2) << 2), 0}) for i := 0; i < 16; i++ {
} // Test all intensity code paths. Alpha is not ignored in this case.
buf := bytes.Buffer{} im.Pix[4*i] = uint8((3 * i) << 2)
o := DefaultOpts im.Pix[4*i+1] = uint8((3*i + 1) << 2)
o.NumPixels = 16 im.Pix[4*i+2] = uint8((3*i + 2) << 2)
o.Intensity = 250 im.Pix[4*i+3] = 0xFF
o.Temperature = 6500 }
d, _ := New(spitest.NewRecordRaw(&buf), &o) return im
if err := d.Draw(d.Bounds(), img, image.Point{}); err != nil { }(),
t.Fatal(err) want: expectedi250t5000,
} opts: Opts{
if !bytes.Equal(expectedi250t6500, buf.Bytes()) { NumPixels: 16,
t.Fatalf("%#v != %#v", expectedi250t5000, buf.Bytes()) Intensity: 250,
} Temperature: 5000,
},
},
} }
func TestDrawRGBA(t *testing.T) { func TestDraws(t *testing.T) {
img := image.NewRGBA(image.Rect(0, 0, 16, 1)) for _, tt := range drawTests {
for i := 0; i < 16; i++ { buf := bytes.Buffer{}
// Test all intensity code paths. Alpha is not ignored in this case. d, _ := New(spitest.NewRecordRaw(&buf), &tt.opts)
img.Pix[4*i] = uint8((3 * i) << 2) if err := d.Draw(d.Bounds(), tt.img, image.Point{}); err != nil {
img.Pix[4*i+1] = uint8((3*i + 1) << 2) t.Fatalf("%s: %v", tt.name, err)
img.Pix[4*i+2] = uint8((3*i + 2) << 2) }
img.Pix[4*i+3] = 0xFF if !bytes.Equal(buf.Bytes(), tt.want) {
} t.Fatalf("%s:\ngot: %#v\nwant: %#v\n", tt.name, buf.Bytes(), tt.want)
buf := bytes.Buffer{} }
o := DefaultOpts
o.NumPixels = 16
o.Intensity = 250
o.Temperature = 5000
d, _ := New(spitest.NewRecordRaw(&buf), &o)
if err := d.Draw(d.Bounds(), img, image.Point{}); err != nil {
t.Fatal(err)
}
if !bytes.Equal(expectedi250t5000, buf.Bytes()) {
t.Fatalf("%#v != %#v", expectedi250t5000, buf.Bytes())
} }
} }

Loading…
Cancel
Save