You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_paramdict.cpp 17 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. // Copyright 2025 Tencent
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. #include <stdio.h>
  4. #include "datareader.h"
  5. #include "paramdict.h"
  6. class ParamDictTest : public ncnn::ParamDict
  7. {
  8. public:
  9. int load_param(const char* str);
  10. int load_param_bin(const unsigned char* mem);
  11. };
  12. int ParamDictTest::load_param(const char* str)
  13. {
  14. const unsigned char* mem = (const unsigned char*)str;
  15. ncnn::DataReaderFromMemory dr(mem);
  16. return ncnn::ParamDict::load_param(dr);
  17. }
  18. int ParamDictTest::load_param_bin(const unsigned char* mem)
  19. {
  20. ncnn::DataReaderFromMemory dr(mem);
  21. return ncnn::ParamDict::load_param_bin(dr);
  22. }
  23. static int test_paramdict_0()
  24. {
  25. ParamDictTest pdt;
  26. pdt.load_param("0=100 1=1,-1,4,5,1,4 2=1.250000 -23303=5,0.1,0.2,-0.4,0.8,1.0 -23304=3,-1,10,-88");
  27. // int
  28. int typei = pdt.type(0);
  29. if (typei != 2)
  30. {
  31. fprintf(stderr, "test_paramdict int type failed %d != 2\n", typei);
  32. return -1;
  33. }
  34. int i = pdt.get(0, 0);
  35. if (i != 100)
  36. {
  37. fprintf(stderr, "test_paramdict int value failed %d != 100\n", i);
  38. return -1;
  39. }
  40. // int array
  41. int typeai = pdt.type(1);
  42. if (typeai != 5)
  43. {
  44. fprintf(stderr, "test_paramdict int array type failed %d != 5\n", typeai);
  45. return -1;
  46. }
  47. ncnn::Mat ai = pdt.get(1, ncnn::Mat());
  48. if (ai.w != 6)
  49. {
  50. fprintf(stderr, "test_paramdict int array size failed %d != 6\n", ai.w);
  51. return -1;
  52. }
  53. const int* p = ai;
  54. if (p[0] != 1 || p[1] != -1 || p[2] != 4 || p[3] != 5 || p[4] != 1 || p[5] != 4)
  55. {
  56. fprintf(stderr, "test_paramdict int array value failed %d %d %d %d %d %d\n", p[0], p[1], p[2], p[3], p[4], p[5]);
  57. return -1;
  58. }
  59. // float
  60. int typef = pdt.type(2);
  61. if (typef != 3)
  62. {
  63. fprintf(stderr, "test_paramdict float type failed %d != 3\n", typef);
  64. return -1;
  65. }
  66. float f = pdt.get(2, 0.f);
  67. if (f != 1.25f)
  68. {
  69. fprintf(stderr, "test_paramdict float value failed %f != 1.25f\n", f);
  70. return -1;
  71. }
  72. // float array
  73. int typeaf = pdt.type(3);
  74. if (typeaf != 6)
  75. {
  76. fprintf(stderr, "test_paramdict float array type failed %d != 6\n", typeaf);
  77. return -1;
  78. }
  79. ncnn::Mat af = pdt.get(3, ncnn::Mat());
  80. if (af.w != 5)
  81. {
  82. fprintf(stderr, "test_paramdict float array size failed %d != 5\n", af.w);
  83. return -1;
  84. }
  85. if (af[0] != 0.1f || af[1] != 0.2f || af[2] != -0.4f || af[3] != 0.8f || af[4] != 1.0f)
  86. {
  87. fprintf(stderr, "test_paramdict float array value failed %f %f %f %f %f\n", af[0], af[1], af[2], af[3], af[4]);
  88. return -1;
  89. }
  90. // int array
  91. typeai = pdt.type(4);
  92. if (typeai != 5)
  93. {
  94. fprintf(stderr, "test_paramdict int array type failed %d != 5\n", typeai);
  95. return -1;
  96. }
  97. ai = pdt.get(4, ncnn::Mat());
  98. if (ai.w != 3)
  99. {
  100. fprintf(stderr, "test_paramdict int array size failed %d != 3\n", ai.w);
  101. return -1;
  102. }
  103. p = ai;
  104. if (p[0] != -1 || p[1] != 10 || p[2] != -88)
  105. {
  106. fprintf(stderr, "test_paramdict int array value failed %d %d %d\n", p[0], p[1], p[2]);
  107. return -1;
  108. }
  109. return 0;
  110. }
  111. static int test_paramdict_1()
  112. {
  113. ParamDictTest pdt;
  114. pdt.load_param("0=-1 1=4, 2=0.01 3=-1.45e-2,3.14");
  115. // int
  116. int typei = pdt.type(0);
  117. if (typei != 2)
  118. {
  119. fprintf(stderr, "test_paramdict int type failed %d != 2\n", typei);
  120. return -1;
  121. }
  122. int i = pdt.get(0, 0);
  123. if (i != -1)
  124. {
  125. fprintf(stderr, "test_paramdict int value failed %d != -1\n", i);
  126. return -1;
  127. }
  128. // int array
  129. int typeai = pdt.type(1);
  130. if (typeai != 5)
  131. {
  132. fprintf(stderr, "test_paramdict int array type failed %d != 5\n", typeai);
  133. return -1;
  134. }
  135. ncnn::Mat ai = pdt.get(1, ncnn::Mat());
  136. if (ai.w != 1)
  137. {
  138. fprintf(stderr, "test_paramdict int array size failed %d != 1\n", ai.w);
  139. return -1;
  140. }
  141. const int* p = ai;
  142. if (p[0] != 4)
  143. {
  144. fprintf(stderr, "test_paramdict int array value failed %d\n", p[0]);
  145. return -1;
  146. }
  147. // float
  148. int typef = pdt.type(2);
  149. if (typef != 3)
  150. {
  151. fprintf(stderr, "test_paramdict float type failed %d != 3\n", typef);
  152. return -1;
  153. }
  154. float f = pdt.get(2, 0.f);
  155. if (f != 0.01f)
  156. {
  157. fprintf(stderr, "test_paramdict float value failed %f != 0.01f\n", f);
  158. return -1;
  159. }
  160. // float array
  161. int typeaf = pdt.type(3);
  162. if (typeaf != 6)
  163. {
  164. fprintf(stderr, "test_paramdict float array type failed %d != 6\n", typeaf);
  165. return -1;
  166. }
  167. ncnn::Mat af = pdt.get(3, ncnn::Mat());
  168. if (af.w != 2)
  169. {
  170. fprintf(stderr, "test_paramdict float array size failed %d != 2\n", af.w);
  171. return -1;
  172. }
  173. if (af[0] != -0.0145f || af[1] != 3.14f)
  174. {
  175. fprintf(stderr, "test_paramdict float array value failed %f %f\n", af[0], af[1]);
  176. return -1;
  177. }
  178. return 0;
  179. }
  180. static int test_paramdict_2()
  181. {
  182. ParamDictTest pdt;
  183. pdt.load_param("0=bij,bjk->bik 1=This_is_a_very_long_long_string 3=\"1,2,3 and 6.667 zzz\" 2=\"X\"");
  184. // string
  185. int types = pdt.type(0);
  186. if (types != 7)
  187. {
  188. fprintf(stderr, "test_paramdict string type failed %d != 7\n", types);
  189. return -1;
  190. }
  191. std::string s = pdt.get(0, "");
  192. if (s != "bij,bjk->bik")
  193. {
  194. fprintf(stderr, "test_paramdict string text failed %s != bij,bjk->bik\n", s.c_str());
  195. return -1;
  196. }
  197. // string
  198. types = pdt.type(1);
  199. if (types != 7)
  200. {
  201. fprintf(stderr, "test_paramdict string type failed %d != 7\n", types);
  202. return -1;
  203. }
  204. s = pdt.get(1, "");
  205. if (s != "This_is_a_very_long_long_string")
  206. {
  207. fprintf(stderr, "test_paramdict string text failed %s != This_is_a_very_long_long_string\n", s.c_str());
  208. return -1;
  209. }
  210. // string
  211. types = pdt.type(2);
  212. if (types != 7)
  213. {
  214. fprintf(stderr, "test_paramdict string type failed %d != 7\n", types);
  215. return -1;
  216. }
  217. s = pdt.get(2, "");
  218. if (s != "X")
  219. {
  220. fprintf(stderr, "test_paramdict string text failed %s != X\n", s.c_str());
  221. return -1;
  222. }
  223. // string
  224. types = pdt.type(3);
  225. if (types != 7)
  226. {
  227. fprintf(stderr, "test_paramdict string type failed %d != 7\n", types);
  228. return -1;
  229. }
  230. s = pdt.get(3, "");
  231. if (s != "1,2,3 and 6.667 zzz")
  232. {
  233. fprintf(stderr, "test_paramdict string text failed %s != \"1,2,3 and 6.667 zzz\"\n", s.c_str());
  234. return -1;
  235. }
  236. return 0;
  237. }
  238. static int test_paramdict_3()
  239. {
  240. const unsigned char mem[] = {
  241. 0x00, 0x00, 0x00, 0x00,
  242. 0x64, 0x00, 0x00, 0x00,
  243. 0xfb, 0xa4, 0xff, 0xff,
  244. 0x06, 0x00, 0x00, 0x00,
  245. 0x01, 0x00, 0x00, 0x00,
  246. 0xff, 0xff, 0xff, 0xff,
  247. 0x04, 0x00, 0x00, 0x00,
  248. 0x05, 0x00, 0x00, 0x00,
  249. 0x01, 0x00, 0x00, 0x00,
  250. 0x04, 0x00, 0x00, 0x00,
  251. 0x02, 0x00, 0x00, 0x00,
  252. 0x00, 0x00, 0xa0, 0x3f,
  253. 0xf9, 0xa4, 0xff, 0xff,
  254. 0x05, 0x00, 0x00, 0x00,
  255. 0xcd, 0xcc, 0xcc, 0x3d,
  256. 0xcd, 0xcc, 0x4c, 0x3e,
  257. 0xcd, 0xcc, 0xcc, 0xbe,
  258. 0xcd, 0xcc, 0x4c, 0x3f,
  259. 0x00, 0x00, 0x80, 0x3f,
  260. 0x17, 0xff, 0xff, 0xff
  261. };
  262. ParamDictTest pdt;
  263. pdt.load_param_bin(mem);
  264. // int
  265. int typei = pdt.type(0);
  266. if (typei != 1)
  267. {
  268. fprintf(stderr, "test_paramdict int type failed %d != 1\n", typei);
  269. return -1;
  270. }
  271. int i = pdt.get(0, 0);
  272. if (i != 100)
  273. {
  274. fprintf(stderr, "test_paramdict int value failed %d != 100\n", i);
  275. return -1;
  276. }
  277. // int array
  278. int typeai = pdt.type(1);
  279. if (typeai != 4)
  280. {
  281. fprintf(stderr, "test_paramdict int array type failed %d != 4\n", typeai);
  282. return -1;
  283. }
  284. ncnn::Mat ai = pdt.get(1, ncnn::Mat());
  285. if (ai.w != 6)
  286. {
  287. fprintf(stderr, "test_paramdict int array size failed %d != 6\n", ai.w);
  288. return -1;
  289. }
  290. const int* p = ai;
  291. if (p[0] != 1 || p[1] != -1 || p[2] != 4 || p[3] != 5 || p[4] != 1 || p[5] != 4)
  292. {
  293. fprintf(stderr, "test_paramdict int array value failed %d %d %d %d %d %d\n", p[0], p[1], p[2], p[3], p[4], p[5]);
  294. return -1;
  295. }
  296. // float
  297. int typef = pdt.type(2);
  298. if (typef != 1)
  299. {
  300. fprintf(stderr, "test_paramdict float type failed %d != 1\n", typef);
  301. return -1;
  302. }
  303. float f = pdt.get(2, 0.f);
  304. if (f != 1.25f)
  305. {
  306. fprintf(stderr, "test_paramdict float value failed %f != 1.25f\n", f);
  307. return -1;
  308. }
  309. // float array
  310. int typeaf = pdt.type(3);
  311. if (typeaf != 4)
  312. {
  313. fprintf(stderr, "test_paramdict float array type failed %d != 4\n", typeaf);
  314. return -1;
  315. }
  316. ncnn::Mat af = pdt.get(3, ncnn::Mat());
  317. if (af.w != 5)
  318. {
  319. fprintf(stderr, "test_paramdict float array size failed %d != 5\n", af.w);
  320. return -1;
  321. }
  322. if (af[0] != 0.1f || af[1] != 0.2f || af[2] != -0.4f || af[3] != 0.8f || af[4] != 1.0f)
  323. {
  324. fprintf(stderr, "test_paramdict float array value failed %f %f %f %f %f\n", af[0], af[1], af[2], af[3], af[4]);
  325. return -1;
  326. }
  327. return 0;
  328. }
  329. static int test_paramdict_4()
  330. {
  331. const unsigned char mem[] = {
  332. 0x00, 0x00, 0x00, 0x00,
  333. 0xff, 0xff, 0xff, 0xff,
  334. 0xfb, 0xa4, 0xff, 0xff,
  335. 0x01, 0x00, 0x00, 0x00,
  336. 0x04, 0x00, 0x00, 0x00,
  337. 0x02, 0x00, 0x00, 0x00,
  338. 0x0a, 0xd7, 0x23, 0x3c,
  339. 0xf9, 0xa4, 0xff, 0xff,
  340. 0x02, 0x00, 0x00, 0x00,
  341. 0x68, 0x91, 0x6d, 0xbc,
  342. 0xc3, 0xf5, 0x48, 0x40,
  343. 0x17, 0xff, 0xff, 0xff
  344. };
  345. ParamDictTest pdt;
  346. pdt.load_param_bin(mem);
  347. // int
  348. int typei = pdt.type(0);
  349. if (typei != 1)
  350. {
  351. fprintf(stderr, "test_paramdict int type failed %d != 1\n", typei);
  352. return -1;
  353. }
  354. int i = pdt.get(0, 0);
  355. if (i != -1)
  356. {
  357. fprintf(stderr, "test_paramdict int value failed %d != -1\n", i);
  358. return -1;
  359. }
  360. // int array
  361. int typeai = pdt.type(1);
  362. if (typeai != 4)
  363. {
  364. fprintf(stderr, "test_paramdict int array type failed %d != 4\n", typeai);
  365. return -1;
  366. }
  367. ncnn::Mat ai = pdt.get(1, ncnn::Mat());
  368. if (ai.w != 1)
  369. {
  370. fprintf(stderr, "test_paramdict int array size failed %d != 1\n", ai.w);
  371. return -1;
  372. }
  373. const int* p = ai;
  374. if (p[0] != 4)
  375. {
  376. fprintf(stderr, "test_paramdict int array value failed %d\n", p[0]);
  377. return -1;
  378. }
  379. // float
  380. int typef = pdt.type(2);
  381. if (typef != 1)
  382. {
  383. fprintf(stderr, "test_paramdict float type failed %d != 1\n", typef);
  384. return -1;
  385. }
  386. float f = pdt.get(2, 0.f);
  387. if (f != 0.01f)
  388. {
  389. fprintf(stderr, "test_paramdict float value failed %f != 0.01f\n", f);
  390. return -1;
  391. }
  392. // float array
  393. int typeaf = pdt.type(3);
  394. if (typeaf != 4)
  395. {
  396. fprintf(stderr, "test_paramdict float array type failed %d != 4\n", typeaf);
  397. return -1;
  398. }
  399. ncnn::Mat af = pdt.get(3, ncnn::Mat());
  400. if (af.w != 2)
  401. {
  402. fprintf(stderr, "test_paramdict float array size failed %d != 2\n", af.w);
  403. return -1;
  404. }
  405. if (af[0] != -0.0145f || af[1] != 3.14f)
  406. {
  407. fprintf(stderr, "test_paramdict float array value failed %f %f\n", af[0], af[1]);
  408. return -1;
  409. }
  410. return 0;
  411. }
  412. static int test_paramdict_5()
  413. {
  414. const unsigned char mem[] = {
  415. 0x98, 0xa4, 0xff, 0xff,
  416. 0x0c, 0x00, 0x00, 0x00,
  417. 0x62, 0x69, 0x6a, 0x2c,
  418. 0x62, 0x6a, 0x6b, 0x2d,
  419. 0x3e, 0x62, 0x69, 0x6b,
  420. 0x97, 0xa4, 0xff, 0xff,
  421. 0x1f, 0x00, 0x00, 0x00,
  422. 0x54, 0x68, 0x69, 0x73,
  423. 0x5f, 0x69, 0x73, 0x5f,
  424. 0x61, 0x5f, 0x76, 0x65,
  425. 0x72, 0x79, 0x5f, 0x6c,
  426. 0x6f, 0x6e, 0x67, 0x5f,
  427. 0x6c, 0x6f, 0x6e, 0x67,
  428. 0x5f, 0x73, 0x74, 0x72,
  429. 0x69, 0x6e, 0x67, 0x00,
  430. 0x96, 0xa4, 0xff, 0xff,
  431. 0x01, 0x00, 0x00, 0x00,
  432. 0x58, 0x00, 0x00, 0x00,
  433. 0x17, 0xff, 0xff, 0xff
  434. };
  435. ParamDictTest pdt;
  436. pdt.load_param_bin(mem);
  437. // string
  438. int types = pdt.type(0);
  439. if (types != 7)
  440. {
  441. fprintf(stderr, "test_paramdict string type failed %d != 7\n", types);
  442. return -1;
  443. }
  444. std::string s = pdt.get(0, "");
  445. if (s != "bij,bjk->bik")
  446. {
  447. fprintf(stderr, "test_paramdict string text failed %s != bij,bjk->bik\n", s.c_str());
  448. return -1;
  449. }
  450. // string
  451. types = pdt.type(1);
  452. if (types != 7)
  453. {
  454. fprintf(stderr, "test_paramdict string type failed %d != 7\n", types);
  455. return -1;
  456. }
  457. s = pdt.get(1, "");
  458. if (s != "This_is_a_very_long_long_string")
  459. {
  460. fprintf(stderr, "test_paramdict string text failed %s != This_is_a_very_long_long_string\n", s.c_str());
  461. return -1;
  462. }
  463. // string
  464. types = pdt.type(2);
  465. if (types != 7)
  466. {
  467. fprintf(stderr, "test_paramdict string type failed %d != 7\n", types);
  468. return -1;
  469. }
  470. s = pdt.get(2, "");
  471. if (s != "X")
  472. {
  473. fprintf(stderr, "test_paramdict string text failed %s != X\n", s.c_str());
  474. return -1;
  475. }
  476. return 0;
  477. }
  478. static int compare_paramdict(const ncnn::ParamDict& pd, const ncnn::ParamDict& pd0)
  479. {
  480. for (int id = 0;; id++)
  481. {
  482. const int type0 = pd0.type(id);
  483. if (type0 == 0)
  484. {
  485. break;
  486. }
  487. else if (type0 == 2)
  488. {
  489. const int i0 = pd0.get(id, 0);
  490. int i = pd.get(id, 0);
  491. if (i != i0)
  492. {
  493. fprintf(stderr, "compare_paramdict int failed %d != %d\n", i, i0);
  494. return -1;
  495. }
  496. }
  497. else if (type0 == 3)
  498. {
  499. const float f0 = pd0.get(id, 0.f);
  500. int f = pd.get(id, 0.f);
  501. if (f != f0)
  502. {
  503. fprintf(stderr, "compare_paramdict float failed %f != %f\n", f, f0);
  504. return -1;
  505. }
  506. }
  507. else if (type0 == 5)
  508. {
  509. const ncnn::Mat ai0 = pd0.get(id, ncnn::Mat());
  510. ncnn::Mat ai = pd.get(id, ncnn::Mat());
  511. if (ai.w != ai0.w)
  512. {
  513. fprintf(stderr, "compare_paramdict int array size failed %d != %d\n", ai.w, ai0.w);
  514. return -1;
  515. }
  516. for (int q = 0; q < ai0.w; q++)
  517. {
  518. int i0 = ((const int*)ai0)[q];
  519. int i = ((const int*)ai)[q];
  520. if (i != i0)
  521. {
  522. fprintf(stderr, "compare_paramdict int array element %d failed %d != %d\n", q, i, i0);
  523. return -1;
  524. }
  525. }
  526. }
  527. else if (type0 == 6)
  528. {
  529. const ncnn::Mat af0 = pd0.get(id, ncnn::Mat());
  530. ncnn::Mat af = pd.get(id, ncnn::Mat());
  531. if (af.w != af0.w)
  532. {
  533. fprintf(stderr, "compare_paramdict float array size failed %d != %d\n", af.w, af0.w);
  534. return -1;
  535. }
  536. for (int q = 0; q < af0.w; q++)
  537. {
  538. float f0 = af0[q];
  539. float f = af[q];
  540. if (f != f0)
  541. {
  542. fprintf(stderr, "compare_paramdict float array element %d failed %f != %f\n", q, f, f0);
  543. return -1;
  544. }
  545. }
  546. }
  547. else if (type0 == 7)
  548. {
  549. const std::string s0 = pd0.get(id, "");
  550. std::string s = pd.get(id, "");
  551. if (s != s0)
  552. {
  553. fprintf(stderr, "compare_paramdict string failed %s != %s\n", s.c_str(), s0.c_str());
  554. return -1;
  555. }
  556. }
  557. else
  558. {
  559. fprintf(stderr, "unexpected paramdict type %d\n", type0);
  560. return -1;
  561. }
  562. }
  563. return 0;
  564. }
  565. static int test_paramdict_6()
  566. {
  567. const int i0 = 11;
  568. const float f0 = -2.2f;
  569. const std::string s0 = "qwqwqwq";
  570. ncnn::Mat ai0(1);
  571. {
  572. int* p = ai0;
  573. p[0] = 233;
  574. }
  575. ncnn::Mat af0(4);
  576. {
  577. float* p = af0;
  578. p[0] = 2.33f;
  579. p[1] = -0.2f;
  580. p[2] = 0.f;
  581. p[3] = 9494.f;
  582. }
  583. ncnn::ParamDict pd0;
  584. pd0.set(1, i0);
  585. pd0.set(2, ai0);
  586. pd0.set(3, f0);
  587. pd0.set(4, af0);
  588. pd0.set(5, s0);
  589. // copy
  590. {
  591. ncnn::ParamDict pd(pd0);
  592. int ret = compare_paramdict(pd, pd0);
  593. if (ret != 0)
  594. {
  595. fprintf(stderr, "paramdict copy failed\n");
  596. return -1;
  597. }
  598. }
  599. // assign
  600. {
  601. ncnn::ParamDict pd;
  602. pd = pd0;
  603. int ret = compare_paramdict(pd, pd0);
  604. if (ret != 0)
  605. {
  606. fprintf(stderr, "paramdict assign failed\n");
  607. return -1;
  608. }
  609. }
  610. return 0;
  611. }
  612. int main()
  613. {
  614. return 0
  615. || test_paramdict_0()
  616. || test_paramdict_1()
  617. || test_paramdict_2()
  618. || test_paramdict_3()
  619. || test_paramdict_4()
  620. || test_paramdict_5()
  621. || test_paramdict_6();
  622. }