Changeset 64340 in vbox for trunk/src/VBox/Runtime/testcase
- Timestamp:
- Oct 20, 2016 6:38:21 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/testcase/tstRTStrFormat.cpp
r63561 r64340 113 113 114 114 /* simple */ 115 static const char s_szSimpleExpect[] = "u32=16 u64=256 u64=0x100"; 115 116 size_t cch = RTStrPrintf(pszBuf, BUF_SIZE, "u32=%d u64=%lld u64=%#llx", u32, u64, u64); 117 if (strcmp(pszBuf, s_szSimpleExpect)) 118 RTTestIFailed("error: '%s'\n" 119 "wanted '%s'\n", pszBuf, s_szSimpleExpect); 120 else if (cch != sizeof(s_szSimpleExpect) - 1) 121 RTTestIFailed("error: got %zd, expected %zd (#1)\n", cch, sizeof(s_szSimpleExpect) - 1); 122 123 ssize_t cch2 = RTStrPrintf2(pszBuf, BUF_SIZE, "u32=%d u64=%lld u64=%#llx", u32, u64, u64); 116 124 if (strcmp(pszBuf, "u32=16 u64=256 u64=0x100")) 117 {118 RTTestIFailed("error: '%s'\n"119 "wanted 'u32=16 u64=256 u64=0x100'\n", pszBuf);120 }125 RTTestIFailed("error: '%s' (#2)\n" 126 "wanted '%s' (#2)\n", pszBuf, s_szSimpleExpect); 127 else if (cch2 != sizeof(s_szSimpleExpect) - 1) 128 RTTestIFailed("error: got %zd, expected %zd (#2)\n", cch2, sizeof(s_szSimpleExpect) - 1); 121 129 122 130 /* just big. */ … … 171 179 RTTestSub(hTest, "RTStrAPrintf"); 172 180 char *psz = (char *)~0; 173 int cch 2= RTStrAPrintf(&psz, "Hey there! %s%s", "This is a test", "!");174 if (cch 2< 0)175 RTTestIFailed("RTStrAPrintf failed, cch 2=%d\n", cch2);181 int cch3 = RTStrAPrintf(&psz, "Hey there! %s%s", "This is a test", "!"); 182 if (cch3 < 0) 183 RTTestIFailed("RTStrAPrintf failed, cch3=%d\n", cch3); 176 184 else if (strcmp(psz, "Hey there! This is a test!")) 177 185 RTTestIFailed("RTStrAPrintf failed\n" … … 179 187 "wanted: 'Hey there! This is a test!'\n", 180 188 psz); 181 else if ((int)strlen(psz) != cch 2)182 RTTestIFailed("RTStrAPrintf failed, cch 2 == %d expected %u\n", cch2, strlen(psz));189 else if ((int)strlen(psz) != cch3) 190 RTTestIFailed("RTStrAPrintf failed, cch3 == %d expected %u\n", cch3, strlen(psz)); 183 191 RTStrFree(psz); 184 192 193 /* This used to be very simple, but is not doing overflow handling checks and two APIs. */ 185 194 #define CHECK42(fmt, arg, out) \ 186 195 do { \ 187 cch = RTStrPrintf(pszBuf, BUF_SIZE, fmt " 42=%d " fmt " 42=%d", arg, 42, arg, 42); \ 188 if (strcmp(pszBuf, out " 42=42 " out " 42=42")) \ 196 static const char g_szCheck42Fmt[] = fmt " 42=%d " fmt " 42=%d" ; \ 197 static const char g_szCheck42Expect[] = out " 42=42 " out " 42=42" ; \ 198 \ 199 cch = RTStrPrintf(pszBuf, BUF_SIZE, g_szCheck42Fmt, arg, 42, arg, 42); \ 200 if (memcmp(pszBuf, g_szCheck42Expect, sizeof(g_szCheck42Expect)) != 0) \ 189 201 RTTestIFailed("at line %d: format '%s'\n" \ 190 202 " output: '%s'\n" \ 191 203 " wanted: '%s'\n", \ 192 __LINE__, fmt, pszBuf, out " 42=42 " out " 42=42"); \193 else if (cch != sizeof( out " 42=42 " out " 42=42") - 1) \204 __LINE__, fmt, pszBuf, g_szCheck42Expect); \ 205 else if (cch != sizeof(g_szCheck42Expect) - 1) \ 194 206 RTTestIFailed("at line %d: Invalid length %d returned, expected %u!\n", \ 195 __LINE__, cch, sizeof(out " 42=42 " out " 42=42") - 1); \ 207 __LINE__, cch, sizeof(g_szCheck42Expect) - 1); \ 208 \ 209 RTTestIDisableAssertions(); \ 210 for (size_t cbBuf = 0; cbBuf <= BUF_SIZE; cbBuf++) \ 211 { \ 212 memset(pszBuf, 0xcc, BUF_SIZE); \ 213 const char chAfter = cbBuf != 0 ? '\0' : 0xcc; \ 214 const size_t cchCompare = cbBuf >= sizeof(g_szCheck42Expect) ? sizeof(g_szCheck42Expect) - 1 \ 215 : cbBuf > 0 ? cbBuf - 1 : 0; \ 216 /*size_t cch1Expect = cchCompare; */ \ 217 ssize_t cch2Expect = cbBuf >= sizeof(g_szCheck42Expect) \ 218 ? sizeof(g_szCheck42Expect) - 1 : -(ssize_t)sizeof(g_szCheck42Expect); \ 219 \ 220 cch2 = RTStrPrintf(pszBuf, cbBuf, g_szCheck42Fmt, arg, 42, arg, 42);\ 221 if ( memcmp(pszBuf, g_szCheck42Expect, cchCompare) != 0 \ 222 || pszBuf[cchCompare] != chAfter) \ 223 RTTestIFailed("at line %d: format '%s' (#1, cbBuf=%zu)\n" \ 224 " output: '%s'\n" \ 225 " wanted: '%s'\n", \ 226 __LINE__, fmt, cbBuf, cbBuf ? pszBuf : "", g_szCheck42Expect); \ 227 /*if (cch != cch1Expect) - code is buggy */ \ 228 /* RTTestIFailed("at line %d: Invalid length %d returned for cbBuf=%zu, expected %zd! (%#1)\n", */ \ 229 /* __LINE__, cch, cbBuf, cch1Expect); */ \ 230 \ 231 cch2 = RTStrPrintf2(pszBuf, cbBuf, g_szCheck42Fmt, arg, 42, arg, 42);\ 232 if ( memcmp(pszBuf, g_szCheck42Expect, cchCompare) != 0 \ 233 || pszBuf[cchCompare] != chAfter) \ 234 RTTestIFailed("at line %d: format '%s' (#2, cbBuf=%zu)\n" \ 235 " output: '%s'\n" \ 236 " wanted: '%s'\n", \ 237 __LINE__, fmt, cbBuf, cbBuf ? pszBuf : "", g_szCheck42Expect); \ 238 if (cch2 != cch2Expect) \ 239 RTTestIFailed("at line %d: Invalid length %d returned for cbBuf=%zu, expected %zd! (%#2)\n", \ 240 __LINE__, cch2, cbBuf, cch2Expect); \ 241 } \ 242 RTTestIRestoreAssertions(); \ 196 243 } while (0) 197 244
Note:
See TracChangeset
for help on using the changeset viewer.