1 /** 2 * Standard C library routine test suite 3 */ 4 5 #include <stdio.h> 6 7 #include "SDL.h" 8 #include "SDL_test.h" 9 10 11 /* Test case functions */ 12 13 /** 14 * @brief Call to SDL_strlcpy 15 */ 16 #undef SDL_strlcpy 17 int 18 stdlib_strlcpy(void *arg) 19 { 20 size_t result; 21 char text[1024]; 22 const char *expected; 23 24 result = SDL_strlcpy(text, "foo", sizeof(text)); 25 expected = "foo"; 26 SDLTest_AssertPass("Call to SDL_strlcpy(\"foo\")"); 27 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text); 28 SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result); 29 30 result = SDL_strlcpy(text, "foo", 2); 31 expected = "f"; 32 SDLTest_AssertPass("Call to SDL_strlcpy(\"foo\") with buffer size 2"); 33 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text); 34 SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result); 35 36 return TEST_COMPLETED; 37 } 38 39 /** 40 * @brief Call to SDL_snprintf 41 */ 42 #undef SDL_snprintf 43 int 44 stdlib_snprintf(void *arg) 45 { 46 int result; 47 char text[1024]; 48 const char *expected; 49 50 result = SDL_snprintf(text, sizeof(text), "%s", "foo"); 51 expected = "foo"; 52 SDLTest_AssertPass("Call to SDL_snprintf(\"%%s\", \"foo\")"); 53 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text); 54 SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result); 55 56 result = SDL_snprintf(text, 2, "%s", "foo"); 57 expected = "f"; 58 SDLTest_AssertPass("Call to SDL_snprintf(\"%%s\", \"foo\") with buffer size 2"); 59 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text); 60 SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result); 61 62 result = SDL_snprintf(NULL, 0, "%s", "foo"); 63 SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result); 64 65 result = SDL_snprintf(text, sizeof(text), "%f", 1.0); 66 expected = "1.000000"; 67 SDLTest_AssertPass("Call to SDL_snprintf(\"%%f\", 1.0)"); 68 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text); 69 SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result); 70 71 result = SDL_snprintf(text, sizeof(text), "%.f", 1.0); 72 expected = "1"; 73 SDLTest_AssertPass("Call to SDL_snprintf(\"%%.f\", 1.0)"); 74 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text); 75 SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result); 76 77 result = SDL_snprintf(text, sizeof(text), "%#.f", 1.0); 78 expected = "1."; 79 SDLTest_AssertPass("Call to SDL_snprintf(\"%%#.f\", 1.0)"); 80 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text); 81 SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result); 82 83 result = SDL_snprintf(text, sizeof(text), "%f", 1.0 + 1.0 / 3.0); 84 expected = "1.333333"; 85 SDLTest_AssertPass("Call to SDL_snprintf(\"%%f\", 1.0 + 1.0 / 3.0)"); 86 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text); 87 SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result); 88 89 result = SDL_snprintf(text, sizeof(text), "%+f", 1.0 + 1.0 / 3.0); 90 expected = "+1.333333"; 91 SDLTest_AssertPass("Call to SDL_snprintf(\"%%+f\", 1.0 + 1.0 / 3.0)"); 92 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text); 93 SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result); 94 95 result = SDL_snprintf(text, sizeof(text), "%.2f", 1.0 + 1.0 / 3.0); 96 expected = "1.33"; 97 SDLTest_AssertPass("Call to SDL_snprintf(\"%%.2f\", 1.0 + 1.0 / 3.0)"); 98 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text); 99 SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result); 100 101 result = SDL_snprintf(text, sizeof(text), "%6.2f", 1.0 + 1.0 / 3.0); 102 expected = " 1.33"; 103 SDLTest_AssertPass("Call to SDL_snprintf(\"%%6.2f\", 1.0 + 1.0 / 3.0)"); 104 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text); 105 SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result); 106 107 result = SDL_snprintf(text, sizeof(text), "%06.2f", 1.0 + 1.0 / 3.0); 108 expected = "001.33"; 109 SDLTest_AssertPass("Call to SDL_snprintf(\"%%06.2f\", 1.0 + 1.0 / 3.0)"); 110 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text); 111 SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result); 112 113 result = SDL_snprintf(text, 5, "%06.2f", 1.0 + 1.0 / 3.0); 114 expected = "001."; 115 SDLTest_AssertPass("Call to SDL_snprintf(\"%%06.2f\", 1.0 + 1.0 / 3.0) with buffer size 5"); 116 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text); 117 SDLTest_AssertCheck(result == 6, "Check result value, expected: 6, got: %d", result); 118 119 return TEST_COMPLETED; 120 } 121 122 /* ================= Test References ================== */ 123 124 /* Standard C routine test cases */ 125 static const SDLTest_TestCaseReference stdlibTest1 = 126 { (SDLTest_TestCaseFp)stdlib_strlcpy, "stdlib_strlcpy", "Call to SDL_strlcpy", TEST_ENABLED }; 127 128 static const SDLTest_TestCaseReference stdlibTest2 = 129 { (SDLTest_TestCaseFp)stdlib_snprintf, "stdlib_snprintf", "Call to SDL_snprintf", TEST_ENABLED }; 130 131 /* Sequence of Standard C routine test cases */ 132 static const SDLTest_TestCaseReference *stdlibTests[] = { 133 &stdlibTest1, &stdlibTest2, NULL 134 }; 135 136 /* Timer test suite (global) */ 137 SDLTest_TestSuiteReference stdlibTestSuite = { 138 "Standard C routines", 139 NULL, 140 stdlibTests, 141 NULL 142 }; 143