Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
hiredis
Commits
dad240f0
Commit
dad240f0
authored
Jul 10, 2011
by
Pieter Noordhuis
Browse files
Test all supported types for printf-like formatting
parent
2da784ce
Changes
1
Hide whitespace changes
Inline
Side-by-side
test.c
View file @
dad240f0
...
@@ -142,25 +142,39 @@ static void test_format_commands(void) {
...
@@ -142,25 +142,39 @@ static void test_format_commands(void) {
len
==
4
+
4
+
(
3
+
2
)
+
4
+
(
1
+
2
)
+
4
+
(
1
+
2
));
len
==
4
+
4
+
(
3
+
2
)
+
4
+
(
1
+
2
)
+
4
+
(
1
+
2
));
free
(
cmd
);
free
(
cmd
);
/* sizeof(long long) is 8 bytes regardless of architecture */
/* Vararg width depends on the type. These tests make sure that the
test
(
"Format command with printf-delegation (long long): "
);
* width is correctly determined using the format and subsequent varargs
len
=
redisFormatCommand
(
&
cmd
,
"key:%08lld str:%s"
,
1234ll
,
"hello"
);
* can correctly be interpolated. */
test_cond
(
strncmp
(
cmd
,
"*2
\r\n
$12
\r\n
key:00001234
\r\n
$9
\r\n
str:hello
\r\n
"
,
len
)
==
0
&&
#define INTEGER_WIDTH_TEST(fmt, type) do { \
len
==
4
+
5
+
(
12
+
2
)
+
4
+
(
9
+
2
));
type value = 123; \
free
(
cmd
);
test("Format command with printf-delegation (" #type "): "); \
len = redisFormatCommand(&cmd,"key:%08" fmt " str:%s", value, "hello"); \
/* sizeof(float) is 4 bytes regardless of architecture */
test_cond(strncmp(cmd,"*2\r\n$12\r\nkey:00000123\r\n$9\r\nstr:hello\r\n",len) == 0 && \
test
(
"Format command with printf-delegation (float): "
);
len == 4+5+(12+2)+4+(9+2)); \
len
=
redisFormatCommand
(
&
cmd
,
"v:%06.1f str:%s"
,
12
.
34
f
,
"hello"
);
free(cmd); \
test_cond
(
strncmp
(
cmd
,
"*2
\r\n
$8
\r\n
v:0012.3
\r\n
$9
\r\n
str:hello
\r\n
"
,
len
)
==
0
&&
} while(0)
len
==
4
+
4
+
(
8
+
2
)
+
4
+
(
9
+
2
));
free
(
cmd
);
#define FLOAT_WIDTH_TEST(type) do { \
type value = 123.0; \
test
(
"Format command with printf-delegation and extra interpolation: "
);
test("Format command with printf-delegation (" #type "): "); \
len
=
redisFormatCommand
(
&
cmd
,
"key:%d %b"
,
1234
,
"foo"
,
3
);
len = redisFormatCommand(&cmd,"key:%08.3f str:%s", value, "hello"); \
test_cond
(
strncmp
(
cmd
,
"*2
\r\n
$8
\r\n
key:1234
\r\n
$3
\r\n
foo
\r\n
"
,
len
)
==
0
&&
test_cond(strncmp(cmd,"*2\r\n$12\r\nkey:0123.000\r\n$9\r\nstr:hello\r\n",len) == 0 && \
len
==
4
+
4
+
(
8
+
2
)
+
4
+
(
3
+
2
));
len == 4+5+(12+2)+4+(9+2)); \
free
(
cmd
);
free(cmd); \
} while(0)
INTEGER_WIDTH_TEST
(
"d"
,
int
);
INTEGER_WIDTH_TEST
(
"hhd"
,
char
);
INTEGER_WIDTH_TEST
(
"hd"
,
short
);
INTEGER_WIDTH_TEST
(
"ld"
,
long
);
INTEGER_WIDTH_TEST
(
"lld"
,
long
long
);
INTEGER_WIDTH_TEST
(
"u"
,
unsigned
int
);
INTEGER_WIDTH_TEST
(
"hhu"
,
unsigned
char
);
INTEGER_WIDTH_TEST
(
"hu"
,
unsigned
short
);
INTEGER_WIDTH_TEST
(
"lu"
,
unsigned
long
);
INTEGER_WIDTH_TEST
(
"llu"
,
unsigned
long
long
);
FLOAT_WIDTH_TEST
(
float
);
FLOAT_WIDTH_TEST
(
double
);
test
(
"Format command with invalid printf format: "
);
test
(
"Format command with invalid printf format: "
);
len
=
redisFormatCommand
(
&
cmd
,
"key:%08p %b"
,
1234
,
"foo"
,
3
);
len
=
redisFormatCommand
(
&
cmd
,
"key:%08p %b"
,
1234
,
"foo"
,
3
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment