/* Copyright (C) 2008 Andreas Baumann This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "port/sys.h" #define WOLF_TEST_SNPRINTF #include "port/string.h" /* for strcmp */ #include "port/stdio.c" /* for snprintf */ #include /* 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; }