unicode_ut.c 15 KB

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