Reactos
at master 131 lines 3.5 kB view raw
1/* 2 * PROJECT: ReactOS api tests 3 * LICENSE: GPL - See COPYING in the top level directory 4 * PURPOSE: Test for _vsnprintf 5 */ 6 7#include <apitest.h> 8 9#define WIN32_NO_STATUS 10#include <stdio.h> 11#include <tchar.h> 12#include <pseh/pseh2.h> 13#include <ndk/mmfuncs.h> 14#include <ndk/rtlfuncs.h> 15 16#ifndef TEST_STATIC_CRT 17 18typedef int (__cdecl *PFN_vsnprintf)(char *buf, size_t cnt, const char *fmt, va_list args); 19static PFN_vsnprintf p_vsnprintf; 20 21static BOOL Init(void) 22{ 23 HMODULE hdll = LoadLibraryA(TEST_DLL_NAME); 24 p_vsnprintf = (PFN_vsnprintf)GetProcAddress(hdll, "_vsnprintf"); 25 ok(p_vsnprintf != NULL, "Failed to load _vsnprintf from %s\n", TEST_DLL_NAME); 26 return (p_vsnprintf != NULL); 27} 28#define _vsnprintf p_vsnprintf 29 30#endif // !TEST_STATIC_CRT 31 32static void call_varargs(char* buf, size_t buf_size, int expected_ret, LPCSTR formatString, ...) 33{ 34 va_list args; 35 int ret; 36 /* Test the basic functionality */ 37 va_start(args, formatString); 38 ret = _vsnprintf(buf, buf_size, formatString, args); 39 va_end(args); 40 ok(expected_ret == ret, "Test failed for `%s`: expected %i, got %i.\n", formatString, expected_ret, ret); 41} 42 43START_TEST(_vsnprintf) 44{ 45 char buffer[255]; 46 47#ifndef TEST_STATIC_CRT 48 if (!Init()) 49 { 50 skip("Skipping tests, because _vsnprintf is not available\n"); 51 return; 52 } 53#endif 54 55 /* Here you can mix wide and ANSI strings */ 56 call_varargs(buffer, 255, 12, "%S world!", L"hello"); 57 call_varargs(buffer, 255, 12, "%s world!", "hello"); 58 call_varargs(buffer, 255, 11, "%u cookies", 100); 59 60 StartSeh() 61#if defined(TEST_CRTDLL)||defined(TEST_USER32) 62 call_varargs(NULL, INT_MAX, -1, "%s it really work?", "does"); 63#else 64 if (GetNTVersion() >= _WIN32_WINNT_VISTA) 65 call_varargs(NULL, INT_MAX, -1, "%s it really work?", "does"); 66 else 67 call_varargs(NULL, INT_MAX, 20, "%s it really work?", "does"); 68#endif 69 70#if defined(TEST_CRTDLL)||defined(TEST_USER32) 71 EndSeh(STATUS_ACCESS_VIOLATION); 72#else 73 EndSeh(STATUS_SUCCESS); 74#endif 75 76#if defined(TEST_USER32) 77 ok_eq_uint(errno, EINVAL); 78#elif defined(TEST_NTDLL) || defined(TEST_CRTDLL) 79 ok_eq_uint(errno, 0); 80#else 81 if (GetNTVersion() >= _WIN32_WINNT_VISTA) 82 ok_eq_uint(errno, ERROR_BAD_COMMAND); 83 else 84 ok_eq_uint(errno, 0); 85#endif 86 87 /* This one is no better */ 88 StartSeh() 89#if defined(TEST_CRTDLL)||defined(TEST_USER32) 90 call_varargs(NULL, 0, -1, "%s it really work?", "does"); 91#else 92 call_varargs(NULL, 0, 20, "%s it really work?", "does"); 93#endif 94 95#if defined(TEST_USER32) 96 EndSeh(STATUS_ACCESS_VIOLATION); 97#else 98 EndSeh(STATUS_SUCCESS); 99#endif 100 101#if defined(TEST_USER32) 102 ok_eq_uint(errno, EINVAL); 103#elif defined(TEST_NTDLL) || defined(TEST_CRTDLL) 104 ok_eq_uint(errno, 0); 105#else 106 if (GetNTVersion() >= _WIN32_WINNT_VISTA) 107 ok_eq_uint(errno, ERROR_BAD_COMMAND); 108 else 109 ok_eq_uint(errno, 0); 110#endif 111 112 /* One more NULL checks */ 113 StartSeh() 114 call_varargs(buffer, 255, -1, NULL); 115#if defined(TEST_CRTDLL) 116 EndSeh(STATUS_ACCESS_VIOLATION); 117#else 118 EndSeh((GetNTVersion() >= _WIN32_WINNT_VISTA) ? 0 : STATUS_ACCESS_VIOLATION); 119#endif 120 121#if defined(TEST_USER32) 122 ok_eq_uint(errno, EINVAL); 123#elif defined(TEST_NTDLL) || defined(TEST_CRTDLL) 124 ok_eq_uint(errno, 0); 125#else 126 if (GetNTVersion() >= _WIN32_WINNT_VISTA) 127 ok_eq_uint(errno, ERROR_BAD_COMMAND); 128 else 129 ok_eq_uint(errno, 0); 130#endif 131}