Skip to content

Instantly share code, notes, and snippets.

@slimsag
Created March 8, 2015 06:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slimsag/c32c51df1a9927a65f52 to your computer and use it in GitHub Desktop.
Save slimsag/c32c51df1a9927a65f52 to your computer and use it in GitHub Desktop.
decoder_test.go
package drum
import (
"bytes"
"fmt"
"io/ioutil"
"path"
"testing"
)
func TestDecodeFile(t *testing.T) {
tData := []struct {
path string
output string
}{
{"pattern_1.splice",
`Saved with HW Version: 0.808-alpha
Tempo: 120
(0) kick |x---|x---|x---|x---|
(1) snare |----|x---|----|x---|
(2) clap |----|x-x-|----|----|
(3) hh-open |--x-|--x-|x-x-|--x-|
(4) hh-close |x---|x---|----|x--x|
(5) cowbell |----|----|--x-|----|
`,
},
{"pattern_2.splice",
`Saved with HW Version: 0.808-alpha
Tempo: 98.4
(0) kick |x---|----|x---|----|
(1) snare |----|x---|----|x---|
(3) hh-open |--x-|--x-|x-x-|--x-|
(5) cowbell |----|----|x---|----|
`,
},
{"pattern_3.splice",
`Saved with HW Version: 0.808-alpha
Tempo: 118
(40) kick |x---|----|x---|----|
(1) clap |----|x---|----|x---|
(3) hh-open |--x-|--x-|x-x-|--x-|
(5) low-tom |----|---x|----|----|
(12) mid-tom |----|----|x---|----|
(9) hi-tom |----|----|-x--|----|
`,
},
{"pattern_4.splice",
`Saved with HW Version: 0.909
Tempo: 240
(0) SubKick |----|----|----|----|
(1) Kick |x---|----|x---|----|
(99) Maracas |x-x-|x-x-|x-x-|x-x-|
(255) Low Conga |----|x---|----|x---|
`,
},
{"pattern_5.splice",
`Saved with HW Version: 0.708-alpha
Tempo: 999
(1) Kick |x---|----|x---|----|
(2) HiHat |x-x-|x-x-|x-x-|x-x-|
`,
},
}
for _, exp := range tData {
decoded, err := DecodeFile(path.Join("fixtures", exp.path))
if err != nil {
t.Fatalf("something went wrong decoding %s - %v", exp.path, err)
}
if fmt.Sprint(decoded) != exp.output {
t.Logf("decoded:\n%#v\n", fmt.Sprint(decoded))
t.Logf("expected:\n%#v\n", exp.output)
t.Fatalf("%s wasn't decoded as expect.\nGot:\n%s\nExpected:\n%s",
exp.path, decoded, exp.output)
}
}
}
func BenchmarkDecode(b *testing.B) {
// Read the entire file into memory so that we don't benchmark the
// disk IO speed.
data, err := ioutil.ReadFile("fixtures/pattern_1.splice")
if err != nil {
b.Fatal(err)
}
for n := 0; n < b.N; n++ {
_, err = decode(bytes.NewReader(data))
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkPatternString(b *testing.B) {
p, err := DecodeFile("fixtures/pattern_1.splice")
if err != nil {
b.Fatal(err)
}
for n := 0; n < b.N; n++ {
p.String()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment