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