Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2008 Travis Geiselbrecht |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files |
| 6 | * (the "Software"), to deal in the Software without restriction, |
| 7 | * including without limitation the rights to use, copy, modify, merge, |
| 8 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | * and to permit persons to whom the Software is furnished to do so, |
| 10 | * subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | #include <app/tests.h> |
| 24 | #include <debug.h> |
| 25 | |
| 26 | void printf_tests(void) |
| 27 | { |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 28 | printf("printf tests\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 29 | |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 30 | printf("numbers:\n"); |
| 31 | printf("int8: %hhd %hhd %hhd\n", -12, 0, 254); |
| 32 | printf("uint8: %hhu %hhu %hhu\n", -12, 0, 254); |
| 33 | printf("int16: %hd %hd %hd\n", -1234, 0, 1234); |
| 34 | printf("uint16:%hu %hu %hu\n", -1234, 0, 1234); |
| 35 | printf("int: %d %d %d\n", -12345678, 0, 12345678); |
| 36 | printf("uint: %u %u %u\n", -12345678, 0, 12345678); |
| 37 | printf("long: %ld %ld %ld\n", -12345678, 0, 12345678); |
| 38 | printf("ulong: %lu %lu %lu\n", -12345678, 0, 12345678); |
| 39 | printf("long: %D %D %D\n", -12345678, 0, 12345678); |
| 40 | printf("ulong: %U %U %U\n", -12345678, 0, 12345678); |
| 41 | printf("longlong: %lli %lli %lli\n", -12345678LL, 0LL, 12345678LL); |
| 42 | printf("ulonglong: %llu %llu %llu\n", -12345678LL, 0LL, 12345678LL); |
| 43 | printf("size_t: %zd %zd %zd\n", -12345678, 0, 12345678); |
| 44 | printf("usize_t: %zu %zu %zu\n", -12345678, 0, 12345678); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 45 | |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 46 | printf("hex:\n"); |
| 47 | printf("uint8: %hhx %hhx %hhx\n", -12, 0, 254); |
| 48 | printf("uint16:%hx %hx %hx\n", -1234, 0, 1234); |
| 49 | printf("uint: %x %x %x\n", -12345678, 0, 12345678); |
| 50 | printf("ulong: %lx %lx %lx\n", -12345678, 0, 12345678); |
| 51 | printf("ulong: %X %X %X\n", -12345678, 0, 12345678); |
| 52 | printf("ulonglong: %llx %llx %llx\n", -12345678LL, 0LL, 12345678LL); |
| 53 | printf("usize_t: %zx %zx %zx\n", -12345678, 0, 12345678); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 54 | |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 55 | printf("alt/sign:\n"); |
| 56 | printf("uint: %#x %#X\n", 0xabcdef, 0xabcdef); |
| 57 | printf("int: %+d %+d\n", 12345678, -12345678); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 58 | |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 59 | printf("formatting\n"); |
| 60 | printf("int: a%8da\n", 12345678); |
| 61 | printf("int: a%9da\n", 12345678); |
| 62 | printf("int: a%-9da\n", 12345678); |
| 63 | printf("int: a%10da\n", 12345678); |
| 64 | printf("int: a%-10da\n", 12345678); |
| 65 | printf("int: a%09da\n", 12345678); |
| 66 | printf("int: a%010da\n", 12345678); |
| 67 | printf("int: a%6da\n", 12345678); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 68 | |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 69 | printf("a%1sa\n", "b"); |
| 70 | printf("a%9sa\n", "b"); |
| 71 | printf("a%-9sa\n", "b"); |
| 72 | printf("a%5sa\n", "thisisatest"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 73 | |
| 74 | int err; |
| 75 | |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 76 | err = printf("a"); |
| 77 | printf(" returned %d\n", err); |
| 78 | err = printf("ab"); |
| 79 | printf(" returned %d\n", err); |
| 80 | err = printf("abc"); |
| 81 | printf(" returned %d\n", err); |
| 82 | err = printf("abcd"); |
| 83 | printf(" returned %d\n", err); |
| 84 | err = printf("abcde"); |
| 85 | printf(" returned %d\n", err); |
| 86 | err = printf("abcdef"); |
| 87 | printf(" returned %d\n", err); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | |