sointu/compiler/encoded_song_test.go
vsariola 5dd81430b7 feat(sointu): make patterns local to track
The global pattern table is constructed only during compilation. At this point, we can do also all sorts of optimizations / changes e.g. remove unnecessary releases and reuse patterns if there's a pattern already that could be used.
2021-01-03 01:06:59 +02:00

82 lines
2.4 KiB
Go

package compiler_test
import (
"reflect"
"testing"
"github.com/vsariola/sointu"
"github.com/vsariola/sointu/compiler"
)
func TestPatternReusing(t *testing.T) {
song := sointu.Song{
Hold: 1,
Tracks: []sointu.Track{{
Patterns: [][]byte{{64, 1, 1, 1, 0, 0, 0, 0}, {72, 0, 0, 0, 0, 0, 0, 0}},
Sequence: []byte{0, 1},
}, {
Patterns: [][]byte{{64, 1, 1, 1, 0, 0, 0, 0}, {84, 0, 0, 0, 0, 0, 0, 0}},
Sequence: []byte{0, 1},
}},
}
encodedSong, err := compiler.EncodeSong(&song)
if err != nil {
t.Fatalf("song encoding error: %v", err)
}
expected := compiler.EncodedSong{
Sequences: [][]byte{{0, 1}, {0, 2}},
Patterns: [][]byte{{64, 1, 1, 1, 0, 0, 0, 0}, {72, 0, 0, 0, 0, 0, 0, 0}, {84, 0, 0, 0, 0, 0, 0, 0}},
}
if !reflect.DeepEqual(*encodedSong, expected) {
t.Fatalf("got different EncodedSong than expected. got: %v expected: %v", *encodedSong, expected)
}
}
func TestUnnecessaryHolds(t *testing.T) {
song := sointu.Song{
Hold: 1,
Tracks: []sointu.Track{{
Patterns: [][]byte{{64, 1, 1, 1, 0, 1, 0, 0}, {72, 0, 1, 0, 1, 0, 0, 0}},
Sequence: []byte{0, 1},
}, {
Patterns: [][]byte{{64, 1, 1, 1, 0, 0, 1, 0}, {84, 0, 0, 0, 1, 1, 0, 0}},
Sequence: []byte{0, 1},
}},
}
encodedSong, err := compiler.EncodeSong(&song)
if err != nil {
t.Fatalf("song encoding error: %v", err)
}
expected := compiler.EncodedSong{
Sequences: [][]byte{{0, 1}, {0, 2}},
Patterns: [][]byte{{64, 1, 1, 1, 0, 0, 0, 0}, {72, 0, 0, 0, 0, 0, 0, 0}, {84, 0, 0, 0, 0, 0, 0, 0}},
}
if !reflect.DeepEqual(*encodedSong, expected) {
t.Fatalf("got different EncodedSong than expected. got: %v expected: %v", *encodedSong, expected)
}
}
func TestDontCares(t *testing.T) {
song := sointu.Song{
Hold: 1,
Tracks: []sointu.Track{{
Patterns: [][]byte{{64, 1, 1, 1, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}},
Sequence: []byte{0, 1},
}, {
Patterns: [][]byte{{64, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 0, 0, 0, 0, 0}},
Sequence: []byte{0, 1},
}},
}
encodedSong, err := compiler.EncodeSong(&song)
if err != nil {
t.Fatalf("song encoding error: %v", err)
}
expected := compiler.EncodedSong{
Sequences: [][]byte{{0, 1}, {2, 1}},
Patterns: [][]byte{{64, 1, 1, 1, 0, 0, 0, 0}, {1, 1, 1, 0, 0, 0, 0, 0}, {64, 1, 1, 1, 1, 1, 1, 1}},
}
if !reflect.DeepEqual(*encodedSong, expected) {
t.Fatalf("got different EncodedSong than expected. got: %v expected: %v", *encodedSong, expected)
}
}