|
19 | 19 | #include "test.h"
|
20 | 20 | #include <pj/log.h>
|
21 | 21 | #include <pj/os.h>
|
| 22 | +#include <string.h> |
| 23 | +#include <stdio.h> |
| 24 | + |
| 25 | +#define THIS_FILE "os.c" |
22 | 26 |
|
23 | 27 | #if INCLUDE_OS_TEST
|
24 | 28 | static int endianness_test32(void)
|
@@ -90,6 +94,141 @@ static int endianness_test32(void)
|
90 | 94 | return 0;
|
91 | 95 | }
|
92 | 96 |
|
| 97 | +static int log_written; |
| 98 | +static char test_large_msg[PJ_LOG_MAX_SIZE]; |
| 99 | + |
| 100 | +static void log_write(int level, const char *buffer, int len) |
| 101 | +{ |
| 102 | + log_written = len; |
| 103 | + //printf("%s", buffer); |
| 104 | +} |
| 105 | + |
| 106 | +int log_test(void) |
| 107 | +{ |
| 108 | + pj_log_func *old_func = pj_log_get_log_func(); |
| 109 | + struct log_test_t { |
| 110 | + const char *title; |
| 111 | + unsigned decor; |
| 112 | + const char *fmt; |
| 113 | + const char *arg; |
| 114 | + int min_len; |
| 115 | + int max_len; |
| 116 | + } log_tests[] = { |
| 117 | + /* String lengths: |
| 118 | + * PJ_LOG_HAS_TIME | PJ_LOG_HAS_MICRO_SEC: 13 |
| 119 | + * PJ_LOG_HAS_SENDER: PJ_LOG_SENDER_WIDTH+1 |
| 120 | + * "Hello world!": 12 |
| 121 | + * PJ_LOG_HAS_NEWLINE: 1 |
| 122 | + */ |
| 123 | + { |
| 124 | + "normal log", |
| 125 | + PJ_LOG_HAS_TIME | PJ_LOG_HAS_MICRO_SEC | |
| 126 | + PJ_LOG_HAS_SENDER | PJ_LOG_HAS_NEWLINE, |
| 127 | + "Hello %s!", |
| 128 | + "world", |
| 129 | + 13+PJ_LOG_SENDER_WIDTH+1+12+1, |
| 130 | + 13+PJ_LOG_SENDER_WIDTH+1+12+1, |
| 131 | + }, |
| 132 | + { |
| 133 | + "normal log with no decor", |
| 134 | + 0, |
| 135 | + "Hello %s!", |
| 136 | + "world", |
| 137 | + 12, |
| 138 | + 12, |
| 139 | + }, |
| 140 | + { |
| 141 | + "normal log with just newline", |
| 142 | + PJ_LOG_HAS_NEWLINE, |
| 143 | + "Hello %s!", |
| 144 | + "world", |
| 145 | + 13, |
| 146 | + 13, |
| 147 | + }, |
| 148 | + { |
| 149 | + "empty string with normal decor", |
| 150 | + PJ_LOG_HAS_TIME | PJ_LOG_HAS_MICRO_SEC | |
| 151 | + PJ_LOG_HAS_SENDER | PJ_LOG_HAS_NEWLINE, |
| 152 | + "%s", |
| 153 | + "", |
| 154 | + 13+PJ_LOG_SENDER_WIDTH+1+1, |
| 155 | + 13+PJ_LOG_SENDER_WIDTH+1+1, |
| 156 | + }, |
| 157 | + { |
| 158 | + "empty string with nodecor", |
| 159 | + 0, |
| 160 | + "%s", |
| 161 | + "", |
| 162 | + 0, |
| 163 | + 0, |
| 164 | + }, |
| 165 | + { |
| 166 | + "empty string with just newline", |
| 167 | + PJ_LOG_HAS_NEWLINE, |
| 168 | + "%s", |
| 169 | + "", |
| 170 | + 1, |
| 171 | + 1, |
| 172 | + }, |
| 173 | + { |
| 174 | + "large message with normal decor", |
| 175 | + PJ_LOG_HAS_TIME | PJ_LOG_HAS_MICRO_SEC | |
| 176 | + PJ_LOG_HAS_SENDER | PJ_LOG_HAS_NEWLINE, |
| 177 | + "%s", |
| 178 | + test_large_msg, |
| 179 | + PJ_LOG_MAX_SIZE-1, |
| 180 | + PJ_LOG_MAX_SIZE-1, |
| 181 | + }, |
| 182 | + { |
| 183 | + "large message with no decor", |
| 184 | + 0, |
| 185 | + "%s", |
| 186 | + test_large_msg, |
| 187 | + PJ_LOG_MAX_SIZE-1, |
| 188 | + PJ_LOG_MAX_SIZE-1, |
| 189 | + }, |
| 190 | + { |
| 191 | + "large message with just newline", |
| 192 | + PJ_LOG_HAS_NEWLINE, |
| 193 | + "%s", |
| 194 | + test_large_msg, |
| 195 | + PJ_LOG_MAX_SIZE-1, |
| 196 | + PJ_LOG_MAX_SIZE-1, |
| 197 | + }, |
| 198 | + }; |
| 199 | + unsigned old_decor = pj_log_get_decor(); |
| 200 | + unsigned i; |
| 201 | + |
| 202 | + memset(test_large_msg, 'A', sizeof(test_large_msg)); |
| 203 | + test_large_msg[sizeof(test_large_msg)-1] = '\0'; |
| 204 | + |
| 205 | + pj_log_set_log_func( &log_write ); |
| 206 | + |
| 207 | + for (i=0; i<PJ_ARRAY_SIZE(log_tests); ++i) { |
| 208 | + struct log_test_t *t = &log_tests[i]; |
| 209 | + |
| 210 | + log_written = -1; |
| 211 | + pj_log_set_decor(t->decor); |
| 212 | + |
| 213 | + PJ_LOG(1,(THIS_FILE, t->fmt, t->arg)); |
| 214 | + |
| 215 | + if (log_written < t->min_len || log_written > t->max_len) { |
| 216 | + pj_log_set_log_func( old_func ); |
| 217 | + pj_log_set_decor(old_decor); |
| 218 | + |
| 219 | + PJ_LOG(1,(THIS_FILE, |
| 220 | + " Error: in test %d (%s), writing (\"%s\", \"%s\"), expecting %d<=len<=%d, got len=%d", |
| 221 | + i, t->title, t->fmt, t->arg, t->min_len, t->max_len, log_written)); |
| 222 | + return 33; |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + pj_log_set_log_func( old_func ); |
| 227 | + pj_log_set_decor(old_decor); |
| 228 | + |
| 229 | + return 0; |
| 230 | +} |
| 231 | + |
93 | 232 | int os_test(void)
|
94 | 233 | {
|
95 | 234 | const pj_sys_info *si;
|
|
0 commit comments