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.

390 lines
11KB

  1. #include "unit-tests.h"
  2. #include "unit-tests_Settings.h"
  3. #include <src/MIDI.h>
  4. #include <test/mocks/test-mocks_SerialMock.h>
  5. BEGIN_MIDI_NAMESPACE
  6. END_MIDI_NAMESPACE
  7. // -----------------------------------------------------------------------------
  8. BEGIN_UNNAMED_NAMESPACE
  9. using namespace testing;
  10. USING_NAMESPACE_UNIT_TESTS
  11. typedef test_mocks::SerialMock<32> SerialMock;
  12. typedef midi::SerialMIDI<SerialMock> Transport;
  13. typedef midi::MidiInterface<Transport> MidiInterface;
  14. typedef std::vector<byte> Buffer;
  15. template<unsigned Size>
  16. struct VariableSysExSettings : midi::DefaultSettings
  17. {
  18. static const unsigned SysExMaxSize = Size;
  19. };
  20. // -----------------------------------------------------------------------------
  21. TEST(MidiThru, defaultValues)
  22. {
  23. SerialMock serial;
  24. Transport transport(serial);
  25. MidiInterface midi((Transport&)transport);
  26. EXPECT_EQ(midi.getThruState(), true);
  27. EXPECT_EQ(midi.getFilterMode(), midi::Thru::Full);
  28. midi.begin(); // Should not change the state
  29. EXPECT_EQ(midi.getThruState(), true);
  30. EXPECT_EQ(midi.getFilterMode(), midi::Thru::Full);
  31. }
  32. TEST(MidiThru, beginEnablesThru)
  33. {
  34. SerialMock serial;
  35. Transport transport(serial);
  36. MidiInterface midi((Transport&)transport);
  37. midi.turnThruOff();
  38. EXPECT_EQ(midi.getThruState(), false);
  39. EXPECT_EQ(midi.getFilterMode(), midi::Thru::Off);
  40. midi.begin();
  41. EXPECT_EQ(midi.getThruState(), true);
  42. EXPECT_EQ(midi.getFilterMode(), midi::Thru::Full);
  43. }
  44. TEST(MidiThru, setGet)
  45. {
  46. SerialMock serial;
  47. Transport transport(serial);
  48. MidiInterface midi((Transport&)transport);
  49. midi.turnThruOff();
  50. EXPECT_EQ(midi.getThruState(), false);
  51. EXPECT_EQ(midi.getFilterMode(), midi::Thru::Off);
  52. midi.turnThruOn();
  53. EXPECT_EQ(midi.getThruState(), true);
  54. EXPECT_EQ(midi.getFilterMode(), midi::Thru::Full);
  55. midi.turnThruOn(midi::Thru::SameChannel);
  56. EXPECT_EQ(midi.getThruState(), true);
  57. EXPECT_EQ(midi.getFilterMode(), midi::Thru::SameChannel);
  58. midi.turnThruOn(midi::Thru::DifferentChannel);
  59. EXPECT_EQ(midi.getThruState(), true);
  60. EXPECT_EQ(midi.getFilterMode(), midi::Thru::DifferentChannel);
  61. midi.setThruFilterMode(midi::Thru::Full);
  62. EXPECT_EQ(midi.getThruState(), true);
  63. EXPECT_EQ(midi.getFilterMode(), midi::Thru::Full);
  64. midi.setThruFilterMode(midi::Thru::SameChannel);
  65. EXPECT_EQ(midi.getThruState(), true);
  66. EXPECT_EQ(midi.getFilterMode(), midi::Thru::SameChannel);
  67. midi.setThruFilterMode(midi::Thru::DifferentChannel);
  68. EXPECT_EQ(midi.getThruState(), true);
  69. EXPECT_EQ(midi.getFilterMode(), midi::Thru::DifferentChannel);
  70. midi.setThruFilterMode(midi::Thru::Off);
  71. EXPECT_EQ(midi.getThruState(), false);
  72. EXPECT_EQ(midi.getFilterMode(), midi::Thru::Off);
  73. }
  74. TEST(MidiThru, off)
  75. {
  76. SerialMock serial;
  77. Transport transport(serial);
  78. MidiInterface midi((Transport&)transport);
  79. midi.begin(MIDI_CHANNEL_OMNI);
  80. midi.turnThruOff();
  81. static const unsigned rxSize = 5;
  82. static const byte rxData[rxSize] = { 0x9b, 12, 34, 56, 78 };
  83. serial.mRxBuffer.write(rxData, rxSize);
  84. EXPECT_EQ(midi.read(), false);
  85. EXPECT_EQ(midi.read(), false);
  86. EXPECT_EQ(midi.read(), true);
  87. EXPECT_EQ(midi.read(), false);
  88. EXPECT_EQ(midi.read(), true);
  89. EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
  90. }
  91. TEST(MidiThru, full)
  92. {
  93. SerialMock serial;
  94. Transport transport(serial);
  95. MidiInterface midi((Transport&)transport);
  96. Buffer buffer;
  97. midi.begin(MIDI_CHANNEL_OMNI);
  98. midi.setThruFilterMode(midi::Thru::Full);
  99. static const unsigned rxSize = 6;
  100. static const byte rxData[rxSize] = { 0x9b, 12, 34, 0x9c, 56, 78 };
  101. serial.mRxBuffer.write(rxData, rxSize);
  102. EXPECT_EQ(midi.read(), false);
  103. EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
  104. EXPECT_EQ(midi.read(), false);
  105. EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
  106. EXPECT_EQ(midi.read(), true);
  107. buffer.clear();
  108. buffer.resize(3);
  109. EXPECT_EQ(serial.mTxBuffer.getLength(), 3);
  110. serial.mTxBuffer.read(&buffer[0], 3);
  111. EXPECT_THAT(buffer, ElementsAreArray({
  112. 0x9b, 12, 34
  113. }));
  114. EXPECT_EQ(midi.read(), false);
  115. EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
  116. EXPECT_EQ(midi.read(), false);
  117. EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
  118. EXPECT_EQ(midi.read(), true);
  119. buffer.clear();
  120. buffer.resize(3);
  121. EXPECT_EQ(serial.mTxBuffer.getLength(), 3);
  122. serial.mTxBuffer.read(&buffer[0], 3);
  123. EXPECT_THAT(buffer, ElementsAreArray({
  124. 0x9c, 56, 78
  125. }));
  126. }
  127. TEST(MidiThru, sameChannel)
  128. {
  129. SerialMock serial;
  130. Transport transport(serial);
  131. MidiInterface midi((Transport&)transport);
  132. Buffer buffer;
  133. midi.begin(12);
  134. midi.setThruFilterMode(midi::Thru::SameChannel);
  135. static const unsigned rxSize = 6;
  136. static const byte rxData[rxSize] = { 0x9b, 12, 34, 0x9c, 56, 78 };
  137. serial.mRxBuffer.write(rxData, rxSize);
  138. EXPECT_EQ(midi.read(), false);
  139. EXPECT_EQ(midi.read(), false);
  140. EXPECT_EQ(midi.read(), true);
  141. EXPECT_EQ(midi.read(), false);
  142. EXPECT_EQ(midi.read(), false);
  143. EXPECT_EQ(midi.read(), false);
  144. buffer.clear();
  145. buffer.resize(3);
  146. EXPECT_EQ(serial.mTxBuffer.getLength(), 3);
  147. serial.mTxBuffer.read(&buffer[0], 3);
  148. EXPECT_THAT(buffer, ElementsAreArray({
  149. 0x9b, 12, 34
  150. }));
  151. }
  152. TEST(MidiThru, sameChannelOmni) // Acts like full
  153. {
  154. SerialMock serial;
  155. Transport transport(serial);
  156. MidiInterface midi((Transport&)transport);
  157. Buffer buffer;
  158. midi.begin(MIDI_CHANNEL_OMNI);
  159. midi.setThruFilterMode(midi::Thru::SameChannel);
  160. static const unsigned rxSize = 6;
  161. static const byte rxData[rxSize] = { 0x9b, 12, 34, 0x9c, 56, 78 };
  162. serial.mRxBuffer.write(rxData, rxSize);
  163. EXPECT_EQ(midi.read(), false);
  164. EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
  165. EXPECT_EQ(midi.read(), false);
  166. EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
  167. EXPECT_EQ(midi.read(), true);
  168. buffer.clear();
  169. buffer.resize(3);
  170. EXPECT_EQ(serial.mTxBuffer.getLength(), 3);
  171. serial.mTxBuffer.read(&buffer[0], 3);
  172. EXPECT_THAT(buffer, ElementsAreArray({
  173. 0x9b, 12, 34
  174. }));
  175. buffer.clear();
  176. buffer.resize(3);
  177. EXPECT_EQ(midi.read(), false);
  178. EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
  179. EXPECT_EQ(midi.read(), false);
  180. EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
  181. EXPECT_EQ(midi.read(), true);
  182. EXPECT_EQ(serial.mTxBuffer.getLength(), 3); // Not using TX running status
  183. serial.mTxBuffer.read(&buffer[0], 3);
  184. EXPECT_THAT(buffer, ElementsAreArray({
  185. 0x9c, 56, 78
  186. }));
  187. }
  188. TEST(MidiThru, differentChannel)
  189. {
  190. SerialMock serial;
  191. Transport transport(serial);
  192. MidiInterface midi((Transport&)transport);
  193. Buffer buffer;
  194. midi.begin(12);
  195. midi.setThruFilterMode(midi::Thru::DifferentChannel);
  196. static const unsigned rxSize = 6;
  197. static const byte rxData[rxSize] = { 0x9b, 12, 34, 0x9c, 56, 78 };
  198. serial.mRxBuffer.write(rxData, rxSize);
  199. EXPECT_EQ(midi.read(), false);
  200. EXPECT_EQ(midi.read(), false);
  201. EXPECT_EQ(midi.read(), true);
  202. EXPECT_EQ(midi.read(), false);
  203. EXPECT_EQ(midi.read(), false);
  204. EXPECT_EQ(midi.read(), false);
  205. buffer.clear();
  206. buffer.resize(3);
  207. EXPECT_EQ(serial.mTxBuffer.getLength(), 3);
  208. serial.mTxBuffer.read(&buffer[0], 3);
  209. EXPECT_THAT(buffer, ElementsAreArray({
  210. 0x9c, 56, 78
  211. }));
  212. }
  213. TEST(MidiThru, differentChannelOmni) // Acts like off
  214. {
  215. SerialMock serial;
  216. Transport transport(serial);
  217. MidiInterface midi((Transport&)transport);
  218. Buffer buffer;
  219. midi.begin(MIDI_CHANNEL_OMNI);
  220. midi.setThruFilterMode(midi::Thru::DifferentChannel);
  221. static const unsigned rxSize = 6;
  222. static const byte rxData[rxSize] = { 0x9b, 12, 34, 0x9c, 56, 78 };
  223. serial.mRxBuffer.write(rxData, rxSize);
  224. EXPECT_EQ(midi.read(), false);
  225. EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
  226. EXPECT_EQ(midi.read(), false);
  227. EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
  228. EXPECT_EQ(midi.read(), true);
  229. EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
  230. EXPECT_EQ(midi.read(), false);
  231. EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
  232. EXPECT_EQ(midi.read(), false);
  233. EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
  234. EXPECT_EQ(midi.read(), true);
  235. EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
  236. }
  237. TEST(MidiThru, multiByteThru)
  238. {
  239. typedef VariableSettings<false, false> MultiByteParsing;
  240. typedef midi::MidiInterface<Transport, MultiByteParsing> MultiByteMidiInterface;
  241. SerialMock serial;
  242. Transport transport(serial);
  243. MultiByteMidiInterface midi((Transport&)transport);
  244. Buffer buffer;
  245. midi.begin(MIDI_CHANNEL_OMNI);
  246. midi.setThruFilterMode(midi::Thru::Full);
  247. static const unsigned rxSize = 6;
  248. static const byte rxData[rxSize] = { 0x9b, 12, 34, 56, 78 };
  249. serial.mRxBuffer.write(rxData, rxSize);
  250. EXPECT_EQ(midi.read(), true);
  251. EXPECT_EQ(serial.mTxBuffer.getLength(), 3);
  252. EXPECT_EQ(midi.read(), true);
  253. EXPECT_EQ(serial.mTxBuffer.getLength(), 6);
  254. buffer.clear();
  255. buffer.resize(6);
  256. serial.mTxBuffer.read(&buffer[0], 6);
  257. EXPECT_THAT(buffer, ElementsAreArray({
  258. 0x9b, 12, 34, 0x9b, 56, 78
  259. }));
  260. }
  261. TEST(MidiThru, withTxRunningStatus)
  262. {
  263. typedef VariableSettings<true, true> Settings;
  264. typedef midi::MidiInterface<Transport, Settings> RsMidiInterface;
  265. SerialMock serial;
  266. Transport transport(serial);
  267. RsMidiInterface midi((Transport&)transport);
  268. Buffer buffer;
  269. midi.begin(MIDI_CHANNEL_OMNI);
  270. midi.setThruFilterMode(midi::Thru::Full);
  271. static const unsigned rxSize = 5;
  272. static const byte rxData[rxSize] = { 0x9b, 12, 34, 56, 78 };
  273. serial.mRxBuffer.write(rxData, rxSize);
  274. EXPECT_EQ(midi.read(), false);
  275. EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
  276. EXPECT_EQ(midi.read(), false);
  277. EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
  278. EXPECT_EQ(midi.read(), true);
  279. buffer.clear();
  280. buffer.resize(3);
  281. EXPECT_EQ(serial.mTxBuffer.getLength(), 3);
  282. serial.mTxBuffer.read(&buffer[0], 3);
  283. EXPECT_THAT(buffer, ElementsAreArray({
  284. 0x9b, 12, 34
  285. }));
  286. EXPECT_EQ(midi.read(), false);
  287. EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
  288. EXPECT_EQ(midi.read(), true);
  289. buffer.clear();
  290. buffer.resize(2);
  291. EXPECT_EQ(serial.mTxBuffer.getLength(), 2);
  292. serial.mTxBuffer.read(&buffer[0], 2);
  293. EXPECT_THAT(buffer, ElementsAreArray({
  294. 56, 78
  295. }));
  296. }
  297. TEST(MidiThru, invalidMode)
  298. {
  299. SerialMock serial;
  300. Transport transport(serial);
  301. MidiInterface midi((Transport&)transport);
  302. midi.begin(MIDI_CHANNEL_OMNI);
  303. midi.setThruFilterMode(midi::Thru::Mode(42));
  304. static const unsigned rxSize = 6;
  305. static const byte rxData[rxSize] = { 0x9b, 12, 34, 0x9c, 56, 78 };
  306. serial.mRxBuffer.write(rxData, rxSize);
  307. EXPECT_EQ(midi.read(), false);
  308. EXPECT_EQ(midi.read(), false);
  309. EXPECT_EQ(midi.read(), true);
  310. EXPECT_EQ(midi.read(), false);
  311. EXPECT_EQ(midi.read(), false);
  312. EXPECT_EQ(midi.read(), true);
  313. EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
  314. }
  315. END_UNNAMED_NAMESPACE