@ -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,13 +356,19 @@ 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
want [ ] byte
opts Opts
} {
{
name : "Temperature" ,
pixels : ToRGB ( [ ] color . NRGBA {
{ 0xFF , 0xFF , 0xFF , 0x00 } ,
{ 0xFF , 0xFF , 0xFF , 0x00 } ,
{ 0xFE , 0xFE , 0xFE , 0x00 } ,
{ 0xFE , 0xFE , 0xFE , 0x00 } ,
{ 0xF0 , 0xF0 , 0xF0 , 0x00 } ,
{ 0xF0 , 0xF0 , 0xF0 , 0x00 } ,
@ -373,15 +379,8 @@ func TestDev(t *testing.T) {
{ 0x00 , 0x00 , 0x10 , 0x00 } ,
{ 0x00 , 0x00 , 0x10 , 0x00 } ,
{ 0x00 , 0x00 , 0x01 , 0x00 } ,
{ 0x00 , 0x00 , 0x01 , 0x00 } ,
{ 0x00 , 0x00 , 0x00 , 0x00 } ,
{ 0x00 , 0x00 , 0x00 , 0x00 } ,
}
} ) ,
o := DefaultOpts
want : [ ] byte {
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 ,
0x00 , 0x00 , 0x00 , 0x00 ,
0xFF , 0xFF , 0xFF , 0xFF ,
0xFF , 0xFF , 0xFF , 0xFF ,
0xFF , 0xFB , 0xFB , 0xFB ,
0xFF , 0xFB , 0xFB , 0xFB ,
@ -394,21 +393,15 @@ func TestDev(t *testing.T) {
0xE1 , 0x01 , 0x00 , 0x00 ,
0xE1 , 0x01 , 0x00 , 0x00 ,
0xE1 , 0x00 , 0x00 , 0x00 ,
0xE1 , 0x00 , 0x00 , 0x00 ,
0xFF ,
0xFF ,
}
} ,
if ! bytes . Equal ( expected , buf . Bytes ( ) ) {
opts : Opts {
t . Fatalf ( "%#v != %#v" , expected , buf . Bytes ( ) )
Intensity : 255 ,
}
Temperature : 6500 ,
}
} ,
} ,
func TestDevColo ( t * testing . T ) {
{
if ( & Dev { } ) . ColorModel ( ) != color . NRGBAModel {
name : "Intensity" ,
t . Fail ( )
pixels : ToRGB ( [ ] color . NRGBA {
}
}
func TestDevIntensity ( t * testing . T ) {
buf := bytes . Buffer { }
colors := [ ] color . NRGBA {
{ 0xFF , 0xFF , 0xFF , 0x00 } ,
{ 0xFF , 0xFF , 0xFF , 0x00 } ,
{ 0xFE , 0xFE , 0xFE , 0x00 } ,
{ 0xFE , 0xFE , 0xFE , 0x00 } ,
{ 0xF0 , 0xF0 , 0xF0 , 0x00 } ,
{ 0xF0 , 0xF0 , 0xF0 , 0x00 } ,
@ -419,16 +412,8 @@ func TestDevIntensity(t *testing.T) {
{ 0x00 , 0x00 , 0x10 , 0x00 } ,
{ 0x00 , 0x00 , 0x10 , 0x00 } ,
{ 0x00 , 0x00 , 0x01 , 0x00 } ,
{ 0x00 , 0x00 , 0x01 , 0x00 } ,
{ 0x00 , 0x00 , 0x00 , 0x00 } ,
{ 0x00 , 0x00 , 0x00 , 0x00 } ,
}
} ) ,
o := DefaultOpts
want : [ ] byte {
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 ,
0x00 , 0x00 , 0x00 , 0x00 ,
0xFF , 0x7F , 0x7F , 0x7F ,
0xFF , 0x7F , 0x7F , 0x7F ,
0xFF , 0x7D , 0x7D , 0x7D ,
0xFF , 0x7D , 0x7D , 0x7D ,
@ -441,9 +426,50 @@ func TestDevIntensity(t *testing.T) {
0xE1 , 0x01 , 0x00 , 0x00 ,
0xE1 , 0x01 , 0x00 , 0x00 ,
0xE1 , 0x00 , 0x00 , 0x00 ,
0xE1 , 0x00 , 0x00 , 0x00 ,
0xFF ,
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 TestWrites ( t * testing . T ) {
for _ , tt := range writeTests {
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 )
}
}
if ! bytes . Equal ( expected , buf . Bytes ( ) ) {
}
t . Fatalf ( "%#v != %#v" , expected , buf . Bytes ( ) )
}
func TestDevColor ( t * testing . T ) {
if ( & Dev { } ) . ColorModel ( ) != color . NRGBAModel {
t . Fail ( )
}
}
}
}
@ -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
d , _ := New ( spitest . NewRecordRaw ( & buf ) , & o )
if n , err := d . Write ( pixels [ : ] ) ; n != len ( pixels ) || err != nil {
t . Fatalf ( "%d %v" , n , err )
}
if ! bytes . Equal ( expectedi250t5000 , buf . Bytes ( ) ) {
t . Fatalf ( "%#v != %#v" , expectedi250t5000 , buf . Bytes ( ) )
}
}
func TestDrawNRGBA ( t * testing . T ) {
img := image . NewNRGBA ( image . Rect ( 0 , 0 , 16 , 1 ) )
for i := 0 ; i < 16 ; i ++ {
for i := 0 ; i < 16 ; i ++ {
// Test all intensity code paths. Confirm that alpha is ignored.
// Test all intensity code paths. Confirm that alpha is ignored.
img . Pix [ 4 * i ] = uint8 ( ( 3 * i ) << 2 )
im . Pix [ 4 * i ] = uint8 ( ( 3 * i ) << 2 )
img . Pix [ 4 * i + 1 ] = uint8 ( ( 3 * i + 1 ) << 2 )
im . Pix [ 4 * i + 1 ] = uint8 ( ( 3 * i + 1 ) << 2 )
img . Pix [ 4 * i + 2 ] = uint8 ( ( 3 * i + 2 ) << 2 )
im . Pix [ 4 * i + 2 ] = uint8 ( ( 3 * i + 2 ) << 2 )
img . Pix [ 4 * i + 3 ] = 0
im . Pix [ 4 * i + 3 ] = 0
}
}
buf := bytes . Buffer { }
return im
o := DefaultOpts
} ( ) ,
o . NumPixels = 16
want : expectedi250t5000 ,
o . Intensity = 250
opts : Opts {
o . Temperature = 5000
NumPixels : 16 ,
d , _ := New ( spitest . NewRecordRaw ( & buf ) , & o )
Intensity : 250 ,
if err := d . Draw ( d . Bounds ( ) , img , image . Point { } ) ; err != nil {
Temperature : 5000 ,
t . Fatal ( err )
} ,
}
} ,
if ! bytes . Equal ( expectedi250t5000 , buf . Bytes ( ) ) {
{
t . Fatalf ( "%#v != %#v" , expectedi250t5000 , buf . Bytes ( ) )
name : "Draw NRGBA Wide" ,
}
img : func ( ) image . Image {
}
im := image . NewNRGBA ( image . Rect ( 0 , 0 , 17 , 2 ) )
func TestDrawNRGBA_wide ( t * testing . T ) {
img := image . NewNRGBA ( image . Rect ( 0 , 0 , 17 , 2 ) )
for x := 0 ; x < 16 ; x ++ {
for x := 0 ; x < 16 ; x ++ {
// Test all intensity code paths. Confirm that alpha is ignored.
// Test all intensity code paths. Confirm that alpha is ignored.
img . SetNRGBA ( x , 0 , color . NRGBA { uint8 ( ( 3 * x ) << 2 ) , uint8 ( ( 3 * x + 1 ) << 2 ) , uint8 ( ( 3 * x + 2 ) << 2 ) , 0 } )
im . SetNRGBA ( x , 0 , color . NRGBA { uint8 ( ( 3 * x ) << 2 ) , uint8 ( ( 3 * x + 1 ) << 2 ) , uint8 ( ( 3 * x + 2 ) << 2 ) , 0 } )
}
}
buf := bytes . Buffer { }
return im
o := DefaultOpts
} ( ) ,
o . NumPixels = 16
want : expectedi250t6500 ,
o . Intensity = 250
opts : Opts {
o . Temperature = 6500
NumPixels : 16 ,
d , _ := New ( spitest . NewRecordRaw ( & buf ) , & o )
Intensity : 250 ,
if err := d . Draw ( d . Bounds ( ) , img , image . Point { } ) ; err != nil {
Temperature : 6500 ,
t . Fatal ( err )
} ,
}
} ,
if ! bytes . Equal ( expectedi250t6500 , buf . Bytes ( ) ) {
{
t . Fatalf ( "%#v != %#v" , expectedi250t5000 , buf . Bytes ( ) )
name : "Draw RGBA" ,
}
img : func ( ) image . Image {
}
im := image . NewRGBA ( image . Rect ( 0 , 0 , 16 , 1 ) )
func TestDrawRGBA ( t * testing . T ) {
img := image . NewRGBA ( image . Rect ( 0 , 0 , 16 , 1 ) )
for i := 0 ; i < 16 ; i ++ {
for i := 0 ; i < 16 ; i ++ {
// Test all intensity code paths. Alpha is not ignored in this case.
// Test all intensity code paths. Alpha is not ignored in this case.
img . Pix [ 4 * i ] = uint8 ( ( 3 * i ) << 2 )
im . Pix [ 4 * i ] = uint8 ( ( 3 * i ) << 2 )
img . Pix [ 4 * i + 1 ] = uint8 ( ( 3 * i + 1 ) << 2 )
im . Pix [ 4 * i + 1 ] = uint8 ( ( 3 * i + 1 ) << 2 )
img . Pix [ 4 * i + 2 ] = uint8 ( ( 3 * i + 2 ) << 2 )
im . Pix [ 4 * i + 2 ] = uint8 ( ( 3 * i + 2 ) << 2 )
img . Pix [ 4 * i + 3 ] = 0xFF
im . Pix [ 4 * i + 3 ] = 0xFF
}
return im
} ( ) ,
want : expectedi250t5000 ,
opts : Opts {
NumPixels : 16 ,
Intensity : 250 ,
Temperature : 5000 ,
} ,
} ,
}
}
func TestDraws ( t * testing . T ) {
for _ , tt := range drawTests {
buf := bytes . Buffer { }
buf := bytes . Buffer { }
o := DefaultOpts
d , _ := New ( spitest . NewRecordRaw ( & buf ) , & tt . opts )
o . NumPixels = 16
if err := d . Draw ( d . Bounds ( ) , tt . img , image . Point { } ) ; err != nil {
o . Intensity = 250
t . Fatalf ( "%s: %v" , tt . name , err )
o . Temperature = 5000
}
d , _ := New ( spitest . NewRecordRaw ( & buf ) , & o )
if ! bytes . Equal ( buf . Bytes ( ) , tt . want ) {
if err := d . Draw ( d . Bounds ( ) , img , image . Point { } ) ; err != nil {
t . Fatalf ( "%s:\ngot: %#v\nwant: %#v\n" , tt . name , buf . Bytes ( ) , tt . want )
t . Fatal ( err )
}
}
if ! bytes . Equal ( expectedi250t5000 , buf . Bytes ( ) ) {
t . Fatalf ( "%#v != %#v" , expectedi250t5000 , buf . Bytes ( ) )
}
}
}
}