Added size_t format specifier test coverage for SDL_snprintf and SDL_sscanf
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
diff --git a/test/testautomation_stdlib.c b/test/testautomation_stdlib.c
index 1956d73..a89ec1e 100644
--- a/test/testautomation_stdlib.c
+++ b/test/testautomation_stdlib.c
@@ -47,6 +47,7 @@ stdlib_snprintf(void *arg)
int predicted;
char text[1024];
const char *expected;
+ size_t size;
result = SDL_snprintf(text, sizeof(text), "%s", "foo");
expected = "foo";
@@ -148,6 +149,13 @@ stdlib_snprintf(void *arg)
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
SDLTest_AssertCheck(result == 6, "Check result value, expected: 6, got: %d", result);
+ size = 64;
+ result = SDL_snprintf(text, sizeof(text), "%zu %s", size, "test");
+ expected = "64 test";
+ SDLTest_AssertPass("Call to SDL_snprintf(text, sizeof(text), \"%%zu %%s\", size, \"test\")");
+ SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
+ SDLTest_AssertCheck(result == 7, "Check result value, expected: 7, got: %d", result);
+
return TEST_COMPLETED;
}
@@ -296,6 +304,11 @@ stdlib_sscanf(void *arg)
int result;
int expected_output;
int expected_result;
+ short short_output, expected_short_output;
+ long long_output, expected_long_output;
+ long long long_long_output, expected_long_long_output;
+ size_t size_output, expected_size_output;
+ char text[128];
expected_output = output = 123;
expected_result = -1;
@@ -327,6 +340,38 @@ stdlib_sscanf(void *arg)
SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output);
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
+#define SIZED_TEST_CASE(type, var, format_specifier) \
+ var##_output = 123; \
+ expected_##var##_output = (type)(((unsigned type)(~0)) >> 1); \
+ expected_result = 1; \
+ result = SDL_snprintf(text, sizeof(text), format_specifier, expected_##var##_output); \
+ result = SDL_sscanf(text, format_specifier, &var##_output); \
+ SDLTest_AssertPass("Call to SDL_sscanf(\"%s\", \"%s\", &output)", text, #format_specifier); \
+ SDLTest_AssertCheck(expected_##var##_output == var##_output, "Check output, expected: " format_specifier ", got: " format_specifier, expected_##var##_output, var##_output); \
+ SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); \
+ \
+ var##_output = 123; \
+ expected_##var##_output = ~(type)(((unsigned type)(~0)) >> 1); \
+ expected_result = 1; \
+ result = SDL_snprintf(text, sizeof(text), format_specifier, expected_##var##_output); \
+ result = SDL_sscanf(text, format_specifier, &var##_output); \
+ SDLTest_AssertPass("Call to SDL_sscanf(\"%s\", \"%s\", &output)", text, #format_specifier); \
+ SDLTest_AssertCheck(expected_##var##_output == var##_output, "Check output, expected: " format_specifier ", got: " format_specifier, expected_##var##_output, var##_output); \
+ SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); \
+
+ SIZED_TEST_CASE(short, short, "%hd")
+ SIZED_TEST_CASE(long, long, "%ld")
+ SIZED_TEST_CASE(long long, long_long, "%lld")
+
+ size_output = 123;
+ expected_size_output = (size_t)~0;
+ expected_result = 1;
+ result = SDL_snprintf(text, sizeof(text), "%zu", expected_size_output);
+ result = SDL_sscanf(text, "%zu", &size_output);
+ SDLTest_AssertPass("Call to SDL_sscanf(\"%s\", \"%%zu\", &output)", text);
+ SDLTest_AssertCheck(expected_size_output == size_output, "Check output, expected: %zu, got: %zu", expected_size_output, size_output);
+ SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
+
return TEST_COMPLETED;
}