2012-07-04 17:33:56 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: ReactOS api tests
|
|
|
|
* LICENSE: GPLv2+ - See COPYING in the top level directory
|
|
|
|
* PURPOSE: Test for sprintf
|
2013-10-12 16:05:54 +00:00
|
|
|
* PROGRAMMER: Thomas Faber <thomas.faber@reactos.org>
|
2012-07-04 17:33:56 +00:00
|
|
|
*/
|
|
|
|
|
2013-09-22 17:52:42 +00:00
|
|
|
#include <apitest.h>
|
2019-07-10 10:25:36 +00:00
|
|
|
#include <apitest_guard.h>
|
2013-09-22 17:52:42 +00:00
|
|
|
|
2012-07-04 17:33:56 +00:00
|
|
|
#define WIN32_NO_STATUS
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <tchar.h>
|
|
|
|
#include <pseh/pseh2.h>
|
|
|
|
#include <ndk/mmfuncs.h>
|
|
|
|
#include <ndk/rtlfuncs.h>
|
|
|
|
|
2015-12-28 11:01:03 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(disable:4778) // unterminated format string '%'
|
2019-08-17 20:05:45 +00:00
|
|
|
#elif defined(__GNUC__)
|
2012-07-04 17:33:56 +00:00
|
|
|
#pragma GCC diagnostic ignored "-Wformat"
|
|
|
|
#pragma GCC diagnostic ignored "-Wformat-zero-length"
|
|
|
|
#pragma GCC diagnostic ignored "-Wnonnull"
|
2019-08-17 20:05:45 +00:00
|
|
|
#if __GNUC__ >= 7
|
|
|
|
#pragma GCC diagnostic ignored "-Wformat-overflow"
|
|
|
|
#endif
|
2012-07-04 17:33:56 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
|
|
|
/* basic parameter tests */
|
|
|
|
StartSeh()
|
|
|
|
Length = sprintf(NULL, NULL);
|
|
|
|
EndSeh(STATUS_ACCESS_VIOLATION);
|
|
|
|
|
|
|
|
StartSeh()
|
|
|
|
Length = sprintf(NULL, "");
|
|
|
|
ok_int(Length, 0);
|
2019-12-09 16:16:02 +00:00
|
|
|
#if TEST_CRTDLL || TEST_USER32
|
2012-07-04 17:33:56 +00:00
|
|
|
EndSeh(STATUS_ACCESS_VIOLATION);
|
2019-12-09 16:16:02 +00:00
|
|
|
#else
|
|
|
|
EndSeh(STATUS_SUCCESS);
|
|
|
|
#endif
|
2012-07-04 17:33:56 +00:00
|
|
|
|
|
|
|
StartSeh()
|
|
|
|
Length = sprintf(NULL, "Hello");
|
|
|
|
ok_int(Length, 5);
|
2019-12-09 16:16:02 +00:00
|
|
|
#if TEST_CRTDLL || TEST_USER32
|
2012-07-04 17:33:56 +00:00
|
|
|
EndSeh(STATUS_ACCESS_VIOLATION);
|
2019-12-09 16:16:02 +00:00
|
|
|
#else
|
|
|
|
EndSeh(STATUS_SUCCESS);
|
|
|
|
#endif
|
2012-07-04 17:33:56 +00:00
|
|
|
|
|
|
|
/* 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");
|
2012-07-10 08:07:44 +00:00
|
|
|
#ifdef TEST_USER32
|
|
|
|
ok_str(Buffer, "*s");
|
|
|
|
ok_int(Length, 2);
|
|
|
|
#else
|
2012-07-04 17:33:56 +00:00
|
|
|
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);
|
2015-12-28 11:01:03 +00:00
|
|
|
|
2012-07-04 17:33:56 +00:00
|
|
|
StartSeh()
|
|
|
|
Length = sprintf(Buffer, "%.*s", -1, "hello");
|
2012-07-10 08:07:44 +00:00
|
|
|
#ifdef TEST_USER32
|
|
|
|
ok_str(Buffer, "*s");
|
|
|
|
ok_int(Length, 2);
|
|
|
|
#else
|
2012-07-04 17:33:56 +00:00
|
|
|
ok_str(Buffer, "hello");
|
|
|
|
ok_int(Length, 5);
|
|
|
|
#endif
|
2012-07-10 08:07:44 +00:00
|
|
|
EndSeh(STATUS_SUCCESS);
|
2012-07-04 17:33:56 +00:00
|
|
|
|
|
|
|
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);
|
2012-07-10 08:07:44 +00:00
|
|
|
#ifdef TEST_USER32
|
|
|
|
EndSeh(STATUS_ACCESS_VIOLATION);
|
|
|
|
#else
|
2012-07-04 17:33:56 +00:00
|
|
|
EndSeh(STATUS_SUCCESS);
|
2012-07-10 08:07:44 +00:00
|
|
|
#endif
|
2012-07-04 17:33:56 +00:00
|
|
|
|
|
|
|
StartSeh()
|
|
|
|
Length = sprintf(Buffer, "%.6s", String);
|
|
|
|
ok_str(Buffer, "hello!");
|
|
|
|
ok_int(Length, 6);
|
2012-07-10 08:07:44 +00:00
|
|
|
#ifdef TEST_USER32
|
|
|
|
EndSeh(STATUS_ACCESS_VIOLATION);
|
|
|
|
#else
|
2012-07-04 17:33:56 +00:00
|
|
|
EndSeh(STATUS_SUCCESS);
|
2012-07-10 08:07:44 +00:00
|
|
|
#endif
|
2012-07-04 17:33:56 +00:00
|
|
|
|
|
|
|
StartSeh()
|
|
|
|
Length = sprintf(Buffer, "%.*s", 5, String);
|
2012-07-10 08:07:44 +00:00
|
|
|
#ifdef TEST_USER32
|
|
|
|
ok_str(Buffer, "*s");
|
|
|
|
ok_int(Length, 2);
|
|
|
|
#else
|
2012-07-04 17:33:56 +00:00
|
|
|
ok_str(Buffer, "hello");
|
|
|
|
ok_int(Length, 5);
|
2012-07-10 08:07:44 +00:00
|
|
|
#endif
|
2012-07-04 17:33:56 +00:00
|
|
|
EndSeh(STATUS_SUCCESS);
|
|
|
|
|
|
|
|
StartSeh()
|
|
|
|
Length = sprintf(Buffer, "%.*s", 6, String);
|
2012-07-10 08:07:44 +00:00
|
|
|
#ifdef TEST_USER32
|
|
|
|
ok_str(Buffer, "*s");
|
|
|
|
ok_int(Length, 2);
|
|
|
|
#else
|
2012-07-04 17:33:56 +00:00
|
|
|
ok_str(Buffer, "hello!");
|
|
|
|
ok_int(Length, 6);
|
2012-07-10 08:07:44 +00:00
|
|
|
#endif
|
2012-07-04 17:33:56 +00:00
|
|
|
EndSeh(STATUS_SUCCESS);
|
|
|
|
|
|
|
|
/* both field width and precision */
|
|
|
|
StartSeh()
|
|
|
|
Length = sprintf(Buffer, "%8.5s", String);
|
|
|
|
ok_str(Buffer, " hello");
|
|
|
|
ok_int(Length, 8);
|
2012-07-10 08:07:44 +00:00
|
|
|
#ifdef TEST_USER32
|
|
|
|
EndSeh(STATUS_ACCESS_VIOLATION);
|
|
|
|
#else
|
2012-07-04 17:33:56 +00:00
|
|
|
EndSeh(STATUS_SUCCESS);
|
2012-07-10 08:07:44 +00:00
|
|
|
#endif
|
2012-07-04 17:33:56 +00:00
|
|
|
|
|
|
|
StartSeh()
|
|
|
|
Length = sprintf(Buffer, "%-*.6s", -8, String);
|
2012-07-10 08:07:44 +00:00
|
|
|
#ifdef TEST_USER32
|
|
|
|
ok_str(Buffer, "*.6s");
|
|
|
|
ok_int(Length, 4);
|
|
|
|
#else
|
2012-07-04 17:33:56 +00:00
|
|
|
ok_str(Buffer, "hello! ");
|
|
|
|
ok_int(Length, 8);
|
2012-07-10 08:07:44 +00:00
|
|
|
#endif
|
2012-07-04 17:33:56 +00:00
|
|
|
EndSeh(STATUS_SUCCESS);
|
|
|
|
|
|
|
|
StartSeh()
|
|
|
|
Length = sprintf(Buffer, "%*.*s", -8, 6, String);
|
2012-07-10 08:07:44 +00:00
|
|
|
#ifdef TEST_USER32
|
|
|
|
ok_str(Buffer, "*.*s");
|
|
|
|
ok_int(Length, 4);
|
|
|
|
#else
|
2012-07-04 17:33:56 +00:00
|
|
|
ok_str(Buffer, "hello! ");
|
|
|
|
ok_int(Length, 8);
|
|
|
|
#endif
|
2012-07-10 08:07:44 +00:00
|
|
|
EndSeh(STATUS_SUCCESS);
|
2012-07-04 17:33:56 +00:00
|
|
|
|
|
|
|
FreeGuarded(String);
|
|
|
|
}
|