MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / test_format_commands

Function test_format_commands

deps/hiredis/test.c:245–363  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

243}
244
245static void test_format_commands(void) {
246 char *cmd;
247 int len;
248
249 test("Format command without interpolation: ");
250 len = redisFormatCommand(&cmd,"SET foo bar");
251 test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n",len) == 0 &&
252 len == 4+4+(3+2)+4+(3+2)+4+(3+2));
253 hi_free(cmd);
254
255 test("Format command with %%s string interpolation: ");
256 len = redisFormatCommand(&cmd,"SET %s %s","foo","bar");
257 test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n",len) == 0 &&
258 len == 4+4+(3+2)+4+(3+2)+4+(3+2));
259 hi_free(cmd);
260
261 test("Format command with %%s and an empty string: ");
262 len = redisFormatCommand(&cmd,"SET %s %s","foo","");
263 test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$0\r\n\r\n",len) == 0 &&
264 len == 4+4+(3+2)+4+(3+2)+4+(0+2));
265 hi_free(cmd);
266
267 test("Format command with an empty string in between proper interpolations: ");
268 len = redisFormatCommand(&cmd,"SET %s %s","","foo");
269 test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$0\r\n\r\n$3\r\nfoo\r\n",len) == 0 &&
270 len == 4+4+(3+2)+4+(0+2)+4+(3+2));
271 hi_free(cmd);
272
273 test("Format command with %%b string interpolation: ");
274 len = redisFormatCommand(&cmd,"SET %b %b","foo",(size_t)3,"b\0r",(size_t)3);
275 test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nb\0r\r\n",len) == 0 &&
276 len == 4+4+(3+2)+4+(3+2)+4+(3+2));
277 hi_free(cmd);
278
279 test("Format command with %%b and an empty string: ");
280 len = redisFormatCommand(&cmd,"SET %b %b","foo",(size_t)3,"",(size_t)0);
281 test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$0\r\n\r\n",len) == 0 &&
282 len == 4+4+(3+2)+4+(3+2)+4+(0+2));
283 hi_free(cmd);
284
285 test("Format command with literal %%: ");
286 len = redisFormatCommand(&cmd,"SET %% %%");
287 test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$1\r\n%\r\n$1\r\n%\r\n",len) == 0 &&
288 len == 4+4+(3+2)+4+(1+2)+4+(1+2));
289 hi_free(cmd);
290
291 /* Vararg width depends on the type. These tests make sure that the
292 * width is correctly determined using the format and subsequent varargs
293 * can correctly be interpolated. */
294#define INTEGER_WIDTH_TEST(fmt, type) do { \
295 type value = 123; \
296 test("Format command with printf-delegation (" #type "): "); \
297 len = redisFormatCommand(&cmd,"key:%08" fmt " str:%s", value, "hello"); \
298 test_cond(strncmp(cmd,"*2\r\n$12\r\nkey:00000123\r\n$9\r\nstr:hello\r\n",len) == 0 && \
299 len == 4+5+(12+2)+4+(9+2)); \
300 hi_free(cmd); \
301} while(0)
302

Callers 1

mainFunction · 0.85

Calls 5

redisFormatCommandFunction · 0.85
redisFormatCommandArgvFunction · 0.85
hi_sdsfreeFunction · 0.85
hi_freeFunction · 0.70

Tested by

no test coverage detected