Something went wrong. Try again.
Reactos
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293/* * PROJECT: ReactOS api tests * LICENSE: GPLv2+ - See COPYING in the top level directory * PURPOSE: Test for sprintf * PROGRAMMER: Thomas Faber <thomas.faber@reactos.org> */
#include <apitest.h>#include <apitest_guard.h>
#define WIN32_NO_STATUS#include <stdio.h>#include <tchar.h>#include <pseh/pseh2.h>#include <ndk/mmfuncs.h>#include <ndk/rtlfuncs.h>
#ifdef _MSC_VER#pragma warning(disable:4778) // unterminated format string '%'#elif defined(__GNUC__)#pragma GCC diagnostic ignored "-Wformat"#pragma GCC diagnostic ignored "-Wformat-zero-length"#pragma GCC diagnostic ignored "-Wnonnull"#if __GNUC__ >= 7#pragma GCC diagnostic ignored "-Wformat-overflow"#endif#endif
#ifndef TEST_STATIC_CRT
typedef int (__cdecl *PFN_sprintf)(char *_Dest, const char *_Format, ...);static PFN_sprintf p_sprintf;
static BOOL Init(void){ HMODULE hdll = LoadLibraryA(TEST_DLL_NAME);#ifdef TEST_USER32 p_sprintf = (PFN_sprintf)GetProcAddress(hdll, "wsprintfA");#else p_sprintf = (PFN_sprintf)GetProcAddress(hdll, "sprintf");#endif ok(p_sprintf != NULL, "Failed to load sprintf from %s\n", TEST_DLL_NAME); return (p_sprintf != NULL);}#define sprintf p_sprintf
#endif // !TEST_STATIC_CRT
/* NOTE: This test is not only used for all the CRT apitests, but also for * user32's wsprintf. Make sure to test them all */START_TEST(sprintf){ int Length; CHAR Buffer[128]; PCHAR String;
#ifndef TEST_STATIC_CRT if (!Init()) { skip("Skipping tests, because sprintf is not available\n"); return; }#endif
/* basic parameter tests */ StartSeh() Length = sprintf(NULL, NULL);#if defined(TEST_CRTDLL) || defined(TEST_USER32) EndSeh(STATUS_ACCESS_VIOLATION);#else EndSeh((GetNTVersion() >= _WIN32_WINNT_VISTA) ? 0 : STATUS_ACCESS_VIOLATION);#endif
StartSeh() Length = sprintf(NULL, ""); ok_int(Length, (GetNTVersion() >= _WIN32_WINNT_VISTA) ? -1 : 0);#if defined(TEST_CRTDLL) || defined(TEST_USER32) EndSeh(STATUS_ACCESS_VIOLATION);#else EndSeh(STATUS_SUCCESS);#endif
StartSeh() Length = sprintf(NULL, "Hello"); ok_int(Length, (GetNTVersion() >= _WIN32_WINNT_VISTA) ? -1 : 5);#if defined(TEST_CRTDLL) || defined(TEST_USER32) EndSeh(STATUS_ACCESS_VIOLATION);#else EndSeh(STATUS_SUCCESS);#endif
/* some basic formats */ Length = sprintf(Buffer, "abcde"); ok_str(Buffer, "abcde"); ok_int(Length, 5);
Length = sprintf(Buffer, "%%"); ok_str(Buffer, "%"); ok_int(Length, 1);
Length = sprintf(Buffer, "%"); ok_str(Buffer, ""); ok_int(Length, 0);
Length = sprintf(Buffer, "%%%"); ok_str(Buffer, "%"); ok_int(Length, 1);
Length = sprintf(Buffer, "%d", 8); ok_str(Buffer, "8"); ok_int(Length, 1);
Length = sprintf(Buffer, "%s", "hello"); ok_str(Buffer, "hello"); ok_int(Length, 5);
/* field width for %s */ Length = sprintf(Buffer, "%8s", "hello"); ok_str(Buffer, " hello"); ok_int(Length, 8);
Length = sprintf(Buffer, "%4s", "hello"); ok_str(Buffer, "hello"); ok_int(Length, 5);
Length = sprintf(Buffer, "%-8s", "hello"); ok_str(Buffer, "hello "); ok_int(Length, 8);
Length = sprintf(Buffer, "%-5s", "hello"); ok_str(Buffer, "hello"); ok_int(Length, 5);
Length = sprintf(Buffer, "%0s", "hello"); ok_str(Buffer, "hello"); ok_int(Length, 5);
Length = sprintf(Buffer, "%-0s", "hello"); ok_str(Buffer, "hello"); ok_int(Length, 5);
Length = sprintf(Buffer, "%*s", -8, "hello");#ifdef TEST_USER32 ok_str(Buffer, "*s"); ok_int(Length, 2);#else ok_str(Buffer, "hello "); ok_int(Length, 8);#endif
/* precision for %s */ Length = sprintf(Buffer, "%.s", "hello"); ok_str(Buffer, ""); ok_int(Length, 0);
Length = sprintf(Buffer, "%.0s", "hello"); ok_str(Buffer, ""); ok_int(Length, 0);
Length = sprintf(Buffer, "%.10s", "hello"); ok_str(Buffer, "hello"); ok_int(Length, 5);
Length = sprintf(Buffer, "%.5s", "hello"); ok_str(Buffer, "hello"); ok_int(Length, 5);
Length = sprintf(Buffer, "%.4s", "hello"); ok_str(Buffer, "hell"); ok_int(Length, 4);
StartSeh() Length = sprintf(Buffer, "%.*s", -1, "hello");#ifdef TEST_USER32 ok_str(Buffer, "*s"); ok_int(Length, 2);#else ok_str(Buffer, "hello"); ok_int(Length, 5);#endif EndSeh(STATUS_SUCCESS);
String = AllocateGuarded(6); if (!String) { skip("Guarded allocation failure\n"); return; }
strcpy(String, "hello"); StartSeh() Length = sprintf(Buffer, "%.8s", String); ok_str(Buffer, "hello"); ok_int(Length, 5); EndSeh(STATUS_SUCCESS);
StartSeh() Length = sprintf(Buffer, "%.6s", String); ok_str(Buffer, "hello"); ok_int(Length, 5); EndSeh(STATUS_SUCCESS);
StartSeh() Length = sprintf(Buffer, "%.5s", String); ok_str(Buffer, "hello"); ok_int(Length, 5); EndSeh(STATUS_SUCCESS);
StartSeh() Length = sprintf(Buffer, "%.4s", String); ok_str(Buffer, "hell"); ok_int(Length, 4); EndSeh(STATUS_SUCCESS);
String[5] = '!'; StartSeh() Length = sprintf(Buffer, "%.5s", String); ok_str(Buffer, "hello"); ok_int(Length, 5);#ifdef TEST_USER32 EndSeh(STATUS_ACCESS_VIOLATION);#else EndSeh(STATUS_SUCCESS);#endif
StartSeh() Length = sprintf(Buffer, "%.6s", String); ok_str(Buffer, "hello!"); ok_int(Length, 6);#ifdef TEST_USER32 EndSeh(STATUS_ACCESS_VIOLATION);#else EndSeh(STATUS_SUCCESS);#endif
StartSeh() Length = sprintf(Buffer, "%.*s", 5, String);#ifdef TEST_USER32 ok_str(Buffer, "*s"); ok_int(Length, 2);#else ok_str(Buffer, "hello"); ok_int(Length, 5);#endif EndSeh(STATUS_SUCCESS);
StartSeh() Length = sprintf(Buffer, "%.*s", 6, String);#ifdef TEST_USER32 ok_str(Buffer, "*s"); ok_int(Length, 2);#else ok_str(Buffer, "hello!"); ok_int(Length, 6);#endif EndSeh(STATUS_SUCCESS);
/* both field width and precision */ StartSeh() Length = sprintf(Buffer, "%8.5s", String); ok_str(Buffer, " hello"); ok_int(Length, 8);#ifdef TEST_USER32 EndSeh(STATUS_ACCESS_VIOLATION);#else EndSeh(STATUS_SUCCESS);#endif
StartSeh() Length = sprintf(Buffer, "%-*.6s", -8, String);#ifdef TEST_USER32 ok_str(Buffer, "*.6s"); ok_int(Length, 4);#else ok_str(Buffer, "hello! "); ok_int(Length, 8);#endif EndSeh(STATUS_SUCCESS);
StartSeh() Length = sprintf(Buffer, "%*.*s", -8, 6, String);#ifdef TEST_USER32 ok_str(Buffer, "*.*s"); ok_int(Length, 4);#else ok_str(Buffer, "hello! "); ok_int(Length, 8);#endif EndSeh(STATUS_SUCCESS);
FreeGuarded(String);}