package sfz import ( "errors" "math/rand" "os" "path" "path/filepath" "reflect" "strconv" "strings" "testing" ) // regionSamples returns the samples of an instrument's regions, in order. func regionSamples(inst *Instrument) string { var out []string for _, r := range inst.Regions { out = append(out, r.Sample) } return strings.Join(out, " ") } // TestSource checks that a typed region finds its own header when other regions // are skipped, which makes the typed and flat indexes differ. func TestSource(t *testing.T) { doc := mustParse(t, " sample=a.wav\n lokey=1\n sample=b.wav\n sample=c.wav hikey=3 hikey=2 lokey=9\n", nil) inst := readInstrument(t, doc) if regionSamples(inst) != "a.wav b.wav" || inst.Skipped[SkippedNoSample] != 1 || inst.Skipped[SkippedNotKeys] != 1 { t.Fatalf("regions %q, skipped %v", regionSamples(inst), inst.Skipped) } src, ok := inst.Source(1) if !ok || src.Own["sample"] != "b.wav" || src.Line != 3 { t.Fatalf("source %+v %v", src, ok) } if err := src.Header.Set("zampler_zone", "abc123"); err != nil { t.Fatal(err) } if got := doc.Flatten().Regions[2].Own["zampler_zone"]; got != "abc123" { t.Errorf("zampler_zone is %q on the flat region", got) } for _, i := range []int{-1, 2} { if _, ok := inst.Source(i); ok { t.Errorf("Source(%d) found a region", i) } } } // TestRegionEdits checks the text and the instrument after each structural // edit, and that comments and opcodes the model doesn't read stay. func TestRegionEdits(t *testing.T) { text := " volume=-3\n sample=a.wav // first\n zampler_zone=1\n sample=b.wav\n sample=c.wav zampler_zone=3\n" tests := map[string]struct { edit func(*Instrument) error want string next string }{ "remove the first": { func(i *Instrument) error { return i.RemoveRegion(0) }, " volume=-3\n sample=b.wav\n sample=c.wav zampler_zone=3\n", "b.wav c.wav", }, "remove the last": { func(i *Instrument) error { return i.RemoveRegion(2) }, " volume=-3\n sample=a.wav // first\n zampler_zone=1\n sample=b.wav\n", "a.wav b.wav", }, "move up": { func(i *Instrument) error { return i.MoveRegion(2, 0) }, " volume=-3\n sample=c.wav zampler_zone=3\n sample=a.wav // first\n zampler_zone=1\n sample=b.wav\n", "c.wav a.wav b.wav", }, "move down": { func(i *Instrument) error { return i.MoveRegion(0, 2) }, " volume=-3\n sample=b.wav\n sample=c.wav zampler_zone=3\n sample=a.wav // first\n zampler_zone=1\n", "b.wav c.wav a.wav", }, "add": { func(i *Instrument) error { r := i.Regions[0] r.Sample, r.Volume = "d.wav", -3 return i.AddRegion(r) }, text + "\nsample=d.wav\nvolume=-3\n", "a.wav b.wav c.wav d.wav", }, } for name, tc := range tests { t.Run(name, func(t *testing.T) { doc := mustParse(t, text, nil) inst := readInstrument(t, doc) if err := tc.edit(inst); err != nil { t.Fatal(err) } if got := doc.String(); got != tc.want { t.Errorf("document\n%q\nwant\n%q", got, tc.want) } if regionSamples(inst) != tc.next { t.Errorf("instrument has %q", regionSamples(inst)) } fresh := readInstrument(t, mustParse(t, doc.String(), nil)) if len(fresh.Regions) != len(inst.Regions) { t.Fatalf("the document reads as %d regions, not %d", len(fresh.Regions), len(inst.Regions)) } for i := range fresh.Regions { if wrong := differing(fresh.Regions[i], inst.Regions[i]); len(wrong) > 0 { t.Errorf("region %d differs from a fresh read in %v", i, wrong) } } // The instrument's sources still lead to the headers. for i := range inst.Regions { src, ok := inst.Source(i) if !ok || src.Header == nil || src.Own["sample"] != inst.Regions[i].Sample { t.Errorf("source %d is %+v", i, src) } } }) } } // TestAddRegionOverridesInherited checks that a new region overrides what the // header above it sets, and that a default_path is taken off its sample. func TestAddRegionOverridesInherited(t *testing.T) { doc := mustParse(t, " default_path=S/\n hikey=60 volume=-6\n sample=a.wav\n", nil) inst := readInstrument(t, doc) r := NewRegion("S/b.wav") if err := inst.AddRegion(r); err != nil { t.Fatal(err) } if got := inst.Regions[1]; got.HighKey != 127 || got.Volume != 0 || got.Sample != "S/b.wav" { t.Errorf("region %+v", got) } if !strings.Contains(doc.String(), "sample=b.wav") || strings.Contains(doc.String(), "S/b.wav") { t.Errorf("document %q", doc.String()) } } // TestAddRegionResetsInheritance checks that a region added under headers that // set what a region can't cancel gets an empty before it, once, and // that the other regions are as they were. func TestAddRegionResetsInheritance(t *testing.T) { text := " note_polyphony=2 volume=-6\n ampeg_release=3 volume_oncc7=-12\n sample=a.wav\n" doc := mustParse(t, text, nil) inst := readInstrument(t, doc) a := inst.Regions[0] r := NewRegion("b.wav") if err := inst.AddRegion(r); err != nil { t.Fatal(err) } if want := text + "\n\nsample=b.wav\n"; doc.String() != want { t.Errorf("document %q, want %q", doc.String(), want) } if wrong := differing(inst.Regions[0], a); len(wrong) > 0 { t.Errorf("the first region changed in %v", wrong) } if wrong := differing(inst.Regions[1], r); len(wrong) > 0 { t.Errorf("the new region differs in %v", wrong) } // The next region inherits nothing, so it needs no header. if err := inst.AddRegion(NewRegion("c.wav")); err != nil { t.Fatal(err) } if got := strings.Count(doc.String(), ""); got != 2 { t.Errorf("%d headers in %q", got, doc.String()) } if err := inst.MoveRegion(2, 1); err != nil { t.Errorf("moving the added regions: %v", err) } } // TestRegionEditRefusals checks that an edit that can't be made says why and // leaves the document as it was. func TestRegionEditRefusals(t *testing.T) { text := " hikey=50\n sample=a.wav\n hikey=70\n sample=b.wav\n sample=c.wav\n" files := map[string]string{"r.sfz": " sample=x.wav\n"} tests := map[string]struct { text string edit func(*Instrument) error want error }{ "remove out of range": {text, func(i *Instrument) error { return i.RemoveRegion(3) }, ErrNoSource}, "move out of range": {text, func(i *Instrument) error { return i.MoveRegion(0, -1) }, ErrNoSource}, "move across a group": {text, func(i *Instrument) error { return i.MoveRegion(0, 2) }, ErrUnpatchable}, "add under a group": {text, func(i *Instrument) error { r := NewRegion("d.wav") r.HighKey = 127 r.Pan = 3 r.Width = nil return i.AddRegion(r) }, nil}, "add a zero region": {text, func(i *Instrument) error { return i.AddRegion(Region{Sample: "d.wav"}) }, ErrInvalid}, "add a bad sample": {text, func(i *Instrument) error { return i.AddRegion(NewRegion("a b=c.wav")) }, ErrInvalid}, "remove an include": {"#include \"r.sfz\"\n sample=y.wav\n", func(i *Instrument) error { return i.RemoveRegion(1) }, nil}, "remove before include": {" sample=y.wav\n#include \"r.sfz\"\n", func(i *Instrument) error { return i.RemoveRegion(0) }, ErrNoSource}, } for name, tc := range tests { t.Run(name, func(t *testing.T) { doc := mustParse(t, tc.text, files) inst := readInstrument(t, doc) before := doc.String() err := tc.edit(inst) if tc.want == nil { if err != nil { t.Fatal(err) } return } if !errors.Is(err, tc.want) { t.Fatalf("error %v, want %v", err, tc.want) } if doc.String() != before { t.Errorf("document changed to %q", doc.String()) } if len(inst.Regions) != len(readInstrument(t, doc).Regions) { t.Errorf("instrument changed") } }) } } // TestRegionEditsKeepUnknownOpcodes checks that opcodes the format doesn't // define, set through a region's header, survive every kind of edit and a // write. func TestRegionEditsKeepUnknownOpcodes(t *testing.T) { doc := mustParse(t, " sample=a.wav\n sample=b.wav\n", nil) inst := readInstrument(t, doc) for i, id := range []string{"za", "zb"} { src, _ := inst.Source(i) if err := src.Header.Set("zampler_zone", id); err != nil { t.Fatal(err) } } if err := inst.AddRegion(NewRegion("c.wav")); err != nil { t.Fatal(err) } if err := inst.MoveRegion(2, 0); err != nil { t.Fatal(err) } if err := inst.RemoveRegion(1); err != nil { t.Fatal(err) } var got []string for i := range inst.Regions { src, _ := inst.Source(i) got = append(got, inst.Regions[i].Sample+":"+src.Own["zampler_zone"]) } if strings.Join(got, " ") != "c.wav: b.wav:zb" { t.Errorf("regions %v\n%s", got, doc.String()) } out := readInstrument(t, mustParse(t, doc.String(), nil)) if src, _ := out.Source(1); src.Own["zampler_zone"] != "zb" { t.Errorf("zampler_zone didn't survive writing: %q", doc.String()) } 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") } } // TestBuildRefusesZeroRegion checks that Build says what is wrong with a zero // 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(), "region 0: the trigger is empty") { t.Errorf("error %v", err) } inst := &Instrument{Regions: []Region{NewRegion("a.wav")}} doc, err := Build(inst) if err != nil { t.Fatal(err) } if got := readInstrument(t, doc); len(got.Regions) != 1 || len(differing(got.Regions[0], inst.Regions[0])) > 0 { t.Errorf("read back %+v", got.Regions) } } // TestRegionEditsOnFixtures checks, on a real instrument, that removing and // moving regions leaves the instrument as a fresh read of the // document, and that a move to another group is refused. func TestRegionEditsOnFixtures(t *testing.T) { doc, err := ParseFile(os.DirFS(filepath.Join("testdata", "splendid")), "Splendid Grand Piano.sfz") if err != nil { t.Fatal(err) } inst := doc.Flatten().Instrument() n := len(inst.Regions) before := doc.String() if err := inst.MoveRegion(n-1, 0); !errors.Is(err, ErrUnpatchable) || doc.String() != before { t.Fatalf("a move to another group returned %v", err) } // Two neighbours that inherit the same opcodes can swap. pair := -1 for i := 0; i+1 < n && pair < 0; i++ { if reflect.DeepEqual(inst.sources[i].Inherited, inst.sources[i+1].Inherited) { pair = i } } if pair < 0 { t.Fatal("no neighbours share a group") } first, second := inst.Regions[pair].Sample, inst.Regions[pair+1].Sample steps := []struct { name string do func() error n int at int want string }{ {"swap neighbours", func() error { return inst.MoveRegion(pair+1, pair) }, n, pair, second}, {"remove one", func() error { return inst.RemoveRegion(pair) }, n - 1, pair, first}, } // A region added at the end gets an empty , as the last group sets // what a region can't cancel. // Its sample is in the folder that the file's default_path names. added := NewRegion(path.Dir(inst.Regions[n-1].Sample) + "/Z.wav") steps = append(steps, struct { name string do func() error n int at int want string }{"add", func() error { return inst.AddRegion(added) }, n, n - 1, added.Sample}) for _, s := range steps { if err := s.do(); err != nil { t.Fatalf("%s: %v", s.name, err) } if len(inst.Regions) != s.n || inst.Regions[s.at].Sample != s.want { t.Fatalf("%s: %d regions, region %d is %q", s.name, len(inst.Regions), s.at, inst.Regions[s.at].Sample) } fresh := doc.Flatten().Instrument() 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", s.name, i, wrong) } } } } // 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": { " sample=a.wav\n sample=b.wav\n/* open", func(i *Instrument) error { return i.MoveRegion(1, 0) }, "", }, "add after a comment that never ends": { " 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": { " sample=a.wav\n sample=b.wav", func(i *Instrument) error { return i.MoveRegion(1, 0) }, " sample=b.wav\n sample=a.wav\n", }, "remove the only region": { " sample=a.wav\n", func(i *Instrument) error { return i.RemoveRegion(0) }, "", }, "comments above a header stay with the header before": { "// first\n sample=a.wav\n// second\n sample=b.wav\n", func(i *Instrument) error { return i.RemoveRegion(0) }, "// first\n sample=b.wav\n", }, "add to an empty document": { "", func(i *Instrument) error { return i.AddRegion(NewRegion("a.wav")) }, "\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, " 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() != " 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 := " sample=a.wav\n lokey=1\n sample=b.wav\n hikey=2\n 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 := " sample=c.wav\n sample=a.wav\n lokey=1\n sample=b.wav\n 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(), "") != 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": " sample=a.wav\n sample=b.wav\n", "two.sfz": " 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 != " sample=c.wav\n sample=a.wav\n 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, "\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 sample=a.wav volume=$v\n#define $v 2\n 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, " 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{ " sample=a.wav\n sample=b.wav\n sample=c.wav\n", " volume=-3\n sample=a.wav // one\n zampler_zone=1\n lokey=3\n hikey=60\n sample=b.wav\n sample=c.wav\n", "// top\n default_path=S/\n sample=S/a.wav /* note */ sample=S/b.wav\n", " sample=a.wav\n sample=b.wav\n/* open", "#define $x 1\n sample=a.wav volume=$x\n#define $x 2\n sample=b.wav volume=$x", " sample=a.wav\r\n 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": " sample=a.wav\n volume=1\n sample=b.wav\n", "two.sfz": " 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) }) }