|
root / base / usr / src / test / libc-tests / tests / wcsncasecmp.c
wcsncasecmp.c C 103 lines 2.6 KB
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2016 Joyent, Inc.
 */

/*
 * General tests for wcsncasecmp(). Test to make sure that the following are
 * true:
 *
 *   o Two identical strings are equal
 *   o Two strings with the same contents are equal
 *   o Case insensitive in ASCII works
 *   o Basic cases where strings aren't equal
 *   o An ASCII string that would compare greater due to case is properly less
 *   o Comparing with zero characters succeeds even if different strings
 *   o Characters in a locale / language that don't have a notion of case are
 *     consistent
 */

#include <wchar.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <locale.h>
#include <sys/debug.h>

int
main(void)
{
	int ret;
	wchar_t a[32], b[32];
	const char *str = "kefka";
	const char *caps = "KEFKA";
	const char *less = "celes";
	const char *more = "terra";
	const char *hikari = "光";
	const char *awake = "目覚め";
	size_t len = strlen(str);

	/*
	 * Start in en_US.UTF-8, which the test suites deps guarantee is
	 * present.
	 */
	(void) setlocale(LC_ALL, "en_US.UTF-8");
	(void) memset(a, 'a', sizeof (a));
	(void) memset(b, 'b', sizeof (b));

	ret = mbstowcs(a, str, len);
	VERIFY3U(ret, ==, len);
	ret = mbstowcs(b, str, len);
	VERIFY3U(ret, ==, len);

	VERIFY0(wcsncasecmp(a, a, len));
	VERIFY0(wcsncasecmp(a, b, len));

	ret = mbstowcs(b, caps, len);
	VERIFY3U(ret, ==, len);
	VERIFY0(wcsncasecmp(a, b, len));

	ret = mbstowcs(b, less, len);
	VERIFY3U(ret, ==, len);
	VERIFY3S(wcsncasecmp(a, b, len), >, 0);

	ret = mbstowcs(b, more, len);
	VERIFY3U(ret, ==, len);
	VERIFY3S(wcsncasecmp(a, b, len), <, 0);

	ret = mbstowcs(a, caps, len);
	VERIFY3U(ret, ==, len);
	ret = mbstowcs(b, less, len);
	VERIFY3U(ret, ==, len);
	VERIFY3S(wcsncmp(a, b, len), <, 0);
	VERIFY3S(wcsncasecmp(a, b, len), >, 0);

	VERIFY3S(wcsncasecmp(a, b, 0), ==, 0);

	/*
	 * This locale is also guaranteed by the test suite.
	 */
	(void) setlocale(LC_ALL, "ja_JP.UTF-8");
	ret = mbstowcs(a, hikari, sizeof (a));
	VERIFY3U(ret, >, 0);
	ret = mbstowcs(b, hikari, sizeof (b));
	VERIFY3U(ret, >, 0);
	VERIFY3S(wcsncasecmp(a, b, 1), ==, 0);

	ret = mbstowcs(b, awake, sizeof (b));
	VERIFY3U(ret, >, 0);
	VERIFY3S(wcsncasecmp(a, b, 1), !=, 0);

	return (0);
}