1 #include <ccan/charset/charset.c>
2 #include <ccan/tap/tap.h>
9 * Testing procedure for from_surrogate_pair and to_surrogate_pair:
11 * * For each Unicode code point from 0x10000 to 0x10FFFF:
12 * - Call to_surrogate_pair, and make sure that:
14 * - uc is 0xD800..0xDBFF
15 * - lc is 0xDC00..0xDFFF
16 * - Call from_surrogate_pair on the pair, and make sure that
17 * it returns the original character.
18 * * For various invalid arguments to to_surrogate_pair
19 * (U+0000..U+FFFF and U+110000...):
20 * - Call to_surrogate_pair, and make sure it:
22 * - Sets *uc and *lc to REPLACEMENT_CHARACTER.
23 * * For various invalid arguments to from_surrogate_pair
24 * (uc: not 0xD800..0xDBFF, lc: not 0xDC00..0xDFFF):
25 * - Call from_surrogate_pair, and make sure
26 * it returns REPLACEMENT_CHARACTER.
29 #define INVALID_TRIAL_COUNT 10000
31 #define range(r, lo, hi) ((r) % ((hi)-(lo)+1) + (lo))
33 static void test_valid(void)
38 for (unicode = 0x10000; unicode <= 0x10FFFF; unicode++) {
39 if (to_surrogate_pair(unicode, &uc, &lc) != true) {
40 fail("to_surrogate_pair did not return true on valid input.");
43 if (!(uc >= 0xD800 && uc <= 0xDBFF)) {
44 fail("to_surrogate_pair: uc is out of range");
47 if (!(lc >= 0xDC00 && lc <= 0xDFFF)) {
48 fail("to_surrogate_pair: lc is out of range");
51 if (from_surrogate_pair(uc, lc) != unicode) {
52 fail("Surrogate pair conversion did not preserve original value (U+%04lX).", (unsigned long)unicode);
57 pass("to_surrogate_pair and from_surrogate_pair work for all valid arguments.");
60 static void test_invalid_to_surrogate_pair(void)
66 for (i = 1; i <= INVALID_TRIAL_COUNT; i++) {
68 unicode = range(rand32(), 0x0, 0xFFFF);
72 } while (unicode < 0x110000);
75 if (to_surrogate_pair(unicode, &uc, &lc) != false) {
76 fail("to_surrogate_pair did not return false on invalid input.");
79 if (uc != REPLACEMENT_CHARACTER || lc != REPLACEMENT_CHARACTER) {
80 fail("to_surrogate_pair did not set uc and lc to the replacement character on invalid input.");
85 pass("to_surrogate_pair seems to handle invalid argument values properly.");
88 static void test_invalid_from_surrogate_pair(void)
93 for (i = 1; i <= INVALID_TRIAL_COUNT; i++) {
94 switch (rand32() % 3) {
96 uc = range(rand32(), 0x0, 0xD7FF);
99 uc = range(rand32(), 0xDC00, 0xDFFF);
102 uc = range(rand32(), 0xE000, 0xFFFF);
105 switch (rand32() % 3) {
107 lc = range(rand32(), 0x0, 0xD7FF);
110 lc = range(rand32(), 0xD800, 0xDBFF);
113 lc = range(rand32(), 0xE000, 0xFFFF);
117 if (from_surrogate_pair(uc, lc) != REPLACEMENT_CHARACTER) {
118 fail("from_surrogate_pair(0x%04X, 0x%04X) did not return the replacement character", uc, lc);
123 pass("from_surrogate_pair seems to handle invalid arguments properly.");
131 test_invalid_to_surrogate_pair();
132 test_invalid_from_surrogate_pair();
134 return exit_status();