summaryrefslogtreecommitdiff
path: root/test.c
diff options
context:
space:
mode:
authorPieter Noordhuis <pcnoordhuis@gmail.com>2010-10-31 10:32:31 +0100
committerPieter Noordhuis <pcnoordhuis@gmail.com>2010-10-31 10:34:29 +0100
commita3a405bcbab4947fea0b155e93cc752953428e52 (patch)
tree9ed439092e99106b1151c66a32fd5cadc72a98c7 /test.c
parente95c9d4c5b434e6829c8e3a2cc36cebdee26edf7 (diff)
Format a command using an argument vector
Diffstat (limited to 'test.c')
-rw-r--r--test.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/test.c b/test.c
index cc15ba5..b4a5447 100644
--- a/test.c
+++ b/test.c
@@ -28,6 +28,48 @@ static void __connect(redisContext **target) {
}
}
+static void test_format_commands() {
+ char *cmd;
+ int len;
+
+ test("Format command without interpolation: ");
+ len = redisFormatCommand(&cmd,"SET foo bar");
+ test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n",len) == 0 &&
+ len == 4+4+(3+2)+4+(3+2)+4+(3+2));
+ free(cmd);
+
+ test("Format command with %%s string interpolation: ");
+ len = redisFormatCommand(&cmd,"SET %s %s","foo","bar");
+ test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n",len) == 0 &&
+ len == 4+4+(3+2)+4+(3+2)+4+(3+2));
+ free(cmd);
+
+ test("Format command with %%b string interpolation: ");
+ len = redisFormatCommand(&cmd,"SET %b %b","foo",3,"b\0r",3);
+ 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 &&
+ len == 4+4+(3+2)+4+(3+2)+4+(3+2));
+ free(cmd);
+
+ const char *argv[3];
+ argv[0] = "SET";
+ argv[1] = "foo";
+ argv[2] = "bar";
+ size_t lens[3] = { 3, 3, 3 };
+ int argc = 3;
+
+ test("Format command by passing argc/argv without lengths: ");
+ len = redisFormatCommandArgv(&cmd,argc,argv,NULL);
+ test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n",len) == 0 &&
+ len == 4+4+(3+2)+4+(3+2)+4+(3+2));
+ free(cmd);
+
+ test("Format command by passing argc/argv with lengths: ");
+ len = redisFormatCommandArgv(&cmd,argc,argv,lens);
+ test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n",len) == 0 &&
+ len == 4+4+(3+2)+4+(3+2)+4+(3+2));
+ free(cmd);
+}
+
static void test_blocking_connection() {
redisContext *c;
redisReply *reply;
@@ -294,6 +336,7 @@ static void test_nonblocking_connection() {
}
int main(void) {
+ test_format_commands();
test_blocking_connection();
test_reply_reader();
test_nonblocking_connection();