1189140Sdas/*-
2189140Sdas * Copyright (c) 2009 David Schultz <das@FreeBSD.org>
3189140Sdas * All rights reserved.
4189140Sdas *
5189140Sdas * Redistribution and use in source and binary forms, with or without
6189140Sdas * modification, are permitted provided that the following conditions
7189140Sdas * are met:
8189140Sdas * 1. Redistributions of source code must retain the above copyright
9189140Sdas *    notice, this list of conditions and the following disclaimer.
10189140Sdas * 2. Redistributions in binary form must reproduce the above copyright
11189140Sdas *    notice, this list of conditions and the following disclaimer in the
12189140Sdas *    documentation and/or other materials provided with the distribution.
13189140Sdas *
14189140Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15189140Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16189140Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17189140Sdas * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18189140Sdas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19189140Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20189140Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21189140Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22189140Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23189140Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24189140Sdas * SUCH DAMAGE.
25189140Sdas */
26189140Sdas
27189140Sdas#include <sys/cdefs.h>
28189140Sdas__FBSDID("$FreeBSD$");
29189140Sdas
30189140Sdas#include <sys/mman.h>
31189140Sdas#include <sys/param.h>
32189140Sdas#include <assert.h>
33189140Sdas#include <stdio.h>
34189140Sdas#include <stdlib.h>
35189140Sdas#include <string.h>
36189140Sdas#include <wchar.h>
37189140Sdas
38189140Sdasstatic void *
39189140Sdasmakebuf(size_t len, int guard_at_end)
40189140Sdas{
41189140Sdas	char *buf;
42189140Sdas	size_t alloc_size = roundup2(len, PAGE_SIZE) + PAGE_SIZE;
43189140Sdas
44189140Sdas	buf = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
45189140Sdas	assert(buf);
46189140Sdas	if (guard_at_end) {
47189140Sdas		assert(munmap(buf + alloc_size - PAGE_SIZE, PAGE_SIZE) == 0);
48189140Sdas		return (buf + alloc_size - PAGE_SIZE - len);
49189140Sdas	} else {
50189140Sdas		assert(munmap(buf, PAGE_SIZE) == 0);
51189140Sdas		return (buf + PAGE_SIZE);
52189140Sdas	}
53189140Sdas}
54189140Sdas
55189140Sdasstatic void
56189140Sdastest_wcsnlen(const wchar_t *s)
57189140Sdas{
58189140Sdas	wchar_t *s1;
59189140Sdas	size_t size, len, bufsize;
60189140Sdas	int i;
61189140Sdas
62189140Sdas	size = wcslen(s) + 1;
63189140Sdas	for (i = 0; i <= 1; i++) {
64189140Sdas	    for (bufsize = 0; bufsize <= size + 10; bufsize++) {
65189140Sdas		s1 = makebuf(bufsize * sizeof(wchar_t), i);
66189140Sdas		wmemcpy(s1, s, bufsize);
67189140Sdas		len = (size > bufsize) ? bufsize : size - 1;
68189140Sdas		assert(wcsnlen(s1, bufsize) == len);
69189140Sdas	    }
70189140Sdas	}
71189140Sdas}
72189140Sdas
73189140Sdasint
74189140Sdasmain(int argc, char *argv[])
75189140Sdas{
76189140Sdas
77189140Sdas	printf("1..3\n");
78189140Sdas
79189140Sdas	test_wcsnlen(L"");
80189140Sdas	printf("ok 1 - wcsnlen\n");
81189140Sdas	test_wcsnlen(L"foo");
82189140Sdas	printf("ok 2 - wcsnlen\n");
83189140Sdas	test_wcsnlen(L"glorp");
84189140Sdas	printf("ok 3 - wcsnlen\n");
85189140Sdas
86189140Sdas	exit(0);
87189140Sdas}
88