/tests/cpp_methods/cppfilter_test.cc

https://github.com/nnstreamer/nnstreamer · C++ · 310 lines · 246 code · 38 blank · 26 comment · 50 complexity · 8248b470e9cb0d950840de4e7f51c4cd MD5 · raw file

  1. /**
  2. * @file cppfilter_test.cc
  3. * @date 15 Jan 2019
  4. * @brief Unit test cases for tensor_filter::cpp
  5. * @see https://github.com/nnstreamer/nnstreamer
  6. * @author MyungJoo Ham <myungjoo.ham@samsung.com>
  7. * @bug No known bugs
  8. */
  9. #include "cppfilter_test.hh"
  10. #include <nnstreamer_util.h>
  11. /** @brief API method */
  12. filter_basic::filter_basic (const char *str) : tensor_filter_cpp (str)
  13. {
  14. }
  15. /** @brief API method */
  16. filter_basic::~filter_basic ()
  17. {
  18. }
  19. /** @brief API method */
  20. int
  21. filter_basic::getInputDim (GstTensorsInfo *info)
  22. {
  23. info->num_tensors = 1;
  24. info->info[0].type = _NNS_UINT8;
  25. info->info[0].dimension[0] = 3;
  26. info->info[0].dimension[1] = 4;
  27. info->info[0].dimension[2] = 4;
  28. info->info[0].dimension[3] = 1;
  29. return 0;
  30. }
  31. /** @brief API method */
  32. int
  33. filter_basic::getOutputDim (GstTensorsInfo *info)
  34. {
  35. info->num_tensors = 1;
  36. info->info[0].type = _NNS_UINT8;
  37. info->info[0].dimension[0] = 3;
  38. info->info[0].dimension[1] = 4;
  39. info->info[0].dimension[2] = 4;
  40. info->info[0].dimension[3] = 2;
  41. return 0;
  42. }
  43. /** @brief API method */
  44. int
  45. filter_basic::setInputDim (const GstTensorsInfo *in, GstTensorsInfo *out)
  46. {
  47. UNUSED (in);
  48. UNUSED (out);
  49. return -EINVAL;
  50. }
  51. /** @brief API method */
  52. bool
  53. filter_basic::isAllocatedBeforeInvoke ()
  54. {
  55. return true;
  56. }
  57. /** @brief API method */
  58. int
  59. filter_basic::invoke (const GstTensorMemory *in, GstTensorMemory *out)
  60. {
  61. g_assert (in);
  62. g_assert (out);
  63. g_assert (prop->input_meta.info[0].dimension[0] == 3U);
  64. g_assert (prop->input_meta.info[0].dimension[1] == 4U);
  65. g_assert (prop->input_meta.info[0].dimension[2] == 4U);
  66. g_assert (prop->input_meta.info[0].dimension[3] == 1U);
  67. g_assert (prop->output_meta.info[0].dimension[0] == 3U);
  68. g_assert (prop->output_meta.info[0].dimension[1] == 4U);
  69. g_assert (prop->output_meta.info[0].dimension[2] == 4U);
  70. g_assert (prop->output_meta.info[0].dimension[3] == 2U);
  71. g_assert (prop->input_meta.info[0].type == _NNS_UINT8);
  72. g_assert (prop->output_meta.info[0].type == _NNS_UINT8);
  73. for (int i = 0; i < 4 * 4 * 3; i++) {
  74. *((uint8_t *)out[0].data + i) = *((uint8_t *)in[0].data + i) * 2;
  75. *((uint8_t *)out[0].data + i + 4 * 4 * 3) = *((uint8_t *)in[0].data + i) + 1;
  76. }
  77. return 0;
  78. }
  79. /** @brief test support */
  80. int
  81. filter_basic::resultCompare (const char *inputFile, const char *outputFile, unsigned int nDropAllowed)
  82. {
  83. std::ifstream is (inputFile);
  84. if (is.fail ()) {
  85. g_printerr ("File not found: (%s : %d)\n", inputFile, is.fail ());
  86. return -255;
  87. }
  88. std::istream_iterator<uint8_t> istart (is), iend;
  89. std::vector<uint8_t> input (istart, iend);
  90. is >> std::noskipws;
  91. std::ifstream os (outputFile);
  92. if (os.fail ()) {
  93. g_printerr ("File not found: (%s : %d)\n", outputFile, os.fail ());
  94. return -254;
  95. }
  96. std::istream_iterator<uint8_t> ostart (os), oend;
  97. std::vector<uint8_t> output (ostart, oend);
  98. os >> std::noskipws;
  99. unsigned int iframes = (input.size () / (3 * 4 * 4));
  100. unsigned int oframes = (output.size () / (3 * 4 * 4 * 2));
  101. if (input.size () % (3 * 4 * 4) != 0) {
  102. g_printerr ("%zu, %zu\n", input.size (), output.size ());
  103. return -1;
  104. }
  105. if (output.size () % (3 * 4 * 4 * 2) != 0) {
  106. g_printerr ("%zu, %zu\n", input.size (), output.size ());
  107. return -2;
  108. }
  109. if (oframes > iframes)
  110. return -3;
  111. if ((oframes + nDropAllowed) < iframes)
  112. return -4;
  113. for (unsigned int frame = 0; frame < oframes; frame++) {
  114. unsigned pos = frame * 3 * 4 * 4;
  115. for (unsigned int i = 0; i < (3 * 4 * 4); i++) {
  116. uint8_t o1 = output[pos * 2 + i];
  117. uint8_t o2 = output[pos * 2 + (3 * 4 * 4) + i];
  118. uint8_t in = input[pos + i];
  119. uint8_t in1 = in * 2;
  120. uint8_t in2 = in + 1;
  121. if (o1 != in1)
  122. return -5;
  123. if (o2 != in2)
  124. return -6;
  125. }
  126. }
  127. return 0;
  128. }
  129. /** @brief API method */
  130. filter_basic2::filter_basic2 (const char *str) : tensor_filter_cpp (str)
  131. {
  132. }
  133. /** @brief API method */
  134. filter_basic2::~filter_basic2 ()
  135. {
  136. }
  137. /** @brief API method */
  138. int
  139. filter_basic2::getInputDim (GstTensorsInfo *info)
  140. {
  141. info->num_tensors = 1;
  142. info->info[0].type = _NNS_UINT8;
  143. info->info[0].dimension[0] = 3;
  144. info->info[0].dimension[1] = 16;
  145. info->info[0].dimension[2] = 16;
  146. info->info[0].dimension[3] = 1;
  147. return 0;
  148. }
  149. /** @brief API method */
  150. int
  151. filter_basic2::getOutputDim (GstTensorsInfo *info)
  152. {
  153. info->num_tensors = 1;
  154. info->info[0].type = _NNS_UINT8;
  155. info->info[0].dimension[0] = 3;
  156. info->info[0].dimension[1] = 16;
  157. info->info[0].dimension[2] = 16;
  158. info->info[0].dimension[3] = 2;
  159. return 0;
  160. }
  161. /** @brief API method */
  162. int
  163. filter_basic2::setInputDim (const GstTensorsInfo *in, GstTensorsInfo *out)
  164. {
  165. UNUSED (in);
  166. UNUSED (out);
  167. return -EINVAL;
  168. }
  169. /** @brief API method */
  170. bool
  171. filter_basic2::isAllocatedBeforeInvoke ()
  172. {
  173. return true;
  174. }
  175. /** @brief API method */
  176. int
  177. filter_basic2::invoke (const GstTensorMemory *in, GstTensorMemory *out)
  178. {
  179. g_assert (in);
  180. g_assert (out);
  181. g_assert (prop->input_meta.info[0].dimension[0] == 3U);
  182. g_assert (prop->input_meta.info[0].dimension[1] == 16U);
  183. g_assert (prop->input_meta.info[0].dimension[2] == 16U);
  184. g_assert (prop->input_meta.info[0].dimension[3] == 1U);
  185. g_assert (prop->output_meta.info[0].dimension[0] == 3U);
  186. g_assert (prop->output_meta.info[0].dimension[1] == 16U);
  187. g_assert (prop->output_meta.info[0].dimension[2] == 16U);
  188. g_assert (prop->output_meta.info[0].dimension[3] == 2U);
  189. g_assert (prop->input_meta.info[0].type == _NNS_UINT8);
  190. g_assert (prop->output_meta.info[0].type == _NNS_UINT8);
  191. for (int i = 0; i < 16 * 16 * 3; i++) {
  192. *((uint8_t *)out[0].data + i) = *((uint8_t *)in[0].data + i) * 3;
  193. *((uint8_t *)out[0].data + i + 16 * 16 * 3) = *((uint8_t *)in[0].data + i) + 2;
  194. }
  195. return 0;
  196. }
  197. /** @brief test support */
  198. int
  199. filter_basic2::resultCompare (
  200. const char *inputFile, const char *outputFile, unsigned int nDropAllowed)
  201. {
  202. std::ifstream is (inputFile);
  203. if (is.fail ()) {
  204. g_printerr ("File not found: (%s : %d)\n", inputFile, is.fail ());
  205. return -255;
  206. }
  207. is >> std::noskipws;
  208. std::istream_iterator<uint8_t> istart (is), iend;
  209. std::vector<uint8_t> input (istart, iend);
  210. std::ifstream os (outputFile);
  211. if (os.fail ()) {
  212. g_printerr ("File not found: (%s : %d)\n", outputFile, os.fail ());
  213. return -254;
  214. }
  215. os >> std::noskipws;
  216. std::istream_iterator<uint8_t> ostart (os), oend;
  217. std::vector<uint8_t> output (ostart, oend);
  218. unsigned int iframes = (input.size () / (3 * 16 * 16));
  219. unsigned int oframes = (output.size () / (3 * 16 * 16 * 2));
  220. if (input.size () % (3 * 16 * 16) != 0) {
  221. g_printerr ("%zu, %zu\n", input.size (), output.size ());
  222. return -1;
  223. }
  224. if (output.size () % (3 * 16 * 16 * 2) != 0) {
  225. g_printerr ("%zu, %zu\n", input.size (), output.size ());
  226. return -2;
  227. }
  228. if (oframes > iframes)
  229. return -3;
  230. if ((oframes + nDropAllowed) < iframes)
  231. return -4;
  232. for (unsigned int frame = 0; frame < oframes; frame++) {
  233. unsigned pos = frame * 3 * 16 * 16;
  234. for (unsigned int i = 0; i < (3 * 16 * 16); i++) {
  235. uint8_t o1 = output[pos * 2 + i];
  236. uint8_t o2 = output[pos * 2 + (3 * 16 * 16) + i];
  237. uint8_t in = input[pos + i];
  238. uint8_t in1 = in * 3;
  239. uint8_t in2 = in + 2;
  240. if (o1 != in1)
  241. return -5;
  242. if (o2 != in2)
  243. return -6;
  244. }
  245. }
  246. return 0;
  247. }
  248. class tensor_filter_cpp *reg1, *reg2, *reg3;
  249. void init_shared_lib (void) __attribute__((constructor));
  250. void fini_shared_lib (void) __attribute__((destructor));
  251. /** @brief API method */
  252. void
  253. init_shared_lib (void)
  254. {
  255. reg1 = new filter_basic ("basic_so_01");
  256. reg2 = new filter_basic ("basic_so_02");
  257. reg3 = new filter_basic2 ("basic_so2");
  258. reg1->_register ();
  259. filter_basic::__register (reg2);
  260. reg3->_register ();
  261. }
  262. /** @brief API method */
  263. void
  264. fini_shared_lib (void)
  265. {
  266. filter_basic::__unregister ("basic_so_01");
  267. reg2->_unregister ();
  268. reg3->_unregister ();
  269. delete reg1;
  270. delete reg2;
  271. delete reg3;
  272. }