Reworked scanf() testing. General cleanups.
[fw/pdclib] / testing / _PDCLIB_test.h
index 7a7ff0504961624accc330f593a196051d5d4253..e6c0c75bb3f04cccf18030eef139af83544f32ae 100644 (file)
 /* -------------------------------------------------------------------------- */
 
 #include <stdio.h>
+#include <limits.h>
+#include <string.h>
 
+/* Some strings used for <string.h> and <stdlib.h> testing. */
 static char const abcde[] = "abcde";
 static char const abcdx[] = "abcdx";
 static char const teststring[] = "1234567890\nABCDEFGHIJKLMNOPQRSTUVWXYZ\nabcdefghijklmnopqrstuvwxyz\n";
+
+/* Temporary file names */
 static char const testfile[]="testing/testfile";
 static char const testfile1[]="testing/testfile1";
 static char const testfile2[]="testing/testfile2";
@@ -23,11 +28,66 @@ static char const testfile2[]="testing/testfile2";
 
 static int TEST_RESULTS = 0;
 
+/* TESTCASE() - generic test */
 #define TESTCASE( x ) if ( x ) {} \
                       else { TEST_RESULTS += 1; printf( "FAILED: " __FILE__ ", line %d - %s\n", __LINE__, #x ); }
 
+/* TESTCASE_NOREG() - PDCLib-only test */
 #ifndef REGTEST
-#define TESTCASE_NOREG( x ) TESTCASE( x )
+    #define TESTCASE_NOREG( x ) TESTCASE( x )
+#else
+    #define TESTCASE_NOREG( x )
+#endif
+
+/* ...printf() tests */
+#if defined( _PDCLIB_FILEIO )
+    #define RESULT_MISMATCH( act, exp ) \
+        rewind( act ), \
+        result_buffer[ fread( result_buffer, 1, strlen( exp ) + 1, act ) ] = '\0', \
+        rewind( act ), \
+        memcmp( result_buffer, exp, strlen( exp ) )
+   #define RESULT_STRING( tgt ) result_buffer
+#elif defined( _PDCLIB_STRINGIO )
+   #define RESULT_MISMATCH( act, exp ) strcmp( act, exp ) != 0
+   #define RESULT_STRING( tgt ) tgt
+#endif
+
+#ifdef _PDCLIB_FILEIO
+#define PREP_RESULT_BUFFER char result_buffer[100];
 #else
-#define TESTCASE_NOREG( x )
+#define PREP_RESULT_BUFFER
 #endif
+
+#define PRINTF_TEST( expected_rc, expected_string, format, ... ) do { \
+        PREP_RESULT_BUFFER \
+        int actual_rc = testprintf( target, format, __VA_ARGS__ ); \
+        if ( ( actual_rc != expected_rc ) || \
+             ( RESULT_MISMATCH( target, expected_string ) ) ) \
+        { \
+            ++TEST_RESULTS; \
+            fprintf( stderr, "FAILED: " __FILE__ " (" _PDCLIB_FILEID "), line %d\n        expected %2d, \"%s\"\n        actual   %2d, \"%s\"\n", __LINE__, expected_rc, expected_string, actual_rc, RESULT_STRING( target ) ); \
+        } \
+    } while ( 0 )
+
+/* ...scanf() tests */
+#if defined( _PDCLIB_FILEIO )
+    #define PREPARE_SOURCE( input_string ) \
+        rewind( source ); \
+        fwrite( input_string, 1, sizeof( input_string ), source ); \
+        rewind( source );
+#elif defined( _PDCLIB_STRINGIO )
+    #define PREPARE_SOURCE( input_string ) \
+        memcpy( source, input_string, sizeof( input_string ) );
+#endif
+
+#define SCANF_TEST( expected_rc, input_string, format, ... ) do { \
+        int actual_rc; \
+        PREPARE_SOURCE( input_string ); \
+        actual_rc = testscanf( source, format, __VA_ARGS__ ); \
+        if ( actual_rc != expected_rc ) \
+        { \
+            ++TEST_RESULTS; \
+            fprintf( stderr, "FAILED: " __FILE__ " (" _PDCLIB_FILEID "), line %d\n        expected %2d,        actual   %2d\n", __LINE__, expected_rc, actual_rc ); \
+        } \
+    } while ( 0 )
+