usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-wgetcwd-lgpl.c
blob95932565c534f5dfce797faae78aa79d21b4b670
1 /* Test of wgetcwd() function.
2 Copyright (C) 2009-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 #include <config.h>
19 #include <wchar.h>
21 #include "signature.h"
22 #if defined _WIN32 && !defined __CYGWIN__
23 SIGNATURE_CHECK (wgetcwd, wchar_t *, (wchar_t *, size_t));
24 #endif
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
30 #include "macros.h"
32 int
33 main ()
35 #if defined _WIN32 && !defined __CYGWIN__
36 wchar_t *pwd1;
37 wchar_t *pwd2;
39 pwd1 = wgetcwd (NULL, 0);
40 ASSERT (pwd1 && *pwd1);
42 /* Make sure the result is usable. */
43 ASSERT (_wchdir (pwd1) == 0);
44 ASSERT (_wchdir (L".//./.") == 0);
46 /* Make sure that result is normalized. */
47 pwd2 = wgetcwd (NULL, 0);
48 ASSERT (pwd2);
49 ASSERT (wcscmp (pwd1, pwd2) == 0);
50 free (pwd2);
52 size_t len = wcslen (pwd1);
53 ssize_t i = len - 10;
54 if (i < 1)
55 i = 1;
56 pwd2 = wgetcwd (NULL, len + 1);
57 ASSERT (pwd2);
58 free (pwd2);
59 pwd2 = malloc ((len + 3) * sizeof (wchar_t));
60 for ( ; i <= len; i++)
62 wchar_t *tmp;
63 errno = 0;
64 ASSERT (wgetcwd (pwd2, i) == NULL);
65 ASSERT (errno == ERANGE);
66 /* Allow either glibc or BSD behavior, since POSIX allows both. */
67 errno = 0;
68 tmp = wgetcwd (NULL, i);
69 if (tmp)
71 ASSERT (wcscmp (pwd1, tmp) == 0);
72 free (tmp);
74 else
76 ASSERT (errno == ERANGE);
79 /* The SIZE argument here needs to be len + 3, not len + 1. */
80 ASSERT (wgetcwd (pwd2, len + 3) == pwd2);
81 pwd2[len] = L'/';
82 pwd2[len + 1] = L'\0';
84 ASSERT (wcsstr (pwd2, L"/./") == NULL);
85 ASSERT (wcsstr (pwd2, L"/../") == NULL);
86 ASSERT (wcsstr (pwd2 + 1 + (pwd2[1] == L'/'), L"//") == NULL);
88 /* Validate a POSIX requirement on size. */
89 errno = 0;
90 ASSERT (wgetcwd (pwd2, 0) == NULL);
91 ASSERT (errno == EINVAL);
93 free (pwd1);
94 free (pwd2);
95 #endif
97 return test_exit_status;