summaryrefslogtreecommitdiff
path: root/tests/port
diff options
context:
space:
mode:
authorAndreas Baumann <abaumann@yahoo.com>2009-03-10 17:15:28 +0100
committerAndreas Baumann <abaumann@yahoo.com>2009-03-10 17:15:28 +0100
commit0de4a082d0f8a156b5aa40719c13e9fe44d2787d (patch)
tree5275d618ec64958b05e88c989c13ad9a4a559ccf /tests/port
parenta6b606fd903e4c7dd188fe6a8dd35ed583e54cbf (diff)
downloadwolfbones-0de4a082d0f8a156b5aa40719c13e9fe44d2787d.tar.gz
wolfbones-0de4a082d0f8a156b5aa40719c13e9fe44d2787d.tar.bz2
cleanup in stdio.h with snprintf (on Linux works at least), also added testing for snprintf
Diffstat (limited to 'tests/port')
-rw-r--r--tests/port/GNUmakefile5
-rw-r--r--tests/port/test_snprintf.c38
2 files changed, 42 insertions, 1 deletions
diff --git a/tests/port/GNUmakefile b/tests/port/GNUmakefile
index dd28702..af58e2f 100644
--- a/tests/port/GNUmakefile
+++ b/tests/port/GNUmakefile
@@ -11,7 +11,8 @@ TEST_BINS = \
test_strerror_r$(EXE) \
test_strcasecmp$(EXE) \
test_strncasecmp$(EXE) \
- test_localtime_r$(EXE)
+ test_localtime_r$(EXE) \
+ test_snprintf$(EXE)
-include $(TOPDIR)/makefiles/gmake/sub.mk
@@ -32,3 +33,5 @@ local_test:
@./test_strncasecmp >/dev/null
@echo "Testing localtime_r.."
@./test_localtime_r >/dev/null
+ @echo "Testing snprintf.."
+ @./test_snprintf > /dev/null
diff --git a/tests/port/test_snprintf.c b/tests/port/test_snprintf.c
new file mode 100644
index 0000000..a9ddb45
--- /dev/null
+++ b/tests/port/test_snprintf.c
@@ -0,0 +1,38 @@
+#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 = snprintf( buf3, 255, "test with %s %d %lu", "test", 12, 234443334L );
+ 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 = rpl_snprintf( small_buf2, 10, "%s", "12345678901234" );
+ 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;
+}