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.

uoCSVWriterTest.cpp 22KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /*********************************************************************
  2. * Software License Agreement (AGPL-3 License)
  3. *
  4. * OpenViBE SDK Test Software
  5. * Based on OpenViBE V1.1.0, Copyright (C) Inria, 2006-2015
  6. * Copyright (C) Inria, 2015-2017,V1.0
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program.
  19. * If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "gtest/gtest.h"
  22. #include "csv/ovICSV.h"
  23. #include <string>
  24. #include <fstream>
  25. #include <streambuf>
  26. #include <numeric>
  27. static std::string directoryPath = "";
  28. TEST(CSV_Writer_Test_Case, signalWriterNormalGoodSignal)
  29. {
  30. OpenViBE::CSV::ICSVHandler* handler = OpenViBE::CSV::createCSVHandler();
  31. const std::string filename = directoryPath + "testCSVSignalWriter01.csv";
  32. ASSERT_TRUE(handler->openFile(filename, OpenViBE::CSV::EFileAccessMode::Write));
  33. handler->setFormatType(OpenViBE::CSV::EStreamType::Signal);
  34. ASSERT_TRUE(handler->setSignalInformation({ "O1", "O2", "Pz", "P1", "P2" }, 8, 8));
  35. double index = 0.0;
  36. while (index < 1.2)
  37. {
  38. const double epoch = index / 0.5;
  39. ASSERT_TRUE(handler->addSample({ index, index + 0.125, { -10.10, -5.05, 0.00, 5.05, 10.10 }, size_t(epoch) }));
  40. if (index == 0.25 || index == 0.75) { ASSERT_TRUE(handler->addEvent(35000, index, 0.0)); }
  41. index += 0.125;
  42. }
  43. ASSERT_TRUE(handler->writeHeaderToFile());
  44. ASSERT_TRUE(handler->writeDataToFile());
  45. ASSERT_TRUE(handler->closeFile());
  46. releaseCSVHandler(handler);
  47. }
  48. TEST(CSV_Writer_Test_Case, signalWriterNoStimulations)
  49. {
  50. OpenViBE::CSV::ICSVHandler* handler = OpenViBE::CSV::createCSVHandler();
  51. const std::string filename = directoryPath + "testCSVSignalWriter02.csv";
  52. ASSERT_TRUE(handler->openFile(filename, OpenViBE::CSV::EFileAccessMode::Write));
  53. handler->setFormatType(OpenViBE::CSV::EStreamType::Signal);
  54. ASSERT_TRUE(handler->setSignalInformation({ "O1", "O2", "Pz", "P1", "P2" }, 8, 8));
  55. ASSERT_TRUE(handler->noEventsUntilDate(2.0));
  56. double index = 0.0;
  57. while (index < 1.2)
  58. {
  59. const double epoch = index / 0.5;
  60. ASSERT_TRUE(handler->addSample({ index, index + 0.125, { -10.10, -5.05, 0.00, 5.05, 10.10 }, size_t(epoch) }));
  61. index += 0.125;
  62. }
  63. ASSERT_TRUE(handler->writeHeaderToFile());
  64. ASSERT_TRUE(handler->writeDataToFile());
  65. ASSERT_TRUE(handler->closeFile());
  66. releaseCSVHandler(handler);
  67. }
  68. TEST(CSV_Writer_Test_Case, signalWriterNoFileOpen)
  69. {
  70. OpenViBE::CSV::ICSVHandler* handler = OpenViBE::CSV::createCSVHandler();
  71. handler->setFormatType(OpenViBE::CSV::EStreamType::Signal);
  72. ASSERT_TRUE(handler->setSignalInformation({ "O1", "O2", "Pz", "P1", "P2" }, 8, 8));
  73. double index = 0.0;
  74. while (index < 1.2)
  75. {
  76. const double epoch = index / 0.5;
  77. ASSERT_TRUE(handler->addSample({ index, index + 0.125, { -10.10, -5.05, 0.00, 5.05, 10.10 }, size_t(epoch) }));
  78. index += 0.125;
  79. }
  80. ASSERT_FALSE(handler->writeHeaderToFile());
  81. ASSERT_TRUE(handler->closeFile());
  82. releaseCSVHandler(handler);
  83. }
  84. TEST(CSV_Writer_Test_Case, signalWriterWrongInputType)
  85. {
  86. OpenViBE::CSV::ICSVHandler* handler = OpenViBE::CSV::createCSVHandler();
  87. handler->setFormatType(OpenViBE::CSV::EStreamType::Spectrum);
  88. ASSERT_FALSE(handler->setSignalInformation({ "O1", "O2", "Pz", "P1", "P2" }, 8, 8));
  89. releaseCSVHandler(handler);
  90. }
  91. TEST(CSV_Writer_Test_Case, signalWriterWrongMatrixSize)
  92. {
  93. OpenViBE::CSV::ICSVHandler* handler = OpenViBE::CSV::createCSVHandler();
  94. const std::string filename = directoryPath + "testCSVSignalWriter05.csv";
  95. ASSERT_TRUE(handler->openFile(filename, OpenViBE::CSV::EFileAccessMode::Write));
  96. handler->setFormatType(OpenViBE::CSV::EStreamType::Signal);
  97. ASSERT_TRUE(handler->setSignalInformation({ "O1", "O2", "Pz", "P1", "P2" }, 8, 8));
  98. ASSERT_FALSE(handler->addSample({ 0, 0.125, { -20.20, -10.10, -5.05, 5.05, 10.10, 20.20 }, 0 }));
  99. ASSERT_TRUE(handler->closeFile());
  100. releaseCSVHandler(handler);
  101. }
  102. // should have nothing in the file
  103. TEST(CSV_Writer_Test_Case, signalWriterTonsOfSignalWithoutSetNoEventsUntilDate)
  104. {
  105. OpenViBE::CSV::ICSVHandler* handler = OpenViBE::CSV::createCSVHandler();
  106. const std::string filename = directoryPath + "testCSVSignalWriter06.csv";
  107. ASSERT_TRUE(handler->openFile(filename, OpenViBE::CSV::EFileAccessMode::Write));
  108. handler->setFormatType(OpenViBE::CSV::EStreamType::Signal);
  109. ASSERT_TRUE(handler->setSignalInformation({ "O1", "O2", "Pz", "P1", "P2" }, 8, 8));
  110. double time = 0.0;
  111. while (time < 100.0)
  112. {
  113. ASSERT_TRUE(handler->addSample({ time, time + 0.125, { -20.20, -10.10, 0.0, 10.10, 20.20 }, size_t(time / 0.125) }));
  114. time += 0.125;
  115. }
  116. ASSERT_TRUE(handler->writeHeaderToFile());
  117. ASSERT_TRUE(handler->writeDataToFile());
  118. releaseCSVHandler(handler);
  119. }
  120. // file should be full
  121. TEST(CSV_Writer_Test_Case, signalWriterTonsOfSignalWithSetNoEventsUntilDate)
  122. {
  123. OpenViBE::CSV::ICSVHandler* handler = OpenViBE::CSV::createCSVHandler();
  124. const std::string filename = directoryPath + "testCSVSignalWriter07.csv";
  125. ASSERT_TRUE(handler->openFile(filename, OpenViBE::CSV::EFileAccessMode::Write));
  126. handler->setFormatType(OpenViBE::CSV::EStreamType::Signal);
  127. ASSERT_TRUE(handler->setSignalInformation({ "O1", "O2", "Pz", "P1", "P2" }, 8, 8));
  128. ASSERT_TRUE(handler->noEventsUntilDate(100.001));
  129. double time = 0.0;
  130. while (time < 100.0)
  131. {
  132. ASSERT_TRUE(handler->addSample({ time, time + 0.125, { -20.20, -10.10, 0.0, 10.10, 20.20 }, size_t(time / 0.5) }));
  133. time += 0.125;
  134. }
  135. ASSERT_TRUE(handler->writeHeaderToFile());
  136. ASSERT_TRUE(handler->writeDataToFile());
  137. ASSERT_TRUE(handler->writeAllDataToFile());
  138. releaseCSVHandler(handler);
  139. }
  140. TEST(CSV_Writer_Test_Case, signalWriterOnlyLastMatrix)
  141. {
  142. OpenViBE::CSV::ICSVHandler* handler = OpenViBE::CSV::createCSVHandler();
  143. handler->setLastMatrixOnlyMode(true);
  144. const std::string filename = directoryPath + "testCSVSignalWriter08.csv";
  145. const std::string expectedFileContent(
  146. "Time:8Hz,Epoch,O1,O2,Pz,P1,P2,Event Id,Event Date,Event Duration\n1.0000000000,2,-10.1000000000,-5.0500000000,0.0000000000,5.0500000000,10.1000000000,35000,1.0000000000,0.0000000000\n1.1250000000,2,-10.1000000000,-5.0500000000,0.0000000000,5.0500000000,10.1000000000,,,\n");
  147. ASSERT_TRUE(handler->openFile(filename, OpenViBE::CSV::EFileAccessMode::Write));
  148. handler->setFormatType(OpenViBE::CSV::EStreamType::Signal);
  149. ASSERT_TRUE(handler->setSignalInformation({ "O1", "O2", "Pz", "P1", "P2" }, 8, 8));
  150. double index = 0.0;
  151. while (index < 1.2)
  152. {
  153. const double epoch = index / 0.5;
  154. ASSERT_TRUE(handler->addSample({ index, index + 0.125, { -10.10, -5.05, 0.00, 5.05, 10.10 }, size_t(epoch) }));
  155. if (index == 0.25 || index == 0.75 || index == 1.0) { ASSERT_TRUE(handler->addEvent(35000, index, 0.0)); }
  156. index += 0.125;
  157. }
  158. ASSERT_TRUE(handler->writeHeaderToFile());
  159. ASSERT_TRUE(handler->writeAllDataToFile());
  160. ASSERT_TRUE(handler->closeFile());
  161. releaseCSVHandler(handler);
  162. std::ifstream t(filename);
  163. const std::string out((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
  164. ASSERT_STREQ(out.c_str(), expectedFileContent.c_str());
  165. }
  166. TEST(CSV_Writer_Test_Case, spectrumWriterNormalGoodSignal)
  167. {
  168. OpenViBE::CSV::ICSVHandler* handler = OpenViBE::CSV::createCSVHandler();
  169. const std::string filename = directoryPath + "testCSVSpectrumWriter01.csv";
  170. ASSERT_TRUE(handler->openFile(filename, OpenViBE::CSV::EFileAccessMode::Write));
  171. handler->setFormatType(OpenViBE::CSV::EStreamType::Spectrum);
  172. std::vector<double> frequencyAbscissa(64);
  173. std::iota(frequencyAbscissa.begin(), frequencyAbscissa.end(), 0.0);
  174. ASSERT_TRUE(handler->setSpectrumInformation({ "O1", "O2" }, frequencyAbscissa, 256));
  175. double time = 0;
  176. for (size_t i = 0; i < 10; ++i)
  177. {
  178. const size_t epoch = i / 4;
  179. std::vector<double> sample(128);
  180. std::iota(sample.begin(), sample.end(), -64);
  181. ASSERT_TRUE(handler->addSample({ time, time + 1.0, sample, epoch }));
  182. time += 0.125;
  183. if (i == 5 || i == 3 || i == 7) { ASSERT_TRUE(handler->addEvent(35001, time, 1.0)); }
  184. }
  185. ASSERT_TRUE(handler->writeHeaderToFile());
  186. ASSERT_TRUE(handler->writeDataToFile());
  187. ASSERT_TRUE(handler->closeFile());
  188. releaseCSVHandler(handler);
  189. }
  190. TEST(CSV_Writer_Test_Case, spectrumWriterWrongInputType)
  191. {
  192. OpenViBE::CSV::ICSVHandler* handler = OpenViBE::CSV::createCSVHandler();
  193. const std::string filename = directoryPath + "testCSVSpectrumWriter02.csv";
  194. ASSERT_TRUE(handler->openFile(filename, OpenViBE::CSV::EFileAccessMode::Write));
  195. handler->setFormatType(OpenViBE::CSV::EStreamType::Signal);
  196. std::vector<double> frequencyAbscissa(64);
  197. std::iota(frequencyAbscissa.begin(), frequencyAbscissa.end(), 0.0);
  198. ASSERT_FALSE(handler->setSpectrumInformation({ "O1", "O2" }, frequencyAbscissa, 256));
  199. ASSERT_TRUE(handler->closeFile());
  200. releaseCSVHandler(handler);
  201. }
  202. TEST(CSV_Writer_Test_Case, spectrumWriterWrongMatrixSize)
  203. {
  204. OpenViBE::CSV::ICSVHandler* handler = OpenViBE::CSV::createCSVHandler();
  205. const std::string filename = directoryPath + "testCSVSpectrumWriter03.csv";
  206. ASSERT_TRUE(handler->openFile(filename, OpenViBE::CSV::EFileAccessMode::Write));
  207. handler->setFormatType(OpenViBE::CSV::EStreamType::Spectrum);
  208. std::vector<double> frequencyAbscissa(64);
  209. std::iota(frequencyAbscissa.begin(), frequencyAbscissa.end(), 0.0);
  210. ASSERT_TRUE(handler->setSpectrumInformation({ "O1", "O2" }, frequencyAbscissa, 256));
  211. ASSERT_FALSE(handler->addSample({ 0, 1, { -20.20, -10.10, 0.0, 10.10, 20.20 }, 0 }));
  212. ASSERT_TRUE(handler->closeFile());
  213. releaseCSVHandler(handler);
  214. }
  215. TEST(CSV_Writer_Test_Case, matrixWriterNormalGoodSignal)
  216. {
  217. OpenViBE::CSV::ICSVHandler* handler = OpenViBE::CSV::createCSVHandler();
  218. const std::string filename = directoryPath + "testCSVMatrixWriter01.csv";
  219. ASSERT_TRUE(handler->openFile(filename, OpenViBE::CSV::EFileAccessMode::Write));
  220. handler->setFormatType(OpenViBE::CSV::EStreamType::StreamedMatrix);
  221. ASSERT_TRUE(handler->setStreamedMatrixInformation({ 2, 2, 2 }, { "LA", "LB", "1", "2", "X", "Y" }));
  222. for (size_t i = 0; i < 50; ++i)
  223. {
  224. const size_t epoch = i / 10;
  225. ASSERT_TRUE(handler->addSample({ double(i), double(i)+1.0, { -20.20, -15.15, -10.10, -5.05, 5.05, 10.10, 15.15, 20.20 }, epoch }));
  226. if (i == 5 || i == 3 || i == 7) { ASSERT_TRUE(handler->addEvent(35001, double(i)+3.5, 1.0)); }
  227. }
  228. ASSERT_TRUE(handler->writeHeaderToFile());
  229. ASSERT_TRUE(handler->writeDataToFile());
  230. ASSERT_TRUE(handler->closeFile());
  231. releaseCSVHandler(handler);
  232. }
  233. TEST(CSV_Writer_Test_Case, matrixWriterEmptyLabels)
  234. {
  235. OpenViBE::CSV::ICSVHandler* handler = OpenViBE::CSV::createCSVHandler();
  236. const std::string filename = directoryPath + "testCSVMatrixWriter02.csv";
  237. ASSERT_TRUE(handler->openFile(filename, OpenViBE::CSV::EFileAccessMode::Write));
  238. handler->setFormatType(OpenViBE::CSV::EStreamType::StreamedMatrix);
  239. ASSERT_TRUE(handler->setStreamedMatrixInformation({ 2, 2, 2 }, { "", "", "", "", "", "" }));
  240. for (size_t i = 0; i < 50; ++i)
  241. {
  242. const size_t epoch = i / 10;
  243. ASSERT_TRUE(handler->addSample({ double(i), double(i)+1.0, { -20.20, -15.15, -10.10, -5.05, 5.05, 10.10, 15.15, 20.20 }, epoch }));
  244. if (i == 5 || i == 3 || i == 7) { ASSERT_TRUE(handler->addEvent(35001, double(i)+3.5, 1.0)); }
  245. }
  246. ASSERT_TRUE(handler->writeHeaderToFile());
  247. ASSERT_TRUE(handler->writeDataToFile());
  248. ASSERT_TRUE(handler->closeFile());
  249. releaseCSVHandler(handler);
  250. }
  251. TEST(CSV_Writer_Test_Case, matrixWithDifferentsDimensionSizes)
  252. {
  253. OpenViBE::CSV::ICSVHandler* handler = OpenViBE::CSV::createCSVHandler();
  254. const std::string filename = directoryPath + "testCSVMatrixWriter03.csv";
  255. ASSERT_TRUE(handler->openFile(filename, OpenViBE::CSV::EFileAccessMode::Write));
  256. handler->setFormatType(OpenViBE::CSV::EStreamType::StreamedMatrix);
  257. ASSERT_TRUE(handler->setStreamedMatrixInformation({ 1, 4 }, { "L1", "A", "B", "C", "D" }));
  258. for (size_t i = 0; i < 50; ++i)
  259. {
  260. const size_t epoch = i / 10;
  261. ASSERT_TRUE(handler->addSample({ double(i), double(i)+1.0, { -20.20, -10.10, 10.10, 20.20 }, epoch }));
  262. if (i == 5 || i == 3 || i == 7) { ASSERT_TRUE(handler->addEvent(35001, double(i)+3.5, 1.0)); }
  263. }
  264. ASSERT_TRUE(handler->writeHeaderToFile());
  265. ASSERT_TRUE(handler->writeDataToFile());
  266. ASSERT_TRUE(handler->closeFile());
  267. releaseCSVHandler(handler);
  268. }
  269. TEST(CSV_Writer_Test_Case, matrixWriterWrongMatrixSize)
  270. {
  271. OpenViBE::CSV::ICSVHandler* handler = OpenViBE::CSV::createCSVHandler();
  272. const std::string filename = directoryPath + "testCSVMatrixWriter04.csv";
  273. ASSERT_TRUE(handler->openFile(filename, OpenViBE::CSV::EFileAccessMode::Write));
  274. handler->setFormatType(OpenViBE::CSV::EStreamType::StreamedMatrix);
  275. ASSERT_TRUE(handler->setStreamedMatrixInformation({ 2, 2, 2 }, { "", "", "", "", "", "" }));
  276. ASSERT_FALSE(handler->addSample({ 0, 1.0, { -25.25, -20.20, -15.15, -10.10, -5.05, 5.05, 10.10, 15.15, 20.20, 25.25 }, 0 }));
  277. ASSERT_TRUE(handler->closeFile());
  278. releaseCSVHandler(handler);
  279. }
  280. TEST(CSV_Writer_Test_Case, matrixWithDifferentsDimensionSizes2)
  281. {
  282. OpenViBE::CSV::ICSVHandler* handler = OpenViBE::CSV::createCSVHandler();
  283. const std::string filename = directoryPath + "testCSVMatrixWriter05.csv";
  284. ASSERT_TRUE(handler->openFile(filename, OpenViBE::CSV::EFileAccessMode::Write));
  285. handler->setFormatType(OpenViBE::CSV::EStreamType::StreamedMatrix);
  286. ASSERT_TRUE(
  287. handler->setStreamedMatrixInformation({ 6, 8, 2 }, { "L1", "L2", "L3", "L4", "L5", "L6", "A1", "B2", "C3", "D4", "E5", "F6", "G7", "H8", "X", "Y" }));
  288. std::vector<double> values(96);
  289. std::iota(values.begin(), values.end(), 0.0);
  290. for (size_t i = 0; i < 50; ++i)
  291. {
  292. const size_t epoch = i / 10;
  293. ASSERT_TRUE(handler->addSample({ double(i), double(i)+1.0, values, epoch }));
  294. if (i == 5 || i == 3 || i == 7) { ASSERT_TRUE(handler->addEvent(35001, double(i)+3.5, 1.0)); }
  295. }
  296. ASSERT_TRUE(handler->writeHeaderToFile());
  297. ASSERT_TRUE(handler->writeDataToFile());
  298. ASSERT_TRUE(handler->closeFile());
  299. releaseCSVHandler(handler);
  300. }
  301. TEST(CSV_Writer_Test_Case, matrixWithDifferentsDimensionSizes3)
  302. {
  303. OpenViBE::CSV::ICSVHandler* handler = OpenViBE::CSV::createCSVHandler();
  304. const std::string filename = directoryPath + "testCSVMatrixWriter06.csv";
  305. ASSERT_TRUE(handler->openFile(filename, OpenViBE::CSV::EFileAccessMode::Write));
  306. handler->setFormatType(OpenViBE::CSV::EStreamType::StreamedMatrix);
  307. ASSERT_TRUE(handler->setStreamedMatrixInformation({ 4, 1, 4 }, { "L1", "L2", "L3", "L4", "X", "R1", "R2", "R3", "R4" }));
  308. std::vector<double> values(16);
  309. std::iota(values.begin(), values.end(), 0.0);
  310. for (size_t i = 0; i < 50; ++i)
  311. {
  312. const size_t epoch = i / 10;
  313. ASSERT_TRUE(handler->addSample({ double(i), double(i)+1.0, values, epoch }));
  314. if (i == 5 || i == 3 || i == 7) { ASSERT_TRUE(handler->addEvent(35001, double(i)+3.5, 1.0)); }
  315. }
  316. ASSERT_TRUE(handler->writeHeaderToFile());
  317. ASSERT_TRUE(handler->writeDataToFile());
  318. ASSERT_TRUE(handler->closeFile());
  319. releaseCSVHandler(handler);
  320. }
  321. // As of 10/01/2017 (commit a11210cf1c3fd81bb52095c7c9c6006c760218a2), this is valid for
  322. TEST(CSV_Writer_Test_Case, matrixWriterWithInvalidTime)
  323. {
  324. OpenViBE::CSV::ICSVHandler* handler = OpenViBE::CSV::createCSVHandler();
  325. const std::string filename = directoryPath + "testCSVMatrixWriter07.csv";
  326. ASSERT_TRUE(handler->openFile(filename, OpenViBE::CSV::EFileAccessMode::Write));
  327. handler->setFormatType(OpenViBE::CSV::EStreamType::StreamedMatrix);
  328. ASSERT_TRUE(handler->setStreamedMatrixInformation({ 1, 1, 1 }, { "X", "Y", "Z" }));
  329. ASSERT_FALSE(handler->addSample({ 1.0, 0, { -20.20, -15.15, -10.10 }, 0 }));
  330. ASSERT_FALSE(handler->addSample({ -1.0, 0, { -20.20, -15.15, -10.10 }, 1 }));
  331. ASSERT_FALSE(handler->addSample({ -1.0, -0.5, { -20.20, -15.15, -10.10 }, 2 }));
  332. ASSERT_FALSE(handler->addSample({ 1.0, -1.0, { -20.20, -15.15, -10.10 }, 3 }));
  333. ASSERT_TRUE(handler->closeFile());
  334. releaseCSVHandler(handler);
  335. }
  336. TEST(CSV_Writer_Test_Case, matrixWriterOnlyLastMatrix)
  337. {
  338. OpenViBE::CSV::ICSVHandler* handler = OpenViBE::CSV::createCSVHandler();
  339. handler->setLastMatrixOnlyMode(true);
  340. const std::string filename = directoryPath + "testCSVMatrixWriter08.csv";
  341. const std::string expectedFileContent(
  342. "Time:2x2x2,End Time,LA:1:X,LA:1:Y,LA:2:X,LA:2:Y,LB:1:X,LB:1:Y,LB:2:X,LB:2:Y,Event Id,Event Date,Event Duration\n49.0000000000,50.0000000000,49.0000000000,1.0000000000,2.0000000000,3.0000000000,4.0000000000,5.0000000000,6.0000000000,7.0000000000,,,\n");
  343. ASSERT_TRUE(handler->openFile(filename, OpenViBE::CSV::EFileAccessMode::Write));
  344. handler->setFormatType(OpenViBE::CSV::EStreamType::StreamedMatrix);
  345. ASSERT_TRUE(handler->setStreamedMatrixInformation({ 2, 2, 2 }, { "LA", "LB", "1", "2", "X", "Y" }));
  346. ASSERT_TRUE(handler->writeHeaderToFile());
  347. for (size_t i = 0; i < 50; ++i)
  348. {
  349. const size_t epoch = i / 10;
  350. ASSERT_TRUE(handler->addSample({ double(i), double(i) + 1.0, { double(i), 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 }, epoch }));
  351. if (i == 3 || i == 5 || i == 7) { ASSERT_TRUE(handler->addEvent(35001, double(i) + 3.5, 1.0)); }
  352. }
  353. ASSERT_TRUE(handler->writeAllDataToFile());
  354. ASSERT_TRUE(handler->closeFile());
  355. releaseCSVHandler(handler);
  356. std::ifstream t(filename);
  357. const std::string out((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
  358. ASSERT_STREQ(out.c_str(), expectedFileContent.c_str());
  359. }
  360. TEST(CSV_Writer_Test_Case, featureVectorNormalGoodSignal)
  361. {
  362. OpenViBE::CSV::ICSVHandler* handler = OpenViBE::CSV::createCSVHandler();
  363. const std::string filename = directoryPath + "testCSVFeatureVectorWriter01.csv";
  364. ASSERT_TRUE(handler->openFile(filename, OpenViBE::CSV::EFileAccessMode::Write));
  365. handler->setFormatType(OpenViBE::CSV::EStreamType::FeatureVector);
  366. ASSERT_TRUE(handler->setFeatureVectorInformation({ "F1", "F2", "F3" }));
  367. for (size_t i = 0; i < 50; ++i)
  368. {
  369. const size_t epoch = i / 10;
  370. ASSERT_TRUE(handler->addSample({ double(i), double(i)+1.0, { -20.20, -15.15, -10.10 }, epoch }));
  371. if (i == 5 || i == 3 || i == 7) { ASSERT_TRUE(handler->addEvent(35001, double(i)+3.5, 1.0)); }
  372. }
  373. ASSERT_TRUE(handler->writeHeaderToFile());
  374. ASSERT_TRUE(handler->writeDataToFile());
  375. ASSERT_TRUE(handler->closeFile());
  376. releaseCSVHandler(handler);
  377. }
  378. TEST(CSV_Writer_Test_Case, featureVectorEmptyLabels)
  379. {
  380. OpenViBE::CSV::ICSVHandler* handler = OpenViBE::CSV::createCSVHandler();
  381. const std::string filename = directoryPath + "testCSVFeatureVectorWriter02.csv";
  382. ASSERT_TRUE(handler->openFile(filename, OpenViBE::CSV::EFileAccessMode::Write));
  383. handler->setFormatType(OpenViBE::CSV::EStreamType::FeatureVector);
  384. ASSERT_TRUE(handler->setFeatureVectorInformation({ "", "", "" }));
  385. for (size_t i = 0; i < 50; ++i)
  386. {
  387. const size_t epoch = i / 10;
  388. ASSERT_TRUE(handler->addSample({ double(i), double(i)+1.0, { -20.20, -15.15, -10.10 }, epoch }));
  389. }
  390. ASSERT_TRUE(handler->writeHeaderToFile());
  391. ASSERT_TRUE(handler->writeDataToFile());
  392. ASSERT_TRUE(handler->closeFile());
  393. releaseCSVHandler(handler);
  394. }
  395. TEST(CSV_Writer_Test_Case, featureVectorWrongVectorSize)
  396. {
  397. OpenViBE::CSV::ICSVHandler* handler = OpenViBE::CSV::createCSVHandler();
  398. const std::string filename = directoryPath + "testCSVFeatureVectorWriter03.csv";
  399. ASSERT_TRUE(handler->openFile(filename, OpenViBE::CSV::EFileAccessMode::Write));
  400. handler->setFormatType(OpenViBE::CSV::EStreamType::FeatureVector);
  401. ASSERT_TRUE(handler->setFeatureVectorInformation({ "F1", "F2", "F3" }));
  402. ASSERT_FALSE(handler->addSample({ 0, 1.0, { -20.20, -15.15, -10.10, 12 }, 0 }));
  403. ASSERT_FALSE(handler->addSample({ 1.0, 2.0, { -20.20, -15.15 }, 1 }));
  404. ASSERT_TRUE(handler->closeFile());
  405. releaseCSVHandler(handler);
  406. }
  407. TEST(CSV_Writer_Test_Case, covarianceMatrixWriterNormalGoodSignal)
  408. {
  409. OpenViBE::CSV::ICSVHandler* handler = OpenViBE::CSV::createCSVHandler();
  410. const std::string filename = directoryPath + "testCSVCovarMatrixWriter01.csv";
  411. ASSERT_TRUE(handler->openFile(filename, OpenViBE::CSV::EFileAccessMode::Write));
  412. handler->setFormatType(OpenViBE::CSV::EStreamType::CovarianceMatrix);
  413. ASSERT_TRUE(handler->setStreamedMatrixInformation({ 2, 2, 2 }, { "C1", "C2", "C1", "C2", "Matrix 1", "Matrix 2" }));
  414. for (size_t i = 0; i < 50; ++i)
  415. {
  416. const size_t epoch = i / 10;
  417. ASSERT_TRUE(handler->addSample({ double(i), double(i)+1.0, { -20.20, -15.15, -10.10, -5.05, 5.05, 10.10, 15.15, 20.20 }, epoch }));
  418. }
  419. handler->noEventsUntilDate(20.0);
  420. ASSERT_TRUE(handler->writeHeaderToFile());
  421. ASSERT_TRUE(handler->writeAllDataToFile());
  422. ASSERT_TRUE(handler->closeFile());
  423. releaseCSVHandler(handler);
  424. }
  425. TEST(CSV_Writer_Test_Case, covarianceMatrixWriterEmptyLabels)
  426. {
  427. OpenViBE::CSV::ICSVHandler* handler = OpenViBE::CSV::createCSVHandler();
  428. const std::string filename = directoryPath + "testCSVCovarMatrixWriter02.csv";
  429. ASSERT_TRUE(handler->openFile(filename, OpenViBE::CSV::EFileAccessMode::Write));
  430. handler->setFormatType(OpenViBE::CSV::EStreamType::CovarianceMatrix);
  431. ASSERT_TRUE(handler->setStreamedMatrixInformation({ 2, 2, 2 }, { "", "", "", "", "", "" }));
  432. for (size_t i = 0; i < 50; ++i)
  433. {
  434. const size_t epoch = i / 10;
  435. ASSERT_TRUE(handler->addSample({ double(i), double(i)+1.0, { -20.20, -15.15, -10.10, -5.05, 5.05, 10.10, 15.15, 20.20 }, epoch }));
  436. }
  437. ASSERT_TRUE(handler->writeHeaderToFile());
  438. ASSERT_TRUE(handler->writeAllDataToFile());
  439. ASSERT_TRUE(handler->closeFile());
  440. releaseCSVHandler(handler);
  441. }
  442. TEST(CSV_Writer_Test_Case, covarianceMatrixWriterWrongMatrixSize)
  443. {
  444. OpenViBE::CSV::ICSVHandler* handler = OpenViBE::CSV::createCSVHandler();
  445. const std::string filename = directoryPath + "testCSVCovarMatrixWriter04.csv";
  446. ASSERT_TRUE(handler->openFile(filename, OpenViBE::CSV::EFileAccessMode::Write));
  447. handler->setFormatType(OpenViBE::CSV::EStreamType::CovarianceMatrix);
  448. ASSERT_TRUE(handler->setStreamedMatrixInformation({ 2, 2, 2 }, { "", "", "", "", "", "" }));
  449. ASSERT_FALSE(handler->addSample({ 0, 1.0, { -25.25, -20.20, -15.15, -10.10, -5.05, 5.05, 10.10, 15.15, 20.20, 25.25 }, 0 }));
  450. ASSERT_TRUE(handler->closeFile());
  451. releaseCSVHandler(handler);
  452. }
  453. int uoCSVWriterTest(int argc, char* argv[])
  454. {
  455. if (argv[1] != nullptr) { directoryPath = argv[1]; }
  456. testing::InitGoogleTest(&argc, argv);
  457. ::testing::GTEST_FLAG(filter) = "CSV_Writer_Test_Case.*";
  458. return RUN_ALL_TESTS();
  459. }