Skip to content

Commit

Permalink
Fix pixarray, effects tets. Add white test values where appropriate w…
Browse files Browse the repository at this point in the history
…hile we're at it.
  • Loading branch information
Jon-Bright committed Jan 4, 2021
1 parent e3234e2 commit f9b3ccb
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 43 deletions.
65 changes: 42 additions & 23 deletions effects/effects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,28 @@ import (
"time"
)

type FakeLEDDev struct {
type testLeds struct {
pixels []pixarray.Pixel
}

func (f *FakeLEDDev) Fd() uintptr {
return 0
func (l *testLeds) GetPixel(i int) pixarray.Pixel {
return l.pixels[i]
}

func (f *FakeLEDDev) Write(b []byte) (n int, err error) {
return len(b), nil
func (l *testLeds) SetPixel(i int, p pixarray.Pixel) {
l.pixels[i] = p
}

func (l *testLeds) Write() error {
return nil
}

func (l *testLeds) MaxPerChannel() int {
return 160
}

func newTestLeds(numPixels int) pixarray.LEDStrip {
return &testLeds{make([]pixarray.Pixel, numPixels)}
}

func d(s string, tb testing.TB) time.Duration {
Expand All @@ -27,10 +40,7 @@ func d(s string, tb testing.TB) time.Duration {
}

func TestAllSameFade(t *testing.T) {
pa, err := pixarray.NewLPD8806(&FakeLEDDev{}, 100, 0, pixarray.GRB)
if err != nil {
t.Fatalf("Failed NewPixArray: %v", err)
}
pa := pixarray.NewPixArray(100, 4, newTestLeds(100))

tests := []struct {
start pixarray.Pixel
Expand All @@ -40,15 +50,16 @@ func TestAllSameFade(t *testing.T) {
r float64
g float64
b float64
w float64
}{
{pixarray.Pixel{0, 0, 0}, pixarray.Pixel{127, 0, 0}, d("1.0s", t), d("0.5s", t), 63.5, 0, 0},
{pixarray.Pixel{0, 127, 0}, pixarray.Pixel{127, 0, 0}, d("1.0s", t), d("0.5s", t), 63.5, 63.5, 0},
{pixarray.Pixel{127, 127, 127}, pixarray.Pixel{127, 0, 127}, d("3.0s", t), d("1.0s", t), 127, 84.66666, 127},
{pixarray.Pixel{127, 127, 127}, pixarray.Pixel{127, 0, 127}, d("3.0s", t), d("2.0s", t), 127, 42.33333, 127},
{pixarray.Pixel{127, 127, 127}, pixarray.Pixel{0, 0, 0}, d("127.0s", t), d("10.5s", t), 116.5, 116.5, 116.5},
{pixarray.Pixel{127, 127, 0}, pixarray.Pixel{0, 0, 127}, d("127.0s", t), d("10.5s", t), 116.5, 116.5, 10.5},
{pixarray.Pixel{126, 126, 0}, pixarray.Pixel{0, 63, 126}, d("126.0s", t), d("10.5s", t), 115.5, 120.75, 10.5},
{pixarray.Pixel{0, 0, 0}, pixarray.Pixel{120, 10, 0}, d("120.0s", t), d("6.0s", t), 6.0, 0.5, 0},
{pixarray.Pixel{0, 0, 0, 0}, pixarray.Pixel{127, 0, 0, 0}, d("1.0s", t), d("0.5s", t), 63.5, 0, 0, 0},
{pixarray.Pixel{0, 127, 0, 0}, pixarray.Pixel{127, 0, 0, 0}, d("1.0s", t), d("0.5s", t), 63.5, 63.5, 0, 0},
{pixarray.Pixel{127, 127, 127, 127}, pixarray.Pixel{127, 0, 127, 0}, d("3.0s", t), d("1.0s", t), 127, 84.66666, 127, 84.66666},
{pixarray.Pixel{127, 127, 127, 127}, pixarray.Pixel{127, 0, 127, 0}, d("3.0s", t), d("2.0s", t), 127, 42.33333, 127, 42.33333},
{pixarray.Pixel{127, 127, 127, 127}, pixarray.Pixel{0, 0, 0, 0}, d("127.0s", t), d("10.5s", t), 116.5, 116.5, 116.5, 116.5},
{pixarray.Pixel{127, 127, 0, 0}, pixarray.Pixel{0, 0, 127, 127}, d("127.0s", t), d("10.5s", t), 116.5, 116.5, 10.5, 10.5},
{pixarray.Pixel{126, 126, 0, 0}, pixarray.Pixel{0, 63, 126, 63}, d("126.0s", t), d("10.5s", t), 115.5, 120.75, 10.5, 5.25},
{pixarray.Pixel{0, 0, 0, 0}, pixarray.Pixel{120, 10, 0, 5}, d("120.0s", t), d("6.0s", t), 6.0, 0.5, 0, 0.25},
}

tm := time.Now()
Expand All @@ -62,16 +73,20 @@ func TestAllSameFade(t *testing.T) {
totR := 0
totG := 0
totB := 0
totW := 0
rc := int(math.Ceil(test.r))
rf := int(math.Floor(test.r))
gc := int(math.Ceil(test.g))
gf := int(math.Floor(test.g))
bc := int(math.Ceil(test.b))
bf := int(math.Floor(test.b))
wc := int(math.Ceil(test.w))
wf := int(math.Floor(test.w))
for i, p := range py {
totR += p.R
totG += p.G
totB += p.B
totW += p.W
if p.R != rc && p.R != rf {
t.Errorf("Wrong red at pixel %d, want %d/%d, got %d", i, rc, rf, p.R)
}
Expand All @@ -81,6 +96,9 @@ func TestAllSameFade(t *testing.T) {
if p.B != bc && p.B != bf {
t.Errorf("Wrong blue at pixel %d, want %d/%d, got %d", i, bc, bf, p.B)
}
if p.W != wc && p.W != wf {
t.Errorf("Wrong white at pixel %d, want %d/%d, got %d", i, wc, wf, p.W)
}
}
dR := float64(totR) / float64(len(py))
if math.Abs(dR-test.r) > 0.01 {
Expand All @@ -94,21 +112,22 @@ func TestAllSameFade(t *testing.T) {
if math.Abs(dB-test.b) > 0.01 {
t.Errorf("Wrong average blue, want %f, got %f", test.b, dB)
}
dW := float64(totW) / float64(len(py))
if math.Abs(dW-test.w) > 0.01 {
t.Errorf("Wrong average white, want %f, got %f", test.w, dW)
}
}
}

func BenchmarkFadeStep(b *testing.B) {
pa, err := pixarray.NewLPD8806(&FakeLEDDev{}, 100, 0, pixarray.GRB)
if err != nil {
b.Fatalf("Failed NewPixArray: %v", err)
}
pa.SetAll(pixarray.Pixel{127, 0, 0})
pa := pixarray.NewPixArray(100, 4, newTestLeds(100))
pa.SetAll(pixarray.Pixel{127, 0, 0, 0})
tm := time.Now()
add := time.Duration((7200 * time.Second).Nanoseconds() / int64(b.N))
if add == 0 {
b.Fatalf("Zero delay")
}
f := NewFade(d("7200.0s", b), pixarray.Pixel{0, 127, 0})
f := NewFade(d("7200.0s", b), pixarray.Pixel{0, 127, 0, 0})
f.Start(pa, tm)
for i := 0; i < b.N; i++ {
tm = tm.Add(add)
Expand Down
66 changes: 46 additions & 20 deletions pixarray/pixarray_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,29 @@ type testLeds struct {
}

func (l *testLeds) GetPixel(i int) Pixel {
return pixels[i]
return l.pixels[i]
}

func (l *testLeds) SetPixel(i int, p Pixel) {
pixels[i] = p
l.pixels[i] = p
}

func newTestLeds(numPixels int) {
func (l *testLeds) Write() error {
return nil
}

func (l *testLeds) MaxPerChannel() int {
return 160
}

func newTestLeds(numPixels int) LEDStrip {
return &testLeds{make([]Pixel, numPixels)}
}

func TestSetOneThenGetOneByOne(t *testing.T) {
pa := NewPixArray(100, 3, newTestLeds(100))
ps := Pixel{10, 25, 45}
pb := Pixel{0, 0, 0}
ps := Pixel{10, 25, 45, 55}
pb := Pixel{0, 0, 0, 0}
pa.SetOne(20, ps)
for i := 0; i < 100; i++ {
pg := pa.GetPixel(i)
Expand All @@ -37,8 +45,8 @@ func TestSetOneThenGetOneByOne(t *testing.T) {

func TestSetOneThenGetAll(t *testing.T) {
pa := NewPixArray(100, 3, newTestLeds(100))
ps := Pixel{10, 25, 45}
pb := Pixel{0, 0, 0}
ps := Pixel{10, 25, 45, 55}
pb := Pixel{0, 0, 0, 0}
pa.SetOne(20, ps)
py := pa.GetPixels()
if len(py) != 100 {
Expand All @@ -55,8 +63,8 @@ func TestSetOneThenGetAll(t *testing.T) {

func TestSetAlternate(t *testing.T) {
pa := NewPixArray(100, 3, newTestLeds(100))
p1 := Pixel{10, 25, 45}
p2 := Pixel{9, 7, 5}
p1 := Pixel{10, 25, 45, 55}
p2 := Pixel{9, 7, 5, 3}

tests := []struct {
num int
Expand Down Expand Up @@ -122,9 +130,9 @@ func TestSetAlternate(t *testing.T) {
}

func TestSetPerChanAlternate(t *testing.T) {
pa := NewPixArray(100, 3, newTestLeds(100))
p1 := Pixel{10, 25, 45}
p2 := Pixel{9, 7, 5}
pa := NewPixArray(100, 4, newTestLeds(100))
p1 := Pixel{10, 25, 45, 55}
p2 := Pixel{9, 7, 5, 3}

tests := []struct {
num Pixel
Expand All @@ -134,8 +142,8 @@ func TestSetPerChanAlternate(t *testing.T) {
cons1 Pixel // Max number of consecutive p1 we expect
cons2 Pixel // Max number of consecutive p2 we expect
}{
{Pixel{9, 5, 1}, 10, Pixel{10, 50, 90}, Pixel{90, 50, 10}, Pixel{1, 1, 9}, Pixel{9, 1, 1}},
{Pixel{51, 52, 99}, 100, Pixel{49, 48, 1}, Pixel{51, 52, 99}, Pixel{1, 1, 1}, Pixel{2, 2, 50}},
{Pixel{9, 5, 1, 1}, 10, Pixel{10, 50, 90, 90}, Pixel{90, 50, 10, 10}, Pixel{1, 1, 9, 9}, Pixel{9, 1, 1, 1}},
{Pixel{51, 52, 99, 99}, 100, Pixel{49, 48, 1, 1}, Pixel{51, 52, 99, 99}, Pixel{1, 1, 1, 1}, Pixel{2, 2, 50, 50}},
}

for _, test := range tests {
Expand Down Expand Up @@ -166,6 +174,11 @@ func TestSetPerChanAlternate(t *testing.T) {
} else {
cons.B = 1
}
if py[i].W == lp.W {
cons.W++
} else {
cons.W = 1
}
if py[i].R == p1.R {
n1.R++
if cons.R > cons1.R {
Expand Down Expand Up @@ -205,6 +218,19 @@ func TestSetPerChanAlternate(t *testing.T) {
} else {
t.Errorf("B(%d/%d): Unexpected pixel got: %v, want: %v or %v", test.num.B, test.div, py[i].B, p1.B, p2.B)
}
if py[i].W == p1.W {
n1.W++
if cons.W > cons1.W {
cons1.W = cons.W
}
} else if py[i].W == p2.W {
n2.W++
if cons.W > cons2.W {
cons2.W = cons.W
}
} else {
t.Errorf("W(%d/%d): Unexpected pixel got: %v, want: %v or %v", test.num.W, test.div, py[i].W, p1.W, p2.W)
}
lp = py[i]
}
if n1 != test.want1 {
Expand All @@ -224,8 +250,8 @@ func TestSetPerChanAlternate(t *testing.T) {

func BenchmarkSetAlternate(b *testing.B) {
pa := NewPixArray(100, 3, newTestLeds(100))
p1 := Pixel{10, 25, 45}
p2 := Pixel{9, 7, 5}
p1 := Pixel{10, 25, 45, 55}
p2 := Pixel{9, 7, 5, 3}
for i := 0; i < b.N/2; i++ {
pa.SetAlternate(5, 7, p1, p2)
pa.SetAlternate(2, 7, p1, p2)
Expand All @@ -234,10 +260,10 @@ func BenchmarkSetAlternate(b *testing.B) {

func BenchmarkSetPerChanAlternate(b *testing.B) {
pa := NewPixArray(100, 3, newTestLeds(100))
p1 := Pixel{10, 25, 45}
p2 := Pixel{9, 7, 5}
s1 := Pixel{5, 1, 2}
s2 := Pixel{2, 6, 5}
p1 := Pixel{10, 25, 45, 55}
p2 := Pixel{9, 7, 5, 3}
s1 := Pixel{5, 1, 2, 3}
s2 := Pixel{2, 6, 5, 4}
for i := 0; i < b.N/2; i++ {
pa.SetPerChanAlternate(s1, 7, p1, p2)
pa.SetPerChanAlternate(s2, 7, p1, p2)
Expand Down

0 comments on commit f9b3ccb

Please sign in to comment.