X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=test%2Futf8.c;h=aa3c0d511c7eff925d7faf6b7bf5f8bc31cc099b;hb=63d9d57f04c1b16e370fa28b328fc33489819e07;hp=17c71567892c38c4d481a0010cca0673ece61fc7;hpb=2bbaf7c7d1f2fecbdc3b2ed6cf34c63d117824bd;p=platform%2Fupstream%2Flibxkbcommon.git diff --git a/test/utf8.c b/test/utf8.c index 17c7156..aa3c0d5 100644 --- a/test/utf8.c +++ b/test/utf8.c @@ -21,12 +21,16 @@ * DEALINGS IN THE SOFTWARE. */ +#include "config.h" + #include #include #include #include +#include #include "utf8.h" +#include "utils.h" #define VALID(lit) assert(is_valid_utf8(lit, sizeof(lit)-1)) #define INVALID(lit) assert(!is_valid_utf8(lit, sizeof(lit)-1)) @@ -36,7 +40,7 @@ test_is_valid_utf8(void) { /* * Mostly taken from: - * http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt + * https://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt */ VALID("ascii"); @@ -148,10 +152,36 @@ test_is_valid_utf8(void) /* INVALID("\xEF\xBF\xBF"); */ } +static void +check_utf32_to_utf8(uint32_t unichar, int expected_length, const char *expected) { + char buffer[7]; + int length; + + length = utf32_to_utf8(unichar, buffer); + + assert(length == expected_length); + assert(streq(buffer, expected)); +} + +static void +test_utf32_to_utf8(void) +{ + check_utf32_to_utf8(0x0, 2, ""); + check_utf32_to_utf8(0x40, 2, "\x40"); + check_utf32_to_utf8(0xA1, 3, "\xc2\xa1"); + check_utf32_to_utf8(0x2701, 4, "\xe2\x9c\x81"); + check_utf32_to_utf8(0xd800, 0, ""); // Unicode surrogates + check_utf32_to_utf8(0xdfff, 0, ""); // Unicode surrogates + check_utf32_to_utf8(0x1f004, 5, "\xf0\x9f\x80\x84"); + check_utf32_to_utf8(0x110000, 0, ""); + check_utf32_to_utf8(0xffffffff, 0, ""); +} + int main(void) { test_is_valid_utf8(); + test_utf32_to_utf8(); return 0; }