1 /* 2 * Copyright 2014 Ran Benita <ran234 (at) gmail.com> 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21 * DEALINGS IN THE SOFTWARE. 22 */ 23 24 #include <assert.h> 25 #include <inttypes.h> 26 #include <stdbool.h> 27 #include <stddef.h> 28 29 #include "utf8.h" 30 31 #define VALID(lit) assert(is_valid_utf8(lit, sizeof(lit)-1)) 32 #define INVALID(lit) assert(!is_valid_utf8(lit, sizeof(lit)-1)) 33 34 static void 35 test_is_valid_utf8(void) 36 { 37 /* 38 * Mostly taken from: 39 * http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt 40 */ 41 42 VALID("ascii"); 43 VALID("\xCE\xBA\xE1\xBD\xB9\xCF\x83\xCE\xBC\xCE\xB5"); 44 45 VALID(""); 46 VALID("\x00"); 47 VALID("\x00\x00"); 48 49 VALID("\x50"); 50 VALID("\xC2\x80"); 51 VALID("\xE0\xA0\x80"); 52 VALID("\xF0\x90\x80\x80"); 53 54 /* 5/6-byte continuations aren't allowed (unlike UTF-8-test). */ 55 INVALID("\xF8\x88\x80\x80\x80"); 56 INVALID("\xFC\x84\x80\x80\x80\x80"); 57 58 VALID("\x7F"); 59 VALID("\xDF\xBF"); 60 VALID("\xEF\xBF\xBF"); 61 /* VALID("\xF7\xBF\xBF\xBF"); */ 62 63 /* 5/6-byte continuations aren't allowed (unlike UTF-8-test). */ 64 INVALID("\xFB\xBF\xBF\xBF\xBF"); 65 INVALID("\xFD\xBFxBF\xBF\xBF"); 66 67 VALID("\xED\x9F\xBF"); 68 VALID("\xEE\x80\x80"); 69 VALID("\xEF\xBF\xBD"); 70 VALID("\xF4\x8F\xBF\xBF"); 71 /* VALID("\xF4\x90\x80\x80"); */ 72 73 INVALID("\x80"); 74 INVALID("\xBF"); 75 INVALID("\x80\xBF"); 76 INVALID("\x80\xBF\x80"); 77 INVALID("\x80\xBF\x80\xBF"); 78 INVALID("\x80\xBF\x80\xBF\x80"); 79 INVALID("\x80\xBF\x80\xBF\x80\xBF"); 80 INVALID("\x80\xBF\x80\xBF\x80\xBF\x80"); 81 INVALID("\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F" 82 "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F" 83 "\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF" 84 "\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF"); 85 86 INVALID("\xC0 \xC1 \xC2 \xC3 \xC4 \xC5 \xC6 \xC7 \xC8 \xC9 \xCA \xCB \xCC " 87 "\xCD \xCE \xCF " 88 "\xD0 \xD1 \xD2 \xD3 \xD4 \xD5 \xD6 \xD7 \xD8 \xD9 \xDA \xDB \xDD " 89 "\xDD \xDE \xDF "); 90 INVALID("\xF0 \xF1 \xF2 \xF3 \xF4 \xF5 \xF6 \xF7 "); 91 INVALID("\xF8 \xF9 \xFA \xFB "); 92 INVALID("\xFC \xFD "); 93 94 INVALID("\xC0"); 95 INVALID("\xE0\x80"); 96 INVALID("\xF0\x80\x80"); 97 INVALID("\xF8\x80\x80\x80"); 98 INVALID("\xFC\x80\x80\x80\x80"); 99 INVALID("\xDF"); 100 INVALID("\xEF\xBF"); 101 INVALID("\xF7\xBF\xBF"); 102 INVALID("\xFB\xBF\xBF\xBF"); 103 INVALID("\xFD\xBF\xBF\xBF\xBF"); 104 105 INVALID("\xC0\xE0\x80\xF0\x80\x80\xF8\x80\x80\x80\xFC\x80\x80\x80\x80" 106 "\xDF\xEF\xBF\xF7\xBF\xBF\xFB\xBF\xBF\xBF\xFD\xBF\xBF\xBF\xBF"); 107 108 INVALID("\xFE"); 109 INVALID("\xFF"); 110 INVALID("\xFE\xFE\xFF\xFF"); 111 112 INVALID("\xC0\xAF"); 113 INVALID("\xE0\x80\xAF"); 114 INVALID("\xF0\x80\x80\xAF"); 115 INVALID("\xF8\x80\x80\x80\xAF"); 116 INVALID("\xFC\x80\x80\x80\x80\xAF"); 117 118 INVALID("\xC1\xBF"); 119 INVALID("\xE0\x9F\xBF"); 120 INVALID("\xF0\x8F\xBF\xBF"); 121 INVALID("\xF8\x87\xBF\xBF\xBF"); 122 INVALID("\xFC\x83\xBF\xBF\xBF\xBF"); 123 124 INVALID("\xC0\x80"); 125 INVALID("\xE0\x80\x80"); 126 INVALID("\xF0\x80\x80\x80"); 127 INVALID("\xF8\x80\x80\x80\x80"); 128 INVALID("\xFC\x80\x80\x80\x80\x80"); 129 130 INVALID("\xED\xA0\x80"); 131 INVALID("\xED\xAD\xBF"); 132 INVALID("\xED\xAE\x80"); 133 INVALID("\xED\xAF\xBF"); 134 INVALID("\xED\xB0\x80"); 135 INVALID("\xED\xBE\x80"); 136 INVALID("\xED\xBF\xBF"); 137 138 INVALID("\xED\xA0\x80\xED\xB0\x80"); 139 INVALID("\xED\xA0\x80\xED\xBF\xBF"); 140 INVALID("\xED\xAD\xBF\xED\xB0\x80"); 141 INVALID("\xED\xAD\xBF\xED\xBF\xBF"); 142 INVALID("\xED\xAE\x80\xED\xB0\x80"); 143 INVALID("\xED\xAE\x80\xED\xBF\xBF"); 144 INVALID("\xED\xAF\xBF\xED\xB0\x80"); 145 INVALID("\xED\xAF\xBF\xED\xBF\xBF"); 146 147 /* INVALID("\xEF\xBF\xBE"); */ 148 /* INVALID("\xEF\xBF\xBF"); */ 149 } 150 151 int 152 main(void) 153 { 154 test_is_valid_utf8(); 155 156 return 0; 157 } 158