---
 mingw-w64-crt/testcases/Makefile.am  |   1 +
 mingw-w64-crt/testcases/t_wcstok_s.c | 101 +++++++++++++++++++++++++++
 2 files changed, 102 insertions(+)
 create mode 100644 mingw-w64-crt/testcases/t_wcstok_s.c

diff --git a/mingw-w64-crt/testcases/Makefile.am 
b/mingw-w64-crt/testcases/Makefile.am
index 464affb41ce6..7580b971b898 100644
--- a/mingw-w64-crt/testcases/Makefile.am
+++ b/mingw-w64-crt/testcases/Makefile.am
@@ -64,6 +64,7 @@ testcase_progs = \
   t_vsscanf \
   t_wcrtomb \
   t_wcsrtombs \
+  t_wcstok_s \
   t_wctob \
   t_wreaddir \
   t_fseeko64
diff --git a/mingw-w64-crt/testcases/t_wcstok_s.c 
b/mingw-w64-crt/testcases/t_wcstok_s.c
new file mode 100644
index 000000000000..a9ce9f3dc9d8
--- /dev/null
+++ b/mingw-w64-crt/testcases/t_wcstok_s.c
@@ -0,0 +1,101 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <wchar.h>
+#include <assert.h>
+#include <errno.h>
+
+static int handler_called = 0;
+static int handler_errno = -1;
+static void handler(const wchar_t *expression __attribute__((unused)), const 
wchar_t *function __attribute__((unused)), const wchar_t *file 
__attribute__((unused)), unsigned int line __attribute__((unused)), uintptr_t 
reserved __attribute__((unused))) {
+    handler_called = 1;
+    handler_errno = errno;
+}
+
+int main() {
+    const wchar_t input[] = L"ABC CDE FGH";
+    wchar_t wcs[sizeof(input)/sizeof(input[0])];
+    wchar_t *state;
+    wchar_t *token;
+
+    _set_invalid_parameter_handler(handler);
+
+    /* wcstok normal usage without errors */
+    handler_called = 0;
+    handler_errno = -1;
+    errno = -1;
+    memcpy(wcs, input, sizeof(input));
+    token = wcstok(wcs, L" ", &state);
+    assert(wcscmp(token, L"ABC") == 0);
+    token = wcstok(NULL, L" ", &state);
+    assert(wcscmp(token, L"CDE") == 0);
+    token = wcstok(NULL, L" ", &state);
+    assert(wcscmp(token, L"FGH") == 0);
+    token = wcstok(NULL, L" ", &state);
+    assert(!token);
+    assert(errno == -1);
+    assert(handler_called == 0);
+    assert(handler_errno == -1);
+
+    /* wcstok for NULL string and state returns NULL and does not set errno 
and does not call handler */
+    handler_called = 0;
+    handler_errno = -1;
+    errno = -1;
+    state = NULL;
+    token = wcstok(NULL, L"", &state);
+    assert(!token);
+    assert(errno == -1);
+    assert(handler_called == 0);
+    assert(handler_errno == -1);
+
+    /* wcstok_s normal usage without errors */
+    handler_called = 0;
+    handler_errno = -1;
+    errno = -1;
+    memcpy(wcs, input, sizeof(input));
+    token = wcstok_s(wcs, L" ", &state);
+    assert(wcscmp(token, L"ABC") == 0);
+    token = wcstok_s(NULL, L" ", &state);
+    assert(wcscmp(token, L"CDE") == 0);
+    token = wcstok_s(NULL, L" ", &state);
+    assert(wcscmp(token, L"FGH") == 0);
+    token = wcstok_s(NULL, L" ", &state);
+    assert(!token);
+    assert(errno == -1);
+    assert(handler_called == 0);
+    assert(handler_errno == -1);
+
+    /* wcstok_s for NULL string and state returns NULL and sets errno and 
calls handler */
+    handler_called = 0;
+    handler_errno = -1;
+    errno = -1;
+    state = NULL;
+    token = wcstok_s(NULL, L" ", &state);
+    assert(!token);
+    assert(errno == EINVAL);
+    assert(handler_called == 1);
+    assert(handler_errno == EINVAL);
+
+    /* wcstok_s validates parameters and on error sets errno and calls handler 
*/
+    handler_called = 0;
+    handler_errno = -1;
+    memcpy(wcs, input, sizeof(input));
+    errno = -1;
+    token = wcstok_s(wcs, L" ", NULL);
+    assert(!token);
+    assert(errno == EINVAL);
+    assert(handler_called == 1);
+    assert(handler_errno == EINVAL);
+
+    handler_called = 0;
+    handler_errno = -1;
+    memcpy(wcs, input, sizeof(input));
+    errno = -1;
+    state = NULL;
+    token = wcstok_s(wcs, NULL, &state);
+    assert(!token);
+    assert(errno == EINVAL);
+    assert(handler_called == 1);
+    assert(handler_errno == EINVAL);
+
+    return 0;
+}
-- 
2.20.1



_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to