Merge my current work done on the kd++ branch:
[reactos.git] / rostests / apitests / crt / _vsnprintf.c
1 /*
2 * PROJECT: ReactOS api tests
3 * LICENSE: GPL - See COPYING in the top level directory
4 * PURPOSE: Test for _vsnprintf
5 */
6
7 #define WIN32_NO_STATUS
8 #include <stdio.h>
9 #include <wine/test.h>
10 #include <tchar.h>
11 #include <pseh/pseh2.h>
12 #include <ndk/mmfuncs.h>
13 #include <ndk/rtlfuncs.h>
14
15 #define StartSeh() ExceptionStatus = STATUS_SUCCESS; _SEH2_TRY {
16 #define EndSeh(ExpectedStatus) } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) { ExceptionStatus = _SEH2_GetExceptionCode(); } _SEH2_END; ok(ExceptionStatus == ExpectedStatus, "Exception %lx, expected %lx\n", ExceptionStatus, ExpectedStatus)
17
18 static void call_varargs(char* buf, size_t buf_size, int expected_ret, LPCSTR formatString, ...)
19 {
20 va_list args;
21 int ret;
22 /* Test the basic functionality */
23 va_start(args, formatString);
24 ret = _vsnprintf(buf, 255, formatString, args);
25 ok(expected_ret == ret, "Test failed: expected %i, got %i.\n", expected_ret, ret);
26 }
27
28 START_TEST(_vsnprintf)
29 {
30 char buffer[255];
31 NTSTATUS ExceptionStatus;
32 /* Here you can mix wide and ANSI strings */
33 call_varargs(buffer, 255, 12, "%S world!", L"hello");
34 call_varargs(buffer, 255, 12, "%s world!", "hello");
35 call_varargs(buffer, 255, 11, "%u cookies", 100);
36 /* This is how WINE implements _vcsprintf, and they are obviously wrong */
37 StartSeh()
38 call_varargs(NULL, INT_MAX, -1, "%s it really work?", "does");
39 #if defined(TEST_CRTDLL) || defined(TEST_USER32)
40 EndSeh(STATUS_ACCESS_VIOLATION);
41 #else
42 EndSeh(STATUS_SUCCESS);
43 #endif
44 #if defined(TEST_MSVCRT) /* NTDLL doesn't use/set errno */
45 ok(errno == EINVAL, "Expected EINVAL, got %u\n", errno);
46 #endif
47 /* This one is no better */
48 StartSeh()
49 call_varargs(NULL, 0, -1, "%s it really work?", "does");
50 #if defined(TEST_CRTDLL) || defined(TEST_USER32)
51 EndSeh(STATUS_ACCESS_VIOLATION);
52 #else
53 EndSeh(STATUS_SUCCESS);
54 #endif
55 #if defined(TEST_MSVCRT) /* NTDLL doesn't use/set errno */
56 ok(errno == EINVAL, "Expected EINVAL, got %u\n", errno);
57 #endif
58 /* One more NULL checks */
59 StartSeh()
60 call_varargs(buffer, 255, -1, NULL);
61 #if defined(TEST_CRTDLL)
62 EndSeh(STATUS_ACCESS_VIOLATION);
63 #else
64 EndSeh(STATUS_SUCCESS);
65 #endif
66 #if defined(TEST_MSVCRT) /* NTDLL doesn't use/set errno */
67 ok(errno == EINVAL, "Expected EINVAL, got %u\n", errno);
68 #endif
69 }