summaryrefslogtreecommitdiff
path: root/tests/port/test_snprintf.c
blob: 14d7adb2cc43405e9c0c1a24cdae496fbf5a56c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "port/sys.h"

#define WOLF_TEST_SNPRINTF
#include "port/stdio.c"		/* for snprintf */

#include "port/string.h"	/* for strcmp */
#include <stdlib.h>		/* for exit, EXIT_SUCCESS, free */

int main( void ) {
	char buf1[255];
	int res1;
	char buf2[255];
	int res2;
	char buf3[255];
	int res3;
	char small_buf1[10];
	int small_res1;
	char small_buf2[10];
	int small_res2;

	/* test if we get the same things for the most important format strings */
	res1 = rpl_snprintf( buf1, 255, "test with %s %d %lu", "test", 12, 234443334L );
	res2 = snprintf( buf2, 255, "test with %s %d %lu", "test", 12, 234443334L );
	res3 = sprintf( buf3, "test with %s %d %lu", "test", 12, 234443334L );
	printf( "RPL snprintf: %d %s\nsnprintf: %d %s\nsprintf: %d %s\n",
		res1, buf1, res2, buf2, res3, buf3 );
	if( res1 != res2 || res1 != res3 || res1 != 27 ) return EXIT_FAILURE;
	if( strcmp( buf1, buf2 ) != 0 ||
	    strcmp( buf1, buf2 ) != 0 ) return EXIT_FAILURE;

	/* most important, does the overflow protection work */
	small_res1 = rpl_snprintf( small_buf1, 10, "%s", "12345678901234" );
	small_res2 = snprintf( small_buf2, 10, "%s", "12345678901234" );
	printf( "RPL snprintf: %d %s\nsnprintf: %d %s\n",
		small_res1, small_buf1, small_res2, small_buf2 );
	if( small_res1 != small_res2 || small_res1 != 14 ) return EXIT_FAILURE;
	if( small_buf1[9] != '\0' || small_buf2[9] != '\0' ) return EXIT_FAILURE;
	if( strcmp( small_buf1, small_buf2 ) != 0 ) return EXIT_FAILURE;
	if( strcmp( small_buf1, "123456789" ) != 0 ) return EXIT_FAILURE;

	return EXIT_SUCCESS;
}