/tests/common/unittest_common.cc

https://github.com/nnstreamer/nnstreamer · C++ · 1782 lines · 1126 code · 298 blank · 358 comment · 33 complexity · ef3b83431eb1ebb5206247cbc1eb7098 MD5 · raw file

  1. /**
  2. * @file unittest_common.cc
  3. * @date 31 May 2018
  4. * @author MyungJoo Ham <myungjoo.ham@samsung.com>
  5. * @brief Unit test module for NNStreamer common library
  6. * @see https://github.com/nnstreamer/nnstreamer
  7. * @bug No known bugs.
  8. *
  9. * @brief Unit test module for NNStreamer common library
  10. * @bug No known bugs except for NYI items
  11. *
  12. * Copyright 2018 Samsung Electronics
  13. *
  14. */
  15. #include <gtest/gtest.h>
  16. #include <glib.h>
  17. #include <glib/gstdio.h>
  18. #include <nnstreamer_conf.h>
  19. #include <nnstreamer_plugin_api.h>
  20. #include <tensor_common.h>
  21. #include <unistd.h>
  22. #include <unittest_util.h>
  23. /**
  24. * @brief Test for int32 type string.
  25. */
  26. TEST (commonGetTensorType, failure_n)
  27. {
  28. EXPECT_EQ (gst_tensor_get_type (""), _NNS_END);
  29. EXPECT_EQ (gst_tensor_get_type (NULL), _NNS_END);
  30. }
  31. /**
  32. * @brief Test for int32 type string.
  33. */
  34. TEST (commonGetTensorType, int32)
  35. {
  36. EXPECT_EQ (gst_tensor_get_type ("int32"), _NNS_INT32);
  37. EXPECT_EQ (gst_tensor_get_type ("INT32"), _NNS_INT32);
  38. EXPECT_EQ (gst_tensor_get_type ("iNt32"), _NNS_INT32);
  39. EXPECT_EQ (gst_tensor_get_type ("InT32"), _NNS_INT32);
  40. }
  41. /**
  42. * @brief Test for int32 type string.
  43. */
  44. TEST (commonGetTensorType, int32_n)
  45. {
  46. EXPECT_EQ (gst_tensor_get_type ("InT322"), _NNS_END);
  47. EXPECT_EQ (gst_tensor_get_type ("int3"), _NNS_END);
  48. }
  49. /**
  50. * @brief Test for int16 type string.
  51. */
  52. TEST (commonGetTensorType, int16)
  53. {
  54. EXPECT_EQ (gst_tensor_get_type ("int16"), _NNS_INT16);
  55. EXPECT_EQ (gst_tensor_get_type ("INT16"), _NNS_INT16);
  56. EXPECT_EQ (gst_tensor_get_type ("iNt16"), _NNS_INT16);
  57. EXPECT_EQ (gst_tensor_get_type ("InT16"), _NNS_INT16);
  58. }
  59. /**
  60. * @brief Test for int16 type string.
  61. */
  62. TEST (commonGetTensorType, int16_n)
  63. {
  64. EXPECT_EQ (gst_tensor_get_type ("InT162"), _NNS_END);
  65. EXPECT_EQ (gst_tensor_get_type ("int1"), _NNS_END);
  66. }
  67. /**
  68. * @brief Test for int8 type string.
  69. */
  70. TEST (commonGetTensorType, int8)
  71. {
  72. EXPECT_EQ (gst_tensor_get_type ("int8"), _NNS_INT8);
  73. EXPECT_EQ (gst_tensor_get_type ("INT8"), _NNS_INT8);
  74. EXPECT_EQ (gst_tensor_get_type ("iNt8"), _NNS_INT8);
  75. EXPECT_EQ (gst_tensor_get_type ("InT8"), _NNS_INT8);
  76. }
  77. /**
  78. * @brief Test for int8 type string.
  79. */
  80. TEST (commonGetTensorType, int8_n)
  81. {
  82. EXPECT_EQ (gst_tensor_get_type ("InT82"), _NNS_END);
  83. EXPECT_EQ (gst_tensor_get_type ("int3"), _NNS_END);
  84. }
  85. /**
  86. * @brief Test for uint32 type string.
  87. */
  88. TEST (commonGetTensorType, uint32)
  89. {
  90. EXPECT_EQ (gst_tensor_get_type ("uint32"), _NNS_UINT32);
  91. EXPECT_EQ (gst_tensor_get_type ("UINT32"), _NNS_UINT32);
  92. EXPECT_EQ (gst_tensor_get_type ("uiNt32"), _NNS_UINT32);
  93. EXPECT_EQ (gst_tensor_get_type ("UInT32"), _NNS_UINT32);
  94. }
  95. /**
  96. * @brief Test for uint32 type string.
  97. */
  98. TEST (commonGetTensorType, uint32_n)
  99. {
  100. EXPECT_EQ (gst_tensor_get_type ("UInT322"), _NNS_END);
  101. EXPECT_EQ (gst_tensor_get_type ("uint3"), _NNS_END);
  102. }
  103. /**
  104. * @brief Test for uint16 type string.
  105. */
  106. TEST (commonGetTensorType, uint16)
  107. {
  108. EXPECT_EQ (gst_tensor_get_type ("uint16"), _NNS_UINT16);
  109. EXPECT_EQ (gst_tensor_get_type ("UINT16"), _NNS_UINT16);
  110. EXPECT_EQ (gst_tensor_get_type ("uiNt16"), _NNS_UINT16);
  111. EXPECT_EQ (gst_tensor_get_type ("UInT16"), _NNS_UINT16);
  112. }
  113. /**
  114. * @brief Test for uint16 type string.
  115. */
  116. TEST (commonGetTensorType, uint16_n)
  117. {
  118. EXPECT_EQ (gst_tensor_get_type ("UInT162"), _NNS_END);
  119. EXPECT_EQ (gst_tensor_get_type ("uint1"), _NNS_END);
  120. }
  121. /**
  122. * @brief Test for uint8 type string.
  123. */
  124. TEST (commonGetTensorType, uint8)
  125. {
  126. EXPECT_EQ (gst_tensor_get_type ("uint8"), _NNS_UINT8);
  127. EXPECT_EQ (gst_tensor_get_type ("UINT8"), _NNS_UINT8);
  128. EXPECT_EQ (gst_tensor_get_type ("uiNt8"), _NNS_UINT8);
  129. EXPECT_EQ (gst_tensor_get_type ("UInT8"), _NNS_UINT8);
  130. }
  131. /**
  132. * @brief Test for uint8 type string.
  133. */
  134. TEST (commonGetTensorType, uint8_n)
  135. {
  136. EXPECT_EQ (gst_tensor_get_type ("UInT82"), _NNS_END);
  137. EXPECT_EQ (gst_tensor_get_type ("uint3"), _NNS_END);
  138. }
  139. /**
  140. * @brief Test for float32 type string.
  141. */
  142. TEST (commonGetTensorType, float32)
  143. {
  144. EXPECT_EQ (gst_tensor_get_type ("float32"), _NNS_FLOAT32);
  145. EXPECT_EQ (gst_tensor_get_type ("FLOAT32"), _NNS_FLOAT32);
  146. EXPECT_EQ (gst_tensor_get_type ("float32"), _NNS_FLOAT32);
  147. EXPECT_EQ (gst_tensor_get_type ("FloaT32"), _NNS_FLOAT32);
  148. }
  149. /**
  150. * @brief Test for float32 type string.
  151. */
  152. TEST (commonGetTensorType, float32_n)
  153. {
  154. EXPECT_EQ (gst_tensor_get_type ("FloaT322"), _NNS_END);
  155. EXPECT_EQ (gst_tensor_get_type ("float3"), _NNS_END);
  156. }
  157. /**
  158. * @brief Test for float64 type string.
  159. */
  160. TEST (commonGetTensorType, float64)
  161. {
  162. EXPECT_EQ (gst_tensor_get_type ("float64"), _NNS_FLOAT64);
  163. EXPECT_EQ (gst_tensor_get_type ("FLOAT64"), _NNS_FLOAT64);
  164. EXPECT_EQ (gst_tensor_get_type ("float64"), _NNS_FLOAT64);
  165. EXPECT_EQ (gst_tensor_get_type ("FloaT64"), _NNS_FLOAT64);
  166. }
  167. /**
  168. * @brief Test for float64 type string.
  169. */
  170. TEST (commonGetTensorType, float64_n)
  171. {
  172. EXPECT_EQ (gst_tensor_get_type ("FloaT642"), _NNS_END);
  173. EXPECT_EQ (gst_tensor_get_type ("float6"), _NNS_END);
  174. }
  175. /**
  176. * @brief Test for int64 type string.
  177. */
  178. TEST (commonGetTensorType, int64)
  179. {
  180. EXPECT_EQ (gst_tensor_get_type ("int64"), _NNS_INT64);
  181. EXPECT_EQ (gst_tensor_get_type ("INT64"), _NNS_INT64);
  182. EXPECT_EQ (gst_tensor_get_type ("iNt64"), _NNS_INT64);
  183. EXPECT_EQ (gst_tensor_get_type ("InT64"), _NNS_INT64);
  184. }
  185. /**
  186. * @brief Test for int64 type string.
  187. */
  188. TEST (commonGetTensorType, int64_n)
  189. {
  190. EXPECT_EQ (gst_tensor_get_type ("InT642"), _NNS_END);
  191. EXPECT_EQ (gst_tensor_get_type ("int6"), _NNS_END);
  192. }
  193. /**
  194. * @brief Test for uint64 type string.
  195. */
  196. TEST (commonGetTensorType, uint64)
  197. {
  198. EXPECT_EQ (gst_tensor_get_type ("uint64"), _NNS_UINT64);
  199. EXPECT_EQ (gst_tensor_get_type ("UINT64"), _NNS_UINT64);
  200. EXPECT_EQ (gst_tensor_get_type ("uiNt64"), _NNS_UINT64);
  201. EXPECT_EQ (gst_tensor_get_type ("UInT64"), _NNS_UINT64);
  202. }
  203. /**
  204. * @brief Test for uint64 type string.
  205. */
  206. TEST (commonGetTensorType, uint64_n)
  207. {
  208. EXPECT_EQ (gst_tensor_get_type ("UInT642"), _NNS_END);
  209. EXPECT_EQ (gst_tensor_get_type ("uint6"), _NNS_END);
  210. }
  211. /**
  212. * @brief Test for tensor format (invalid param).
  213. */
  214. TEST (commonGetTensorFormat, invalidParam01_n)
  215. {
  216. EXPECT_EQ (gst_tensor_get_format (""), _NNS_TENSOR_FORMAT_END);
  217. EXPECT_EQ (gst_tensor_get_format (NULL), _NNS_TENSOR_FORMAT_END);
  218. EXPECT_EQ (gst_tensor_get_format ("invalid-fmt"), _NNS_TENSOR_FORMAT_END);
  219. }
  220. /**
  221. * @brief Test for tensor format string (invalid param).
  222. */
  223. TEST (commonGetTensorFormat, invalidParam02_n)
  224. {
  225. EXPECT_STREQ (gst_tensor_get_format_string (_NNS_TENSOR_FORMAT_END), NULL);
  226. EXPECT_STREQ (gst_tensor_get_format_string ((tensor_format) -1), NULL);
  227. }
  228. /**
  229. * @brief Test to find index of the key.
  230. */
  231. TEST (commonFindKeyStrv, keyIndex)
  232. {
  233. const gchar *teststrv[] = { "abcde", "ABCDEF", "1234", "abcabc", "tester", NULL };
  234. EXPECT_EQ (find_key_strv (teststrv, "abcde"), 0);
  235. EXPECT_EQ (find_key_strv (teststrv, "ABCDE"), 0);
  236. EXPECT_EQ (find_key_strv (teststrv, "ABCDEF"), 1);
  237. EXPECT_EQ (find_key_strv (teststrv, "1234"), 2);
  238. EXPECT_EQ (find_key_strv (teststrv, "tester"), 4);
  239. EXPECT_EQ (find_key_strv (teststrv, "abcabcd"), -1);
  240. }
  241. /**
  242. * @brief Test for tensor dimension.
  243. */
  244. TEST (commonGetTensorDimension, case1)
  245. {
  246. tensor_dim dim;
  247. gchar *dim_str;
  248. guint rank;
  249. rank = gst_tensor_parse_dimension ("345:123:433:177", dim);
  250. EXPECT_EQ (rank, 4U);
  251. EXPECT_EQ (dim[0], 345U);
  252. EXPECT_EQ (dim[1], 123U);
  253. EXPECT_EQ (dim[2], 433U);
  254. EXPECT_EQ (dim[3], 177U);
  255. dim_str = gst_tensor_get_dimension_string (dim);
  256. EXPECT_STREQ (dim_str, "345:123:433:177");
  257. g_free (dim_str);
  258. }
  259. /**
  260. * @brief Test for tensor dimension.
  261. */
  262. TEST (commonGetTensorDimension, case2)
  263. {
  264. tensor_dim dim;
  265. gchar *dim_str;
  266. guint rank;
  267. rank = gst_tensor_parse_dimension ("345:123:433", dim);
  268. EXPECT_EQ (rank, 3U);
  269. EXPECT_EQ (dim[0], 345U);
  270. EXPECT_EQ (dim[1], 123U);
  271. EXPECT_EQ (dim[2], 433U);
  272. EXPECT_EQ (dim[3], 1U);
  273. dim_str = gst_tensor_get_dimension_string (dim);
  274. EXPECT_STREQ (dim_str, "345:123:433:1");
  275. g_free (dim_str);
  276. }
  277. /**
  278. * @brief Test for tensor dimension.
  279. */
  280. TEST (commonGetTensorDimension, case3)
  281. {
  282. tensor_dim dim;
  283. gchar *dim_str;
  284. guint rank;
  285. rank = gst_tensor_parse_dimension ("345:123", dim);
  286. EXPECT_EQ (rank, 2U);
  287. EXPECT_EQ (dim[0], 345U);
  288. EXPECT_EQ (dim[1], 123U);
  289. EXPECT_EQ (dim[2], 1U);
  290. EXPECT_EQ (dim[3], 1U);
  291. dim_str = gst_tensor_get_dimension_string (dim);
  292. EXPECT_STREQ (dim_str, "345:123:1:1");
  293. g_free (dim_str);
  294. }
  295. /**
  296. * @brief Test for tensor dimension.
  297. */
  298. TEST (commonGetTensorDimension, case4)
  299. {
  300. tensor_dim dim;
  301. gchar *dim_str;
  302. guint rank;
  303. rank = gst_tensor_parse_dimension ("345", dim);
  304. EXPECT_EQ (rank, 1U);
  305. EXPECT_EQ (dim[0], 345U);
  306. EXPECT_EQ (dim[1], 1U);
  307. EXPECT_EQ (dim[2], 1U);
  308. EXPECT_EQ (dim[3], 1U);
  309. dim_str = gst_tensor_get_dimension_string (dim);
  310. EXPECT_STREQ (dim_str, "345:1:1:1");
  311. g_free (dim_str);
  312. }
  313. /**
  314. * @brief Test to copy tensor info.
  315. */
  316. TEST (commonTensorInfo, copyTensor)
  317. {
  318. GstTensorInfo src, dest;
  319. gchar *test_name = g_strdup ("test-tensor");
  320. gst_tensor_info_init (&src);
  321. gst_tensor_info_init (&dest);
  322. src = { test_name, _NNS_FLOAT32, { 1, 2, 3, 4 } };
  323. gst_tensor_info_copy (&dest, &src);
  324. EXPECT_TRUE (dest.name != src.name);
  325. EXPECT_STREQ (dest.name, test_name);
  326. EXPECT_EQ (dest.type, src.type);
  327. EXPECT_EQ (dest.dimension[0], src.dimension[0]);
  328. EXPECT_EQ (dest.dimension[1], src.dimension[1]);
  329. EXPECT_EQ (dest.dimension[2], src.dimension[2]);
  330. EXPECT_EQ (dest.dimension[3], src.dimension[3]);
  331. gst_tensor_info_free (&dest);
  332. src = { NULL, _NNS_INT32, { 5, 6, 7, 8 } };
  333. gst_tensor_info_copy (&dest, &src);
  334. EXPECT_TRUE (dest.name == NULL);
  335. EXPECT_EQ (dest.type, src.type);
  336. EXPECT_EQ (dest.dimension[0], src.dimension[0]);
  337. EXPECT_EQ (dest.dimension[1], src.dimension[1]);
  338. EXPECT_EQ (dest.dimension[2], src.dimension[2]);
  339. EXPECT_EQ (dest.dimension[3], src.dimension[3]);
  340. gst_tensor_info_free (&dest);
  341. g_free (test_name);
  342. }
  343. /**
  344. * @brief Test to copy tensor info.
  345. */
  346. TEST (commonTensorInfo, copyTensors)
  347. {
  348. GstTensorsInfo src, dest;
  349. gchar *test_name = g_strdup ("test-tensors");
  350. guint i;
  351. gst_tensors_info_init (&src);
  352. gst_tensors_info_init (&dest);
  353. src.num_tensors = 2;
  354. src.info[0] = { test_name, _NNS_INT32, { 1, 2, 3, 4 } };
  355. src.info[1] = { test_name, _NNS_FLOAT32, { 5, 6, 7, 8 } };
  356. gst_tensors_info_copy (&dest, &src);
  357. EXPECT_EQ (dest.num_tensors, src.num_tensors);
  358. for (i = 0; i < src.num_tensors; i++) {
  359. EXPECT_TRUE (dest.info[i].name != src.info[i].name);
  360. EXPECT_STREQ (dest.info[i].name, test_name);
  361. EXPECT_EQ (dest.info[i].type, src.info[i].type);
  362. EXPECT_EQ (dest.info[i].dimension[0], src.info[i].dimension[0]);
  363. EXPECT_EQ (dest.info[i].dimension[1], src.info[i].dimension[1]);
  364. EXPECT_EQ (dest.info[i].dimension[2], src.info[i].dimension[2]);
  365. EXPECT_EQ (dest.info[i].dimension[3], src.info[i].dimension[3]);
  366. }
  367. gst_tensors_info_free (&dest);
  368. g_free (test_name);
  369. }
  370. /**
  371. * @brief Internal function to update tensors info.
  372. */
  373. static void
  374. fill_tensors_info_for_test (GstTensorsInfo *info1, GstTensorsInfo *info2)
  375. {
  376. g_assert (info1 != NULL && info2 != NULL);
  377. gst_tensors_info_init (info1);
  378. gst_tensors_info_init (info2);
  379. info1->num_tensors = info2->num_tensors = 2;
  380. info1->info[0].type = info2->info[0].type = _NNS_INT64;
  381. info1->info[1].type = info2->info[1].type = _NNS_FLOAT64;
  382. info1->info[0].dimension[0] = info2->info[0].dimension[0] = 2;
  383. info1->info[0].dimension[1] = info2->info[0].dimension[1] = 3;
  384. info1->info[0].dimension[2] = info2->info[0].dimension[2] = 1;
  385. info1->info[0].dimension[3] = info2->info[0].dimension[3] = 1;
  386. info1->info[1].dimension[0] = info2->info[1].dimension[0] = 5;
  387. info1->info[1].dimension[1] = info2->info[1].dimension[1] = 5;
  388. info1->info[1].dimension[2] = info2->info[1].dimension[2] = 1;
  389. info1->info[1].dimension[3] = info2->info[1].dimension[3] = 1;
  390. }
  391. /**
  392. * @brief Internal function to update tensors info.
  393. */
  394. static void
  395. fill_tensors_config_for_test (GstTensorsConfig *conf1, GstTensorsConfig *conf2)
  396. {
  397. g_assert (conf1 != NULL && conf2 != NULL);
  398. gst_tensors_config_init (conf1);
  399. gst_tensors_config_init (conf2);
  400. conf1->rate_n = conf2->rate_n = 1;
  401. conf1->rate_d = conf2->rate_d = 2;
  402. fill_tensors_info_for_test (&conf1->info, &conf2->info);
  403. }
  404. /**
  405. * @brief Test for data size.
  406. */
  407. TEST (commonTensorInfo, size01_p)
  408. {
  409. GstTensorsInfo info1, info2;
  410. gsize size1, size2;
  411. guint i;
  412. fill_tensors_info_for_test (&info1, &info2);
  413. size1 = gst_tensor_info_get_size (&info1.info[0]);
  414. size2 = gst_tensors_info_get_size (&info1, 0);
  415. EXPECT_TRUE (size1 == size2);
  416. size1 = 0;
  417. for (i = 0; i < info2.num_tensors; i++) {
  418. size1 += gst_tensor_info_get_size (&info2.info[i]);
  419. }
  420. size2 = gst_tensors_info_get_size (&info2, -1);
  421. EXPECT_TRUE (size1 == size2);
  422. }
  423. /**
  424. * @brief Test for data size.
  425. */
  426. TEST (commonTensorInfo, size02_n)
  427. {
  428. GstTensorsInfo info1, info2;
  429. gsize size1;
  430. gint index;
  431. fill_tensors_info_for_test (&info1, &info2);
  432. /* get size with null param */
  433. index = (gint)info1.num_tensors - 1;
  434. size1 = gst_tensors_info_get_size (NULL, index);
  435. EXPECT_TRUE (size1 == 0);
  436. }
  437. /**
  438. * @brief Test for data size.
  439. */
  440. TEST (commonTensorInfo, size03_n)
  441. {
  442. GstTensorsInfo info1, info2;
  443. gsize size1;
  444. gint index;
  445. fill_tensors_info_for_test (&info1, &info2);
  446. /* get size with invalid index */
  447. index = (gint)info1.num_tensors;
  448. size1 = gst_tensors_info_get_size (&info1, index);
  449. EXPECT_TRUE (size1 == 0);
  450. }
  451. /**
  452. * @brief Test for same tensors info.
  453. */
  454. TEST (commonTensorInfo, equal01_p)
  455. {
  456. GstTensorsInfo info1, info2;
  457. fill_tensors_info_for_test (&info1, &info2);
  458. EXPECT_TRUE (gst_tensors_info_is_equal (&info1, &info2));
  459. }
  460. /**
  461. * @brief Test for same tensors info.
  462. */
  463. TEST (commonTensorInfo, equal02_n)
  464. {
  465. GstTensorsInfo info1, info2;
  466. gst_tensors_info_init (&info1);
  467. gst_tensors_info_init (&info2);
  468. /* test with invalid info */
  469. EXPECT_FALSE (gst_tensors_info_is_equal (&info1, &info2));
  470. }
  471. /**
  472. * @brief Test for same tensors info.
  473. */
  474. TEST (commonTensorInfo, equal03_n)
  475. {
  476. GstTensorsInfo info1, info2;
  477. fill_tensors_info_for_test (&info1, &info2);
  478. /* change info, this should not be compatible */
  479. info1.num_tensors = 1;
  480. EXPECT_FALSE (gst_tensors_info_is_equal (&info1, &info2));
  481. }
  482. /**
  483. * @brief Test for same tensors info.
  484. */
  485. TEST (commonTensorInfo, equal04_n)
  486. {
  487. GstTensorsInfo info1, info2;
  488. fill_tensors_info_for_test (&info1, &info2);
  489. /* change info, this should not be compatible */
  490. info1.info[0].type = _NNS_UINT64;
  491. EXPECT_FALSE (gst_tensors_info_is_equal (&info1, &info2));
  492. }
  493. /**
  494. * @brief Test for same tensors info.
  495. */
  496. TEST (commonTensorInfo, equal05_n)
  497. {
  498. GstTensorsInfo info1, info2;
  499. fill_tensors_info_for_test (&info1, &info2);
  500. /* change info, this should not be compatible */
  501. info2.info[1].dimension[0] = 10;
  502. EXPECT_FALSE (gst_tensors_info_is_equal (&info1, &info2));
  503. }
  504. /**
  505. * @brief Test for getting size of the tensor info with invalid param.
  506. */
  507. TEST (commonTensorInfo, sizeInvalidParam_n)
  508. {
  509. EXPECT_EQ (0U, gst_tensor_info_get_size (NULL));
  510. }
  511. /**
  512. * @brief Test for validating of the tensor info with invalid param.
  513. */
  514. TEST (commonTensorInfo, validateInvalidParam_n)
  515. {
  516. EXPECT_FALSE (gst_tensor_info_validate (NULL));
  517. }
  518. /**
  519. * @brief Test for validating of the tensors info with invalid param.
  520. */
  521. TEST (commonTensorsInfo, validateInvalidParam_n)
  522. {
  523. EXPECT_FALSE (gst_tensors_info_validate (NULL));
  524. }
  525. /**
  526. * @brief Test for comparing two tensor info with invalid param.
  527. */
  528. TEST (commonTensorInfo, equalInvalidParam0_n)
  529. {
  530. GstTensorInfo info;
  531. gst_tensor_info_init (&info);
  532. EXPECT_FALSE (gst_tensor_info_is_equal (NULL, &info));
  533. }
  534. /**
  535. * @brief Test for comparing two tensor info with invalid param.
  536. */
  537. TEST (commonTensorInfo, equalInvalidParam1_n)
  538. {
  539. GstTensorInfo info;
  540. gst_tensor_info_init (&info);
  541. EXPECT_FALSE (gst_tensor_info_is_equal (&info, NULL));
  542. }
  543. /**
  544. * @brief Test for comparing two tensors info with invalid param.
  545. */
  546. TEST (commonTensorsInfo, equalInvalidParam0_n)
  547. {
  548. GstTensorsInfo info;
  549. gst_tensors_info_init (&info);
  550. EXPECT_FALSE (gst_tensors_info_is_equal (NULL, &info));
  551. }
  552. /**
  553. * @brief Test for comparing two tensors info with invalid param.
  554. */
  555. TEST (commonTensorsInfo, equalInvalidParam1_n)
  556. {
  557. GstTensorsInfo info;
  558. gst_tensors_info_init (&info);
  559. EXPECT_FALSE (gst_tensors_info_is_equal (&info, NULL));
  560. }
  561. /**
  562. * @brief Test for getting rank with invalid param.
  563. */
  564. TEST (commonTensorInfo, getrankInvalidParam0_n)
  565. {
  566. EXPECT_EQ (0, gst_tensor_info_get_rank (NULL));
  567. }
  568. /**
  569. * @brief Test for parsing dimension with invalid param.
  570. */
  571. TEST (commonTensorsInfo, parsingDimInvalidParam0_n)
  572. {
  573. const gchar *dim_str = "1:2:3:4";
  574. EXPECT_EQ (0U, gst_tensors_info_parse_dimensions_string (NULL, dim_str));
  575. }
  576. /**
  577. * @brief Test for parsing dimension with invalid param.
  578. */
  579. TEST (commonTensorsInfo, parsingDimInvalidParam1_n)
  580. {
  581. GstTensorsInfo info;
  582. gst_tensors_info_init (&info);
  583. EXPECT_EQ (0U, gst_tensors_info_parse_dimensions_string (&info, NULL));
  584. }
  585. /**
  586. * @brief Test for parsing type with invalid param.
  587. */
  588. TEST (commonTensorsInfo, parsingTypeInvalidParam0_n)
  589. {
  590. const gchar *dim_str = "uint8";
  591. EXPECT_EQ (0U, gst_tensors_info_parse_types_string (NULL, dim_str));
  592. }
  593. /**
  594. * @brief Test for parsing type with invalid param.
  595. */
  596. TEST (commonTensorsInfo, parsingTypeInvalidParam1_n)
  597. {
  598. GstTensorsInfo info;
  599. gst_tensors_info_init (&info);
  600. EXPECT_EQ (0U, gst_tensors_info_parse_types_string (&info, NULL));
  601. }
  602. /**
  603. * @brief Test for parsing name with invalid param.
  604. */
  605. TEST (commonTensorsInfo, parsingNameInvalidParam0_n)
  606. {
  607. const gchar *dim_str = "tname";
  608. EXPECT_EQ (0U, gst_tensors_info_parse_names_string (NULL, dim_str));
  609. }
  610. /**
  611. * @brief Test for parsing name with invalid param.
  612. */
  613. TEST (commonTensorsInfo, parsingNameInvalidParam1_n)
  614. {
  615. GstTensorsInfo info;
  616. gst_tensors_info_init (&info);
  617. EXPECT_EQ (0U, gst_tensors_info_parse_names_string (&info, NULL));
  618. }
  619. /**
  620. * @brief Test for getting dimension with invalid param.
  621. */
  622. TEST (commonTensorsInfo, getDimInvalidParam0_n)
  623. {
  624. EXPECT_EQ (NULL, gst_tensors_info_get_dimensions_string (NULL));
  625. }
  626. /**
  627. * @brief Test for getting dimension with invalid param.
  628. */
  629. TEST (commonTensorsInfo, getDimInvalidParam1_n)
  630. {
  631. GstTensorsInfo info;
  632. gst_tensors_info_init (&info);
  633. info.num_tensors = 0;
  634. EXPECT_EQ (0U, gst_tensors_info_get_dimensions_string (&info));
  635. }
  636. /**
  637. * @brief Test for getting type with invalid param.
  638. */
  639. TEST (commonTensorsInfo, getTypeInvalidParam0_n)
  640. {
  641. EXPECT_EQ (NULL, gst_tensors_info_get_types_string (NULL));
  642. }
  643. /**
  644. * @brief Test for getting type with invalid param.
  645. */
  646. TEST (commonTensorsInfo, getTypeInvalidParam1_n)
  647. {
  648. GstTensorsInfo info;
  649. gst_tensors_info_init (&info);
  650. info.num_tensors = 0;
  651. EXPECT_EQ (0U, gst_tensors_info_get_types_string (&info));
  652. }
  653. /**
  654. * @brief Test for getting name with invalid param.
  655. */
  656. TEST (commonTensorsInfo, getNameInvalidParam0_n)
  657. {
  658. EXPECT_EQ (NULL, gst_tensors_info_get_names_string (NULL));
  659. }
  660. /**
  661. * @brief Test for getting name with invalid param.
  662. */
  663. TEST (commonTensorsInfo, getNameInvalidParam1_n)
  664. {
  665. GstTensorsInfo info;
  666. gst_tensors_info_init (&info);
  667. info.num_tensors = 0;
  668. EXPECT_EQ (0U, gst_tensors_info_get_names_string (&info));
  669. }
  670. /**
  671. * @brief Test for same tensors config.
  672. */
  673. TEST (commonTensorsConfig, equal01_p)
  674. {
  675. GstTensorsConfig conf1, conf2;
  676. fill_tensors_config_for_test (&conf1, &conf2);
  677. EXPECT_TRUE (gst_tensors_config_is_equal (&conf1, &conf2));
  678. }
  679. /**
  680. * @brief Test for same tensors config.
  681. */
  682. TEST (commonTensorsConfig, equal02_p)
  683. {
  684. GstTensorsConfig conf1, conf2;
  685. fill_tensors_config_for_test (&conf1, &conf2);
  686. conf1.rate_n *= 2;
  687. conf1.rate_d *= 2;
  688. EXPECT_TRUE (gst_tensors_config_is_equal (&conf1, &conf2));
  689. }
  690. /**
  691. * @brief Test for same tensors config.
  692. */
  693. TEST (commonTensorsConfig, equal03_p)
  694. {
  695. GstTensorsConfig conf1, conf2;
  696. fill_tensors_config_for_test (&conf1, &conf2);
  697. conf1.rate_n *= 0;
  698. conf2.rate_n *= 0;
  699. EXPECT_TRUE (gst_tensors_config_is_equal (&conf1, &conf2));
  700. }
  701. /**
  702. * @brief Test for same tensors config.
  703. */
  704. TEST (commonTensorsConfig, equal04_n)
  705. {
  706. GstTensorsConfig conf1, conf2;
  707. gst_tensors_config_init (&conf1);
  708. gst_tensors_config_init (&conf2);
  709. EXPECT_FALSE (gst_tensors_config_is_equal (&conf1, &conf2));
  710. }
  711. /**
  712. * @brief Test for same tensors config.
  713. */
  714. TEST (commonTensorsConfig, equal05_n)
  715. {
  716. GstTensorsConfig conf1, conf2;
  717. fill_tensors_config_for_test (&conf1, &conf2);
  718. conf1.rate_n *= 2;
  719. conf1.rate_d *= 4;
  720. EXPECT_FALSE (gst_tensors_config_is_equal (&conf1, &conf2));
  721. }
  722. /**
  723. * @brief Test for same tensors config.
  724. */
  725. TEST (commonTensorsConfig, equal06_n)
  726. {
  727. GstTensorsConfig conf1, conf2;
  728. fill_tensors_config_for_test (&conf1, &conf2);
  729. conf1.rate_d *= 0;
  730. EXPECT_FALSE (gst_tensors_config_is_equal (&conf1, &conf2));
  731. }
  732. /**
  733. * @brief Test for same tensors config.
  734. */
  735. TEST (commonTensorsConfig, equal07_n)
  736. {
  737. GstTensorsConfig conf;
  738. gst_tensors_config_init (&conf);
  739. EXPECT_FALSE (gst_tensors_config_is_equal (NULL, &conf));
  740. }
  741. /**
  742. * @brief Test for same tensors config.
  743. */
  744. TEST (commonTensorsConfig, equal08_n)
  745. {
  746. GstTensorsConfig conf;
  747. gst_tensors_config_init (&conf);
  748. EXPECT_FALSE (gst_tensors_config_is_equal (&conf, NULL));
  749. }
  750. /**
  751. * @brief Test for same tensors config.
  752. */
  753. TEST (commonTensorsConfig, equal09_p)
  754. {
  755. GstTensorsConfig conf1, conf2;
  756. fill_tensors_config_for_test (&conf1, &conf2);
  757. /* compare flexible tensor */
  758. conf1.format = conf2.format = _NNS_TENSOR_FORMAT_FLEXIBLE;
  759. EXPECT_TRUE (gst_tensors_config_is_equal (&conf1, &conf2));
  760. }
  761. /**
  762. * @brief Test for same tensors config.
  763. */
  764. TEST (commonTensorsConfig, equal10_n)
  765. {
  766. GstTensorsConfig conf1, conf2;
  767. fill_tensors_config_for_test (&conf1, &conf2);
  768. /* change format, this should not be compatible */
  769. conf2.format = _NNS_TENSOR_FORMAT_FLEXIBLE;
  770. EXPECT_FALSE (gst_tensors_config_is_equal (&conf1, &conf2));
  771. }
  772. /**
  773. * @brief Test for validating of the tensors config with invalid param.
  774. */
  775. TEST (commonTensorsConfig, validateInvalidParam0_n)
  776. {
  777. EXPECT_FALSE (gst_tensors_config_validate (NULL));
  778. }
  779. /**
  780. * @brief Test for validating of the tensors config with invalid param.
  781. */
  782. TEST (commonTensorsConfig, validateInvalidParam1_n)
  783. {
  784. GstTensorsConfig conf;
  785. gst_tensors_config_init (&conf);
  786. conf.rate_n = 1;
  787. EXPECT_FALSE (gst_tensors_config_validate (NULL));
  788. }
  789. /**
  790. * @brief Test for validating of the tensors config with invalid param.
  791. */
  792. TEST (commonTensorsConfig, validateInvalidParam2_n)
  793. {
  794. GstTensorsConfig conf;
  795. gst_tensors_config_init (&conf);
  796. conf.rate_d = 1;
  797. EXPECT_FALSE (gst_tensors_config_validate (NULL));
  798. }
  799. /**
  800. * @brief Test for getting config from strucrure with invalid param.
  801. */
  802. TEST (commonTensorsConfig, fromStructreInvalidParam0_n)
  803. {
  804. GstStructure structure = { 0 };
  805. EXPECT_FALSE (gst_tensors_config_from_structure (NULL, &structure));
  806. }
  807. /**
  808. * @brief Test for getting config from strucrure with invalid param.
  809. */
  810. TEST (commonTensorsConfig, fromStructreInvalidParam1_n)
  811. {
  812. GstTensorsConfig conf;
  813. gst_tensors_config_init (&conf);
  814. EXPECT_FALSE (gst_tensors_config_from_structure (&conf, NULL));
  815. }
  816. /**
  817. * @brief Test for getting tensor cap with invalid param.
  818. */
  819. TEST (commonTensorConfig, capInvalidParam0_n)
  820. {
  821. EXPECT_FALSE (gst_tensor_caps_from_config (NULL));
  822. }
  823. /**
  824. * @brief Test for getting tensor cap with invalid param.
  825. */
  826. TEST (commonTensorsConfig, capInvalidParam1_n)
  827. {
  828. EXPECT_FALSE (gst_tensors_caps_from_config (NULL));
  829. }
  830. /**
  831. * @brief Test for dimensions string in tensors info.
  832. */
  833. TEST (commonTensorsInfoString, dimensions)
  834. {
  835. GstTensorsInfo info;
  836. guint num_dims;
  837. gchar *str_dims;
  838. gst_tensors_info_init (&info);
  839. /* 1 tensor info */
  840. num_dims = gst_tensors_info_parse_dimensions_string (&info, "1:2:3:4");
  841. EXPECT_EQ (num_dims, 1U);
  842. info.num_tensors = num_dims;
  843. str_dims = gst_tensors_info_get_dimensions_string (&info);
  844. EXPECT_STREQ (str_dims, "1:2:3:4");
  845. g_free (str_dims);
  846. /* 4 tensors info */
  847. num_dims = gst_tensors_info_parse_dimensions_string (&info, "1, 2:2, 3:3:3, 4:4:4:4");
  848. EXPECT_EQ (num_dims, 4U);
  849. info.num_tensors = num_dims;
  850. str_dims = gst_tensors_info_get_dimensions_string (&info);
  851. EXPECT_STREQ (str_dims, "1:1:1:1,2:2:1:1,3:3:3:1,4:4:4:4");
  852. g_free (str_dims);
  853. /* max */
  854. num_dims = gst_tensors_info_parse_dimensions_string (&info,
  855. "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20");
  856. EXPECT_EQ (num_dims, (guint)NNS_TENSOR_SIZE_LIMIT);
  857. }
  858. /**
  859. * @brief Test for types string in tensors info.
  860. */
  861. TEST (commonTensorsInfoString, types)
  862. {
  863. GstTensorsInfo info;
  864. guint num_types;
  865. gchar *str_types;
  866. gst_tensors_info_init (&info);
  867. /* 1 tensor info */
  868. num_types = gst_tensors_info_parse_types_string (&info, "uint16");
  869. EXPECT_EQ (num_types, 1U);
  870. info.num_tensors = num_types;
  871. str_types = gst_tensors_info_get_types_string (&info);
  872. EXPECT_STREQ (str_types, "uint16");
  873. g_free (str_types);
  874. /* 4 tensors info */
  875. num_types = gst_tensors_info_parse_types_string (&info, "int8, int16, int32, int64");
  876. EXPECT_EQ (num_types, 4U);
  877. info.num_tensors = num_types;
  878. str_types = gst_tensors_info_get_types_string (&info);
  879. EXPECT_STREQ (str_types, "int8,int16,int32,int64");
  880. g_free (str_types);
  881. /* max */
  882. num_types = gst_tensors_info_parse_types_string (&info,
  883. "int8, int8, int8, int8, int8, int8, int8, int8, int8, int8, int8, "
  884. "int8, int8, int8, int8, int8, int8, int8, int8, int8, int8, int8");
  885. EXPECT_EQ (num_types, (guint)NNS_TENSOR_SIZE_LIMIT);
  886. }
  887. /**
  888. * @brief Test for names string in tensors info.
  889. */
  890. TEST (commonTensorsInfoString, names)
  891. {
  892. GstTensorsInfo info;
  893. guint i, num_names;
  894. gchar *str_names;
  895. gst_tensors_info_init (&info);
  896. /* 1 tensor info */
  897. num_names = gst_tensors_info_parse_names_string (&info, "t1");
  898. EXPECT_EQ (num_names, 1U);
  899. info.num_tensors = num_names;
  900. str_names = gst_tensors_info_get_names_string (&info);
  901. EXPECT_STREQ (str_names, "t1");
  902. g_free (str_names);
  903. /* 4 tensors info */
  904. num_names = gst_tensors_info_parse_names_string (&info, "tensor1, tensor2, tensor3, tensor4");
  905. EXPECT_EQ (num_names, 4U);
  906. info.num_tensors = num_names;
  907. str_names = gst_tensors_info_get_names_string (&info);
  908. EXPECT_STREQ (str_names, "tensor1,tensor2,tensor3,tensor4");
  909. g_free (str_names);
  910. gst_tensors_info_free (&info);
  911. /* empty name string */
  912. num_names = gst_tensors_info_parse_names_string (&info, ",,");
  913. EXPECT_EQ (num_names, 3U);
  914. info.num_tensors = num_names;
  915. for (i = 0; i < num_names; ++i) {
  916. EXPECT_TRUE (info.info[i].name == NULL);
  917. }
  918. str_names = gst_tensors_info_get_names_string (&info);
  919. EXPECT_STREQ (str_names, ",,");
  920. g_free (str_names);
  921. gst_tensors_info_free (&info);
  922. /* max */
  923. num_names = gst_tensors_info_parse_names_string (&info,
  924. "t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, "
  925. "t16, t17, t18, t19, t20, t21, t22, t23, t24, t25, t26, t27, t28");
  926. EXPECT_EQ (num_names, (guint)NNS_TENSOR_SIZE_LIMIT);
  927. info.num_tensors = num_names;
  928. gst_tensors_info_free (&info);
  929. }
  930. /**
  931. * @brief Test for tensor meta info (default value after init).
  932. */
  933. TEST (commonMetaInfo, initDefaultValue)
  934. {
  935. GstTensorMetaInfo meta;
  936. guint i, major, minor;
  937. major = minor = 0;
  938. gst_tensor_meta_info_init (&meta);
  939. EXPECT_EQ (meta.type, _NNS_END);
  940. EXPECT_EQ (meta.format, _NNS_TENSOR_FORMAT_STATIC);
  941. EXPECT_EQ ((media_type) meta.media_type, _NNS_TENSOR);
  942. for (i = 0; i < NNS_TENSOR_META_RANK_LIMIT; i++)
  943. EXPECT_EQ (meta.dimension[i], 0U);
  944. /* current version after init */
  945. gst_tensor_meta_info_get_version (&meta, &major, &minor);
  946. EXPECT_TRUE (major > 0 || minor > 0);
  947. }
  948. /**
  949. * @brief Test for tensor meta info (header size with invalid param).
  950. */
  951. TEST (commonMetaInfo, headerSizeInvalidParam01_n)
  952. {
  953. gsize hsize;
  954. hsize = gst_tensor_meta_info_get_header_size (NULL);
  955. EXPECT_EQ (hsize, 0U);
  956. }
  957. /**
  958. * @brief Test for tensor meta info (header size with invalid meta).
  959. */
  960. TEST (commonMetaInfo, headerSizeInvalidParam02_n)
  961. {
  962. GstTensorMetaInfo meta = {0, };
  963. gsize hsize;
  964. hsize = gst_tensor_meta_info_get_header_size (&meta);
  965. EXPECT_EQ (hsize, 0U);
  966. }
  967. /**
  968. * @brief Test for tensor meta info (data size with invalid param).
  969. */
  970. TEST (commonMetaInfo, dataSizeInvalidParam01_n)
  971. {
  972. gsize dsize;
  973. dsize = gst_tensor_meta_info_get_data_size (NULL);
  974. EXPECT_EQ (dsize, 0U);
  975. }
  976. /**
  977. * @brief Test for tensor meta info (data size with invalid meta).
  978. */
  979. TEST (commonMetaInfo, dataSizeInvalidParam02_n)
  980. {
  981. GstTensorMetaInfo meta = {0, };
  982. gsize dsize;
  983. dsize = gst_tensor_meta_info_get_data_size (&meta);
  984. EXPECT_EQ (dsize, 0U);
  985. }
  986. /**
  987. * @brief Test for tensor meta info (validate meta with invalid param).
  988. */
  989. TEST (commonMetaInfo, validateInvalidParam01_n)
  990. {
  991. gboolean valid;
  992. valid = gst_tensor_meta_info_validate (NULL);
  993. EXPECT_FALSE (valid);
  994. }
  995. /**
  996. * @brief Test for tensor meta info (validate meta with invalid meta).
  997. */
  998. TEST (commonMetaInfo, validateInvalidParam02_n)
  999. {
  1000. GstTensorMetaInfo meta = { 0, };
  1001. gboolean valid;
  1002. /* invalid version */
  1003. valid = gst_tensor_meta_info_validate (&meta);
  1004. EXPECT_FALSE (valid);
  1005. /* set valid meta */
  1006. gst_tensor_meta_info_init (&meta);
  1007. meta.type = _NNS_UINT8;
  1008. meta.dimension[0] = 10;
  1009. meta.format = _NNS_TENSOR_FORMAT_FLEXIBLE;
  1010. meta.media_type = _NNS_VIDEO;
  1011. valid = gst_tensor_meta_info_validate (&meta);
  1012. EXPECT_TRUE (valid);
  1013. /* invalid type */
  1014. meta.type = _NNS_END;
  1015. valid = gst_tensor_meta_info_validate (&meta);
  1016. EXPECT_FALSE (valid);
  1017. meta.type = _NNS_UINT8;
  1018. /* invalid dimension */
  1019. meta.dimension[0] = 0;
  1020. valid = gst_tensor_meta_info_validate (&meta);
  1021. EXPECT_FALSE (valid);
  1022. meta.dimension[0] = 10;
  1023. /* invalid format */
  1024. meta.format = 100;
  1025. valid = gst_tensor_meta_info_validate (&meta);
  1026. EXPECT_FALSE (valid);
  1027. meta.format = _NNS_TENSOR_FORMAT_FLEXIBLE;
  1028. /* invalid media type */
  1029. meta.media_type = _NNS_MEDIA_ANY;
  1030. valid = gst_tensor_meta_info_validate (&meta);
  1031. EXPECT_FALSE (valid);
  1032. }
  1033. /**
  1034. * @brief Test for tensor meta info (update header with invalid param).
  1035. */
  1036. TEST (commonMetaInfo, updateHeaderInvalidParam_n)
  1037. {
  1038. GstTensorMetaInfo meta;
  1039. gpointer header;
  1040. gsize hsize;
  1041. gboolean ret;
  1042. gst_tensor_meta_info_init (&meta);
  1043. hsize = gst_tensor_meta_info_get_header_size (&meta);
  1044. header = g_malloc0 (hsize);
  1045. ret = gst_tensor_meta_info_update_header (NULL, header);
  1046. EXPECT_FALSE (ret);
  1047. ret = gst_tensor_meta_info_update_header (&meta, NULL);
  1048. EXPECT_FALSE (ret);
  1049. g_free (header);
  1050. }
  1051. /**
  1052. * @brief Test for tensor meta info (parse header with invalid param).
  1053. */
  1054. TEST (commonMetaInfo, parseHeaderInvalidParam_n)
  1055. {
  1056. GstTensorMetaInfo meta;
  1057. gpointer header;
  1058. gsize hsize;
  1059. gboolean ret;
  1060. gst_tensor_meta_info_init (&meta);
  1061. hsize = gst_tensor_meta_info_get_header_size (&meta);
  1062. header = g_malloc0 (hsize);
  1063. ret = gst_tensor_meta_info_parse_header (NULL, header);
  1064. EXPECT_FALSE (ret);
  1065. ret = gst_tensor_meta_info_parse_header (&meta, NULL);
  1066. EXPECT_FALSE (ret);
  1067. g_free (header);
  1068. }
  1069. /**
  1070. * @brief Test for tensor meta info (parse memory with invalid param).
  1071. */
  1072. TEST (commonMetaInfo, parseMemInvalidParam_n)
  1073. {
  1074. GstTensorMetaInfo meta;
  1075. GstMemory *mem;
  1076. gsize hsize;
  1077. gboolean ret;
  1078. gst_tensor_meta_info_init (&meta);
  1079. hsize = gst_tensor_meta_info_get_header_size (&meta);
  1080. mem = gst_allocator_alloc (NULL, hsize, NULL);
  1081. ret = gst_tensor_meta_info_parse_memory (NULL, mem);
  1082. EXPECT_FALSE (ret);
  1083. ret = gst_tensor_meta_info_parse_memory (&meta, NULL);
  1084. EXPECT_FALSE (ret);
  1085. gst_memory_unref (mem);
  1086. }
  1087. /**
  1088. * @brief Test for tensor meta info (append header to memory).
  1089. */
  1090. TEST (commonMetaInfo, appendHeader)
  1091. {
  1092. GstTensorMetaInfo meta1, meta2;
  1093. GstMemory *result, *data;
  1094. gsize hsize, msize;
  1095. gboolean ret;
  1096. gst_tensor_meta_info_init (&meta1);
  1097. meta1.type = _NNS_INT16;
  1098. meta1.format = _NNS_TENSOR_FORMAT_FLEXIBLE;
  1099. meta1.media_type = _NNS_OCTET;
  1100. meta1.dimension[0] = 300U;
  1101. meta1.dimension[1] = 1U;
  1102. hsize = gst_tensor_meta_info_get_header_size (&meta1);
  1103. data = gst_allocator_alloc (NULL, 300, NULL);
  1104. result = gst_tensor_meta_info_append_header (&meta1, data);
  1105. EXPECT_TRUE (result != NULL);
  1106. msize = gst_memory_get_sizes (result, NULL, NULL);
  1107. EXPECT_EQ (msize, hsize + 300U);
  1108. ret = gst_tensor_meta_info_parse_memory (&meta2, result);
  1109. EXPECT_TRUE (ret);
  1110. EXPECT_EQ (meta1.version, meta2.version);
  1111. EXPECT_EQ (meta2.type, _NNS_INT16);
  1112. EXPECT_EQ (meta2.format, _NNS_TENSOR_FORMAT_FLEXIBLE);
  1113. EXPECT_EQ ((media_type) meta2.media_type, _NNS_OCTET);
  1114. EXPECT_EQ (meta2.dimension[0], 300U);
  1115. EXPECT_EQ (meta2.dimension[1], 1U);
  1116. gst_memory_unref (data);
  1117. gst_memory_unref (result);
  1118. }
  1119. /**
  1120. * @brief Test for tensor meta info (append header to memory with invalid param).
  1121. */
  1122. TEST (commonMetaInfo, appendHeaderInvalidParam_n)
  1123. {
  1124. GstTensorMetaInfo meta;
  1125. GstMemory *result, *data;
  1126. gst_tensor_meta_info_init (&meta);
  1127. data = gst_allocator_alloc (NULL, 300, NULL);
  1128. result = gst_tensor_meta_info_append_header (NULL, data);
  1129. EXPECT_FALSE (result != NULL);
  1130. result = gst_tensor_meta_info_append_header (&meta, NULL);
  1131. EXPECT_FALSE (result != NULL);
  1132. gst_memory_unref (data);
  1133. }
  1134. /**
  1135. * @brief Test for tensor meta info (convert meta).
  1136. */
  1137. TEST (commonMetaInfo, convertMeta)
  1138. {
  1139. GstTensorMetaInfo meta;
  1140. GstTensorInfo info1, info2;
  1141. gboolean ret;
  1142. gst_tensor_info_init (&info1);
  1143. info1.type = _NNS_INT16;
  1144. gst_tensor_parse_dimension ("300:1", info1.dimension);
  1145. ret = gst_tensor_info_convert_to_meta (&info1, &meta);
  1146. EXPECT_TRUE (ret);
  1147. gst_tensor_info_init (&info2);
  1148. ret = gst_tensor_meta_info_convert (&meta, &info2);
  1149. EXPECT_TRUE (ret);
  1150. EXPECT_EQ (info2.type, _NNS_INT16);
  1151. EXPECT_EQ (info2.dimension[0], 300U);
  1152. EXPECT_EQ (info2.dimension[1], 1U);
  1153. }
  1154. /**
  1155. * @brief Test for tensor meta info (convert gst-info with invalid param).
  1156. */
  1157. TEST (commonMetaInfo, convertMetaInvalidParam01_n)
  1158. {
  1159. GstTensorMetaInfo meta;
  1160. GstTensorInfo info;
  1161. gboolean ret;
  1162. gst_tensor_info_init (&info);
  1163. info.type = _NNS_UINT16;
  1164. gst_tensor_parse_dimension ("100:1", info.dimension);
  1165. ret = gst_tensor_info_convert_to_meta (NULL, &meta);
  1166. EXPECT_FALSE (ret);
  1167. ret = gst_tensor_info_convert_to_meta (&info, NULL);
  1168. EXPECT_FALSE (ret);
  1169. }
  1170. /**
  1171. * @brief Test for tensor meta info (convert meta with invalid param).
  1172. */
  1173. TEST (commonMetaInfo, convertMetaInvalidParam02_n)
  1174. {
  1175. GstTensorMetaInfo meta;
  1176. GstTensorInfo info;
  1177. gboolean ret;
  1178. gst_tensor_meta_info_init (&meta);
  1179. meta.type = _NNS_UINT16;
  1180. meta.format = _NNS_TENSOR_FORMAT_STATIC;
  1181. gst_tensor_parse_dimension ("100:1", meta.dimension);
  1182. ret = gst_tensor_meta_info_convert (NULL, &info);
  1183. EXPECT_FALSE (ret);
  1184. ret = gst_tensor_meta_info_convert (&meta, NULL);
  1185. EXPECT_FALSE (ret);
  1186. }
  1187. /**
  1188. * @brief Test for tensor meta info (dimension rank mismatched).
  1189. */
  1190. TEST (commonMetaInfo, convertMetaInvalidParam03_n)
  1191. {
  1192. GstTensorMetaInfo meta;
  1193. GstTensorInfo info;
  1194. guint i;
  1195. gboolean ret;
  1196. gst_tensor_meta_info_init (&meta);
  1197. meta.type = _NNS_UINT16;
  1198. meta.format = _NNS_TENSOR_FORMAT_STATIC;
  1199. /* rank > NNS_TENSOR_RANK_LIMIT */
  1200. for (i = 0; i < NNS_TENSOR_RANK_LIMIT + 3; i++)
  1201. meta.dimension[i] = 2;
  1202. ret = gst_tensor_meta_info_convert (&meta, &info);
  1203. EXPECT_FALSE (ret);
  1204. }
  1205. /**
  1206. * @brief Test to replace string.
  1207. */
  1208. TEST (commonStringUtil, replaceStr01)
  1209. {
  1210. gchar *result;
  1211. guint changed;
  1212. result = g_strdup ("sourceelement ! parser ! converter ! format ! converter ! format ! converter ! sink");
  1213. result = replace_string (result, "sourceelement", "src", NULL, &changed);
  1214. EXPECT_EQ (changed, 1U);
  1215. EXPECT_STREQ (result, "src ! parser ! converter ! format ! converter ! format ! converter ! sink");
  1216. result = replace_string (result, "format", "fmt", NULL, &changed);
  1217. EXPECT_EQ (changed, 2U);
  1218. EXPECT_STREQ (result, "src ! parser ! converter ! fmt ! converter ! fmt ! converter ! sink");
  1219. result = replace_string (result, "converter", "conv", NULL, &changed);
  1220. EXPECT_EQ (changed, 3U);
  1221. EXPECT_STREQ (result, "src ! parser ! conv ! fmt ! conv ! fmt ! conv ! sink");
  1222. result = replace_string (result, "invalidname", "invalid", NULL, &changed);
  1223. EXPECT_EQ (changed, 0U);
  1224. EXPECT_STREQ (result, "src ! parser ! conv ! fmt ! conv ! fmt ! conv ! sink");
  1225. g_free (result);
  1226. }
  1227. /**
  1228. * @brief Test to replace string.
  1229. */
  1230. TEST (commonStringUtil, replaceStr02)
  1231. {
  1232. gchar *result;
  1233. guint changed;
  1234. result = g_strdup ("source! parser ! sources ! mysource ! source ! format !source! conv source");
  1235. result = replace_string (result, "source", "src", " !", &changed);
  1236. EXPECT_EQ (changed, 4U);
  1237. EXPECT_STREQ (result, "src! parser ! sources ! mysource ! src ! format !src! conv src");
  1238. result = replace_string (result, "src", "mysource", "! ", &changed);
  1239. EXPECT_EQ (changed, 4U);
  1240. EXPECT_STREQ (result, "mysource! parser ! sources ! mysource ! mysource ! format !mysource! conv mysource");
  1241. result = replace_string (result, "source", "src", NULL, &changed);
  1242. EXPECT_EQ (changed, 6U);
  1243. EXPECT_STREQ (result, "mysrc! parser ! srcs ! mysrc ! mysrc ! format !mysrc! conv mysrc");
  1244. result = replace_string (result, "mysrc", "src", ";", &changed);
  1245. EXPECT_EQ (changed, 0U);
  1246. EXPECT_STREQ (result, "mysrc! parser ! srcs ! mysrc ! mysrc ! format !mysrc! conv mysrc");
  1247. g_free (result);
  1248. }
  1249. /**
  1250. * @brief Test for aggregation utils (clear data).
  1251. */
  1252. TEST (commonAggregationUtil, clearData)
  1253. {
  1254. const guint32 key1 = 0U;
  1255. const guint32 key2 = 100U;
  1256. GHashTable *table;
  1257. table = gst_tensor_aggregation_init ();
  1258. EXPECT_TRUE (gst_tensor_aggregation_get_adapter (table, key1) != NULL);
  1259. EXPECT_TRUE (gst_tensor_aggregation_get_adapter (table, key2) != NULL);
  1260. gst_adapter_push (gst_tensor_aggregation_get_adapter (table, key1), gst_buffer_new_allocate (NULL, 1024U, 0));
  1261. gst_adapter_push (gst_tensor_aggregation_get_adapter (table, key2), gst_buffer_new_allocate (NULL, 512U, 0));
  1262. gst_adapter_push (gst_tensor_aggregation_get_adapter (table, key1), gst_buffer_new_allocate (NULL, 100U, 0));
  1263. EXPECT_EQ (gst_adapter_available (gst_tensor_aggregation_get_adapter (table, key1)), 1124U);
  1264. EXPECT_EQ (gst_adapter_available (gst_tensor_aggregation_get_adapter (table, key2)), 512U);
  1265. gst_tensor_aggregation_clear (table, key2);
  1266. EXPECT_EQ (gst_adapter_available (gst_tensor_aggregation_get_adapter (table, key2)), 0U);
  1267. gst_adapter_push (gst_tensor_aggregation_get_adapter (table, key2), gst_buffer_new_allocate (NULL, 200U, 0));
  1268. EXPECT_EQ (gst_adapter_available (gst_tensor_aggregation_get_adapter (table, key2)), 200U);
  1269. gst_tensor_aggregation_clear_all (table);
  1270. EXPECT_EQ (gst_adapter_available (gst_tensor_aggregation_get_adapter (table, key1)), 0U);
  1271. EXPECT_EQ (gst_adapter_available (gst_tensor_aggregation_get_adapter (table, key2)), 0U);
  1272. g_hash_table_destroy (table);
  1273. }
  1274. /**
  1275. * @brief Test for aggregation utils (null param).
  1276. */
  1277. TEST (commonAggregationUtil, nullParam_n)
  1278. {
  1279. GstAdapter *adapter;
  1280. adapter = gst_tensor_aggregation_get_adapter (NULL, 0U);
  1281. EXPECT_FALSE (adapter != NULL);
  1282. }
  1283. /**
  1284. * @brief Create null files
  1285. */
  1286. static gchar *
  1287. create_null_file (const gchar *dir, const gchar *file)
  1288. {
  1289. gchar *fullpath = g_build_path ("/", dir, file, NULL);
  1290. FILE *fp = g_fopen (fullpath, "w");
  1291. if (fp) {
  1292. fclose (fp);
  1293. } else {
  1294. g_free (fullpath);
  1295. return NULL;
  1296. }
  1297. return fullpath;
  1298. }
  1299. /**
  1300. * @brief Check string custom conf
  1301. */
  1302. static gboolean
  1303. check_custom_conf (const gchar *group, const gchar *key, const gchar *expected)
  1304. {
  1305. gchar *str = nnsconf_get_custom_value_string (group, key);
  1306. gboolean ret = (0 == g_strcmp0 (str, expected));
  1307. g_free (str);
  1308. return ret;
  1309. }
  1310. /**
  1311. * @brief Test custom configurations
  1312. */
  1313. TEST (confCustom, envStr01)
  1314. {
  1315. gchar *fullpath = g_build_path ("/", g_get_tmp_dir (), "nns-tizen-XXXXXX", NULL);
  1316. gchar *dir = g_mkdtemp (fullpath);
  1317. gchar *filename = g_build_path ("/", dir, "nnstreamer.ini", NULL);
  1318. gchar *dirf = g_build_path ("/", dir, "filters", NULL);
  1319. gchar *dircf = g_build_path ("/", dir, "custom", NULL);
  1320. gchar *dird = g_build_path ("/", dir, "decoders", NULL);
  1321. EXPECT_EQ (g_mkdir (dirf, 0755), 0);
  1322. EXPECT_EQ (g_mkdir (dircf, 0755), 0);
  1323. EXPECT_EQ (g_mkdir (dird, 0755), 0);
  1324. FILE *fp = g_fopen (filename, "w");
  1325. const gchar *fn;
  1326. const gchar *base_confenv;
  1327. gchar *confenv;
  1328. ASSERT_TRUE (fp != NULL);
  1329. base_confenv = g_getenv ("NNSTREAMER_CONF");
  1330. confenv = (base_confenv != NULL) ? g_strdup (base_confenv) : NULL;
  1331. g_fprintf (fp, "[common]\n");
  1332. g_fprintf (fp, "enable_envvar=True\n");
  1333. g_fprintf (fp, "[filter]\n");
  1334. g_fprintf (fp, "filters=%s\n", dirf);
  1335. g_fprintf (fp, "customfilters=%s\n", dircf);
  1336. g_fprintf (fp, "[decoder]\n");
  1337. g_fprintf (fp, "decoders=%s\n", dird);
  1338. g_fprintf (fp, "[customX]\n");
  1339. g_fprintf (fp, "abc=OFF\n");
  1340. g_fprintf (fp, "def=on\n");
  1341. g_fprintf (fp, "ghi=TRUE\n");
  1342. g_fprintf (fp, "n01=fAlSe\n");
  1343. g_fprintf (fp, "n02=yeah\n");
  1344. g_fprintf (fp, "n03=NAH\n");
  1345. g_fprintf (fp, "n04=1\n");
  1346. g_fprintf (fp, "n05=0\n");
  1347. g_fprintf (fp, "mzx=whatsoever\n");
  1348. g_fprintf (fp, "[customY]\n");
  1349. g_fprintf (fp, "mzx=dunno\n");
  1350. g_fprintf (fp, "[customZ]\n");
  1351. g_fprintf (fp, "mzx=wth\n");
  1352. g_fprintf (fp, "n05=1\n");
  1353. fclose (fp);
  1354. gchar *f1 = create_null_file (
  1355. dirf, "libnnstreamer_filter_fantastic" NNSTREAMER_SO_FILE_EXTENSION);
  1356. gchar *f2 = create_null_file (
  1357. dirf, "libnnstreamer_filter_neuralnetwork" NNSTREAMER_SO_FILE_EXTENSION);
  1358. gchar *f3 = create_null_file (dird, "libnnstreamer_decoder_omg" NNSTREAMER_SO_FILE_EXTENSION);
  1359. gchar *f4 = create_null_file (
  1360. dird, "libnnstreamer_decoder_wthisgoingon" NNSTREAMER_SO_FILE_EXTENSION);
  1361. gchar *f5 = create_null_file (dircf, "custom_mechanism" NNSTREAMER_SO_FILE_EXTENSION);
  1362. gchar *f6 = create_null_file (dircf, "fastfaster" NNSTREAMER_SO_FILE_EXTENSION);
  1363. EXPECT_TRUE (g_setenv ("NNSTREAMER_CONF", filename, TRUE));
  1364. EXPECT_TRUE (nnsconf_loadconf (TRUE));
  1365. fn = nnsconf_get_fullpath ("fantastic", NNSCONF_PATH_FILTERS);
  1366. EXPECT_STREQ (fn, f1);
  1367. fn = nnsconf_get_fullpath ("neuralnetwork", NNSCONF_PATH_FILTERS);
  1368. EXPECT_STREQ (fn, f2);
  1369. fn = nnsconf_get_fullpath ("notfound", NNSCONF_PATH_FILTERS);
  1370. EXPECT_STREQ (fn, NULL);
  1371. fn = nnsconf_get_fullpath ("omg", NNSCONF_PATH_DECODERS);
  1372. EXPECT_STREQ (fn, f3);
  1373. fn = nnsconf_get_fullpath ("wthisgoingon", NNSCONF_PATH_DECODERS);
  1374. EXPECT_STREQ (fn, f4);
  1375. fn = nnsconf_get_fullpath ("notfound", NNSCONF_PATH_DECODERS);
  1376. EXPECT_STREQ (fn, NULL);
  1377. fn = nnsconf_get_fullpath ("custom_mechanism", NNSCONF_PATH_CUSTOM_FILTERS);
  1378. EXPECT_STREQ (fn, f5);
  1379. fn = nnsconf_get_fullpath ("fastfaster", NNSCONF_PATH_CUSTOM_FILTERS);
  1380. EXPECT_STREQ (fn, f6);
  1381. fn = nnsconf_get_fullpath ("notfound", NNSCONF_PATH_CUSTOM_FILTERS);
  1382. EXPECT_STREQ (fn, NULL);
  1383. EXPECT_TRUE (check_custom_conf ("customX", "abc", "OFF"));
  1384. EXPECT_FALSE (nnsconf_get_custom_value_bool ("customX", "abc", TRUE));
  1385. EXPECT_FALSE (nnsconf_get_custom_value_bool ("customX", "abc", FALSE));
  1386. EXPECT_TRUE (check_custom_conf ("customX", "def", "on"));
  1387. EXPECT_TRUE (nnsconf_get_custom_value_bool ("customX", "def", FALSE));
  1388. EXPECT_TRUE (nnsconf_get_custom_value_bool ("customX", "def", TRUE));
  1389. EXPECT_TRUE (check_custom_conf ("customX", "ghi", "TRUE"));
  1390. EXPECT_TRUE (nnsconf_get_custom_value_bool ("customX", "ghi", FALSE));
  1391. EXPECT_TRUE (nnsconf_get_custom_value_bool ("customX", "ghi", TRUE));
  1392. EXPECT_TRUE (check_custom_conf ("customX", "n02", "yeah"));
  1393. EXPECT_TRUE (nnsconf_get_custom_value_bool ("customX", "n02", FALSE));
  1394. EXPECT_TRUE (nnsconf_get_custom_value_bool ("customX", "n02", TRUE));
  1395. EXPECT_TRUE (check_custom_conf ("customX", "n03", "NAH"));
  1396. EXPECT_FALSE (nnsconf_get_custom_value_bool ("customX", "n03", FALSE));
  1397. EXPECT_FALSE (nnsconf_get_custom_value_bool ("customX", "n03", TRUE));
  1398. EXPECT_TRUE (check_custom_conf ("customX", "n04", "1"));
  1399. EXPECT_TRUE (nnsconf_get_custom_value_bool ("customX", "n04", FALSE));
  1400. EXPECT_TRUE (nnsconf_get_custom_value_bool ("customX", "n04", TRUE));
  1401. EXPECT_TRUE (check_custom_conf ("customX", "n05", "0"));
  1402. EXPECT_FALSE (nnsconf_get_custom_value_bool ("customX", "n05", FALSE));
  1403. EXPECT_FALSE (nnsconf_get_custom_value_bool ("customX", "n05", TRUE));
  1404. EXPECT_TRUE (check_custom_conf ("customX", "mzx", "whatsoever"));
  1405. EXPECT_FALSE (nnsconf_get_custom_value_bool ("customX", "mzx", FALSE));
  1406. EXPECT_TRUE (nnsconf_get_custom_value_bool ("customX", "mzx", TRUE));
  1407. EXPECT_TRUE (check_custom_conf ("customY", "mzx", "dunno"));
  1408. EXPECT_FALSE (nnsconf_get_custom_value_bool ("customY", "mzx", FALSE));
  1409. EXPECT_TRUE (nnsconf_get_custom_value_bool ("customY", "mzx", TRUE));
  1410. EXPECT_TRUE (check_custom_conf ("customZ", "mzx", "wth"));
  1411. EXPECT_FALSE (nnsconf_get_custom_value_bool ("customZ", "mzx", FALSE));
  1412. EXPECT_TRUE (nnsconf_get_custom_value_bool ("customZ", "mzx", TRUE));
  1413. EXPECT_TRUE (check_custom_conf ("customZ", "n05", "1"));
  1414. EXPECT_TRUE (nnsconf_get_custom_value_bool ("customZ", "n05", FALSE));
  1415. EXPECT_TRUE (nnsconf_get_custom_value_bool ("customZ", "n05", TRUE));
  1416. EXPECT_TRUE (check_custom_conf ("customW", "n05", NULL));
  1417. EXPECT_FALSE (nnsconf_get_custom_value_bool ("customW", "n05", FALSE));
  1418. EXPECT_TRUE (nnsconf_get_custom_value_bool ("customW", "n05", TRUE));
  1419. g_free (f1);
  1420. g_free (f2);
  1421. g_free (f3);
  1422. g_free (f4);
  1423. g_free (f5);
  1424. g_free (f6);
  1425. g_free (fullpath);
  1426. g_free (filename);
  1427. g_free (dirf);
  1428. g_free (dircf);
  1429. g_free (dird);
  1430. if (confenv) {
  1431. EXPECT_TRUE (g_setenv ("NNSTREAMER_CONF", confenv, TRUE));
  1432. g_free (confenv);
  1433. } else {
  1434. g_unsetenv ("NNSTREAMER_CONF");
  1435. }
  1436. }
  1437. /**
  1438. * @brief Test nnstreamer conf util (name prefix with invalid param).
  1439. */
  1440. TEST (confCustom, subpluginPrefix_n)
  1441. {
  1442. EXPECT_STREQ (nnsconf_get_subplugin_name_prefix (NNSCONF_PATH_END), NULL);
  1443. EXPECT_STREQ (nnsconf_get_subplugin_name_prefix ((nnsconf_type_path) -1), NULL);
  1444. }
  1445. /**
  1446. * @brief Test version control (positive)
  1447. */
  1448. TEST (versionControl, getVer01)
  1449. {
  1450. gchar *verstr = nnstreamer_version_string ();
  1451. guint major, minor, micro;
  1452. gchar *verstr2, *verstr3;
  1453. nnstreamer_version_fetch (&major, &minor, &micro);
  1454. verstr2 = g_strdup_printf ("NNStreamer %u.%u.%u", major, minor, micro);
  1455. verstr3 = g_strdup_printf ("%u.%u.%u", major, minor, micro);
  1456. EXPECT_STRCASEEQ (verstr, verstr2);
  1457. EXPECT_STRCASEEQ (VERSION, verstr3);
  1458. EXPECT_EQ ((int)major, NNSTREAMER_VERSION_MAJOR);
  1459. EXPECT_EQ ((int)minor, NNSTREAMER_VERSION_MINOR);
  1460. EXPECT_EQ ((int)micro, NNSTREAMER_VERSION_MICRO);
  1461. g_free (verstr);
  1462. g_free (verstr2);
  1463. g_free (verstr3);
  1464. }
  1465. /**
  1466. * @brief Main function for unit test.
  1467. */
  1468. int
  1469. main (int argc, char **argv)
  1470. {
  1471. int ret = -1;
  1472. try {
  1473. testing::InitGoogleTest (&argc, argv);
  1474. } catch (...) {
  1475. g_warning ("catch 'testing::internal::<unnamed>::ClassUniqueToAlwaysTrue'");
  1476. }
  1477. gst_init (&argc, &argv);
  1478. try {
  1479. ret = RUN_ALL_TESTS ();
  1480. } catch (...) {
  1481. g_warning ("catch `testing::internal::GoogleTestFailureException`");
  1482. }
  1483. return ret;
  1484. }