1 /*
2  * Copyright (c) 2008-2014 Travis Geiselbrecht
3  *
4  * Use of this source code is governed by a MIT-style
5  * license that can be found in the LICENSE file or at
6  * https://opensource.org/licenses/MIT
7  */
8 #include <app/tests.h>
9 #include <lk/err.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <lk/debug.h>
13 
14 // We're doing a few things here that the compiler doesn't like, so disable printf warnings
15 #pragma GCC diagnostic push
16 #pragma GCC diagnostic ignored "-Wformat"
17 
printf_tests(int argc,const console_cmd_args * argv)18 int printf_tests(int argc, const console_cmd_args *argv) {
19     printf("printf tests\n");
20 
21     printf("numbers:\n");
22     printf("int8:  %hhd %hhd %hhd\n", -12, 0, 254);
23     printf("uint8: %hhu %hhu %hhu\n", -12, 0, 254);
24     printf("int16: %hd %hd %hd\n", -1234, 0, 1234);
25     printf("uint16:%hu %hu %hu\n", -1234, 0, 1234);
26     printf("int:   %d %d %d\n", -12345678, 0, 12345678);
27     printf("uint:  %u %u %u\n", -12345678, 0, 12345678);
28     printf("long:  %ld %ld %ld\n", -12345678L, 0L, 12345678L);
29     printf("ulong: %lu %lu %lu\n", -12345678UL, 0UL, 12345678UL);
30 
31     printf("longlong: %lli %lli %lli\n", -12345678LL, 0LL, 12345678LL);
32     printf("ulonglong: %llu %llu %llu\n", -12345678LL, 0LL, 12345678LL);
33     printf("ssize_t: %zd %zd %zd\n", (ssize_t)-12345678, (ssize_t)0, (ssize_t)12345678);
34     printf("usize_t: %zu %zu %zu\n", (size_t)-12345678, (size_t)0, (size_t)12345678);
35     printf("intmax_t: %jd %jd %jd\n", (intmax_t)-12345678, (intmax_t)0, (intmax_t)12345678);
36     printf("uintmax_t: %ju %ju %ju\n", (uintmax_t)-12345678, (uintmax_t)0, (uintmax_t)12345678);
37     printf("ptrdiff_t: %td %td %td\n", (ptrdiff_t)-12345678, (ptrdiff_t)0, (ptrdiff_t)12345678);
38     printf("ptrdiff_t (u): %tu %tu %tu\n", (ptrdiff_t)-12345678, (ptrdiff_t)0, (ptrdiff_t)12345678);
39 
40     printf("hex:\n");
41     printf("uint8: %hhx %hhx %hhx\n", -12, 0, 254);
42     printf("uint16:%hx %hx %hx\n", -1234, 0, 1234);
43     printf("uint:  %x %x %x\n", -12345678, 0, 12345678);
44     printf("ulong: %lx %lx %lx\n", -12345678UL, 0UL, 12345678UL);
45     printf("ulong: %X %X %X\n", -12345678, 0, 12345678);
46     printf("ulonglong: %llx %llx %llx\n", -12345678LL, 0LL, 12345678LL);
47     printf("usize_t: %zx %zx %zx\n", (size_t)-12345678, (size_t)0, (size_t)12345678);
48 
49     printf("alt/sign:\n");
50     printf("uint: %#x %#X\n", 0xabcdef, 0xabcdef);
51     printf("int: %+d %+d\n", 12345678, -12345678);
52     printf("int: % d %+d\n", 12345678, 12345678);
53 
54     printf("formatting\n");
55     printf("int: a%8da\n", 12345678);
56     printf("int: a%9da\n", 12345678);
57     printf("int: a%-9da\n", 12345678);
58     printf("int: a%10da\n", 12345678);
59     printf("int: a%-10da\n", 12345678);
60     printf("int: a%09da\n", 12345678);
61     printf("int: a%010da\n", 12345678);
62     printf("int: a%6da\n", 12345678);
63 
64     printf("a%1sa\n", "b");
65     printf("a%9sa\n", "b");
66     printf("a%-9sa\n", "b");
67     printf("a%5sa\n", "thisisatest");
68 
69     printf("%03d\n", -2);       /* '-02' */
70     printf("%0+3d\n", -2);      /* '-02' */
71     printf("%0+3d\n", 2);       /* '+02' */
72     printf("%+3d\n", 2);        /* ' +2' */
73     printf("% 3d\n", -2000);    /* '-2000' */
74     printf("% 3d\n", 2000);     /* ' 2000' */
75     printf("%+3d\n", 2000);     /* '+2000' */
76     printf("%10s\n", "test");   /* '      test' */
77     printf("%010s\n", "test");  /* '      test' */
78     printf("%-10s\n", "test");  /* 'test      ' */
79     printf("%-010s\n", "test"); /* 'test      ' */
80 
81     int err;
82 
83     err = printf("a");
84     printf(" returned %d\n", err);
85     err = printf("ab");
86     printf(" returned %d\n", err);
87     err = printf("abc");
88     printf(" returned %d\n", err);
89     err = printf("abcd");
90     printf(" returned %d\n", err);
91     err = printf("abcde");
92     printf(" returned %d\n", err);
93     err = printf("abcdef");
94     printf(" returned %d\n", err);
95 
96     /* make sure snprintf terminates at the right spot */
97     char buf[32];
98 
99     memset(buf, 0, sizeof(buf));
100     err = sprintf(buf, "0123456789abcdef012345678");
101     printf("sprintf returns %d\n", err);
102     hexdump8(buf, sizeof(buf));
103 
104     memset(buf, 0, sizeof(buf));
105     err = snprintf(buf, 15, "0123456789abcdef012345678");
106     printf("snprintf returns %d\n", err);
107     hexdump8(buf, sizeof(buf));
108 
109     return NO_ERROR;
110 }
111 
112 #include "float_test_vec.c"
113 
printf_tests_float(int argc,const console_cmd_args * argv)114 int printf_tests_float(int argc, const console_cmd_args *argv) {
115     printf("floating point printf tests\n");
116 
117     for (size_t i = 0; i < float_test_vec_size; i++) {
118         PRINT_FLOAT;
119     }
120 
121     return NO_ERROR;
122 }
123 
124 #pragma GCC diagnostic pop
125 
126