test-vphn.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <stdio.h>
  3. #include <byteswap.h>
  4. #include "utils.h"
  5. #include "subunit.h"
  6. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  7. #define cpu_to_be32(x) bswap_32(x)
  8. #define be32_to_cpu(x) bswap_32(x)
  9. #define be16_to_cpup(x) bswap_16(*x)
  10. #define cpu_to_be64(x) bswap_64(x)
  11. #else
  12. #define cpu_to_be32(x) (x)
  13. #define be32_to_cpu(x) (x)
  14. #define be16_to_cpup(x) (*x)
  15. #define cpu_to_be64(x) (x)
  16. #endif
  17. #include "vphn.c"
  18. static struct test {
  19. char *descr;
  20. long input[VPHN_REGISTER_COUNT];
  21. u32 expected[VPHN_ASSOC_BUFSIZE];
  22. } all_tests[] = {
  23. {
  24. "vphn: no data",
  25. {
  26. 0xffffffffffffffff,
  27. 0xffffffffffffffff,
  28. 0xffffffffffffffff,
  29. 0xffffffffffffffff,
  30. 0xffffffffffffffff,
  31. 0xffffffffffffffff,
  32. },
  33. {
  34. 0x00000000
  35. }
  36. },
  37. {
  38. "vphn: 1 x 16-bit value",
  39. {
  40. 0x8001ffffffffffff,
  41. 0xffffffffffffffff,
  42. 0xffffffffffffffff,
  43. 0xffffffffffffffff,
  44. 0xffffffffffffffff,
  45. 0xffffffffffffffff,
  46. },
  47. {
  48. 0x00000001,
  49. 0x00000001
  50. }
  51. },
  52. {
  53. "vphn: 2 x 16-bit values",
  54. {
  55. 0x80018002ffffffff,
  56. 0xffffffffffffffff,
  57. 0xffffffffffffffff,
  58. 0xffffffffffffffff,
  59. 0xffffffffffffffff,
  60. 0xffffffffffffffff,
  61. },
  62. {
  63. 0x00000002,
  64. 0x00000001,
  65. 0x00000002
  66. }
  67. },
  68. {
  69. "vphn: 3 x 16-bit values",
  70. {
  71. 0x800180028003ffff,
  72. 0xffffffffffffffff,
  73. 0xffffffffffffffff,
  74. 0xffffffffffffffff,
  75. 0xffffffffffffffff,
  76. 0xffffffffffffffff,
  77. },
  78. {
  79. 0x00000003,
  80. 0x00000001,
  81. 0x00000002,
  82. 0x00000003
  83. }
  84. },
  85. {
  86. "vphn: 4 x 16-bit values",
  87. {
  88. 0x8001800280038004,
  89. 0xffffffffffffffff,
  90. 0xffffffffffffffff,
  91. 0xffffffffffffffff,
  92. 0xffffffffffffffff,
  93. 0xffffffffffffffff,
  94. },
  95. {
  96. 0x00000004,
  97. 0x00000001,
  98. 0x00000002,
  99. 0x00000003,
  100. 0x00000004
  101. }
  102. },
  103. {
  104. /* Parsing the next 16-bit value out of the next 64-bit input
  105. * value.
  106. */
  107. "vphn: 5 x 16-bit values",
  108. {
  109. 0x8001800280038004,
  110. 0x8005ffffffffffff,
  111. 0xffffffffffffffff,
  112. 0xffffffffffffffff,
  113. 0xffffffffffffffff,
  114. 0xffffffffffffffff,
  115. },
  116. {
  117. 0x00000005,
  118. 0x00000001,
  119. 0x00000002,
  120. 0x00000003,
  121. 0x00000004,
  122. 0x00000005
  123. }
  124. },
  125. {
  126. /* Parse at most 6 x 64-bit input values */
  127. "vphn: 24 x 16-bit values",
  128. {
  129. 0x8001800280038004,
  130. 0x8005800680078008,
  131. 0x8009800a800b800c,
  132. 0x800d800e800f8010,
  133. 0x8011801280138014,
  134. 0x8015801680178018
  135. },
  136. {
  137. 0x00000018,
  138. 0x00000001,
  139. 0x00000002,
  140. 0x00000003,
  141. 0x00000004,
  142. 0x00000005,
  143. 0x00000006,
  144. 0x00000007,
  145. 0x00000008,
  146. 0x00000009,
  147. 0x0000000a,
  148. 0x0000000b,
  149. 0x0000000c,
  150. 0x0000000d,
  151. 0x0000000e,
  152. 0x0000000f,
  153. 0x00000010,
  154. 0x00000011,
  155. 0x00000012,
  156. 0x00000013,
  157. 0x00000014,
  158. 0x00000015,
  159. 0x00000016,
  160. 0x00000017,
  161. 0x00000018
  162. }
  163. },
  164. {
  165. "vphn: 1 x 32-bit value",
  166. {
  167. 0x00000001ffffffff,
  168. 0xffffffffffffffff,
  169. 0xffffffffffffffff,
  170. 0xffffffffffffffff,
  171. 0xffffffffffffffff,
  172. 0xffffffffffffffff
  173. },
  174. {
  175. 0x00000001,
  176. 0x00000001
  177. }
  178. },
  179. {
  180. "vphn: 2 x 32-bit values",
  181. {
  182. 0x0000000100000002,
  183. 0xffffffffffffffff,
  184. 0xffffffffffffffff,
  185. 0xffffffffffffffff,
  186. 0xffffffffffffffff,
  187. 0xffffffffffffffff
  188. },
  189. {
  190. 0x00000002,
  191. 0x00000001,
  192. 0x00000002
  193. }
  194. },
  195. {
  196. /* Parsing the next 32-bit value out of the next 64-bit input
  197. * value.
  198. */
  199. "vphn: 3 x 32-bit values",
  200. {
  201. 0x0000000100000002,
  202. 0x00000003ffffffff,
  203. 0xffffffffffffffff,
  204. 0xffffffffffffffff,
  205. 0xffffffffffffffff,
  206. 0xffffffffffffffff
  207. },
  208. {
  209. 0x00000003,
  210. 0x00000001,
  211. 0x00000002,
  212. 0x00000003
  213. }
  214. },
  215. {
  216. /* Parse at most 6 x 64-bit input values */
  217. "vphn: 12 x 32-bit values",
  218. {
  219. 0x0000000100000002,
  220. 0x0000000300000004,
  221. 0x0000000500000006,
  222. 0x0000000700000008,
  223. 0x000000090000000a,
  224. 0x0000000b0000000c
  225. },
  226. {
  227. 0x0000000c,
  228. 0x00000001,
  229. 0x00000002,
  230. 0x00000003,
  231. 0x00000004,
  232. 0x00000005,
  233. 0x00000006,
  234. 0x00000007,
  235. 0x00000008,
  236. 0x00000009,
  237. 0x0000000a,
  238. 0x0000000b,
  239. 0x0000000c
  240. }
  241. },
  242. {
  243. "vphn: 16-bit value followed by 32-bit value",
  244. {
  245. 0x800100000002ffff,
  246. 0xffffffffffffffff,
  247. 0xffffffffffffffff,
  248. 0xffffffffffffffff,
  249. 0xffffffffffffffff,
  250. 0xffffffffffffffff
  251. },
  252. {
  253. 0x00000002,
  254. 0x00000001,
  255. 0x00000002
  256. }
  257. },
  258. {
  259. "vphn: 32-bit value followed by 16-bit value",
  260. {
  261. 0x000000018002ffff,
  262. 0xffffffffffffffff,
  263. 0xffffffffffffffff,
  264. 0xffffffffffffffff,
  265. 0xffffffffffffffff,
  266. 0xffffffffffffffff
  267. },
  268. {
  269. 0x00000002,
  270. 0x00000001,
  271. 0x00000002
  272. }
  273. },
  274. {
  275. /* Parse a 32-bit value split accross two consecutives 64-bit
  276. * input values.
  277. */
  278. "vphn: 16-bit value followed by 2 x 32-bit values",
  279. {
  280. 0x8001000000020000,
  281. 0x0003ffffffffffff,
  282. 0xffffffffffffffff,
  283. 0xffffffffffffffff,
  284. 0xffffffffffffffff,
  285. 0xffffffffffffffff
  286. },
  287. {
  288. 0x00000003,
  289. 0x00000001,
  290. 0x00000002,
  291. 0x00000003,
  292. 0x00000004,
  293. 0x00000005
  294. }
  295. },
  296. {
  297. /* The lower bits in 0x0001ffff don't get mixed up with the
  298. * 0xffff terminator.
  299. */
  300. "vphn: 32-bit value has all ones in 16 lower bits",
  301. {
  302. 0x0001ffff80028003,
  303. 0xffffffffffffffff,
  304. 0xffffffffffffffff,
  305. 0xffffffffffffffff,
  306. 0xffffffffffffffff,
  307. 0xffffffffffffffff
  308. },
  309. {
  310. 0x00000003,
  311. 0x0001ffff,
  312. 0x00000002,
  313. 0x00000003
  314. }
  315. },
  316. {
  317. /* The following input doesn't follow the specification.
  318. */
  319. "vphn: last 32-bit value is truncated",
  320. {
  321. 0x0000000100000002,
  322. 0x0000000300000004,
  323. 0x0000000500000006,
  324. 0x0000000700000008,
  325. 0x000000090000000a,
  326. 0x0000000b800c2bad
  327. },
  328. {
  329. 0x0000000c,
  330. 0x00000001,
  331. 0x00000002,
  332. 0x00000003,
  333. 0x00000004,
  334. 0x00000005,
  335. 0x00000006,
  336. 0x00000007,
  337. 0x00000008,
  338. 0x00000009,
  339. 0x0000000a,
  340. 0x0000000b,
  341. 0x0000000c
  342. }
  343. },
  344. {
  345. "vphn: garbage after terminator",
  346. {
  347. 0xffff2bad2bad2bad,
  348. 0x2bad2bad2bad2bad,
  349. 0x2bad2bad2bad2bad,
  350. 0x2bad2bad2bad2bad,
  351. 0x2bad2bad2bad2bad,
  352. 0x2bad2bad2bad2bad
  353. },
  354. {
  355. 0x00000000
  356. }
  357. },
  358. {
  359. NULL
  360. }
  361. };
  362. static int test_one(struct test *test)
  363. {
  364. __be32 output[VPHN_ASSOC_BUFSIZE] = { 0 };
  365. int i, len;
  366. vphn_unpack_associativity(test->input, output);
  367. len = be32_to_cpu(output[0]);
  368. if (len != test->expected[0]) {
  369. printf("expected %d elements, got %d\n", test->expected[0],
  370. len);
  371. return 1;
  372. }
  373. for (i = 1; i < len; i++) {
  374. u32 val = be32_to_cpu(output[i]);
  375. if (val != test->expected[i]) {
  376. printf("element #%d is 0x%x, should be 0x%x\n", i, val,
  377. test->expected[i]);
  378. return 1;
  379. }
  380. }
  381. return 0;
  382. }
  383. static int test_vphn(void)
  384. {
  385. static struct test *test;
  386. for (test = all_tests; test->descr; test++) {
  387. int ret;
  388. ret = test_one(test);
  389. test_finish(test->descr, ret);
  390. if (ret)
  391. return ret;
  392. }
  393. return 0;
  394. }
  395. int main(int argc, char **argv)
  396. {
  397. return test_harness(test_vphn, "test-vphn");
  398. }