]> git.ozlabs.org Git - ccan/blob - ccan/base64/test/moretap.h
base64: implements rfc4648, the base64 encoding
[ccan] / ccan / base64 / test / moretap.h
1 #ifndef _BASE64_MORETAP_H
2 #define _BASE64_MORETAP_H
3
4 #include <ccan/str/str.h>
5
6 /**
7  * is_str - OK if strings are equal
8  * @e1: expression for the variable string
9  * @e2: expression for the expected string
10  *
11  * If the strings are equal, the test passes.
12  *
13  * Example:
14  *     is_str(give_me_a_fred(),"fred");
15  */
16 static void _is_str(char *got,const char *expected, const char *got_string, const char *expected_string, const char *func, const char *file, int line) {
17         if (streq(expected,got)) {
18                 _gen_result(1, func, file, line,"%s eq %s",
19                             got_string,expected_string);
20         } else {
21                 _gen_result(0, func, file, line,"%s eq %s",
22                             got_string,expected_string);
23                 diag("Expected: %s",expected);
24                 diag("     Got: %s",got);
25         }
26 }
27 # define is_str(got,expected) _is_str(got,expected,#got,#expected,__func__, __FILE__, __LINE__)
28
29
30 /**
31  * is_int - OK if arguments are equal when cast to integers
32  * @e1: expression for the number
33  * @e2: expression for the expected number
34  *
35  * If the numbers are equal, the test passes.
36  *
37  * Example:
38  *     is_int(give_me_17(),17);
39  */
40 # define is_int(e1,e2 ...)                                              \
41   (((int)e1)==((int)e2) ?                                               \
42    _gen_result(1, __func__, __FILE__, __LINE__,"%s == %s",#e1,#e2) :    \
43    (_gen_result(0, __func__, __FILE__, __LINE__,"%s == %s",#e1,#e2)) || (diag("Expected: %d",e2),diag("     Got: %d",e1),0)) /* diag is void; note commas. */
44
45
46
47 /**
48  * is_mem - OK if arguments are identical up to length @e3
49  * @e1: expression for the buffer
50  * @e2: expression for the expected buffer
51  * @e2: length to compare in buffers
52  *
53  * If the buffers are equal up to @e2, the test passes.
54  *
55  * Example:
56  *     is_mem(give_me_foo(),"foo",3);
57  */
58 static void _is_mem(const char *got, const char *expected, const size_t len,
59               const char *got_string, const char *expected_string, const char *len_string,
60               const char *func, const char *file, int line) {
61         size_t offset = 0;
62
63         for (offset=0; offset<len; offset++) {
64                 if (got[offset] != expected[offset]) {
65                         _gen_result(0, func, file, line,"%s eq %s",got_string,expected_string);
66                         /* diag("Expected: %s",e2); */
67                         /* diag("     Got: %s",e1); */
68                         diag("Buffers differ at offset %zd (got=0x%02x expected=0x%02x)",
69                              offset,got[offset],expected[offset]);
70                         return;
71                 }
72         }
73
74         _gen_result(1, __func__, __FILE__, __LINE__,"%s eq %s",
75                     expected_string,got_string);
76 }
77 # define is_mem(got,expected,len) \
78         _is_mem(got,expected,len,#got,#expected,#len,__func__, __FILE__, __LINE__)
79
80 /**
81  * is_size_t - OK if arguments are equal when cast to size_t
82  * @e1: expression for the number
83  * @e2: expression for the expected number
84  *
85  * If the numbers are equal, the test passes.
86  *
87  * Example:
88  *     is_size_t(give_me_17(),17);
89  */
90 # define is_size_t(e1,e2 ...)                                           \
91   ((size_t)(e1)==((size_t)e2) ?                                         \
92    _gen_result(1, __func__, __FILE__, __LINE__,"%s == %s",#e1,#e2) :    \
93    (_gen_result(0, __func__, __FILE__, __LINE__,                        \
94                 "%s == %s",#e1,#e2)) || (diag("Expected: %zd",(size_t)e2),diag("     Got: %zd",(size_t)e1),0)) /* diag is void; note commas. */
95
96 #endif