1
0

project_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. package project
  2. import (
  3. "bytes"
  4. "compress/gzip"
  5. "encoding/binary"
  6. "fmt"
  7. "io"
  8. "math"
  9. "os"
  10. "path/filepath"
  11. "strconv"
  12. "strings"
  13. "testing"
  14. "git.monoexp.com/bajdev/mpckit/internal/xpj"
  15. )
  16. const testdata = "../../testdata"
  17. func decompress(t *testing.T, path string) []byte {
  18. t.Helper()
  19. fh, err := os.Open(path)
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  23. defer fh.Close()
  24. zr, err := gzip.NewReader(fh)
  25. if err != nil {
  26. t.Fatal(err)
  27. }
  28. b, err := io.ReadAll(zr)
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. return b
  33. }
  34. func encode(t *testing.T, p *Project) []byte {
  35. t.Helper()
  36. b, err := p.File().Encode()
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. return b
  41. }
  42. func must(t *testing.T, err error) {
  43. t.Helper()
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. }
  48. func newProject(t *testing.T) *Project {
  49. t.Helper()
  50. p, err := New("Test")
  51. must(t, err)
  52. return p
  53. }
  54. // leaves flattens a tree into path -> literal text, for comparing files.
  55. func leaves(n *xpj.Node, path string, out map[string]string) map[string]string {
  56. if out == nil {
  57. out = map[string]string{}
  58. }
  59. switch n.Kind {
  60. case xpj.Object:
  61. for _, m := range n.Members {
  62. leaves(m.Value, path+"."+m.Key, out)
  63. }
  64. case xpj.Array:
  65. for i, it := range n.Items {
  66. leaves(it, fmt.Sprintf("%s[%d]", path, i), out)
  67. }
  68. if len(n.Items) == 0 {
  69. out[path] = "[]"
  70. }
  71. default:
  72. b, _ := n.MarshalJSON()
  73. out[path] = string(b)
  74. }
  75. return out
  76. }
  77. func diffPaths(a, b *xpj.Node) []string {
  78. la, lb := leaves(a, "", nil), leaves(b, "", nil)
  79. var diff []string
  80. for k, v := range la {
  81. if lb[k] != v {
  82. diff = append(diff, k)
  83. }
  84. }
  85. for k := range lb {
  86. if _, ok := la[k]; !ok {
  87. diff = append(diff, k)
  88. }
  89. }
  90. return diff
  91. }
  92. // The prototype's edits for MpckitT1 and T3 (build plan appendix), made
  93. // through the model. The output must equal the files the device accepted.
  94. func buildAccepted(t *testing.T, name string, shared bool) *Project {
  95. t.Helper()
  96. p, err := New(name)
  97. must(t, err)
  98. dir := filepath.Join(testdata, "accepted", name+DataSuffix)
  99. must(t, p.AssignSample(0, filepath.Join(dir, "Linn 9000 kick.wav")))
  100. second := "Linn 9000 kick001.wav"
  101. if shared {
  102. second = "Linn 9000 kick.wav"
  103. }
  104. must(t, p.AssignSample(16, filepath.Join(dir, second)))
  105. must(t, p.SetTune(16, 5, 0))
  106. must(t, p.SetSequence(0, "Verse", 96, 8))
  107. must(t, p.AddSequence(1))
  108. must(t, p.SetSequence(1, "Chorus", 96, 4))
  109. return p
  110. }
  111. func TestGoldenMpckitT1(t *testing.T) {
  112. p := buildAccepted(t, "MpckitT1", false)
  113. want := decompress(t, filepath.Join(testdata, "accepted", "MpckitT1.xpj"))
  114. if got := encode(t, p); !bytes.Equal(got, want) {
  115. f, _ := xpj.Decode(want)
  116. t.Fatalf("differs from MpckitT1.xpj at %v", diffPaths(p.File().Root, f.Root))
  117. }
  118. if p.SampleCount() != 2 {
  119. t.Errorf("%d samples, want 2", p.SampleCount())
  120. }
  121. }
  122. func TestGoldenMpckitT3(t *testing.T) {
  123. p := buildAccepted(t, "MpckitT3", true)
  124. want := decompress(t, filepath.Join(testdata, "accepted", "MpckitT3.xpj"))
  125. if got := encode(t, p); !bytes.Equal(got, want) {
  126. f, _ := xpj.Decode(want)
  127. t.Fatalf("differs from MpckitT3.xpj at %v", diffPaths(p.File().Root, f.Root))
  128. }
  129. if p.SampleCount() != 1 {
  130. t.Errorf("%d samples, want 1 shared", p.SampleCount())
  131. }
  132. }
  133. // Rebuild Demo02 from the blank template and compare with the device's file.
  134. func TestRebuildDemo02(t *testing.T) {
  135. dir := filepath.Join(testdata, "device", "Demo02"+DataSuffix)
  136. p := newProject(t)
  137. must(t, p.AssignSample(0, filepath.Join(dir, "Linn 9000 kick.wav")))
  138. must(t, p.AssignSample(4, filepath.Join(dir, "Linn 9000 kick001.wav")))
  139. must(t, p.SetTune(4, 12, 40))
  140. // Start/end trim is a 2.0 feature; its fields are verified, so set them
  141. // directly to complete the rebuild.
  142. l, err := layer(p.pad(4))
  143. must(t, err)
  144. must(t, l.Set("sliceInfo.Start", xpj.NewInt(328)))
  145. must(t, l.Set("sliceInfo.LoopStart", xpj.NewInt(328)))
  146. must(t, l.Set("sliceInfo.End", xpj.NewInt(3777)))
  147. must(t, p.SetSequence(0, "Sequence 01", 105, 2))
  148. must(t, p.AddSequence(1))
  149. must(t, p.SetSequence(1, "Sequence 02", 105, 4))
  150. want, err := xpj.ReadFile(filepath.Join(testdata, "device", "Demo02.xpj"))
  151. must(t, err)
  152. got, wantLeaves := leaves(p.File().Root, "", nil), leaves(want.Root, "", nil)
  153. var unexpected []string
  154. for _, path := range diffPaths(p.File().Root, want.Root) {
  155. switch {
  156. case sameFloat(got[path], wantLeaves[path]):
  157. // The device prints some floats with 17 digits where we print
  158. // the shortest form: pitch 12.399999618530274 vs ...273 is the
  159. // same float64.
  160. case path == ".data.tracks[0].colour",
  161. path == ".data.assignableEnvelopeFollowerAssignments.currentControlIndex",
  162. strings.Contains(path, ".eventList.events"):
  163. // Known noise and recorded notes (build plan M2).
  164. case path == ".data.tracks[0].program.drum.instruments[4].layersv[0].sliceInfo.LoopCrossfadeLength":
  165. // The device wrote a different value on the pad it trimmed; we
  166. // write -1 as for every assigned sample (format doc section 3).
  167. default:
  168. unexpected = append(unexpected, path)
  169. }
  170. }
  171. if len(unexpected) > 0 {
  172. t.Errorf("unexpected differences from Demo02: %v", unexpected)
  173. }
  174. }
  175. func sameFloat(a, b string) bool {
  176. fa, errA := strconv.ParseFloat(a, 64)
  177. fb, errB := strconv.ParseFloat(b, 64)
  178. return errA == nil && errB == nil && fa == fb
  179. }
  180. // writeWAV writes a short 16-bit mono WAV of frames frames.
  181. func writeWAV(t *testing.T, path string, frames int) string {
  182. t.Helper()
  183. data := make([]byte, frames*2)
  184. var b bytes.Buffer
  185. le := binary.LittleEndian
  186. b.WriteString("RIFF")
  187. binary.Write(&b, le, uint32(4+8+16+8+len(data)))
  188. b.WriteString("WAVEfmt ")
  189. for _, v := range []any{uint32(16), uint16(1), uint16(1), uint32(44100), uint32(88200), uint16(2), uint16(16)} {
  190. binary.Write(&b, le, v)
  191. }
  192. b.WriteString("data")
  193. binary.Write(&b, le, uint32(len(data)))
  194. b.Write(data)
  195. must(t, os.MkdirAll(filepath.Dir(path), 0o755))
  196. must(t, os.WriteFile(path, b.Bytes(), 0o644))
  197. return path
  198. }
  199. func padOf(t *testing.T, p *Project, slot int) Pad {
  200. t.Helper()
  201. v, err := p.Pad(slot)
  202. must(t, err)
  203. return v
  204. }
  205. func TestAssignAndPoolReuse(t *testing.T) {
  206. dir := t.TempDir()
  207. kick := writeWAV(t, filepath.Join(dir, "kick.wav"), 100)
  208. p := newProject(t)
  209. must(t, p.AssignSample(0, kick))
  210. must(t, p.AssignSample(1, kick))
  211. if p.SampleCount() != 1 {
  212. t.Fatalf("same source on two pads: %d pool entries, want 1", p.SampleCount())
  213. }
  214. a := padOf(t, p, 1)
  215. if a.File != "kick.wav" || a.SampleName != "kick" || a.End != 99 {
  216. t.Errorf("pad A2 = %+v", a)
  217. }
  218. if !p.Dirty {
  219. t.Error("not dirty after assign")
  220. }
  221. // Reassigning the same source to its only pad keeps the entry.
  222. must(t, p.ClearPad(1))
  223. must(t, p.AssignSample(0, kick))
  224. if p.SampleCount() != 1 || padOf(t, p, 0).File != "kick.wav" {
  225. t.Errorf("reassign same source: %d entries, pad %+v", p.SampleCount(), padOf(t, p, 0))
  226. }
  227. if got := p.Sources["kick.wav"]; got != kick {
  228. t.Errorf("source = %q, want %q", got, kick)
  229. }
  230. // Replacing drops the old entry when no pad uses it.
  231. snare := writeWAV(t, filepath.Join(dir, "snare.wav"), 50)
  232. must(t, p.AssignSample(0, snare))
  233. if p.SampleCount() != 1 || p.inPool("kick.wav") || p.Sources["kick.wav"] != "" {
  234. t.Errorf("replace did not drop kick: %d entries", p.SampleCount())
  235. }
  236. }
  237. func TestNameCollision(t *testing.T) {
  238. dir := t.TempDir()
  239. p := newProject(t)
  240. must(t, p.AssignSample(0, writeWAV(t, filepath.Join(dir, "a", "kick.wav"), 10)))
  241. must(t, p.AssignSample(1, writeWAV(t, filepath.Join(dir, "b", "kick.wav"), 20)))
  242. must(t, p.AssignSample(2, writeWAV(t, filepath.Join(dir, "c", "Kick.WAV"), 30)))
  243. want := []string{"kick.wav", "kick001.wav", "Kick002.WAV"}
  244. for i, w := range want {
  245. if got := padOf(t, p, i); got.File != w || got.SampleName != strings.TrimSuffix(w, filepath.Ext(w)) {
  246. t.Errorf("pad %d = %q/%q, want %q", i, got.SampleName, got.File, w)
  247. }
  248. }
  249. // Both pools hold the same entries; the track pool in reverse order.
  250. var pool, track []string
  251. for _, e := range p.pool().Items {
  252. pool = append(pool, e.Lookup("path").Text)
  253. }
  254. for _, e := range p.trackPool().Items {
  255. track = append(track, e.Lookup("path").Text)
  256. }
  257. if strings.Join(pool, ",") != "kick.wav,kick001.wav,Kick002.WAV" || strings.Join(track, ",") != "Kick002.WAV,kick001.wav,kick.wav" {
  258. t.Errorf("pool %v, track pool %v", pool, track)
  259. }
  260. }
  261. func TestSampleLimit(t *testing.T) {
  262. dir := t.TempDir()
  263. p := newProject(t)
  264. // With one sample per pad the pool can only fill up when it also holds
  265. // entries no pad uses, as a loaded project may. Simulate one.
  266. must(t, p.addPoolEntry("unused.wav"))
  267. for i := 0; i < MaxSamples-1; i++ {
  268. must(t, p.AssignSample(i, writeWAV(t, filepath.Join(dir, fmt.Sprintf("s%03d.wav", i)), 4)))
  269. }
  270. if p.SampleCount() != MaxSamples {
  271. t.Fatalf("%d samples, want %d", p.SampleCount(), MaxSamples)
  272. }
  273. err := p.AssignSample(127, writeWAV(t, filepath.Join(dir, "x.wav"), 4))
  274. if err == nil || !strings.Contains(err.Error(), "128") {
  275. t.Fatalf("129th sample: %v", err)
  276. }
  277. if !padOf(t, p, 127).Empty() || p.SampleCount() != MaxSamples {
  278. t.Error("refused assign changed the project")
  279. }
  280. // Reusing a file already in the pool needs no new entry.
  281. must(t, p.AssignSample(127, filepath.Join(dir, "s000.wav")))
  282. // Replacing a pad's only sample frees its entry first.
  283. must(t, p.AssignSample(1, filepath.Join(dir, "x.wav")))
  284. if p.SampleCount() != MaxSamples || padOf(t, p, 1).File != "x.wav" {
  285. t.Errorf("replace at the limit: %d samples, pad %+v", p.SampleCount(), padOf(t, p, 1))
  286. }
  287. }
  288. func TestAssignRejectsBadFiles(t *testing.T) {
  289. dir := t.TempDir()
  290. p := newProject(t)
  291. bad := filepath.Join(dir, "bad.wav")
  292. must(t, os.WriteFile(bad, []byte("not a wav"), 0o644))
  293. if err := p.AssignSample(0, bad); err == nil {
  294. t.Error("non-WAV accepted")
  295. }
  296. if err := p.AssignSample(0, filepath.Join(dir, "missing.wav")); err == nil {
  297. t.Error("missing file accepted")
  298. }
  299. if err := p.AssignSample(128, writeWAV(t, filepath.Join(dir, "ok.wav"), 4)); err == nil {
  300. t.Error("slot 128 accepted")
  301. }
  302. if p.Dirty || p.SampleCount() != 0 {
  303. t.Error("failed assigns changed the project")
  304. }
  305. }
  306. func TestClearCopyMoveSwap(t *testing.T) {
  307. dir := t.TempDir()
  308. p := newProject(t)
  309. kick := writeWAV(t, filepath.Join(dir, "kick.wav"), 10)
  310. snare := writeWAV(t, filepath.Join(dir, "snare.wav"), 20)
  311. must(t, p.AssignSample(0, kick))
  312. must(t, p.AssignSample(1, snare))
  313. must(t, p.SetTune(0, 3, -20))
  314. must(t, p.SetMuteGroup(0, 2))
  315. // Copy across banks keeps tuning and mute group and shares the entry.
  316. must(t, p.CopyPad(0, 40))
  317. c := padOf(t, p, 40)
  318. if c.File != "kick.wav" || c.Coarse != 3 || c.Fine != -20 || c.MuteGroup != 2 || p.SampleCount() != 2 {
  319. t.Errorf("copy: %+v, %d samples", c, p.SampleCount())
  320. }
  321. // The copy is independent of its source.
  322. must(t, p.SetTune(40, 0, 0))
  323. if padOf(t, p, 0).Coarse != 3 {
  324. t.Error("copy shares data with its source")
  325. }
  326. // Move onto a filled pad replaces it; the snare's entry goes.
  327. must(t, p.MovePad(0, 1))
  328. if !padOf(t, p, 0).Empty() || padOf(t, p, 1).File != "kick.wav" || p.inPool("snare.wav") {
  329. t.Errorf("move: A1 %+v, A2 %+v", padOf(t, p, 0), padOf(t, p, 1))
  330. }
  331. blank, _ := template.pad.MarshalJSON()
  332. if got, _ := p.pad(0).MarshalJSON(); !bytes.Equal(got, blank) {
  333. t.Error("moved-from pad is not the blank pad")
  334. }
  335. // Swap.
  336. must(t, p.AssignSample(5, snare))
  337. must(t, p.SwapPads(1, 5))
  338. if padOf(t, p, 1).File != "snare.wav" || padOf(t, p, 5).File != "kick.wav" {
  339. t.Error("swap")
  340. }
  341. // Clearing the last user drops the entry; clearing a shared one keeps it.
  342. must(t, p.ClearPad(5))
  343. if !p.inPool("kick.wav") {
  344. t.Error("cleared a shared entry (kick is still on pad 40)")
  345. }
  346. must(t, p.ClearPad(40))
  347. if p.inPool("kick.wav") || p.SampleCount() != 1 {
  348. t.Errorf("kick entry kept after its last pad was cleared (%d samples)", p.SampleCount())
  349. }
  350. // No-ops and range checks.
  351. p.Dirty = false
  352. must(t, p.ClearPad(100))
  353. must(t, p.CopyPad(1, 1))
  354. must(t, p.MovePad(1, 1))
  355. must(t, p.SwapPads(1, 1))
  356. if p.Dirty {
  357. t.Error("no-op marked the project dirty")
  358. }
  359. for _, err := range []error{p.CopyPad(-1, 0), p.MovePad(0, 128), p.SwapPads(200, 0), p.ClearPad(128), p.SetTune(128, 0, 0), p.SetMuteGroup(-1, 0)} {
  360. if err == nil {
  361. t.Error("out-of-range slot accepted")
  362. }
  363. }
  364. }
  365. func TestTuneAndMuteGroup(t *testing.T) {
  366. p := newProject(t)
  367. must(t, p.SetTune(0, 12, 40))
  368. l, _ := layer(p.pad(0))
  369. if got := l.Lookup("pitch").Text; got != "12.399999618530273" {
  370. t.Errorf("pitch = %s", got)
  371. }
  372. must(t, p.SetTune(0, 99, -500))
  373. if v := padOf(t, p, 0); v.Coarse != 24 || v.Fine != -90 {
  374. t.Errorf("clamped tune = %d %d", v.Coarse, v.Fine)
  375. }
  376. if got := l.Lookup("pitch").Text; got != "23.100000381469727" {
  377. t.Errorf("pitch = %s", got)
  378. }
  379. must(t, p.SetMuteGroup(3, 1))
  380. if padOf(t, p, 3).MuteGroup != 1 {
  381. t.Error("mute group")
  382. }
  383. must(t, p.SetMuteGroup(3, 16))
  384. if err := p.SetMuteGroup(3, MaxMuteGroup+1); err == nil {
  385. t.Error("mute group out of range accepted")
  386. }
  387. }
  388. func TestSequences(t *testing.T) {
  389. p := newProject(t)
  390. must(t, p.AddSequence(4))
  391. s := p.Sequences()
  392. if len(s) != 2 || s[1] != (Sequence{Slot: 4, Name: "Sequence 05", BPM: DefaultBPM, Bars: 2}) {
  393. t.Fatalf("after add: %+v", s)
  394. }
  395. if err := p.AddSequence(4); err == nil {
  396. t.Error("duplicate slot accepted")
  397. }
  398. if err := p.AddSequence(MaxSequences); err == nil {
  399. t.Error("slot 128 accepted")
  400. }
  401. must(t, p.SetSequence(4, "Drop", 140.5, 16))
  402. must(t, p.AddSequence(127))
  403. s = p.Sequences()
  404. if s[1].Name != "Drop" || s[1].BPM != 140.5 || s[1].Bars != 16 || s[2].Name != "Sequence 128" || s[2].BPM != 140.5 {
  405. t.Errorf("after edit: %+v", s)
  406. }
  407. // Newest first in the file, as the device writes.
  408. if num(p.sequences().Items[0].Lookup("key")) != 127 {
  409. t.Error("new sequence not inserted first")
  410. }
  411. // All 26 length fields.
  412. v := p.sequences().Items[p.findSequence(4)].Lookup("value")
  413. n := 0
  414. for path, val := range leaves(v, "", nil) {
  415. if strings.HasSuffix(path, "lengthBars") || strings.HasSuffix(path, "loopEndBar") {
  416. n++
  417. if val != "16" {
  418. t.Errorf("%s = %s", path, val)
  419. }
  420. }
  421. if strings.HasSuffix(path, ".lengthPulses") || strings.HasSuffix(path, "loopEndPulses") || strings.HasSuffix(path, ".endPulses") {
  422. n++
  423. if val != "61440" {
  424. t.Errorf("%s = %s", path, val)
  425. }
  426. }
  427. }
  428. if n != 26 {
  429. t.Errorf("%d length fields, want 26", n)
  430. }
  431. for _, bad := range []struct {
  432. name string
  433. bpm float64
  434. bars int
  435. }{{"", 120, 2}, {"a/b", 120, 2}, {"ok", 0, 2}, {"ok", -1, 2}, {"ok", 19.9, 2}, {"ok", 999.5, 2}, {"ok", math.NaN(), 2}, {"ok", math.Inf(1), 2}, {"ok", 120, 0}, {"ok", 120, MaxBars + 1}} {
  436. if err := p.SetSequence(4, bad.name, bad.bpm, bad.bars); err == nil {
  437. t.Errorf("SetSequence(%q, %v, %d) accepted", bad.name, bad.bpm, bad.bars)
  438. }
  439. }
  440. must(t, p.SetSequence(4, "Min", MinBPM, 1))
  441. must(t, p.SetSequence(4, "Max", MaxBPM, MaxBars))
  442. if err := p.SetSequence(9, "x", 120, 2); err == nil {
  443. t.Error("missing sequence accepted")
  444. }
  445. // Remove: current sequence moves to the lowest remaining slot.
  446. must(t, p.data.SetKey("currentSequence", xpj.NewInt(4)))
  447. must(t, p.RemoveSequence(4))
  448. if cur := num(p.data.Lookup("currentSequence")); cur != 0 {
  449. t.Errorf("currentSequence = %d, want 0", cur)
  450. }
  451. must(t, p.RemoveSequence(0))
  452. if cur := num(p.data.Lookup("currentSequence")); cur != 127 {
  453. t.Errorf("currentSequence = %d, want 127", cur)
  454. }
  455. if err := p.RemoveSequence(127); err == nil {
  456. t.Error("removed the last sequence")
  457. }
  458. if err := p.RemoveSequence(3); err == nil {
  459. t.Error("removed a missing sequence")
  460. }
  461. }
  462. func TestLoadDemoFile(t *testing.T) {
  463. p, warnings, err := Load(filepath.Join(testdata, "device", "DemoFile.xpj"))
  464. must(t, err)
  465. if len(warnings) != 0 {
  466. t.Errorf("warnings: %v", warnings)
  467. }
  468. if p.Name != "DemoFile" || p.Dirty || p.SampleCount() != 6 {
  469. t.Errorf("name %q dirty %v samples %d", p.Name, p.Dirty, p.SampleCount())
  470. }
  471. for file, src := range p.Sources {
  472. if _, err := os.Stat(src); err != nil {
  473. t.Errorf("source for %s: %v", file, err)
  474. }
  475. }
  476. // The sequences have notes on A1-A4 (36-39); the other pads have none.
  477. for slot := 0; slot < Pads; slot++ {
  478. if got, want := p.PadHasNotes(slot), slot < 4; got != want {
  479. t.Errorf("PadHasNotes(%s) = %v", SlotName(slot), got)
  480. }
  481. }
  482. s := p.Sequences()
  483. if len(s) != 2 || s[0].Name != "Intro" || !s[0].HasNotes || s[1].Name != "Sequence 02" || !s[1].HasNotes {
  484. t.Errorf("sequences %+v", s)
  485. }
  486. if v := padOf(t, p, 2); v.MuteGroup != 1 || v.File != "80s-Hat-Dreamer1 SA.wav" {
  487. t.Errorf("A3 = %+v", v)
  488. }
  489. // Loaded and unchanged: writes back byte-identical.
  490. want := decompress(t, filepath.Join(testdata, "device", "DemoFile.xpj"))
  491. if !bytes.Equal(encode(t, p), want) {
  492. t.Error("loaded project does not write back unchanged")
  493. }
  494. // Assigning a file already in the project folder reuses its entry.
  495. must(t, p.AssignSample(50, filepath.Join(testdata, "device", "DemoFile"+DataSuffix, "Bass-Pure C.wav")))
  496. if p.SampleCount() != 6 {
  497. t.Errorf("%d samples after reusing a loaded file", p.SampleCount())
  498. }
  499. }
  500. func TestBlankPadsAndTemplate(t *testing.T) {
  501. p := newProject(t)
  502. if !bytes.Equal(encode(t, p), decompress(t, filepath.Join(testdata, "device", "DemoBlank.xpj"))) {
  503. t.Error("New() is not DemoBlank (is internal/project/template out of date?)")
  504. }
  505. for slot := 0; slot < Pads; slot++ {
  506. if !padOf(t, p, slot).Empty() || p.PadHasNotes(slot) {
  507. t.Fatalf("pad %d not empty", slot)
  508. }
  509. }
  510. }
  511. func TestNames(t *testing.T) {
  512. p := newProject(t)
  513. must(t, p.SetName("My Kit 2"))
  514. if p.Name != "My Kit 2" || !p.Dirty {
  515. t.Error("SetName")
  516. }
  517. for _, bad := range []string{"", " ", "a:b", "a?", "end.", "end ", "CON", "com1.txt", "tab\there"} {
  518. if err := ValidName(bad); err == nil {
  519. t.Errorf("ValidName(%q) accepted", bad)
  520. }
  521. }
  522. if _, err := New("bad|name"); err == nil {
  523. t.Error("New with a bad name accepted")
  524. }
  525. for slot, want := range map[int]string{0: "A1", 4: "A5", 16: "B1", 17: "B2", 127: "H16"} {
  526. if got := SlotName(slot); got != want {
  527. t.Errorf("SlotName(%d) = %s, want %s", slot, got, want)
  528. }
  529. }
  530. }