crt: Add _stprintf test

This is test for MS-specific _stprintf() function. All test variants passes
also when compiled with MSVC compiler and run under msvcrt or UCRT runtime.

Note that gcc throws sprintf format warning for %hs usage in this test.
It is because gcc thinks that %hs takes unsigned short* argument.
But this is incorrect expectation, as all msvcrt and UCRT runtime versions
expect char* argument for %hs format. So this gcc format warning for %hs is
a gcc's bug.

Signed-off-by: Martin Storsjö <martin@martin.st>
This commit is contained in:
Pali Rohár 2024-11-15 18:00:00 +01:00 committed by Martin Storsjö
parent 1e2529bf12
commit f2b56d2672
8 changed files with 56 additions and 0 deletions

View File

@ -4199,6 +4199,12 @@ testcase_progs = \
testcases/t_snwprintf \
testcases/t_snwprintf0 \
testcases/t_snwprintf1 \
testcases/t_stprintf_a \
testcases/t_stprintf_u \
testcases/t_stprintf0_a \
testcases/t_stprintf0_u \
testcases/t_stprintf1_a \
testcases/t_stprintf1_u \
testcases/t_setjmp \
testcases/t_sigv \
testcases/t_speed_powl \

View File

@ -0,0 +1,3 @@
#define __USE_MINGW_ANSI_STDIO 0
#undef _UNICODE
#include "t_stprintf_tmpl.h"

View File

@ -0,0 +1,3 @@
#define __USE_MINGW_ANSI_STDIO 0
#define _UNICODE
#include "t_stprintf_tmpl.h"

View File

@ -0,0 +1,3 @@
#define __USE_MINGW_ANSI_STDIO 1
#undef _UNICODE
#include "t_stprintf_tmpl.h"

View File

@ -0,0 +1,3 @@
#define __USE_MINGW_ANSI_STDIO 1
#define _UNICODE
#include "t_stprintf_tmpl.h"

View File

@ -0,0 +1,2 @@
#undef _UNICODE
#include "t_stprintf_tmpl.h"

View File

@ -0,0 +1,34 @@
#include <stdio.h>
#include <string.h>
#include <wchar.h>
#include <tchar.h>
int main() {
int i;
TCHAR buffer[10] = { _T('X'), _T('X'), _T('X'), _T('X'), _T('X'), _T('X'), _T('X'), _T('X'), _T('X'), _T('X') };
#ifdef _UNICODE
int exp_sizeof_buffer = 2*10;
#else
int exp_sizeof_buffer = 10;
#endif
/* _stprintf format:
* %s takes _T string
* %hs takes char* string
* %ls takes wchar_t* string
*/
int ret = _stprintf(buffer, _T("%sBB%hs%ls"), _T("AA"), "CC", L"DD");
if (sizeof(buffer) != exp_sizeof_buffer) {
fprintf(stderr, "sizeof(buffer): expected=%d got=%d\n", exp_sizeof_buffer, (int)sizeof(buffer));
return 1;
}
if (ret != 8 || memcmp(buffer, _T("AABBCCDD\0X"), sizeof(buffer)) != 0) {
fprintf(stderr, "ret: expected=8 got=%d\n", ret);
fprintf(stderr, "buffer:");
for (i = 0; i < 10; i++) {
fprintf(stderr, " 0x%02x", (int)buffer[i]);
}
fprintf(stderr, "\n");
return 1;
}
return 0;
}

View File

@ -0,0 +1,2 @@
#define _UNICODE
#include "t_stprintf_tmpl.h"