PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
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.

unit-tests_SysExCodec.cpp 5.6KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include "unit-tests.h"
  2. #include <src/MIDI.h>
  3. BEGIN_MIDI_NAMESPACE
  4. END_MIDI_NAMESPACE
  5. // -----------------------------------------------------------------------------
  6. BEGIN_UNNAMED_NAMESPACE
  7. using namespace testing;
  8. TEST(SysExCodec, EncoderAscii)
  9. {
  10. const byte input[] = "Hello, World!";
  11. byte buffer[16];
  12. memset(buffer, 0, 16 * sizeof(byte));
  13. const unsigned encodedSize = midi::encodeSysEx(input, buffer, 13);
  14. EXPECT_EQ(encodedSize, unsigned(15));
  15. const byte expected[16] = {
  16. 0, 'H', 'e', 'l', 'l', 'o', ',', ' ',
  17. 0, 'W', 'o', 'r', 'l', 'd', '!', 0,
  18. };
  19. EXPECT_THAT(buffer, Each(Le(0x7f))); // All elements are <= 127
  20. EXPECT_THAT(buffer, ContainerEq(expected));
  21. }
  22. TEST(SysExCodec, EncoderNonAscii)
  23. {
  24. const byte input[] = {
  25. 182, 236, 167, 177, 61, 91, 120, // 01111000 -> 120
  26. 107, 94, 209, 87, 94 // 000100xx -> 16
  27. };
  28. byte buffer[16];
  29. memset(buffer, 0, 16 * sizeof(byte));
  30. const unsigned encodedSize = midi::encodeSysEx(input, buffer, 12);
  31. EXPECT_EQ(encodedSize, unsigned(14));
  32. const byte expected[16] = {
  33. // MSB Data
  34. 120, 54, 108, 39, 49, 61, 91, 120,
  35. 16, 107, 94, 81, 87, 94, 0, 0,
  36. };
  37. EXPECT_THAT(buffer, Each(Le(0x7f))); // All elements are <= 127
  38. EXPECT_THAT(buffer, ContainerEq(expected));
  39. }
  40. TEST(SysExCodec, EncoderNonAsciiFlipHeader)
  41. {
  42. const byte input[] = {
  43. 182, 236, 167, 177, 61, 91, 120, // 00011111 -> 15
  44. 107, 94, 209, 87, 94 // 0xx00100 -> 4
  45. };
  46. byte buffer[16];
  47. memset(buffer, 0, 16 * sizeof(byte));
  48. const unsigned encodedSize = midi::encodeSysEx(input, buffer, 12, true);
  49. EXPECT_EQ(encodedSize, unsigned(14));
  50. const byte expected[16] = {
  51. // MSB Data
  52. 15, 54, 108, 39, 49, 61, 91, 120,
  53. 4, 107, 94, 81, 87, 94, 0, 0,
  54. };
  55. EXPECT_THAT(buffer, Each(Le(0x7f))); // All elements are <= 127
  56. EXPECT_THAT(buffer, ContainerEq(expected));
  57. }
  58. // -----------------------------------------------------------------------------
  59. TEST(SysExCodec, DecoderAscii)
  60. {
  61. const byte input[] = {
  62. 0, 'H', 'e', 'l', 'l', 'o', ',', ' ',
  63. 0, 'W', 'o', 'r', 'l', 'd', '!',
  64. };
  65. byte buffer[16];
  66. memset(buffer, 0, 16 * sizeof(byte));
  67. const unsigned decodedSize = midi::decodeSysEx(input, buffer, 15);
  68. EXPECT_EQ(decodedSize, unsigned(13));
  69. const byte expected[16] = {
  70. 'H', 'e', 'l', 'l', 'o', ',', ' ', 'W',
  71. 'o', 'r', 'l', 'd', '!', 0, 0, 0,
  72. };
  73. EXPECT_THAT(buffer, Each(Le(0x7f))); // All elements are <= 127
  74. EXPECT_THAT(buffer, ContainerEq(expected));
  75. }
  76. // Non-ASCII content
  77. TEST(SysExCodec, DecoderNonAscii)
  78. {
  79. const byte input[] = {
  80. // MSB Data
  81. 120, 54, 108, 39, 49, 61, 91, 120,
  82. 16, 107, 94, 81, 87, 94,
  83. };
  84. byte buffer[16];
  85. memset(buffer, 0, 16 * sizeof(byte));
  86. const unsigned encodedSize = midi::decodeSysEx(input, buffer, 14);
  87. EXPECT_EQ(encodedSize, unsigned(12));
  88. const byte expected[16] = {
  89. 182, 236, 167, 177, 61, 91, 120,
  90. 107, 94, 209, 87, 94, 0, 0,
  91. 0, 0,
  92. };
  93. EXPECT_THAT(input, Each(Le(0x7f))); // All elements are <= 127
  94. EXPECT_THAT(buffer, ContainerEq(expected));
  95. }
  96. TEST(SysExCodec, DecoderNonAsciiFlipHeader)
  97. {
  98. const byte input[] = {
  99. // MSB Data
  100. 15, 54, 108, 39, 49, 61, 91, 120,
  101. 4, 107, 94, 81, 87, 94,
  102. };
  103. byte buffer[16];
  104. memset(buffer, 0, 16 * sizeof(byte));
  105. const unsigned encodedSize = midi::decodeSysEx(input, buffer, 14, true);
  106. EXPECT_EQ(encodedSize, unsigned(12));
  107. const byte expected[16] = {
  108. 182, 236, 167, 177, 61, 91, 120,
  109. 107, 94, 209, 87, 94, 0, 0,
  110. 0, 0,
  111. };
  112. EXPECT_THAT(input, Each(Le(0x7f))); // All elements are <= 127
  113. EXPECT_THAT(buffer, ContainerEq(expected));
  114. }
  115. // -----------------------------------------------------------------------------
  116. TEST(SysExCodec, CodecAscii)
  117. {
  118. const byte input[] = "Hello, World!";
  119. byte buffer1[16];
  120. byte buffer2[16];
  121. memset(buffer1, 0, 16 * sizeof(byte));
  122. memset(buffer2, 0, 16 * sizeof(byte));
  123. const unsigned encodedSize = midi::encodeSysEx(input, buffer1, 13);
  124. EXPECT_EQ(encodedSize, unsigned(15));
  125. const unsigned decodedSize = midi::decodeSysEx(buffer1, buffer2, encodedSize);
  126. EXPECT_EQ(decodedSize, unsigned(13));
  127. EXPECT_STREQ(reinterpret_cast<const char*>(buffer2),
  128. reinterpret_cast<const char*>(input));
  129. }
  130. TEST(SysExCodec, CodecNonAscii)
  131. {
  132. const byte input[] = {
  133. // MSB Data
  134. 182, 236, 167, 177, 61, 91, 120,
  135. 107, 94, 209, 87, 94
  136. };
  137. byte buffer1[14];
  138. byte buffer2[12];
  139. memset(buffer1, 0, 14 * sizeof(byte));
  140. memset(buffer2, 0, 12 * sizeof(byte));
  141. const unsigned encodedSize = midi::encodeSysEx(input, buffer1, 12);
  142. EXPECT_EQ(encodedSize, unsigned(14));
  143. const unsigned decodedSize = midi::decodeSysEx(buffer1, buffer2, encodedSize);
  144. EXPECT_EQ(decodedSize, unsigned(12));
  145. EXPECT_THAT(buffer2, ContainerEq(input));
  146. }
  147. TEST(SysExCodec, CodecNonAsciiFlipHeader)
  148. {
  149. const byte input[] = {
  150. // MSB Data
  151. 182, 236, 167, 177, 61, 91, 120,
  152. 107, 94, 209, 87, 94
  153. };
  154. byte buffer1[14];
  155. byte buffer2[12];
  156. memset(buffer1, 0, 14 * sizeof(byte));
  157. memset(buffer2, 0, 12 * sizeof(byte));
  158. const unsigned encodedSize = midi::encodeSysEx(input, buffer1, 12, true);
  159. EXPECT_EQ(encodedSize, unsigned(14));
  160. const unsigned decodedSize = midi::decodeSysEx(buffer1, buffer2, encodedSize, true);
  161. EXPECT_EQ(decodedSize, unsigned(12));
  162. EXPECT_THAT(buffer2, ContainerEq(input));
  163. }
  164. END_UNNAMED_NAMESPACE