Check the text of structural edits and refuse stale instruments

AddRegion, RemoveRegion and MoveRegion now refuse an edit whose written text
would read differently from the nodes, as when a comment that never ends
takes in the text after it, and an instrument whose regions were changed
other than by Apply. Build refuses an unknown trigger and an empty random
range. Add tests for edits across files, defines, skipped regions and edge
cases, a random-sequence test and FuzzRegionEdits.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-02 01:26:36 +00:00
co-authored by Claude Sonnet 5.5
parent 49fd867283
commit 8db34cc16c
4 changed files with 395 additions and 17 deletions
+3 -3
View File
@@ -45,16 +45,16 @@ The model is read with each setting that a controller moves taken at the control
- `Instrument.Apply(i, region)` writes a change to a typed region back to the document that the instrument was read from, as changes to the region's own opcodes. It touches only the settings that changed, leaves unknown opcodes, comments and the other regions as they were, and puts every change in the region's own header, over what the region inherits. `Instrument.Patches` returns the changes without making them.
- It returns `ErrNoSource` if the region has no header to edit: a variable made the header, or an include or a variable brings in opcodes after it.
- It returns `ErrUnpatchable`, and changes nothing, for a change the header can't hold: removing an opcode the region inherits, or a sample outside `default_path`.
- `Instrument.AddRegion(region)`, `RemoveRegion(i)` and `MoveRegion(from, to)` change the document's structure and read the instrument again, so it is as the document reads. A removed or moved region takes the comments and spacing that follow its header. A new region goes at the end of the main file. Each changes nothing, and returns an error, if the result wouldn't read as intended.
- `Instrument.AddRegion(region)`, `RemoveRegion(i)` and `MoveRegion(from, to)` change the document's structure and read the instrument again, so it is as the document reads. A removed or moved region takes the comments and spacing that follow its header, so a comment on the lines above a `<region>` stays with the header before it. They read the document again, so they refuse an instrument whose regions were changed other than by `Apply`, or whose document was edited since it was read. A new region goes at the end of the main file. Each changes nothing, and returns an error, if the result wouldn't read as intended.
- `ErrNoSource` is for a region that has no header to move or remove, or an instrument that wasn't read from a document.
- `ErrUnpatchable` is for a region that would read differently where it ends up: a new region that a `default_path` changes, or a region moved under another `<group>`.
- `ErrUnpatchable` is for a region that would read differently where it ends up: a new region that a `default_path` changes, or a region moved under another `<group>` or over a `#define` that changes it, or text that wouldn't read back as the edited nodes, such as a comment that never ends.
- `Node` positions are worked out when asked for, with `File.Position`, so edits stay cheap.
## Writing and building
- `Document.Write` writes one file, and `WriteFiles` writes the main file and each file it includes, through a `FileWriter`. They take `WriteOptions`: `InlineIncludes` puts included files into the main file, `ExpandVariables` replaces variables with their values, and `LineEnding` sets the line break. The zero value writes each file as it was read.
- `Write` and `WriteTo` return an error for a document with includes unless `InlineIncludes` is set, as the files they hold would be lost. `WriteFiles` also returns an error for an include whose path is absolute or leaves the folder (`AllowParentPaths` opts in to `..` paths, not absolute ones), and for a file included more than once that would be written differently each time.
- `Build` makes a new document from an `Instrument`, with `BuildOptions`: `SplitFiles` puts regions in included files, `Variables` hoists repeated values into `#define`s and the samples' folder into `default_path`, and `Indent` sets the indent. Reading the result gives the instrument back, except for the opcodes and headers the typed model doesn't hold. It returns an error wrapping `ErrInvalid` for a value that can't be written, and for a region that wouldn't read back as it is, such as a zero `Region` with an empty `Trigger`. `NewRegion(sample)` makes a region with the format's defaults.
- `Build` makes a new document from an `Instrument`, with `BuildOptions`: `SplitFiles` puts regions in included files, `Variables` hoists repeated values into `#define`s and the samples' folder into `default_path`, and `Indent` sets the indent. Reading the result gives the instrument back, except for the opcodes and headers the typed model doesn't hold. It returns an error wrapping `ErrInvalid` for a value that can't be written, and for a region that wouldn't read back as it is, such as a zero `Region` with an empty `Trigger`, a `Trigger` the format doesn't have, or a random range that is empty. `NewRegion(sample)` makes a region with the format's defaults.
- A `Region.Sample` is written as one opcode, so `Build` and `Apply` refuse one with a line break, `//`, `/*`, `<`, or a space followed by `=` (as in `a b=c.wav`). A backslash reads back as `/`. A host that has such file names needs to map them to safe ones.
## Includes
+11
View File
@@ -5,6 +5,7 @@ import (
"fmt"
"math"
"path"
"slices"
"sort"
"strconv"
"strings"
@@ -72,6 +73,12 @@ func NewRegion(sample string) Region {
return r
}
// triggers returns the values that trigger can have.
func triggers() []string {
info, _ := Lookup("trigger")
return info.Options
}
// checkRegion returns an error saying why a region can't be written as it is:
// the opcodes for it would be empty, or would be read as something else, or
// would never match a note.
@@ -81,10 +88,14 @@ func checkRegion(r Region) error {
return errors.New("the sample is empty")
case r.Trigger == "":
return errors.New("the trigger is empty")
case !slices.Contains(triggers(), r.Trigger):
return fmt.Errorf("the trigger %q isn't one of %s", r.Trigger, strings.Join(triggers(), ", "))
case r.OffMode != "fast" && r.OffMode != "time" && r.OffMode != "normal":
return fmt.Errorf("the off mode %q isn't fast, time or normal", r.OffMode)
case r.SequenceLength < 1 || r.SequencePosition < 1:
return errors.New("the sequence length and position start at 1")
case r.HighRandom <= r.LowRandom:
return errors.New("the random range is empty, so the region never plays")
case r.HighKey < r.LowKey:
return errors.New("the highest key is below the lowest")
case r.HighVelocity < r.LowVelocity:
+37 -6
View File
@@ -30,7 +30,9 @@ func (inst *Instrument) Source(i int) (*FlatRegion, bool) {
// a document.
//
// Afterwards the instrument is as the document reads, with Skipped, Unread and
// Controls too. Use [Instrument.Source] to set opcodes the typed model doesn't
// Controls too. It returns an error wrapping [ErrNoSource], and changes nothing,
// if the document was edited since the instrument was read, or the instrument's
// regions were changed other than with [Instrument.Apply]. Use [Instrument.Source] to set opcodes the typed model doesn't
// hold on the new region's header.
func (inst *Instrument) AddRegion(r Region) error {
if err := checkRegion(r); err != nil {
@@ -73,7 +75,9 @@ func (inst *Instrument) writeRegion(r Region, all bool) error {
}
// RemoveRegion removes region i from the document that the instrument was read
// from, with the comments and spacing that follow its header. It changes
// from, with the comments and spacing that follow its header. A comment on the
// lines above a <region> follows the header before it, so it isn't removed
// with the region it is above. It changes
// nothing, and returns an error, if the document can't be edited there: an
// error wrapping [ErrNoSource] if there is no region i or it has no header of
// its own to remove. Afterwards the instrument is as the document reads.
@@ -95,7 +99,8 @@ func (inst *Instrument) RemoveRegion(i int) error {
// MoveRegion moves region from in the document that the instrument was read
// from so that it is region to, with the comments and spacing that follow its
// header. The regions between keep their order, and so do the regions the typed
// header, as for [Instrument.RemoveRegion]. The regions between keep their
// order, and so do the regions the typed
// model has no region for. It changes nothing, and returns an error, if the
// document can't be edited there: an error wrapping [ErrNoSource] if either
// region has no header of its own to move, and [ErrUnpatchable] if the headers
@@ -157,7 +162,7 @@ func (inst *Instrument) restructure(change func() error, want func(old []Region)
}
before := inst.doc.Flatten().Instrument()
if !inst.matches(before) {
return fmt.Errorf("%w: the document has changed since the instrument was read", ErrNoSource)
return fmt.Errorf("%w: the document or the instrument has changed since the instrument was read, so read it again", ErrNoSource)
}
saved := map[*File][]*Node{}
for _, f := range inst.doc.Files() {
@@ -173,6 +178,15 @@ func (inst *Instrument) restructure(change func() error, want func(old []Region)
undo()
return err
}
// The text that is written has to read back as the nodes, which it doesn't
// when a comment that never ends, or a line comment, would take in the text
// that comes after it.
for _, f := range inst.doc.Files() {
if !slices.Equal(significantNodes(lex(f.String())), significantNodes(f.Nodes)) {
undo()
return fmt.Errorf("%w: the text of %q would read differently from the nodes", ErrUnpatchable, f.Path)
}
}
after := inst.doc.Flatten().Instrument()
expected := want(before.Regions)
if len(after.Regions) != len(expected) {
@@ -190,10 +204,24 @@ func (inst *Instrument) restructure(change func() error, want func(old []Region)
return nil
}
// significantNodes returns the kind and text of the nodes that aren't
// whitespace.
func significantNodes(nodes []*Node) []string {
var out []string
for _, n := range nodes {
if n.Kind != KindSpace {
out = append(out, n.Kind.String()+":"+n.Raw)
}
}
return out
}
// matches reports whether other, read from the instrument's document, has the
// regions that the instrument was read with, from the same headers.
// regions that the instrument holds, from the same headers. An instrument whose
// regions were changed other than by [Instrument.Apply] doesn't match, as
// reading it again would lose the change.
func (inst *Instrument) matches(other *Instrument) bool {
if len(inst.sources) != len(other.sources) {
if len(inst.sources) != len(other.sources) || len(inst.Regions) != len(other.Regions) {
return false
}
for i, s := range inst.sources {
@@ -201,6 +229,9 @@ func (inst *Instrument) matches(other *Instrument) bool {
if (s.Header == nil) != (o.Header == nil) || s.Header != nil && s.Header.Node != o.Header.Node {
return false
}
if len(differing(inst.Regions[i], other.Regions[i])) > 0 {
return false
}
}
return true
}
+341 -5
View File
@@ -2,9 +2,11 @@ package sfz
import (
"errors"
"math/rand"
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
"testing"
)
@@ -210,14 +212,15 @@ func TestRegionEditsKeepUnknownOpcodes(t *testing.T) {
if src, _ := out.Source(1); src.Own["zampler_zone"] != "zb" {
t.Errorf("zampler_zone didn't survive writing: %q", doc.String())
}
if problems := doc.Validate(); len(problems) == 0 {
t.Log("zampler_zone isn't reported")
} else {
for _, p := range problems {
var reported bool
for _, p := range doc.Validate() {
if p.Severity != SeverityInfo {
t.Errorf("problem %+v", p)
}
reported = reported || p.Code == "opcode.unknown"
}
if !reported {
t.Error("zampler_zone isn't reported as an unknown opcode")
}
}
@@ -225,7 +228,7 @@ func TestRegionEditsKeepUnknownOpcodes(t *testing.T) {
// region, and that a region from NewRegion builds and reads back.
func TestBuildRefusesZeroRegion(t *testing.T) {
_, err := Build(&Instrument{Regions: []Region{{Sample: "a.wav"}}})
if !errors.Is(err, ErrInvalid) || !strings.Contains(err.Error(), "trigger") {
if !errors.Is(err, ErrInvalid) || !strings.Contains(err.Error(), "region 0: the trigger is empty") {
t.Errorf("error %v", err)
}
inst := &Instrument{Regions: []Region{NewRegion("a.wav")}}
@@ -288,3 +291,336 @@ func TestRegionEditsOnFixtures(t *testing.T) {
}
}
}
// TestCheckRegion checks each way that a region is refused.
func TestCheckRegion(t *testing.T) {
tests := map[string]struct {
change func(*Region)
want string
}{
"empty sample": {func(r *Region) { r.Sample = "" }, "sample is empty"},
"empty trigger": {func(r *Region) { r.Trigger = "" }, "trigger is empty"},
"unknown trigger": {func(r *Region) { r.Trigger = "bogus" }, `trigger "bogus"`},
"unknown off mode": {func(r *Region) { r.OffMode = "" }, "off mode"},
"sequence length": {func(r *Region) { r.SequenceLength = 0 }, "sequence"},
"sequence position": {func(r *Region) { r.SequencePosition = 0 }, "sequence"},
"never plays": {func(r *Region) { r.HighRandom = 0 }, "random range"},
"empty random range": {func(r *Region) { r.LowRandom, r.HighRandom = 0.5, 0.5 }, "random range"},
"keys": {func(r *Region) { r.LowKey, r.HighKey = 10, 9 }, "highest key"},
"velocities": {func(r *Region) { r.LowVelocity, r.HighVelocity = 10, 9 }, "highest velocity"},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
r := NewRegion("a.wav")
tc.change(&r)
_, err := Build(&Instrument{Regions: []Region{r}})
if !errors.Is(err, ErrInvalid) || !strings.Contains(err.Error(), tc.want) {
t.Errorf("Build returned %v", err)
}
})
}
for _, trigger := range []string{"attack", "release", "first", "legato", "release_key"} {
r := NewRegion("a.wav")
r.Trigger = trigger
if _, err := Build(&Instrument{Regions: []Region{r}}); err != nil {
t.Errorf("trigger %s: %v", trigger, err)
}
}
}
// TestRegionEditEdgeCases checks edits at the edges of the text: layout that
// the text of a region's span can't be moved through, and documents that don't
// end in a line break.
func TestRegionEditEdgeCases(t *testing.T) {
tests := map[string]struct {
text string
edit func(*Instrument) error
want string // the document afterwards, or "" if the edit is refused
}{
"move a region that ends in a comment that never ends": {
"<region> sample=a.wav\n<region> sample=b.wav\n/* open",
func(i *Instrument) error { return i.MoveRegion(1, 0) }, "",
},
"add after a comment that never ends": {
"<region> sample=a.wav /* open",
func(i *Instrument) error { return i.AddRegion(NewRegion("b.wav")) }, "",
},
"move the last of a file with no final line break": {
"<region> sample=a.wav\n<region> sample=b.wav",
func(i *Instrument) error { return i.MoveRegion(1, 0) }, "<region> sample=b.wav\n<region> sample=a.wav\n",
},
"remove the only region": {
"<region> sample=a.wav\n",
func(i *Instrument) error { return i.RemoveRegion(0) }, "",
},
"comments above a header stay with the header before": {
"// first\n<region> sample=a.wav\n// second\n<region> sample=b.wav\n",
func(i *Instrument) error { return i.RemoveRegion(0) }, "// first\n<region> sample=b.wav\n",
},
"add to an empty document": {
"",
func(i *Instrument) error { return i.AddRegion(NewRegion("a.wav")) }, "<region>\nsample=a.wav\n",
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
doc := mustParse(t, tc.text, nil)
inst := readInstrument(t, doc)
n := len(inst.Regions)
err := tc.edit(inst)
switch {
case tc.want == "" && name == "remove the only region":
if err != nil || doc.String() != "" || len(inst.Regions) != 0 {
t.Errorf("err %v, document %q, %d regions", err, doc.String(), len(inst.Regions))
}
case tc.want == "":
if !(errors.Is(err, ErrUnpatchable) || errors.Is(err, ErrInvalid)) || doc.String() != tc.text || len(inst.Regions) != n {
t.Errorf("err %v, document %q, %d regions", err, doc.String(), len(inst.Regions))
}
default:
if err != nil || doc.String() != tc.want {
t.Errorf("err %v, document %q, want %q", err, doc.String(), tc.want)
}
}
if got := readInstrument(t, mustParse(t, doc.String(), nil)); len(got.Regions) != len(inst.Regions) {
t.Errorf("the written text reads as %d regions, the instrument has %d", len(got.Regions), len(inst.Regions))
}
})
}
}
// TestRegionEditsNeedAnUnchangedInstrument checks that an edit refuses an
// instrument that the document or the caller has changed since it was read.
func TestRegionEditsNeedAnUnchangedInstrument(t *testing.T) {
doc := mustParse(t, "<region> sample=a.wav\n", nil)
inst := readInstrument(t, doc)
inst.Regions[0].Volume = -9
if err := inst.AddRegion(NewRegion("b.wav")); !errors.Is(err, ErrNoSource) || doc.String() != "<region> sample=a.wav\n" {
t.Errorf("a changed region: %v, %q", err, doc.String())
}
// Apply is how a region is changed.
inst = readInstrument(t, doc)
r := inst.Regions[0]
r.Volume = -9
if err := inst.Apply(0, r); err != nil {
t.Fatal(err)
}
if err := inst.AddRegion(NewRegion("b.wav")); err != nil {
t.Errorf("after Apply: %v", err)
}
h, _ := doc.AddHeader("region", nil)
h.Set("sample", "q.wav")
if err := inst.RemoveRegion(0); !errors.Is(err, ErrNoSource) {
t.Errorf("a changed document: %v", err)
}
if err := (&Instrument{Regions: []Region{NewRegion("a.wav")}}).AddRegion(NewRegion("b.wav")); !errors.Is(err, ErrNoSource) {
t.Errorf("an instrument not read from a document: %v", err)
}
}
// TestRegionEditsWithSkippedRegions checks that typed indexes count only the
// regions the model has, that skipped regions stay where they are, and that
// Apply and Source follow the edits.
func TestRegionEditsWithSkippedRegions(t *testing.T) {
text := "<region> sample=a.wav\n<region> lokey=1\n<region> sample=b.wav\n<region> hikey=2\n<region> sample=c.wav\n"
doc := mustParse(t, text, nil)
inst := readInstrument(t, doc)
if err := inst.MoveRegion(2, 0); err != nil {
t.Fatal(err)
}
if want := "<region> sample=c.wav\n<region> sample=a.wav\n<region> lokey=1\n<region> sample=b.wav\n<region> hikey=2\n"; doc.String() != want {
t.Errorf("document %q", doc.String())
}
if regionSamples(inst) != "c.wav a.wav b.wav" || inst.Skipped[SkippedNoSample] != 2 {
t.Errorf("regions %q, skipped %v", regionSamples(inst), inst.Skipped)
}
r := inst.Regions[2]
r.Pan = 10
if err := inst.Apply(2, r); err != nil {
t.Fatal(err)
}
if !strings.Contains(doc.String(), "sample=b.wav pan=10") {
t.Errorf("Apply changed the wrong header: %q", doc.String())
}
if err := inst.RemoveRegion(1); err != nil {
t.Fatal(err)
}
if regionSamples(inst) != "c.wav b.wav" || strings.Count(doc.String(), "<region>") != 4 {
t.Errorf("regions %q, document %q", regionSamples(inst), doc.String())
}
}
// TestRegionEditsAcrossFiles checks edits of documents that have includes.
func TestRegionEditsAcrossFiles(t *testing.T) {
files := map[string]string{"one.sfz": "<region> sample=a.wav\n<region> sample=b.wav\n", "two.sfz": "<region> sample=c.wav\n"}
doc := mustParse(t, "#include \"one.sfz\"\n#include \"two.sfz\"\n", files)
inst := readInstrument(t, doc)
if err := inst.MoveRegion(2, 0); err != nil {
t.Fatal(err)
}
if regionSamples(inst) != "c.wav a.wav b.wav" {
t.Errorf("regions %q", regionSamples(inst))
}
var one, two string
for _, f := range doc.Files() {
if f.Path == "one.sfz" {
one = f.String()
} else if f.Path == "two.sfz" {
two = f.String()
}
}
if one != "<region> sample=c.wav\n<region> sample=a.wav\n<region> sample=b.wav\n" || strings.Contains(two, "region") {
t.Errorf("one.sfz %q, two.sfz %q", one, two)
}
if err := inst.AddRegion(NewRegion("d.wav")); err != nil {
t.Fatal(err)
}
if got := doc.Root.String(); !strings.HasSuffix(got, "<region>\nsample=d.wav\n") || strings.Count(got, "#include") != 2 {
t.Errorf("main file %q", got)
}
if regionSamples(inst) != "c.wav a.wav b.wav d.wav" {
t.Errorf("regions %q", regionSamples(inst))
}
}
// TestRegionEditsWithDefines checks that moving a region over a #define that
// changes it is refused, and that one that doesn't is allowed.
func TestRegionEditsWithDefines(t *testing.T) {
text := "#define $v 1\n<region> sample=a.wav volume=$v\n#define $v 2\n<region> sample=b.wav volume=$v\n"
doc := mustParse(t, text, nil)
inst := readInstrument(t, doc)
if err := inst.MoveRegion(1, 0); !errors.Is(err, ErrUnpatchable) || doc.String() != text {
t.Errorf("a move past a #define: %v, %q", err, doc.String())
}
}
// TestAddRegionFields checks that a region with settings that are read from
// several opcodes is added as it is.
func TestAddRegionFields(t *testing.T) {
doc := mustParse(t, "<region> sample=a.wav\n", nil)
inst := readInstrument(t, doc)
r := NewRegion("b.wav")
width := 50.0
r.RootFromSample, r.Width, r.Tune, r.Pan = true, &width, 12, -30
r.LowKey, r.HighKey, r.LowVelocity, r.HighVelocity = 36, 72, 20, 100
r.VelocityCurve = []CurvePoint{{Velocity: 0, Gain: 0}, {Velocity: 30, Gain: 0.5}, {Velocity: 100, Gain: 0.9}, {Velocity: 127, Gain: 1}}
r.Envelope.Attack, r.Envelope.Release = 0.1, 1.5
r.Trigger, r.SequenceLength, r.SequencePosition = "release", 4, 2
r.LowRandom, r.HighRandom = 0.25, 0.5
if err := inst.AddRegion(r); err != nil {
t.Fatal(err)
}
if wrong := differing(inst.Regions[1], r); len(wrong) > 0 {
t.Errorf("the region differs in %v", wrong)
}
}
// editOps applies the edits that ops asks for to the document of text, and
// fails the test if an edit that is accepted writes text that reads as
// something else than the instrument, or one that is refused changes anything.
// Each byte of ops is an edit: its low bits choose which, and the rest which
// regions.
func editOps(t *testing.T, text string, inc map[string]string, ops []byte) {
t.Helper()
doc, err := Parse(strings.NewReader(text), WithResolver(files(inc)))
if err != nil {
t.Skip()
}
inst := doc.Flatten().Instrument()
for _, op := range ops {
n := len(inst.Regions)
before := doc.String()
var err error
var name string
switch op % 3 {
case 0:
name = "AddRegion"
err = inst.AddRegion(NewRegion("n" + strconv.Itoa(int(op)) + ".wav"))
case 1:
if n == 0 {
continue
}
name = "RemoveRegion"
err = inst.RemoveRegion(int(op/3) % n)
case 2:
if n == 0 {
continue
}
name = "MoveRegion"
err = inst.MoveRegion(int(op/3)%n, int(op/3/7)%n)
}
if err != nil {
if doc.String() != before || len(inst.Regions) != n {
t.Fatalf("%s was refused with %v and changed the document to %q", name, err, doc.String())
}
continue
}
fresh := doc.Flatten().Instrument()
reparsed, perr := Parse(strings.NewReader(doc.String()), WithResolver(files(inc)))
if perr != nil {
t.Fatalf("%s wrote text that doesn't parse: %v", name, perr)
}
// The text of each file has to read back as its nodes. A file with
// includes isn't written by String, so each file is checked on its own.
for _, f := range doc.Files() {
if got, want := significant(lex(f.String())), significant(f.Nodes); !reflect.DeepEqual(got, want) {
t.Fatalf("%s wrote %q, which reads as %q, not %q", name, f.String(), got, want)
}
}
if len(fresh.Regions) != len(inst.Regions) {
t.Fatalf("%s: the instrument has %d regions, the document %d", name, len(inst.Regions), len(fresh.Regions))
}
for i := range fresh.Regions {
if wrong := differing(fresh.Regions[i], inst.Regions[i]); len(wrong) > 0 {
t.Fatalf("%s: region %d differs from a fresh read in %v", name, i, wrong)
}
}
if len(reparsed.Flatten().Instrument().Regions) != len(inst.Regions) && len(inc) == 0 {
t.Fatalf("%s: the written text %q reads as other than the instrument", name, doc.String())
}
}
}
// regionEditTexts are documents for the tests of edits in sequence.
var regionEditTexts = []string{
"<region> sample=a.wav\n<region> sample=b.wav\n<region> sample=c.wav\n",
"<group> volume=-3\n<region> sample=a.wav // one\n zampler_zone=1\n<region> lokey=3\n<group> hikey=60\n<region> sample=b.wav\n<region> sample=c.wav\n",
"// top\n<control> default_path=S/\n<region> sample=S/a.wav /* note */ <region> sample=S/b.wav\n",
"<region> sample=a.wav\n<region> sample=b.wav\n/* open",
"#define $x 1\n<region> sample=a.wav volume=$x\n#define $x 2\n<region> sample=b.wav volume=$x",
"<region> sample=a.wav\r\n<region> sample=b.wav\r\n",
"",
}
// TestRegionEditSequences applies random sequences of edits to documents.
func TestRegionEditSequences(t *testing.T) {
rng := rand.New(rand.NewSource(1))
for _, text := range regionEditTexts {
for run := 0; run < 60; run++ {
ops := make([]byte, 1+rng.Intn(12))
for i := range ops {
ops[i] = byte(rng.Intn(256))
}
editOps(t, text, nil, ops)
}
}
files := map[string]string{"one.sfz": "<region> sample=a.wav\n<group> volume=1\n<region> sample=b.wav\n", "two.sfz": "<region> sample=c.wav\n"}
for run := 0; run < 60; run++ {
ops := make([]byte, 1+rng.Intn(12))
for i := range ops {
ops[i] = byte(rng.Intn(256))
}
editOps(t, "#include \"one.sfz\"\n#include \"two.sfz\"\n", files, ops)
}
}
// FuzzRegionEdits checks that any sequence of region edits, of any document,
// either fails and changes nothing, or writes text that reads as the instrument.
func FuzzRegionEdits(f *testing.F) {
for _, text := range regionEditTexts {
f.Add(text, []byte{0, 4, 5, 8, 14, 3, 20})
}
f.Fuzz(func(t *testing.T, text string, ops []byte) {
editOps(t, text, nil, ops)
})
}