1 /* 2 * Copyright (c) 2014 Lauri Tirkkonen <lotheac@iki.fi> 3 * 4 * Permission to use, copy, modify, and distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17 /* 18 * This program tests that the characters defined in the POSIX-1.2008 Portable 19 * Character Set are classified correctly by the iswctype and isw* functions. 20 */ 21 22 #include <stdlib.h> 23 #include <stdio.h> 24 #include <wchar.h> 25 #include <wctype.h> 26 #include <locale.h> 27 #include <err.h> 28 #include "test_common.h" 29 30 wint_t upper_should[] = L"ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 31 wint_t lower_should[] = L"abcdefghijklmnopqrstuvwxyz"; 32 wint_t digit_should[] = L"0123456789"; 33 wint_t space_should[] = L"\t\n\v\f\r "; 34 wint_t cntrl_should[] = L"\a\b\t\n\v\f\r\0\001\002\003\004\005\006\016\017\020" 35 "\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037"; 36 wint_t punct_should[] = L"!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; 37 wint_t xdigit_should[] = L"0123456789ABCDEFabcdef"; 38 wint_t blank_should[] = L" \t"; 39 wint_t only_space_should[] = L" "; 40 41 #define test_ctype_subset(x, y) do {\ 42 test_t t = test_start(#x "_should is subset of " #y);\ 43 wctype_t class = wctype(#y);\ 44 if (!class) test_failed(t, "wctype(\"%s\") returned 0", #y);\ 45 size_t nchars = (sizeof (x ## _should) / sizeof (*x ## _should)) - 1;\ 46 for (wint_t *wc = x ## _should; wc < x ## _should + nchars; wc++) {\ 47 if (!iswctype(*wc, class))\ 48 test_failed(t, "iswctype(L'%lc', wctype(\"%s\"))"\ 49 "returned 0", *wc, #y);\ 50 if (!isw ## y(*wc))\ 51 test_failed(t, "isw%s(L'%lc') returned 0", #y, *wc);\ 52 }\ 53 test_passed(t);\ 54 } while (*"\0") 55 56 #define test_ctype(x) test_ctype_subset(x, x) 57 58 int main(void) { 59 if (!setlocale(LC_CTYPE, "POSIX")) 60 err(1, "setlocale POSIX failed"); 61 test_ctype(upper); 62 test_ctype(lower); 63 test_ctype(digit); 64 test_ctype(space); 65 test_ctype(cntrl); 66 test_ctype(punct); 67 test_ctype(xdigit); 68 test_ctype(blank); 69 70 test_ctype_subset(upper, alpha); 71 test_ctype_subset(lower, alpha); 72 test_ctype_subset(upper, alnum); 73 test_ctype_subset(lower, alnum); 74 test_ctype_subset(digit, alnum); 75 test_ctype_subset(upper, print); 76 test_ctype_subset(lower, print); 77 test_ctype_subset(digit, print); 78 test_ctype_subset(punct, print); 79 test_ctype_subset(only_space, print); 80 test_ctype_subset(upper, graph); 81 test_ctype_subset(lower, graph); 82 test_ctype_subset(digit, graph); 83 test_ctype_subset(punct, graph); 84 return (0); 85 } 86