You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

audio-sadd.cpp 9.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include <lol/lib/doctest>
  2. #include <lol/audio/stream>
  3. TEST_CASE("sample saturated add: int8_t")
  4. {
  5. // Underflow
  6. CHECK(lol::audio::sample::sadd<int8_t>(-0x80, -0x80) == -0x80);
  7. CHECK(lol::audio::sample::sadd<int8_t>(-0x41, -0x41) == -0x80);
  8. CHECK(lol::audio::sample::sadd<int8_t>(-0x40, -0x41) == -0x80);
  9. // Standard operating mode
  10. CHECK(lol::audio::sample::sadd<int8_t>(-0x40, -0x40) == -0x80);
  11. CHECK(lol::audio::sample::sadd<int8_t>(-0x3f, -0x3f) == -0x7e);
  12. CHECK(lol::audio::sample::sadd<int8_t>(-0x01, -0x01) == -0x02);
  13. CHECK(lol::audio::sample::sadd<int8_t>(-0x01, 0x00) == -0x01);
  14. CHECK(lol::audio::sample::sadd<int8_t>( 0x00, 0x00) == 0x00);
  15. CHECK(lol::audio::sample::sadd<int8_t>( 0x00, 0x01) == 0x01);
  16. CHECK(lol::audio::sample::sadd<int8_t>( 0x01, 0x01) == 0x02);
  17. CHECK(lol::audio::sample::sadd<int8_t>( 0x3f, 0x3f) == 0x7e);
  18. CHECK(lol::audio::sample::sadd<int8_t>( 0x3f, 0x40) == 0x7f);
  19. // Overflow
  20. CHECK(lol::audio::sample::sadd<int8_t>( 0x40, 0x40) == 0x7f);
  21. CHECK(lol::audio::sample::sadd<int8_t>( 0x7f, 0x7f) == 0x7f);
  22. }
  23. TEST_CASE("sample saturated add: uint8_t")
  24. {
  25. // Underflow
  26. CHECK(lol::audio::sample::sadd<uint8_t>(0x00, 0x00) == 0x00);
  27. CHECK(lol::audio::sample::sadd<uint8_t>(0x3f, 0x3f) == 0x00);
  28. // Standard operating mode
  29. CHECK(lol::audio::sample::sadd<uint8_t>(0x40, 0x40) == 0x00);
  30. CHECK(lol::audio::sample::sadd<uint8_t>(0x41, 0x41) == 0x02);
  31. CHECK(lol::audio::sample::sadd<uint8_t>(0x7f, 0x7f) == 0x7e);
  32. CHECK(lol::audio::sample::sadd<uint8_t>(0x7f, 0x80) == 0x7f);
  33. CHECK(lol::audio::sample::sadd<uint8_t>(0x80, 0x80) == 0x80);
  34. CHECK(lol::audio::sample::sadd<uint8_t>(0x80, 0x81) == 0x81);
  35. CHECK(lol::audio::sample::sadd<uint8_t>(0x81, 0x81) == 0x82);
  36. CHECK(lol::audio::sample::sadd<uint8_t>(0xbf, 0xbf) == 0xfe);
  37. CHECK(lol::audio::sample::sadd<uint8_t>(0xbf, 0xc0) == 0xff);
  38. // Overflow
  39. CHECK(lol::audio::sample::sadd<uint8_t>(0xc0, 0xc0) == 0xff);
  40. CHECK(lol::audio::sample::sadd<uint8_t>(0xff, 0xff) == 0xff);
  41. }
  42. TEST_CASE("sample saturated add: int16_t")
  43. {
  44. // Underflow
  45. CHECK(lol::audio::sample::sadd<int16_t>(-0x8000, -0x8000) == -0x8000);
  46. CHECK(lol::audio::sample::sadd<int16_t>(-0x4001, -0x4001) == -0x8000);
  47. CHECK(lol::audio::sample::sadd<int16_t>(-0x4000, -0x4001) == -0x8000);
  48. // Standard operating mode
  49. CHECK(lol::audio::sample::sadd<int16_t>(-0x4000, -0x4000) == -0x8000);
  50. CHECK(lol::audio::sample::sadd<int16_t>(-0x3fff, -0x3fff) == -0x7ffe);
  51. CHECK(lol::audio::sample::sadd<int16_t>(-0x0001, -0x0001) == -0x0002);
  52. CHECK(lol::audio::sample::sadd<int16_t>(-0x0001, 0x0000) == -0x0001);
  53. CHECK(lol::audio::sample::sadd<int16_t>( 0x0000, 0x0000) == 0x0000);
  54. CHECK(lol::audio::sample::sadd<int16_t>( 0x0000, 0x0001) == 0x0001);
  55. CHECK(lol::audio::sample::sadd<int16_t>( 0x0001, 0x0001) == 0x0002);
  56. CHECK(lol::audio::sample::sadd<int16_t>( 0x3fff, 0x3fff) == 0x7ffe);
  57. CHECK(lol::audio::sample::sadd<int16_t>( 0x3fff, 0x4000) == 0x7fff);
  58. // Overflow
  59. CHECK(lol::audio::sample::sadd<int16_t>( 0x4000, 0x4000) == 0x7fff);
  60. CHECK(lol::audio::sample::sadd<int16_t>( 0x7fff, 0x7fff) == 0x7fff);
  61. }
  62. TEST_CASE("sample saturated add: uint16_t")
  63. {
  64. // Underflow
  65. CHECK(lol::audio::sample::sadd<uint16_t>(0x0000, 0x0000) == 0x0000);
  66. CHECK(lol::audio::sample::sadd<uint16_t>(0x3fff, 0x3fff) == 0x0000);
  67. // Standard operating mode
  68. CHECK(lol::audio::sample::sadd<uint16_t>(0x4000, 0x4000) == 0x0000);
  69. CHECK(lol::audio::sample::sadd<uint16_t>(0x4001, 0x4001) == 0x0002);
  70. CHECK(lol::audio::sample::sadd<uint16_t>(0x7fff, 0x7fff) == 0x7ffe);
  71. CHECK(lol::audio::sample::sadd<uint16_t>(0x7fff, 0x8000) == 0x7fff);
  72. CHECK(lol::audio::sample::sadd<uint16_t>(0x8000, 0x8000) == 0x8000);
  73. CHECK(lol::audio::sample::sadd<uint16_t>(0x8000, 0x8001) == 0x8001);
  74. CHECK(lol::audio::sample::sadd<uint16_t>(0x8001, 0x8001) == 0x8002);
  75. CHECK(lol::audio::sample::sadd<uint16_t>(0xbfff, 0xbfff) == 0xfffe);
  76. CHECK(lol::audio::sample::sadd<uint16_t>(0xbfff, 0xc000) == 0xffff);
  77. // Overflow
  78. CHECK(lol::audio::sample::sadd<uint16_t>(0xc000, 0xc000) == 0xffff);
  79. CHECK(lol::audio::sample::sadd<uint16_t>(0xffff, 0xffff) == 0xffff);
  80. }
  81. TEST_CASE("sample saturated add: uint32_t")
  82. {
  83. // Underflow
  84. CHECK(lol::audio::sample::sadd<uint32_t>(0x00000000, 0x00000000) == 0x00000000);
  85. CHECK(lol::audio::sample::sadd<uint32_t>(0x3fffffff, 0x3fffffff) == 0x00000000);
  86. // Standard operating mode
  87. CHECK(lol::audio::sample::sadd<uint32_t>(0x40000000, 0x40000000) == 0x00000000);
  88. CHECK(lol::audio::sample::sadd<uint32_t>(0x40000001, 0x40000001) == 0x00000002);
  89. CHECK(lol::audio::sample::sadd<uint32_t>(0x7fffffff, 0x7fffffff) == 0x7ffffffe);
  90. CHECK(lol::audio::sample::sadd<uint32_t>(0x7fffffff, 0x80000000) == 0x7fffffff);
  91. CHECK(lol::audio::sample::sadd<uint32_t>(0x80000000, 0x80000000) == 0x80000000);
  92. CHECK(lol::audio::sample::sadd<uint32_t>(0x80000000, 0x80000001) == 0x80000001);
  93. CHECK(lol::audio::sample::sadd<uint32_t>(0x80000001, 0x80000001) == 0x80000002);
  94. CHECK(lol::audio::sample::sadd<uint32_t>(0xbfffffff, 0xbfffffff) == 0xfffffffe);
  95. CHECK(lol::audio::sample::sadd<uint32_t>(0xbfffffff, 0xc0000000) == 0xffffffff);
  96. // Overflow
  97. CHECK(lol::audio::sample::sadd<uint32_t>(0xc0000000, 0xc0000000) == 0xffffffff);
  98. CHECK(lol::audio::sample::sadd<uint32_t>(0xffffffff, 0xffffffff) == 0xffffffff);
  99. }
  100. TEST_CASE("sample saturated add: int32_t")
  101. {
  102. // Underflow
  103. CHECK(lol::audio::sample::sadd<int32_t>(-0x80000000, -0x80000000) == -0x80000000);
  104. CHECK(lol::audio::sample::sadd<int32_t>(-0x40000001, -0x40000001) == -0x80000000);
  105. CHECK(lol::audio::sample::sadd<int32_t>(-0x40000000, -0x40000001) == -0x80000000);
  106. // Standard operating mode
  107. CHECK(lol::audio::sample::sadd<int32_t>(-0x40000000, -0x40000000) == -0x80000000);
  108. CHECK(lol::audio::sample::sadd<int32_t>(-0x3fffffff, -0x3fffffff) == -0x7ffffffe);
  109. CHECK(lol::audio::sample::sadd<int32_t>(-0x00000001, -0x00000001) == -0x00000002);
  110. CHECK(lol::audio::sample::sadd<int32_t>(-0x00000001, 0x00000000) == -0x00000001);
  111. CHECK(lol::audio::sample::sadd<int32_t>( 0x00000000, 0x00000000) == 0x00000000);
  112. CHECK(lol::audio::sample::sadd<int32_t>( 0x00000000, 0x00000001) == 0x00000001);
  113. CHECK(lol::audio::sample::sadd<int32_t>( 0x00000001, 0x00000001) == 0x00000002);
  114. CHECK(lol::audio::sample::sadd<int32_t>( 0x3fffffff, 0x3fffffff) == 0x7ffffffe);
  115. CHECK(lol::audio::sample::sadd<int32_t>( 0x3fffffff, 0x40000000) == 0x7fffffff);
  116. // Overflow
  117. CHECK(lol::audio::sample::sadd<int32_t>( 0x40000000, 0x40000000) == 0x7fffffff);
  118. CHECK(lol::audio::sample::sadd<int32_t>( 0x7fffffff, 0x7fffffff) == 0x7fffffff);
  119. }
  120. TEST_CASE("sample saturated add: uint64_t")
  121. {
  122. // Underflow
  123. CHECK(lol::audio::sample::sadd<uint64_t>(0x0000000000000000, 0x0000000000000000) == 0x0000000000000000);
  124. CHECK(lol::audio::sample::sadd<uint64_t>(0x3fffffffffffffff, 0x3fffffffffffffff) == 0x0000000000000000);
  125. // Standard operating mode
  126. CHECK(lol::audio::sample::sadd<uint64_t>(0x4000000000000000, 0x4000000000000000) == 0x0000000000000000);
  127. CHECK(lol::audio::sample::sadd<uint64_t>(0x4000000000000001, 0x4000000000000001) == 0x0000000000000002);
  128. CHECK(lol::audio::sample::sadd<uint64_t>(0x7fffffffffffffff, 0x7fffffffffffffff) == 0x7ffffffffffffffe);
  129. CHECK(lol::audio::sample::sadd<uint64_t>(0x7fffffffffffffff, 0x8000000000000000) == 0x7fffffffffffffff);
  130. CHECK(lol::audio::sample::sadd<uint64_t>(0x8000000000000000, 0x8000000000000000) == 0x8000000000000000);
  131. CHECK(lol::audio::sample::sadd<uint64_t>(0x8000000000000000, 0x8000000000000001) == 0x8000000000000001);
  132. CHECK(lol::audio::sample::sadd<uint64_t>(0x8000000000000001, 0x8000000000000001) == 0x8000000000000002);
  133. CHECK(lol::audio::sample::sadd<uint64_t>(0xbfffffffffffffff, 0xbfffffffffffffff) == 0xfffffffffffffffe);
  134. CHECK(lol::audio::sample::sadd<uint64_t>(0xbfffffffffffffff, 0xc000000000000000) == 0xffffffffffffffff);
  135. // Overflow
  136. CHECK(lol::audio::sample::sadd<uint64_t>(0xc000000000000000, 0xc000000000000000) == 0xffffffffffffffff);
  137. CHECK(lol::audio::sample::sadd<uint64_t>(0xffffffffffffffff, 0xffffffffffffffff) == 0xffffffffffffffff);
  138. }
  139. TEST_CASE("sample saturated add: int64_t")
  140. {
  141. // Underflow
  142. CHECK(lol::audio::sample::sadd<int64_t>(-0x8000000000000000, -0x8000000000000000) == -0x8000000000000000);
  143. CHECK(lol::audio::sample::sadd<int64_t>(-0x4000000000000001, -0x4000000000000001) == -0x8000000000000000);
  144. CHECK(lol::audio::sample::sadd<int64_t>(-0x4000000000000000, -0x4000000000000001) == -0x8000000000000000);
  145. // Standard operating mode
  146. CHECK(lol::audio::sample::sadd<int64_t>(-0x4000000000000000, -0x4000000000000000) == -0x8000000000000000);
  147. CHECK(lol::audio::sample::sadd<int64_t>(-0x3fffffffffffffff, -0x3fffffffffffffff) == -0x7ffffffffffffffe);
  148. CHECK(lol::audio::sample::sadd<int64_t>(-0x0000000000000001, -0x0000000000000001) == -0x0000000000000002);
  149. CHECK(lol::audio::sample::sadd<int64_t>(-0x0000000000000001, 0x0000000000000000) == -0x0000000000000001);
  150. CHECK(lol::audio::sample::sadd<int64_t>( 0x0000000000000000, 0x0000000000000000) == 0x0000000000000000);
  151. CHECK(lol::audio::sample::sadd<int64_t>( 0x0000000000000000, 0x0000000000000001) == 0x0000000000000001);
  152. CHECK(lol::audio::sample::sadd<int64_t>( 0x0000000000000001, 0x0000000000000001) == 0x0000000000000002);
  153. CHECK(lol::audio::sample::sadd<int64_t>( 0x3fffffffffffffff, 0x3fffffffffffffff) == 0x7ffffffffffffffe);
  154. CHECK(lol::audio::sample::sadd<int64_t>( 0x3fffffffffffffff, 0x4000000000000000) == 0x7fffffffffffffff);
  155. // Overflow
  156. CHECK(lol::audio::sample::sadd<int64_t>( 0x4000000000000000, 0x4000000000000000) == 0x7fffffffffffffff);
  157. CHECK(lol::audio::sample::sadd<int64_t>( 0x7fffffffffffffff, 0x7fffffffffffffff) == 0x7fffffffffffffff);
  158. }