Audio plugin host https://kx.studio/carla
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.

192 lines
4.7KB

  1. /*
  2. * CarlaRingBuffer Tests
  3. * Copyright (C) 2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaRingBuffer.hpp"
  18. // -----------------------------------------------------------------------
  19. // simple types
  20. template <class BufferStruct>
  21. static void test_CarlaRingBuffer1(CarlaRingBuffer<BufferStruct>& b) noexcept
  22. {
  23. // start empty
  24. assert(b.isEmpty());
  25. // write all types
  26. b.writeBool(true);
  27. b.writeByte(123);
  28. b.writeShort(1234);
  29. b.writeInt(99999123);
  30. b.writeLong(0xffffffff);
  31. b.writeFloat(0.0123f);
  32. b.writeDouble(0.0123);
  33. // should be considered empty until a commitWrite
  34. assert(b.isEmpty());
  35. // commit
  36. assert(b.commitWrite());
  37. // should have data now
  38. assert(! b.isEmpty());
  39. // read all types back
  40. assert(b.readBool() == true);
  41. assert(b.readByte() == 123);
  42. assert(b.readShort() == 1234);
  43. assert(b.readInt() == 99999123);
  44. assert(b.readLong() == 0xffffffff);
  45. assert(b.readFloat() == 0.0123f);
  46. // still has data
  47. assert(! b.isEmpty());
  48. assert(b.readDouble() == 0.0123);
  49. // now empty
  50. assert(b.isEmpty());
  51. }
  52. // -----------------------------------------------------------------------
  53. // custom type
  54. struct BufferTestStruct {
  55. BufferTestStruct()
  56. : b(false), i(255), l(9999) {}
  57. bool b;
  58. int32_t i;
  59. char _pad[999];
  60. int64_t l;
  61. bool operator==(const BufferTestStruct& s) const noexcept
  62. {
  63. return (b == s.b && i == s.i && l == s.l);
  64. }
  65. };
  66. template <class BufferStruct>
  67. static void test_CarlaRingBuffer2(CarlaRingBuffer<BufferStruct>& b) noexcept
  68. {
  69. // start empty
  70. assert(b.isEmpty());
  71. // write unmodified
  72. BufferTestStruct t1, t2;
  73. assert(t1 == t2);
  74. b.writeCustomType(t1);
  75. assert(b.commitWrite());
  76. // test read
  77. b.readCustomType(t1);
  78. assert(t1 == t2);
  79. // modify t1
  80. b.writeCustomType(t1);
  81. assert(b.commitWrite());
  82. carla_zeroStruct(t1);
  83. // test read
  84. b.readCustomType(t1);
  85. assert(t1 == t2);
  86. // now empty
  87. assert(b.isEmpty());
  88. }
  89. // -----------------------------------------------------------------------
  90. // custom data
  91. template <class BufferStruct>
  92. static void test_CarlaRingBuffer3(CarlaRingBuffer<BufferStruct>& b) noexcept
  93. {
  94. static const char* const kLicense = ""
  95. "This program is free software; you can redistribute it and/or\n"
  96. "modify it under the terms of the GNU General Public License as\n"
  97. "published by the Free Software Foundation; either version 2 of\n"
  98. "the License, or any later version.\n"
  99. "\n"
  100. "This program is distributed in the hope that it will be useful,\n"
  101. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  102. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  103. "GNU General Public License for more details.\n"
  104. "\n"
  105. "For a full copy of the GNU General Public License see the doc/GPL.txt file.\n"
  106. "\n";
  107. static const std::size_t kLicenseSize = std::strlen(kLicense);
  108. // start empty
  109. assert(b.isEmpty());
  110. // write big chunk
  111. b.writeCustomData(kLicense, kLicenseSize);
  112. // still empty
  113. assert(b.isEmpty());
  114. // commit
  115. assert(b.commitWrite());
  116. // not empty
  117. assert(! b.isEmpty());
  118. // read data
  119. char license[kLicenseSize+1];
  120. carla_zeroChar(license, kLicenseSize+1);
  121. b.readCustomData(license, kLicenseSize);
  122. // now empty again
  123. assert(b.isEmpty());
  124. // test data integrity
  125. assert(std::strcmp(license, kLicense) == 0);
  126. }
  127. // -----------------------------------------------------------------------
  128. int main()
  129. {
  130. CarlaHeapRingBuffer heap;
  131. CarlaStackRingBuffer stack;
  132. // small test first
  133. heap.createBuffer(4096);
  134. heap.deleteBuffer();
  135. heap.createBuffer(1);
  136. heap.deleteBuffer();
  137. // ok
  138. heap.createBuffer(1024);
  139. test_CarlaRingBuffer1(heap);
  140. test_CarlaRingBuffer1(stack);
  141. test_CarlaRingBuffer2(heap);
  142. test_CarlaRingBuffer2(stack);
  143. // test the big chunk a couple of times to ensure circular writing
  144. for (int i=0; i<20; ++i)
  145. {
  146. test_CarlaRingBuffer3(heap);
  147. test_CarlaRingBuffer3(stack);
  148. }
  149. return 0;
  150. }
  151. // -----------------------------------------------------------------------