1*f2d34afaSRobert Mustacchi /*
2*f2d34afaSRobert Mustacchi * This file and its contents are supplied under the terms of the
3*f2d34afaSRobert Mustacchi * Common Development and Distribution License ("CDDL"), version 1.0.
4*f2d34afaSRobert Mustacchi * You may only use this file in accordance with the terms of version
5*f2d34afaSRobert Mustacchi * 1.0 of the CDDL.
6*f2d34afaSRobert Mustacchi *
7*f2d34afaSRobert Mustacchi * A full copy of the text of the CDDL should have accompanied this
8*f2d34afaSRobert Mustacchi * source. A copy of the CDDL is also available via the Internet at
9*f2d34afaSRobert Mustacchi * http://www.illumos.org/license/CDDL.
10*f2d34afaSRobert Mustacchi */
11*f2d34afaSRobert Mustacchi
12*f2d34afaSRobert Mustacchi /*
13*f2d34afaSRobert Mustacchi * Copyright 2016 Joyent, Inc.
14*f2d34afaSRobert Mustacchi */
15*f2d34afaSRobert Mustacchi
16*f2d34afaSRobert Mustacchi /*
17*f2d34afaSRobert Mustacchi * General tests for wcsncasecmp(). Test to make sure that the following are
18*f2d34afaSRobert Mustacchi * true:
19*f2d34afaSRobert Mustacchi *
20*f2d34afaSRobert Mustacchi * o Two identical strings are equal
21*f2d34afaSRobert Mustacchi * o Two strings with the same contents are equal
22*f2d34afaSRobert Mustacchi * o Case insensitive in ASCII works
23*f2d34afaSRobert Mustacchi * o Basic cases where strings aren't equal
24*f2d34afaSRobert Mustacchi * o An ASCII string that would compare greater due to case is properly less
25*f2d34afaSRobert Mustacchi * o Comparing with zero characters succeeds even if different strings
26*f2d34afaSRobert Mustacchi * o Characters in a locale / language that don't have a notion of case are
27*f2d34afaSRobert Mustacchi * consistent
28*f2d34afaSRobert Mustacchi */
29*f2d34afaSRobert Mustacchi
30*f2d34afaSRobert Mustacchi #include <wchar.h>
31*f2d34afaSRobert Mustacchi #include <string.h>
32*f2d34afaSRobert Mustacchi #include <strings.h>
33*f2d34afaSRobert Mustacchi #include <stdlib.h>
34*f2d34afaSRobert Mustacchi #include <locale.h>
35*f2d34afaSRobert Mustacchi #include <sys/debug.h>
36*f2d34afaSRobert Mustacchi
37*f2d34afaSRobert Mustacchi int
main(void)38*f2d34afaSRobert Mustacchi main(void)
39*f2d34afaSRobert Mustacchi {
40*f2d34afaSRobert Mustacchi int ret;
41*f2d34afaSRobert Mustacchi wchar_t a[32], b[32];
42*f2d34afaSRobert Mustacchi const char *str = "kefka";
43*f2d34afaSRobert Mustacchi const char *caps = "KEFKA";
44*f2d34afaSRobert Mustacchi const char *less = "celes";
45*f2d34afaSRobert Mustacchi const char *more = "terra";
46*f2d34afaSRobert Mustacchi const char *hikari = "光";
47*f2d34afaSRobert Mustacchi const char *awake = "目覚め";
48*f2d34afaSRobert Mustacchi size_t len = strlen(str);
49*f2d34afaSRobert Mustacchi
50*f2d34afaSRobert Mustacchi /*
51*f2d34afaSRobert Mustacchi * Start in en_US.UTF-8, which the test suites deps guarantee is
52*f2d34afaSRobert Mustacchi * present.
53*f2d34afaSRobert Mustacchi */
54*f2d34afaSRobert Mustacchi (void) setlocale(LC_ALL, "en_US.UTF-8");
55*f2d34afaSRobert Mustacchi (void) memset(a, 'a', sizeof (a));
56*f2d34afaSRobert Mustacchi (void) memset(b, 'b', sizeof (b));
57*f2d34afaSRobert Mustacchi
58*f2d34afaSRobert Mustacchi ret = mbstowcs(a, str, len);
59*f2d34afaSRobert Mustacchi VERIFY3U(ret, ==, len);
60*f2d34afaSRobert Mustacchi ret = mbstowcs(b, str, len);
61*f2d34afaSRobert Mustacchi VERIFY3U(ret, ==, len);
62*f2d34afaSRobert Mustacchi
63*f2d34afaSRobert Mustacchi VERIFY0(wcsncasecmp(a, a, len));
64*f2d34afaSRobert Mustacchi VERIFY0(wcsncasecmp(a, b, len));
65*f2d34afaSRobert Mustacchi
66*f2d34afaSRobert Mustacchi ret = mbstowcs(b, caps, len);
67*f2d34afaSRobert Mustacchi VERIFY3U(ret, ==, len);
68*f2d34afaSRobert Mustacchi VERIFY0(wcsncasecmp(a, b, len));
69*f2d34afaSRobert Mustacchi
70*f2d34afaSRobert Mustacchi ret = mbstowcs(b, less, len);
71*f2d34afaSRobert Mustacchi VERIFY3U(ret, ==, len);
72*f2d34afaSRobert Mustacchi VERIFY3S(wcsncasecmp(a, b, len), >, 0);
73*f2d34afaSRobert Mustacchi
74*f2d34afaSRobert Mustacchi ret = mbstowcs(b, more, len);
75*f2d34afaSRobert Mustacchi VERIFY3U(ret, ==, len);
76*f2d34afaSRobert Mustacchi VERIFY3S(wcsncasecmp(a, b, len), <, 0);
77*f2d34afaSRobert Mustacchi
78*f2d34afaSRobert Mustacchi ret = mbstowcs(a, caps, len);
79*f2d34afaSRobert Mustacchi VERIFY3U(ret, ==, len);
80*f2d34afaSRobert Mustacchi ret = mbstowcs(b, less, len);
81*f2d34afaSRobert Mustacchi VERIFY3U(ret, ==, len);
82*f2d34afaSRobert Mustacchi VERIFY3S(wcsncmp(a, b, len), <, 0);
83*f2d34afaSRobert Mustacchi VERIFY3S(wcsncasecmp(a, b, len), >, 0);
84*f2d34afaSRobert Mustacchi
85*f2d34afaSRobert Mustacchi VERIFY3S(wcsncasecmp(a, b, 0), ==, 0);
86*f2d34afaSRobert Mustacchi
87*f2d34afaSRobert Mustacchi /*
88*f2d34afaSRobert Mustacchi * This locale is also guaranteed by the test suite.
89*f2d34afaSRobert Mustacchi */
90*f2d34afaSRobert Mustacchi (void) setlocale(LC_ALL, "ja_JP.UTF-8");
91*f2d34afaSRobert Mustacchi ret = mbstowcs(a, hikari, sizeof (a));
92*f2d34afaSRobert Mustacchi VERIFY3U(ret, >, 0);
93*f2d34afaSRobert Mustacchi ret = mbstowcs(b, hikari, sizeof (b));
94*f2d34afaSRobert Mustacchi VERIFY3U(ret, >, 0);
95*f2d34afaSRobert Mustacchi VERIFY3S(wcsncasecmp(a, b, 1), ==, 0);
96*f2d34afaSRobert Mustacchi
97*f2d34afaSRobert Mustacchi ret = mbstowcs(b, awake, sizeof (b));
98*f2d34afaSRobert Mustacchi VERIFY3U(ret, >, 0);
99*f2d34afaSRobert Mustacchi VERIFY3S(wcsncasecmp(a, b, 1), !=, 0);
100*f2d34afaSRobert Mustacchi
101*f2d34afaSRobert Mustacchi return (0);
102*f2d34afaSRobert Mustacchi }
103