Commit | Line | Data |
---|---|---|
d42e7319 JB |
1 | /* |
2 | * Copyright (C) 2010 The Android Open Source Project | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * * Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * * Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in | |
12 | * the documentation and/or other materials provided with the | |
13 | * distribution. | |
14 | * | |
15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | |
22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | |
23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
26 | * SUCH DAMAGE. | |
27 | */ | |
28 | ||
29 | #include <stdarg.h> | |
30 | #include <string.h> | |
31 | #include <errno.h> | |
32 | #include <unistd.h> | |
33 | #include <stdint.h> | |
34 | #include <stddef.h> | |
35 | #include "linker_format.h" | |
36 | #include "linker_debug.h" | |
37 | ||
38 | /* define UNIT_TESTS to build this file as a single executable that runs | |
39 | * the formatter's unit tests | |
40 | */ | |
41 | #define xxUNIT_TESTS | |
42 | ||
43 | /*** Generic output sink | |
44 | ***/ | |
45 | ||
46 | typedef struct { | |
47 | void *opaque; | |
48 | void (*send)(void *opaque, const char *data, int len); | |
49 | } Out; | |
50 | ||
51 | static void | |
52 | out_send(Out *o, const void *data, size_t len) | |
53 | { | |
54 | o->send(o->opaque, data, (int)len); | |
55 | } | |
56 | ||
57 | static void | |
58 | out_send_repeat(Out *o, char ch, int count) | |
59 | { | |
60 | char pad[8]; | |
61 | const int padSize = (int)sizeof(pad); | |
62 | ||
63 | memset(pad, ch, sizeof(pad)); | |
64 | while (count > 0) { | |
65 | int avail = count; | |
66 | if (avail > padSize) { | |
67 | avail = padSize; | |
68 | } | |
69 | o->send(o->opaque, pad, avail); | |
70 | count -= avail; | |
71 | } | |
72 | } | |
73 | ||
74 | /* forward declaration */ | |
75 | static void | |
76 | out_vformat(Out *o, const char *format, va_list args); | |
77 | ||
78 | /*** Bounded buffer output | |
79 | ***/ | |
80 | ||
81 | typedef struct { | |
82 | Out out[1]; | |
83 | char *buffer; | |
84 | char *pos; | |
85 | char *end; | |
86 | int total; | |
87 | } BufOut; | |
88 | ||
89 | static void | |
90 | buf_out_send(void *opaque, const char *data, int len) | |
91 | { | |
92 | BufOut *bo = opaque; | |
93 | ||
94 | if (len < 0) | |
95 | len = strlen(data); | |
96 | ||
97 | bo->total += len; | |
98 | ||
99 | while (len > 0) { | |
100 | int avail = bo->end - bo->pos; | |
101 | if (avail == 0) | |
102 | break; | |
103 | if (avail > len) | |
104 | avail = len; | |
105 | memcpy(bo->pos, data, avail); | |
106 | bo->pos += avail; | |
107 | bo->pos[0] = '\0'; | |
108 | len -= avail; | |
109 | } | |
110 | } | |
111 | ||
112 | static Out* | |
113 | buf_out_init(BufOut *bo, char *buffer, size_t size) | |
114 | { | |
115 | if (size == 0) | |
116 | return NULL; | |
117 | ||
118 | bo->out->opaque = bo; | |
119 | bo->out->send = buf_out_send; | |
120 | bo->buffer = buffer; | |
121 | bo->end = buffer + size - 1; | |
122 | bo->pos = bo->buffer; | |
123 | bo->pos[0] = '\0'; | |
124 | bo->total = 0; | |
125 | ||
126 | return bo->out; | |
127 | } | |
128 | ||
129 | static int | |
130 | buf_out_length(BufOut *bo) | |
131 | { | |
132 | return bo->total; | |
133 | } | |
134 | ||
135 | static int | |
136 | vformat_buffer(char *buff, size_t buffsize, const char *format, va_list args) | |
137 | { | |
138 | BufOut bo; | |
139 | Out *out; | |
140 | ||
141 | out = buf_out_init(&bo, buff, buffsize); | |
142 | if (out == NULL) | |
143 | return 0; | |
144 | ||
145 | out_vformat(out, format, args); | |
146 | ||
147 | return buf_out_length(&bo); | |
148 | } | |
149 | ||
150 | int | |
151 | format_buffer(char *buff, size_t buffsize, const char *format, ...) | |
152 | { | |
153 | va_list args; | |
154 | int ret; | |
155 | ||
156 | va_start(args, format); | |
157 | ret = vformat_buffer(buff, buffsize, format, args); | |
158 | va_end(args); | |
159 | ||
160 | return ret; | |
161 | } | |
162 | ||
163 | /* The __stack_chk_fail() function calls __libc_android_log_print() | |
164 | * which calls vsnprintf(). | |
165 | * | |
166 | * We define our version of the function here to avoid dragging | |
167 | * about 25 KB of C library routines related to formatting. | |
168 | */ | |
169 | #if 0 | |
170 | int | |
171 | vsnprintf(char *buff, size_t bufsize, const char *format, va_list args) | |
172 | { | |
173 | return format_buffer(buff, bufsize, format, args); | |
174 | } | |
175 | #endif | |
176 | ||
177 | #if LINKER_DEBUG | |
178 | ||
179 | #if !LINKER_DEBUG_TO_LOG | |
180 | ||
181 | /*** File descriptor output | |
182 | ***/ | |
183 | ||
184 | typedef struct { | |
185 | Out out[1]; | |
186 | int fd; | |
187 | int total; | |
188 | } FdOut; | |
189 | ||
190 | static void | |
191 | fd_out_send(void *opaque, const char *data, int len) | |
192 | { | |
193 | FdOut *fdo = opaque; | |
194 | ||
195 | if (len < 0) | |
196 | len = strlen(data); | |
197 | ||
198 | while (len > 0) { | |
199 | int ret = write(fdo->fd, data, len); | |
200 | if (ret < 0) { | |
201 | if (errno == EINTR) | |
202 | continue; | |
203 | break; | |
204 | } | |
205 | data += ret; | |
206 | len -= ret; | |
207 | fdo->total += ret; | |
208 | } | |
209 | } | |
210 | ||
211 | static Out* | |
212 | fd_out_init(FdOut *fdo, int fd) | |
213 | { | |
214 | fdo->out->opaque = fdo; | |
215 | fdo->out->send = fd_out_send; | |
216 | fdo->fd = fd; | |
217 | fdo->total = 0; | |
218 | ||
219 | return fdo->out; | |
220 | } | |
221 | ||
222 | static int | |
223 | fd_out_length(FdOut *fdo) | |
224 | { | |
225 | return fdo->total; | |
226 | } | |
227 | ||
228 | ||
229 | int | |
230 | format_fd(int fd, const char *format, ...) | |
231 | { | |
232 | FdOut fdo; | |
233 | Out* out; | |
234 | va_list args; | |
235 | ||
236 | out = fd_out_init(&fdo, fd); | |
237 | if (out == NULL) | |
238 | return 0; | |
239 | ||
240 | va_start(args, format); | |
241 | out_vformat(out, format, args); | |
242 | va_end(args); | |
243 | ||
244 | return fd_out_length(&fdo); | |
245 | } | |
246 | ||
247 | #else /* LINKER_DEBUG_TO_LOG */ | |
248 | ||
249 | /*** Log output | |
250 | ***/ | |
251 | ||
252 | /* We need our own version of __libc_android_log_vprint, otherwise | |
253 | * the log output is completely broken. Probably due to the fact | |
254 | * that the C library is not initialized yet. | |
255 | * | |
256 | * You can test that by setting CUSTOM_LOG_VPRINT to 0 | |
257 | */ | |
258 | #define CUSTOM_LOG_VPRINT 1 | |
259 | ||
260 | #if CUSTOM_LOG_VPRINT | |
261 | ||
262 | #include <unistd.h> | |
263 | #include <fcntl.h> | |
264 | #include <sys/uio.h> | |
265 | ||
266 | static int log_vprint(int prio, const char *tag, const char *fmt, va_list args) | |
267 | { | |
268 | char buf[1024]; | |
269 | int result; | |
270 | static int log_fd = -1; | |
271 | ||
272 | result = vformat_buffer(buf, sizeof buf, fmt, args); | |
273 | ||
274 | if (log_fd < 0) { | |
275 | log_fd = open("/dev/log/main", O_WRONLY); | |
276 | if (log_fd < 0) | |
277 | return result; | |
278 | } | |
279 | ||
280 | { | |
281 | ssize_t ret; | |
282 | struct iovec vec[3]; | |
283 | ||
284 | vec[0].iov_base = (unsigned char *) &prio; | |
285 | vec[0].iov_len = 1; | |
286 | vec[1].iov_base = (void *) tag; | |
287 | vec[1].iov_len = strlen(tag) + 1; | |
288 | vec[2].iov_base = (void *) buf; | |
289 | vec[2].iov_len = strlen(buf) + 1; | |
290 | ||
291 | do { | |
292 | ret = writev(log_fd, vec, 3); | |
293 | } while ((ret < 0) && (errno == EINTR)); | |
294 | } | |
295 | return result; | |
296 | } | |
297 | ||
298 | #define __libc_android_log_vprint log_vprint | |
299 | ||
300 | #else /* !CUSTOM_LOG_VPRINT */ | |
301 | ||
302 | extern int __libc_android_log_vprint(int prio, const char* tag, const char* format, va_list ap); | |
303 | ||
304 | #endif /* !CUSTOM_LOG_VPRINT */ | |
305 | ||
306 | int | |
307 | format_log(int prio, const char *tag, const char *format, ...) | |
308 | { | |
309 | int ret; | |
310 | va_list args; | |
311 | va_start(args, format); | |
312 | ret = __libc_android_log_vprint(prio, tag, format, args); | |
313 | va_end(args); | |
314 | return ret; | |
315 | } | |
316 | ||
317 | #endif /* LINKER_DEBUG_TO_LOG */ | |
318 | ||
319 | #endif /* LINKER_DEBUG */ | |
320 | ||
321 | /*** formatted output implementation | |
322 | ***/ | |
323 | ||
324 | /* Parse a decimal string from 'format + *ppos', | |
325 | * return the value, and writes the new position past | |
326 | * the decimal string in '*ppos' on exit. | |
327 | * | |
328 | * NOTE: Does *not* handle a sign prefix. | |
329 | */ | |
330 | static unsigned | |
331 | parse_decimal(const char *format, int *ppos) | |
332 | { | |
333 | const char* p = format + *ppos; | |
334 | unsigned result = 0; | |
335 | ||
336 | for (;;) { | |
337 | int ch = *p; | |
338 | unsigned d = (unsigned)(ch - '0'); | |
339 | ||
340 | if (d >= 10U) | |
341 | break; | |
342 | ||
343 | result = result*10 + d; | |
344 | p++; | |
345 | } | |
346 | *ppos = p - format; | |
347 | return result; | |
348 | } | |
349 | ||
350 | /* write an octal/decimal/number into a bounded buffer. | |
351 | * assumes that bufsize > 0, and 'digits' is a string of | |
352 | * digits of at least 'base' values. | |
353 | */ | |
354 | static void | |
355 | format_number(char *buffer, size_t bufsize, uint64_t value, int base, const char *digits) | |
356 | { | |
357 | char *pos = buffer; | |
358 | char *end = buffer + bufsize - 1; | |
359 | ||
360 | /* generate digit string in reverse order */ | |
361 | while (value) { | |
362 | unsigned d = value % base; | |
363 | value /= base; | |
364 | if (pos < end) { | |
365 | *pos++ = digits[d]; | |
366 | } | |
367 | } | |
368 | ||
369 | /* special case for 0 */ | |
370 | if (pos == buffer) { | |
371 | if (pos < end) { | |
372 | *pos++ = '0'; | |
373 | } | |
374 | } | |
375 | pos[0] = '\0'; | |
376 | ||
377 | /* now reverse digit string in-place */ | |
378 | end = pos - 1; | |
379 | pos = buffer; | |
380 | while (pos < end) { | |
381 | int ch = pos[0]; | |
382 | pos[0] = end[0]; | |
383 | end[0] = (char) ch; | |
384 | pos++; | |
385 | end--; | |
386 | } | |
387 | } | |
388 | ||
389 | /* Write an integer (octal or decimal) into a buffer, assumes buffsize > 2 */ | |
390 | static void | |
391 | format_integer(char *buffer, size_t buffsize, uint64_t value, int base, int isSigned) | |
392 | { | |
393 | if (isSigned && (int64_t)value < 0) { | |
394 | buffer[0] = '-'; | |
395 | buffer += 1; | |
396 | buffsize -= 1; | |
397 | value = (uint64_t)(-(int64_t)value); | |
398 | } | |
399 | ||
400 | format_number(buffer, buffsize, value, base, "0123456789"); | |
401 | } | |
402 | ||
403 | /* Write an octal into a buffer, assumes buffsize > 2 */ | |
404 | static void | |
405 | format_octal(char *buffer, size_t buffsize, uint64_t value, int isSigned) | |
406 | { | |
407 | format_integer(buffer, buffsize, value, 8, isSigned); | |
408 | } | |
409 | ||
410 | /* Write a decimal into a buffer, assumes buffsize > 2 */ | |
411 | static void | |
412 | format_decimal(char *buffer, size_t buffsize, uint64_t value, int isSigned) | |
413 | { | |
414 | format_integer(buffer, buffsize, value, 10, isSigned); | |
415 | } | |
416 | ||
417 | /* Write an hexadecimal into a buffer, isCap is true for capital alphas. | |
418 | * Assumes bufsize > 2 */ | |
419 | static void | |
420 | format_hex(char *buffer, size_t buffsize, uint64_t value, int isCap) | |
421 | { | |
422 | const char *digits = isCap ? "0123456789ABCDEF" : "0123456789abcdef"; | |
423 | ||
424 | format_number(buffer, buffsize, value, 16, digits); | |
425 | } | |
426 | ||
427 | ||
428 | /* Perform formatted output to an output target 'o' */ | |
429 | static void | |
430 | out_vformat(Out *o, const char *format, va_list args) | |
431 | { | |
432 | int nn = 0; | |
433 | ||
434 | for (;;) { | |
435 | int mm; | |
436 | int padZero = 0; | |
437 | int padLeft = 0; | |
438 | char sign = '\0'; | |
439 | int width = -1; | |
440 | int prec = -1; | |
441 | size_t bytelen = sizeof(int); | |
442 | const char* str; | |
443 | int slen; | |
444 | char buffer[32]; /* temporary buffer used to format numbers */ | |
445 | ||
446 | char c; | |
447 | ||
448 | /* first, find all characters that are not 0 or '%' */ | |
449 | /* then send them to the output directly */ | |
450 | mm = nn; | |
451 | do { | |
452 | c = format[mm]; | |
453 | if (c == '\0' || c == '%') | |
454 | break; | |
455 | mm++; | |
456 | } while (1); | |
457 | ||
458 | if (mm > nn) { | |
459 | out_send(o, format+nn, mm-nn); | |
460 | nn = mm; | |
461 | } | |
462 | ||
463 | /* is this it ? then exit */ | |
464 | if (c == '\0') | |
465 | break; | |
466 | ||
467 | /* nope, we are at a '%' modifier */ | |
468 | nn++; // skip it | |
469 | ||
470 | /* parse flags */ | |
471 | for (;;) { | |
472 | c = format[nn++]; | |
473 | if (c == '\0') { /* single trailing '%' ? */ | |
474 | c = '%'; | |
475 | out_send(o, &c, 1); | |
476 | return; | |
477 | } | |
478 | else if (c == '0') { | |
479 | padZero = 1; | |
480 | continue; | |
481 | } | |
482 | else if (c == '-') { | |
483 | padLeft = 1; | |
484 | continue; | |
485 | } | |
486 | else if (c == ' ' || c == '+') { | |
487 | sign = c; | |
488 | continue; | |
489 | } | |
490 | break; | |
491 | } | |
492 | ||
493 | /* parse field width */ | |
494 | if ((c >= '0' && c <= '9')) { | |
495 | nn --; | |
496 | width = (int)parse_decimal(format, &nn); | |
497 | c = format[nn++]; | |
498 | } | |
499 | ||
500 | /* parse precision */ | |
501 | if (c == '.') { | |
502 | prec = (int)parse_decimal(format, &nn); | |
503 | c = format[nn++]; | |
504 | } | |
505 | ||
506 | /* length modifier */ | |
507 | switch (c) { | |
508 | case 'h': | |
509 | bytelen = sizeof(short); | |
510 | if (format[nn] == 'h') { | |
511 | bytelen = sizeof(char); | |
512 | nn += 1; | |
513 | } | |
514 | c = format[nn++]; | |
515 | break; | |
516 | case 'l': | |
517 | bytelen = sizeof(long); | |
518 | if (format[nn] == 'l') { | |
519 | bytelen = sizeof(long long); | |
520 | nn += 1; | |
521 | } | |
522 | c = format[nn++]; | |
523 | break; | |
524 | case 'z': | |
525 | bytelen = sizeof(size_t); | |
526 | c = format[nn++]; | |
527 | break; | |
528 | case 't': | |
529 | bytelen = sizeof(ptrdiff_t); | |
530 | c = format[nn++]; | |
531 | break; | |
532 | default: | |
533 | ; | |
534 | } | |
535 | ||
536 | /* conversion specifier */ | |
537 | if (c == 's') { | |
538 | /* string */ | |
539 | str = va_arg(args, const char*); | |
540 | } else if (c == 'c') { | |
541 | /* character */ | |
542 | /* NOTE: char is promoted to int when passed through the stack */ | |
543 | buffer[0] = (char) va_arg(args, int); | |
544 | buffer[1] = '\0'; | |
545 | str = buffer; | |
546 | } else if (c == 'p') { | |
547 | uint64_t value = (uintptr_t) va_arg(args, void*); | |
548 | buffer[0] = '0'; | |
549 | buffer[1] = 'x'; | |
550 | format_hex(buffer + 2, sizeof buffer-2, value, 0); | |
551 | str = buffer; | |
552 | } else { | |
553 | /* integers - first read value from stack */ | |
554 | uint64_t value; | |
555 | int isSigned = (c == 'd' || c == 'i' || c == 'o'); | |
556 | ||
557 | /* NOTE: int8_t and int16_t are promoted to int when passed | |
558 | * through the stack | |
559 | */ | |
560 | switch (bytelen) { | |
561 | case 1: value = (uint8_t) va_arg(args, int); break; | |
562 | case 2: value = (uint16_t) va_arg(args, int); break; | |
563 | case 4: value = va_arg(args, uint32_t); break; | |
564 | case 8: value = va_arg(args, uint64_t); break; | |
565 | default: return; /* should not happen */ | |
566 | } | |
567 | ||
568 | /* sign extension, if needed */ | |
569 | if (isSigned) { | |
570 | int shift = 64 - 8*bytelen; | |
571 | value = (uint64_t)(((int64_t)(value << shift)) >> shift); | |
572 | } | |
573 | ||
574 | /* format the number properly into our buffer */ | |
575 | switch (c) { | |
576 | case 'i': case 'd': | |
577 | format_integer(buffer, sizeof buffer, value, 10, isSigned); | |
578 | break; | |
579 | case 'o': | |
580 | format_integer(buffer, sizeof buffer, value, 8, isSigned); | |
581 | break; | |
582 | case 'x': case 'X': | |
583 | format_hex(buffer, sizeof buffer, value, (c == 'X')); | |
584 | break; | |
585 | default: | |
586 | buffer[0] = '\0'; | |
587 | } | |
588 | /* then point to it */ | |
589 | str = buffer; | |
590 | } | |
591 | ||
592 | /* if we are here, 'str' points to the content that must be | |
593 | * outputted. handle padding and alignment now */ | |
594 | ||
595 | slen = strlen(str); | |
596 | ||
597 | if (slen < width && !padLeft) { | |
598 | char padChar = padZero ? '0' : ' '; | |
599 | out_send_repeat(o, padChar, width - slen); | |
600 | } | |
601 | ||
602 | out_send(o, str, slen); | |
603 | ||
604 | if (slen < width && padLeft) { | |
605 | char padChar = padZero ? '0' : ' '; | |
606 | out_send_repeat(o, padChar, width - slen); | |
607 | } | |
608 | } | |
609 | } | |
610 | ||
611 | ||
612 | #ifdef UNIT_TESTS | |
613 | ||
614 | #include <stdio.h> | |
615 | ||
616 | static int gFails = 0; | |
617 | ||
618 | #define MARGIN 40 | |
619 | ||
620 | #define UTEST_CHECK(condition,message) \ | |
621 | printf("Checking %-*s: ", MARGIN, message); fflush(stdout); \ | |
622 | if (!(condition)) { \ | |
623 | printf("KO\n"); \ | |
624 | gFails += 1; \ | |
625 | } else { \ | |
626 | printf("ok\n"); \ | |
627 | } | |
628 | ||
629 | static void | |
630 | utest_BufOut(void) | |
631 | { | |
632 | char buffer[16]; | |
633 | BufOut bo[1]; | |
634 | Out* out; | |
635 | int ret; | |
636 | ||
637 | buffer[0] = '1'; | |
638 | out = buf_out_init(bo, buffer, sizeof buffer); | |
639 | UTEST_CHECK(buffer[0] == '\0', "buf_out_init clears initial byte"); | |
640 | out_send(out, "abc", 3); | |
641 | UTEST_CHECK(!memcmp(buffer, "abc", 4), "out_send() works with BufOut"); | |
642 | out_send_repeat(out, 'X', 4); | |
643 | UTEST_CHECK(!memcmp(buffer, "abcXXXX", 8), "out_send_repeat() works with BufOut"); | |
644 | buffer[sizeof buffer-1] = 'x'; | |
645 | out_send_repeat(out, 'Y', 2*sizeof(buffer)); | |
646 | UTEST_CHECK(buffer[sizeof buffer-1] == '\0', "overflows always zero-terminates"); | |
647 | ||
648 | out = buf_out_init(bo, buffer, sizeof buffer); | |
649 | out_send_repeat(out, 'X', 2*sizeof(buffer)); | |
650 | ret = buf_out_length(bo); | |
651 | UTEST_CHECK(ret == 2*sizeof(buffer), "correct size returned on overflow"); | |
652 | } | |
653 | ||
654 | static void | |
655 | utest_expect(const char* result, const char* format, ...) | |
656 | { | |
657 | va_list args; | |
658 | BufOut bo[1]; | |
659 | char buffer[256]; | |
660 | Out* out = buf_out_init(bo, buffer, sizeof buffer); | |
661 | ||
662 | printf("Checking %-*s: ", MARGIN, format); fflush(stdout); | |
663 | va_start(args, format); | |
664 | out_vformat(out, format, args); | |
665 | va_end(args); | |
666 | ||
667 | if (strcmp(result, buffer)) { | |
668 | printf("KO. got '%s' expecting '%s'\n", buffer, result); | |
669 | gFails += 1; | |
670 | } else { | |
671 | printf("ok. got '%s'\n", result); | |
672 | } | |
673 | } | |
674 | ||
675 | int main(void) | |
676 | { | |
677 | utest_BufOut(); | |
678 | utest_expect("", ""); | |
679 | utest_expect("a", "a"); | |
680 | utest_expect("01234", "01234", ""); | |
681 | utest_expect("01234", "%s", "01234"); | |
682 | utest_expect("aabbcc", "aa%scc", "bb"); | |
683 | utest_expect("a", "%c", 'a'); | |
684 | utest_expect("1234", "%d", 1234); | |
685 | utest_expect("-8123", "%d", -8123); | |
686 | utest_expect("16", "%hd", 0x7fff0010); | |
687 | utest_expect("16", "%hhd", 0x7fffff10); | |
688 | utest_expect("68719476736", "%lld", 0x1000000000LL); | |
689 | utest_expect("70000", "%ld", 70000); | |
690 | utest_expect("0xb0001234", "%p", (void*)0xb0001234); | |
691 | utest_expect("12ab", "%x", 0x12ab); | |
692 | utest_expect("12AB", "%X", 0x12ab); | |
693 | utest_expect("00123456", "%08x", 0x123456); | |
694 | utest_expect("01234", "0%d", 1234); | |
695 | utest_expect(" 1234", "%5d", 1234); | |
696 | utest_expect("01234", "%05d", 1234); | |
697 | utest_expect(" 1234", "%8d", 1234); | |
698 | utest_expect("1234 ", "%-8d", 1234); | |
699 | utest_expect("abcdef ", "%-11s", "abcdef"); | |
700 | utest_expect("something:1234", "%s:%d", "something", 1234); | |
701 | utest_expect("005:5:05", "%03d:%d:%02d", 5, 5, 5); | |
702 | utest_expect("5,0x0", "%d,%p", 5, NULL); | |
703 | utest_expect("68719476736,6,7,8", "%lld,%d,%d,%d", 0x1000000000LL, 6, 7, 8); | |
704 | return gFails != 0; | |
705 | } | |
706 | ||
707 | #endif /* UNIT_TESTS */ |