author | Tero Marttila <terom@fixme.fi> |
Tue, 05 May 2009 03:15:25 +0300 | |
changeset 171 | b54f393c3df0 |
parent 168 | a58ad50911fc |
permissions | -rw-r--r-- |
168 | 1 |
/** |
2 |
* @file |
|
3 |
* |
|
4 |
* Test functions for the str module. |
|
5 |
*/ |
|
171
b54f393c3df0
evil chain.h macro magic, fix irc_conn_set_nickname bug, misc. test bugs (mem leaks, missing #includes)
Tero Marttila <terom@fixme.fi>
parents:
168
diff
changeset
|
6 |
#include "test.h" |
b54f393c3df0
evil chain.h macro magic, fix irc_conn_set_nickname bug, misc. test bugs (mem leaks, missing #includes)
Tero Marttila <terom@fixme.fi>
parents:
168
diff
changeset
|
7 |
|
b54f393c3df0
evil chain.h macro magic, fix irc_conn_set_nickname bug, misc. test bugs (mem leaks, missing #includes)
Tero Marttila <terom@fixme.fi>
parents:
168
diff
changeset
|
8 |
#include "../str.h" |
168 | 9 |
|
10 |
void assert_str_quote (size_t buf_size, const char *data, ssize_t len, const char *target, size_t out) |
|
11 |
{ |
|
12 |
char buf[buf_size]; |
|
13 |
||
14 |
size_t ret = str_quote(buf, buf_size, data, len); |
|
15 |
||
16 |
log_debug("str_quote(%zu, %zd) -> %s:%zu / %s:%zu", buf_size, len, buf, ret, target, out); |
|
17 |
||
18 |
assert_strcmp(buf, target); |
|
19 |
assert(ret == out); |
|
20 |
} |
|
21 |
||
22 |
void test_str_quote (void) |
|
23 |
{ |
|
24 |
log_info("testing str_quote()"); |
|
25 |
||
26 |
assert_str_quote(5, NULL, -1, "NULL", 4 ); |
|
27 |
assert_str_quote(16, "foo", -1, "'foo'", 5 ); |
|
28 |
assert_str_quote(16, "foobar", 3, "'foo'", 5 ); |
|
29 |
assert_str_quote(16, "\r\n", -1, "'\\r\\n'", 6 ); |
|
30 |
assert_str_quote(16, "\x13", -1, "'\\x13'", 6 ); |
|
31 |
assert_str_quote(16, "x'y", -1, "'x\\'y'", 6 ); |
|
32 |
assert_str_quote(7, "1234567890", -1, "'1'...", 12 ); |
|
33 |
assert_str_quote(9, "1234567890", -1, "'123'...", 12 ); |
|
34 |
} |
|
35 |
||
36 |
struct str_format_ctx { |
|
37 |
const char *name; |
|
38 |
||
39 |
const char *value; |
|
40 |
}; |
|
41 |
||
42 |
err_t test_str_format_cb (const char *name, const char **value, ssize_t *value_len, void *arg) |
|
43 |
{ |
|
44 |
struct str_format_ctx *ctx = arg; |
|
45 |
||
46 |
assert_strcmp(name, ctx->name); |
|
47 |
||
48 |
*value = ctx->value; |
|
49 |
*value_len = -1; |
|
50 |
||
51 |
return SUCCESS; |
|
52 |
} |
|
53 |
||
54 |
void assert_str_format (const char *format, const char *name, const char *value, const char *out) |
|
55 |
{ |
|
56 |
struct str_format_ctx ctx = { name, value }; |
|
57 |
char buf[512]; |
|
58 |
||
59 |
assert_success(str_format(buf, sizeof(buf), format, test_str_format_cb, &ctx)); |
|
60 |
||
61 |
log_debug("str_format(%s), { %s:%s } -> %s / %s", format, name, value, buf, out); |
|
62 |
||
63 |
assert_strcmp(buf, out); |
|
64 |
} |
|
65 |
||
66 |
void test_str_format (void) |
|
67 |
{ |
|
68 |
log_info("test str_format()"); |
|
69 |
||
70 |
assert_str_format("foo", NULL, NULL, "foo"); |
|
71 |
assert_str_format("foo {bar} quux", "bar", "XXX", "foo XXX quux"); |
|
72 |
} |
|
73 |
||
74 |
void test_dump_str (void) |
|
75 |
{ |
|
76 |
log_info("dumping example strings on stdout:"); |
|
77 |
||
78 |
log_debug("normal: %s", dump_str("Hello World")); |
|
79 |
log_debug("escapes: %s", dump_str("foo\r\nbar\a\001")); |
|
80 |
log_debug("length: %s", dump_strn("<-->**", 4)); |
|
81 |
log_debug("overflow: %s", dump_str( "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx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