loop_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2018 The Ebiten Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package audio_test
  15. import (
  16. "bytes"
  17. "io"
  18. "math"
  19. "testing"
  20. . "github.com/hajimehoshi/ebiten/v2/audio"
  21. )
  22. func TestInfiniteLoop(t *testing.T) {
  23. indexToByte := func(index int) byte {
  24. return byte(math.Sin(float64(index)) * 256)
  25. }
  26. src := make([]byte, 256)
  27. for i := range src {
  28. src[i] = indexToByte(i)
  29. }
  30. l := NewInfiniteLoop(bytes.NewReader(src), int64(len(src)))
  31. buf := make([]byte, len(src)*4)
  32. if _, err := io.ReadFull(l, buf); err != nil {
  33. t.Error(err)
  34. }
  35. for i, b := range buf {
  36. got := b
  37. want := indexToByte(i % len(src))
  38. if got != want {
  39. t.Errorf("index: %d, got: %v, want: %v", i, got, want)
  40. }
  41. }
  42. n, err := l.Seek(int64(len(src))*5+128, io.SeekStart)
  43. if err != nil {
  44. t.Error(err)
  45. }
  46. if want := int64(128); n != want {
  47. t.Errorf("got: %v, want: %v", n, want)
  48. }
  49. n2, err := l.Seek(int64(len(src))*6+64, io.SeekCurrent)
  50. if err != nil {
  51. t.Error(err)
  52. }
  53. if want := int64(192); n2 != want {
  54. t.Errorf("got: %v, want: %v", n, want)
  55. }
  56. buf2 := make([]byte, len(src)*7)
  57. if _, err := io.ReadFull(l, buf2); err != nil {
  58. t.Error(err)
  59. }
  60. for i, b := range buf2 {
  61. got := b
  62. want := indexToByte((i + 192) % len(src))
  63. if got != want {
  64. t.Errorf("index: %d, got: %v, want: %v", i, got, want)
  65. }
  66. }
  67. // Seek to negative position is an error.
  68. if _, err := l.Seek(-1, io.SeekStart); err == nil {
  69. t.Errorf("got: %v, want: %v", err, nil)
  70. }
  71. }
  72. func TestInfiniteLoopWithIntro(t *testing.T) {
  73. const (
  74. srcLength = 17 * 4
  75. introLength = 19 * 4
  76. loopLength = 23 * 4
  77. )
  78. indexToByte := func(index int) byte {
  79. return byte(math.Sin(float64(index)) * 256)
  80. }
  81. src := make([]byte, srcLength)
  82. for i := range src {
  83. src[i] = indexToByte(i)
  84. }
  85. srcInf := NewInfiniteLoop(bytes.NewReader(src), srcLength)
  86. l := NewInfiniteLoopWithIntro(srcInf, introLength, loopLength)
  87. buf := make([]byte, srcLength*4)
  88. if _, err := io.ReadFull(l, buf); err != nil {
  89. t.Error(err)
  90. }
  91. for i, b := range buf {
  92. got := b
  93. want := byte(0)
  94. if i < introLength {
  95. want = indexToByte(i % srcLength)
  96. } else {
  97. want = indexToByte(((i-introLength)%loopLength + introLength) % srcLength)
  98. }
  99. if got != want {
  100. t.Errorf("index: %d, got: %v, want: %v", i, got, want)
  101. }
  102. }
  103. n, err := l.Seek(srcLength*5+128, io.SeekStart)
  104. if err != nil {
  105. t.Error(err)
  106. }
  107. if want := int64((srcLength*5+128-introLength)%loopLength + introLength); n != want {
  108. t.Errorf("got: %v, want: %v", n, want)
  109. }
  110. n2, err := l.Seek(srcLength*6+64, io.SeekCurrent)
  111. if err != nil {
  112. t.Error(err)
  113. }
  114. if want := int64(((srcLength*11+192)-introLength)%loopLength + introLength); n2 != want {
  115. t.Errorf("got: %v, want: %v", n, want)
  116. }
  117. buf2 := make([]byte, srcLength*7)
  118. if _, err := io.ReadFull(l, buf2); err != nil {
  119. t.Error(err)
  120. }
  121. for i, b := range buf2 {
  122. got := b
  123. idx := ((int(n2+int64(i))-introLength)%loopLength + introLength) % srcLength
  124. want := indexToByte(idx)
  125. if got != want {
  126. t.Errorf("index: %d, got: %v, want: %v", i, got, want)
  127. }
  128. }
  129. // Seek to negative position is an error.
  130. if _, err := l.Seek(-1, io.SeekStart); err == nil {
  131. t.Errorf("got: %v, want: %v", err, nil)
  132. }
  133. }
  134. func TestInfiniteLoopWithIncompleteSize(t *testing.T) {
  135. // s1 should work as if 4092 is given.
  136. s1 := NewInfiniteLoop(bytes.NewReader(make([]byte, 4096)), 4095)
  137. n1, err := s1.Seek(4093, io.SeekStart)
  138. if err != nil {
  139. t.Error(err)
  140. }
  141. if got, want := n1, int64(4093-4092); got != want {
  142. t.Errorf("got: %d, want: %d", got, want)
  143. }
  144. // s2 should work as if 2044 and 2044 are given.
  145. s2 := NewInfiniteLoopWithIntro(bytes.NewReader(make([]byte, 4096)), 2047, 2046)
  146. n2, err := s2.Seek(4093, io.SeekStart)
  147. if err != nil {
  148. t.Error(err)
  149. }
  150. if got, want := n2, int64(2044+(4093-(2044+2044))); got != want {
  151. t.Errorf("got: %d, want: %d", got, want)
  152. }
  153. }