unicode_ut.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Unit tests for Unicode functions
  4. *
  5. * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
  6. */
  7. #include <common.h>
  8. #include <charset.h>
  9. #include <command.h>
  10. #include <errno.h>
  11. #include <log.h>
  12. #include <malloc.h>
  13. #include <test/test.h>
  14. #include <test/suites.h>
  15. #include <test/ut.h>
  16. /* Linker list entry for a Unicode test */
  17. #define UNICODE_TEST(_name) UNIT_TEST(_name, 0, unicode_test)
  18. /* Constants c1-c4 and d1-d4 encode the same letters */
  19. /* Six characters translating to one utf-8 byte each. */
  20. static const u16 c1[] = {0x55, 0x2d, 0x42, 0x6f, 0x6f, 0x74, 0x00};
  21. /* One character translating to two utf-8 bytes */
  22. static const u16 c2[] = {0x6b, 0x61, 0x66, 0x62, 0xe1, 0x74, 0x75, 0x72, 0x00};
  23. /* Three characters translating to three utf-8 bytes each */
  24. static const u16 c3[] = {0x6f5c, 0x6c34, 0x8266, 0x00};
  25. /* Three letters translating to four utf-8 bytes each */
  26. static const u16 c4[] = {0xd801, 0xdc8d, 0xd801, 0xdc96, 0xd801, 0xdc87,
  27. 0x0000};
  28. /* Illegal utf-16 strings */
  29. static const u16 i1[] = {0x69, 0x31, 0xdc87, 0x6c, 0x00};
  30. static const u16 i2[] = {0x69, 0x32, 0xd801, 0xd801, 0x6c, 0x00};
  31. static const u16 i3[] = {0x69, 0x33, 0xd801, 0x00};
  32. /* Six characters translating to one utf-16 word each. */
  33. static const char d1[] = {0x55, 0x2d, 0x42, 0x6f, 0x6f, 0x74, 0x00};
  34. /* Eight characters translating to one utf-16 word each */
  35. static const char d2[] = {0x6b, 0x61, 0x66, 0x62, 0xc3, 0xa1, 0x74, 0x75,
  36. 0x72, 0x00};
  37. /* Three characters translating to one utf-16 word each */
  38. static const char d3[] = {0xe6, 0xbd, 0x9c, 0xe6, 0xb0, 0xb4, 0xe8, 0x89,
  39. 0xa6, 0x00};
  40. /* Three letters translating to two utf-16 word each */
  41. static const char d4[] = {0xf0, 0x90, 0x92, 0x8d, 0xf0, 0x90, 0x92, 0x96,
  42. 0xf0, 0x90, 0x92, 0x87, 0x00};
  43. /* Illegal utf-8 strings */
  44. static const char j1[] = {0x6a, 0x31, 0xa1, 0x6c, 0x00};
  45. static const char j2[] = {0x6a, 0x32, 0xc3, 0xc3, 0x6c, 0x00};
  46. static const char j3[] = {0x6a, 0x33, 0xf0, 0x90, 0xf0, 0x00};
  47. static int unicode_test_u16_strlen(struct unit_test_state *uts)
  48. {
  49. ut_asserteq(6, u16_strlen(c1));
  50. ut_asserteq(8, u16_strlen(c2));
  51. ut_asserteq(3, u16_strlen(c3));
  52. ut_asserteq(6, u16_strlen(c4));
  53. return 0;
  54. }
  55. UNICODE_TEST(unicode_test_u16_strlen);
  56. static int unicode_test_u16_strdup(struct unit_test_state *uts)
  57. {
  58. u16 *copy = u16_strdup(c4);
  59. ut_assert(copy != c4);
  60. ut_asserteq_mem(copy, c4, sizeof(c4));
  61. free(copy);
  62. return 0;
  63. }
  64. UNICODE_TEST(unicode_test_u16_strdup);
  65. static int unicode_test_u16_strcpy(struct unit_test_state *uts)
  66. {
  67. u16 *r;
  68. u16 copy[10];
  69. r = u16_strcpy(copy, c1);
  70. ut_assert(r == copy);
  71. ut_asserteq_mem(copy, c1, sizeof(c1));
  72. return 0;
  73. }
  74. UNICODE_TEST(unicode_test_u16_strcpy);
  75. /* U-Boot uses UTF-16 strings in the EFI context only. */
  76. #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
  77. static int unicode_test_string16(struct unit_test_state *uts)
  78. {
  79. char buf[20];
  80. /* Test length and precision */
  81. memset(buf, 0xff, sizeof(buf));
  82. sprintf(buf, "%8.6ls", c2);
  83. ut_asserteq(' ', buf[1]);
  84. ut_assert(!strncmp(&buf[2], d2, 7));
  85. ut_assert(!buf[9]);
  86. memset(buf, 0xff, sizeof(buf));
  87. sprintf(buf, "%8.6ls", c4);
  88. ut_asserteq(' ', buf[4]);
  89. ut_assert(!strncmp(&buf[5], d4, 12));
  90. ut_assert(!buf[17]);
  91. memset(buf, 0xff, sizeof(buf));
  92. sprintf(buf, "%-8.2ls", c4);
  93. ut_asserteq(' ', buf[8]);
  94. ut_assert(!strncmp(buf, d4, 8));
  95. ut_assert(!buf[14]);
  96. /* Test handling of illegal utf-16 sequences */
  97. memset(buf, 0xff, sizeof(buf));
  98. sprintf(buf, "%ls", i1);
  99. ut_asserteq_str("i1?l", buf);
  100. memset(buf, 0xff, sizeof(buf));
  101. sprintf(buf, "%ls", i2);
  102. ut_asserteq_str("i2?l", buf);
  103. memset(buf, 0xff, sizeof(buf));
  104. sprintf(buf, "%ls", i3);
  105. ut_asserteq_str("i3?", buf);
  106. return 0;
  107. }
  108. UNICODE_TEST(unicode_test_string16);
  109. #endif
  110. static int unicode_test_utf8_get(struct unit_test_state *uts)
  111. {
  112. const char *s;
  113. s32 code;
  114. int i;
  115. /* Check characters less than 0x800 */
  116. s = d2;
  117. for (i = 0; i < 8; ++i) {
  118. code = utf8_get((const char **)&s);
  119. /* c2 is the utf-8 encoding of d2 */
  120. ut_asserteq(c2[i], code);
  121. if (!code)
  122. break;
  123. }
  124. ut_asserteq_ptr(s, d2 + 9)
  125. /* Check characters less than 0x10000 */
  126. s = d3;
  127. for (i = 0; i < 4; ++i) {
  128. code = utf8_get((const char **)&s);
  129. /* c3 is the utf-8 encoding of d3 */
  130. ut_asserteq(c3[i], code);
  131. if (!code)
  132. break;
  133. }
  134. ut_asserteq_ptr(s, d3 + 9)
  135. /* Check character greater 0xffff */
  136. s = d4;
  137. code = utf8_get((const char **)&s);
  138. ut_asserteq(0x0001048d, code);
  139. ut_asserteq_ptr(s, d4 + 4);
  140. return 0;
  141. }
  142. UNICODE_TEST(unicode_test_utf8_get);
  143. static int unicode_test_utf8_put(struct unit_test_state *uts)
  144. {
  145. char buffer[8] = { 0, };
  146. char *pos;
  147. /* Commercial at, translates to one character */
  148. pos = buffer;
  149. ut_assert(!utf8_put('@', &pos))
  150. ut_asserteq(1, pos - buffer);
  151. ut_asserteq('@', buffer[0]);
  152. ut_assert(!buffer[1]);
  153. /* Latin letter G with acute, translates to two charactes */
  154. pos = buffer;
  155. ut_assert(!utf8_put(0x1f4, &pos));
  156. ut_asserteq(2, pos - buffer);
  157. ut_asserteq_str("\xc7\xb4", buffer);
  158. /* Tagalog letter i, translates to three characters */
  159. pos = buffer;
  160. ut_assert(!utf8_put(0x1701, &pos));
  161. ut_asserteq(3, pos - buffer);
  162. ut_asserteq_str("\xe1\x9c\x81", buffer);
  163. /* Hamster face, translates to four characters */
  164. pos = buffer;
  165. ut_assert(!utf8_put(0x1f439, &pos));
  166. ut_asserteq(4, pos - buffer);
  167. ut_asserteq_str("\xf0\x9f\x90\xb9", buffer);
  168. /* Illegal code */
  169. pos = buffer;
  170. ut_asserteq(-1, utf8_put(0xd888, &pos));
  171. return 0;
  172. }
  173. UNICODE_TEST(unicode_test_utf8_put);
  174. static int unicode_test_utf8_utf16_strlen(struct unit_test_state *uts)
  175. {
  176. ut_asserteq(6, utf8_utf16_strlen(d1));
  177. ut_asserteq(8, utf8_utf16_strlen(d2));
  178. ut_asserteq(3, utf8_utf16_strlen(d3));
  179. ut_asserteq(6, utf8_utf16_strlen(d4));
  180. /* illegal utf-8 sequences */
  181. ut_asserteq(4, utf8_utf16_strlen(j1));
  182. ut_asserteq(4, utf8_utf16_strlen(j2));
  183. ut_asserteq(3, utf8_utf16_strlen(j3));
  184. return 0;
  185. }
  186. UNICODE_TEST(unicode_test_utf8_utf16_strlen);
  187. static int unicode_test_utf8_utf16_strnlen(struct unit_test_state *uts)
  188. {
  189. ut_asserteq(3, utf8_utf16_strnlen(d1, 3));
  190. ut_asserteq(6, utf8_utf16_strnlen(d1, 13));
  191. ut_asserteq(6, utf8_utf16_strnlen(d2, 6));
  192. ut_asserteq(2, utf8_utf16_strnlen(d3, 2));
  193. ut_asserteq(4, utf8_utf16_strnlen(d4, 2));
  194. ut_asserteq(6, utf8_utf16_strnlen(d4, 3));
  195. /* illegal utf-8 sequences */
  196. ut_asserteq(4, utf8_utf16_strnlen(j1, 16));
  197. ut_asserteq(4, utf8_utf16_strnlen(j2, 16));
  198. ut_asserteq(3, utf8_utf16_strnlen(j3, 16));
  199. return 0;
  200. }
  201. UNICODE_TEST(unicode_test_utf8_utf16_strnlen);
  202. /**
  203. * ut_u16_strcmp() - Compare to u16 strings.
  204. *
  205. * @a1: first string
  206. * @a2: second string
  207. * @count: number of u16 to compare
  208. * Return: -1 if a1 < a2, 0 if a1 == a2, 1 if a1 > a2
  209. */
  210. static int unicode_test_u16_strcmp(const u16 *a1, const u16 *a2, size_t count)
  211. {
  212. for (; (*a1 || *a2) && count; ++a1, ++a2, --count) {
  213. if (*a1 < *a2)
  214. return -1;
  215. if (*a1 > *a2)
  216. return 1;
  217. }
  218. return 0;
  219. }
  220. static int unicode_test_utf8_utf16_strcpy(struct unit_test_state *uts)
  221. {
  222. u16 buf[16];
  223. u16 *pos;
  224. pos = buf;
  225. utf8_utf16_strcpy(&pos, d1);
  226. ut_asserteq(6, pos - buf);
  227. ut_assert(!unicode_test_u16_strcmp(buf, c1, SIZE_MAX));
  228. pos = buf;
  229. utf8_utf16_strcpy(&pos, d2);
  230. ut_asserteq(8, pos - buf);
  231. ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX));
  232. pos = buf;
  233. utf8_utf16_strcpy(&pos, d3);
  234. ut_asserteq(3, pos - buf);
  235. ut_assert(!unicode_test_u16_strcmp(buf, c3, SIZE_MAX));
  236. pos = buf;
  237. utf8_utf16_strcpy(&pos, d4);
  238. ut_asserteq(6, pos - buf);
  239. ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX));
  240. /* Illegal utf-8 strings */
  241. pos = buf;
  242. utf8_utf16_strcpy(&pos, j1);
  243. ut_asserteq(4, pos - buf);
  244. ut_assert(!unicode_test_u16_strcmp(buf, L"j1?l", SIZE_MAX));
  245. pos = buf;
  246. utf8_utf16_strcpy(&pos, j2);
  247. ut_asserteq(4, pos - buf);
  248. ut_assert(!unicode_test_u16_strcmp(buf, L"j2?l", SIZE_MAX));
  249. pos = buf;
  250. utf8_utf16_strcpy(&pos, j3);
  251. ut_asserteq(3, pos - buf);
  252. ut_assert(!unicode_test_u16_strcmp(buf, L"j3?", SIZE_MAX));
  253. return 0;
  254. }
  255. UNICODE_TEST(unicode_test_utf8_utf16_strcpy);
  256. static int unicode_test_utf8_utf16_strncpy(struct unit_test_state *uts)
  257. {
  258. u16 buf[16];
  259. u16 *pos;
  260. pos = buf;
  261. memset(buf, 0, sizeof(buf));
  262. utf8_utf16_strncpy(&pos, d1, 4);
  263. ut_asserteq(4, pos - buf);
  264. ut_assert(!buf[4]);
  265. ut_assert(!unicode_test_u16_strcmp(buf, c1, 4));
  266. pos = buf;
  267. memset(buf, 0, sizeof(buf));
  268. utf8_utf16_strncpy(&pos, d2, 10);
  269. ut_asserteq(8, pos - buf);
  270. ut_assert(buf[4]);
  271. ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX));
  272. pos = buf;
  273. memset(buf, 0, sizeof(buf));
  274. utf8_utf16_strncpy(&pos, d3, 2);
  275. ut_asserteq(2, pos - buf);
  276. ut_assert(!buf[2]);
  277. ut_assert(!unicode_test_u16_strcmp(buf, c3, 2));
  278. pos = buf;
  279. memset(buf, 0, sizeof(buf));
  280. utf8_utf16_strncpy(&pos, d4, 2);
  281. ut_asserteq(4, pos - buf);
  282. ut_assert(!buf[4]);
  283. ut_assert(!unicode_test_u16_strcmp(buf, c4, 4));
  284. pos = buf;
  285. memset(buf, 0, sizeof(buf));
  286. utf8_utf16_strncpy(&pos, d4, 10);
  287. ut_asserteq(6, pos - buf);
  288. ut_assert(buf[5]);
  289. ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX));
  290. return 0;
  291. }
  292. UNICODE_TEST(unicode_test_utf8_utf16_strncpy);
  293. static int unicode_test_utf16_get(struct unit_test_state *uts)
  294. {
  295. const u16 *s;
  296. s32 code;
  297. int i;
  298. /* Check characters less than 0x10000 */
  299. s = c2;
  300. for (i = 0; i < 9; ++i) {
  301. code = utf16_get((const u16 **)&s);
  302. ut_asserteq(c2[i], code);
  303. if (!code)
  304. break;
  305. }
  306. ut_asserteq_ptr(c2 + 8, s);
  307. /* Check character greater 0xffff */
  308. s = c4;
  309. code = utf16_get((const u16 **)&s);
  310. ut_asserteq(0x0001048d, code);
  311. ut_asserteq_ptr(c4 + 2, s);
  312. return 0;
  313. }
  314. UNICODE_TEST(unicode_test_utf16_get);
  315. static int unicode_test_utf16_put(struct unit_test_state *uts)
  316. {
  317. u16 buffer[4] = { 0, };
  318. u16 *pos;
  319. /* Commercial at, translates to one word */
  320. pos = buffer;
  321. ut_assert(!utf16_put('@', &pos));
  322. ut_asserteq(1, pos - buffer);
  323. ut_asserteq((u16)'@', buffer[0]);
  324. ut_assert(!buffer[1]);
  325. /* Hamster face, translates to two words */
  326. pos = buffer;
  327. ut_assert(!utf16_put(0x1f439, &pos));
  328. ut_asserteq(2, pos - buffer);
  329. ut_asserteq((u16)0xd83d, buffer[0]);
  330. ut_asserteq((u16)0xdc39, buffer[1]);
  331. ut_assert(!buffer[2]);
  332. /* Illegal code */
  333. pos = buffer;
  334. ut_asserteq(-1, utf16_put(0xd888, &pos));
  335. return 0;
  336. }
  337. UNICODE_TEST(unicode_test_utf16_put);
  338. static int unicode_test_utf16_strnlen(struct unit_test_state *uts)
  339. {
  340. ut_asserteq(3, utf16_strnlen(c1, 3));
  341. ut_asserteq(6, utf16_strnlen(c1, 13));
  342. ut_asserteq(6, utf16_strnlen(c2, 6));
  343. ut_asserteq(2, utf16_strnlen(c3, 2));
  344. ut_asserteq(2, utf16_strnlen(c4, 2));
  345. ut_asserteq(3, utf16_strnlen(c4, 3));
  346. /* illegal utf-16 word sequences */
  347. ut_asserteq(4, utf16_strnlen(i1, 16));
  348. ut_asserteq(4, utf16_strnlen(i2, 16));
  349. ut_asserteq(3, utf16_strnlen(i3, 16));
  350. return 0;
  351. }
  352. UNICODE_TEST(unicode_test_utf16_strnlen);
  353. static int unicode_test_utf16_utf8_strlen(struct unit_test_state *uts)
  354. {
  355. ut_asserteq(6, utf16_utf8_strlen(c1));
  356. ut_asserteq(9, utf16_utf8_strlen(c2));
  357. ut_asserteq(9, utf16_utf8_strlen(c3));
  358. ut_asserteq(12, utf16_utf8_strlen(c4));
  359. /* illegal utf-16 word sequences */
  360. ut_asserteq(4, utf16_utf8_strlen(i1));
  361. ut_asserteq(4, utf16_utf8_strlen(i2));
  362. ut_asserteq(3, utf16_utf8_strlen(i3));
  363. return 0;
  364. }
  365. UNICODE_TEST(unicode_test_utf16_utf8_strlen);
  366. static int unicode_test_utf16_utf8_strnlen(struct unit_test_state *uts)
  367. {
  368. ut_asserteq(3, utf16_utf8_strnlen(c1, 3));
  369. ut_asserteq(6, utf16_utf8_strnlen(c1, 13));
  370. ut_asserteq(7, utf16_utf8_strnlen(c2, 6));
  371. ut_asserteq(6, utf16_utf8_strnlen(c3, 2));
  372. ut_asserteq(8, utf16_utf8_strnlen(c4, 2));
  373. ut_asserteq(12, utf16_utf8_strnlen(c4, 3));
  374. return 0;
  375. }
  376. UNICODE_TEST(unicode_test_utf16_utf8_strnlen);
  377. static int unicode_test_utf16_utf8_strcpy(struct unit_test_state *uts)
  378. {
  379. char buf[16];
  380. char *pos;
  381. pos = buf;
  382. utf16_utf8_strcpy(&pos, c1);
  383. ut_asserteq(6, pos - buf);
  384. ut_asserteq_str(d1, buf);
  385. pos = buf;
  386. utf16_utf8_strcpy(&pos, c2);
  387. ut_asserteq(9, pos - buf);
  388. ut_asserteq_str(d2, buf);
  389. pos = buf;
  390. utf16_utf8_strcpy(&pos, c3);
  391. ut_asserteq(9, pos - buf);
  392. ut_asserteq_str(d3, buf);
  393. pos = buf;
  394. utf16_utf8_strcpy(&pos, c4);
  395. ut_asserteq(12, pos - buf);
  396. ut_asserteq_str(d4, buf);
  397. /* Illegal utf-16 strings */
  398. pos = buf;
  399. utf16_utf8_strcpy(&pos, i1);
  400. ut_asserteq(4, pos - buf);
  401. ut_asserteq_str("i1?l", buf);
  402. pos = buf;
  403. utf16_utf8_strcpy(&pos, i2);
  404. ut_asserteq(4, pos - buf);
  405. ut_asserteq_str("i2?l", buf);
  406. pos = buf;
  407. utf16_utf8_strcpy(&pos, i3);
  408. ut_asserteq(3, pos - buf);
  409. ut_asserteq_str("i3?", buf);
  410. return 0;
  411. }
  412. UNICODE_TEST(unicode_test_utf16_utf8_strcpy);
  413. static int unicode_test_utf16_utf8_strncpy(struct unit_test_state *uts)
  414. {
  415. char buf[16];
  416. char *pos;
  417. pos = buf;
  418. memset(buf, 0, sizeof(buf));
  419. utf16_utf8_strncpy(&pos, c1, 4);
  420. ut_asserteq(4, pos - buf);
  421. ut_assert(!buf[4]);
  422. ut_assert(!strncmp(buf, d1, 4));
  423. pos = buf;
  424. memset(buf, 0, sizeof(buf));
  425. utf16_utf8_strncpy(&pos, c2, 10);
  426. ut_asserteq(9, pos - buf);
  427. ut_assert(buf[4]);
  428. ut_assert(!strncmp(buf, d2, SIZE_MAX));
  429. pos = buf;
  430. memset(buf, 0, sizeof(buf));
  431. utf16_utf8_strncpy(&pos, c3, 2);
  432. ut_asserteq(6, pos - buf);
  433. ut_assert(!buf[6]);
  434. ut_assert(!strncmp(buf, d3, 6));
  435. pos = buf;
  436. memset(buf, 0, sizeof(buf));
  437. utf16_utf8_strncpy(&pos, c4, 2);
  438. ut_asserteq(8, pos - buf);
  439. ut_assert(!buf[8]);
  440. ut_assert(!strncmp(buf, d4, 8));
  441. pos = buf;
  442. memset(buf, 0, sizeof(buf));
  443. utf16_utf8_strncpy(&pos, c4, 10);
  444. ut_asserteq(12, pos - buf);
  445. ut_assert(buf[5]);
  446. ut_assert(!strncmp(buf, d4, SIZE_MAX));
  447. return 0;
  448. }
  449. UNICODE_TEST(unicode_test_utf16_utf8_strncpy);
  450. static int unicode_test_utf_to_lower(struct unit_test_state *uts)
  451. {
  452. ut_asserteq('@', utf_to_lower('@'));
  453. ut_asserteq('a', utf_to_lower('A'));
  454. ut_asserteq('z', utf_to_lower('Z'));
  455. ut_asserteq('[', utf_to_lower('['));
  456. ut_asserteq('m', utf_to_lower('m'));
  457. /* Latin letter O with diaresis (umlaut) */
  458. ut_asserteq(0x00f6, utf_to_lower(0x00d6));
  459. #ifdef CONFIG_EFI_UNICODE_CAPITALIZATION
  460. /* Cyrillic letter I*/
  461. ut_asserteq(0x0438, utf_to_lower(0x0418));
  462. #endif
  463. return 0;
  464. }
  465. UNICODE_TEST(unicode_test_utf_to_lower);
  466. static int unicode_test_utf_to_upper(struct unit_test_state *uts)
  467. {
  468. ut_asserteq('`', utf_to_upper('`'));
  469. ut_asserteq('A', utf_to_upper('a'));
  470. ut_asserteq('Z', utf_to_upper('z'));
  471. ut_asserteq('{', utf_to_upper('{'));
  472. ut_asserteq('M', utf_to_upper('M'));
  473. /* Latin letter O with diaresis (umlaut) */
  474. ut_asserteq(0x00d6, utf_to_upper(0x00f6));
  475. #ifdef CONFIG_EFI_UNICODE_CAPITALIZATION
  476. /* Cyrillic letter I */
  477. ut_asserteq(0x0418, utf_to_upper(0x0438));
  478. #endif
  479. return 0;
  480. }
  481. UNICODE_TEST(unicode_test_utf_to_upper);
  482. static int unicode_test_u16_strncmp(struct unit_test_state *uts)
  483. {
  484. ut_assert(u16_strncmp(L"abc", L"abc", 3) == 0);
  485. ut_assert(u16_strncmp(L"abcdef", L"abcghi", 3) == 0);
  486. ut_assert(u16_strncmp(L"abcdef", L"abcghi", 6) < 0);
  487. ut_assert(u16_strncmp(L"abcghi", L"abcdef", 6) > 0);
  488. ut_assert(u16_strcmp(L"abc", L"abc") == 0);
  489. ut_assert(u16_strcmp(L"abcdef", L"deghi") < 0);
  490. ut_assert(u16_strcmp(L"deghi", L"abcdef") > 0);
  491. return 0;
  492. }
  493. UNICODE_TEST(unicode_test_u16_strncmp);
  494. static int unicode_test_u16_strsize(struct unit_test_state *uts)
  495. {
  496. ut_asserteq_64(u16_strsize(c1), 14);
  497. ut_asserteq_64(u16_strsize(c2), 18);
  498. ut_asserteq_64(u16_strsize(c3), 8);
  499. ut_asserteq_64(u16_strsize(c4), 14);
  500. return 0;
  501. }
  502. UNICODE_TEST(unicode_test_u16_strsize);
  503. int do_ut_unicode(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  504. {
  505. struct unit_test *tests = ll_entry_start(struct unit_test, unicode_test);
  506. const int n_ents = ll_entry_count(struct unit_test, unicode_test);
  507. return cmd_ut_category("Unicode", "unicode_test_",
  508. tests, n_ents, argc, argv);
  509. }