1 /*
2  * Copyright (c) 2015 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <ztest.h>
8 
9 #define BUF_SZ 1024
10 
11 static int pos;
12 char pk_console[BUF_SZ];
13 
14 void __printk_hook_install(int (*fn)(int));
15 void *__printk_get_hook(void);
16 int (*_old_char_out)(int);
17 
18 #if defined(CONFIG_CBPRINTF_FULL_INTEGRAL)
19 char *expected = "22 113 10000 32768 40000 22\n"
20 		 "p 112 -10000 -32768 -40000 -22\n"
21 		 "0x1 0x01 0x0001 0x00000001 0x0000000000000001\n"
22 		 "0x1 0x 1 0x   1 0x       1\n"
23 		 "42 42 0042 00000042\n"
24 		 "-42 -42 -042 -0000042\n"
25 		 "42 42   42       42\n"
26 		 "42 42 0042 00000042\n"
27 		 "255     42    abcdef        42\n"
28 		 "68719476735 -1 18446744073709551615 ffffffffffffffff\n"
29 		 "0xcafebabe 0xbeef 0x2a\n"
30 ;
31 #elif defined(CONFIG_CBPRINTF_COMPLETE)
32 char *expected = "22 113 10000 32768 40000 %llu\n"
33 		 "p 112 -10000 -32768 -40000 %lld\n"
34 		 "0x1 0x01 0x0001 0x00000001 0x0000000000000001\n"
35 		 "0x1 0x 1 0x   1 0x       1\n"
36 		 "42 42 0042 00000042\n"
37 		 "-42 -42 -042 -0000042\n"
38 		 "42 42   42       42\n"
39 		 "42 42 0042 00000042\n"
40 		 "255     42    abcdef        42\n"
41 		 "%lld %lld %llu %llx\n"
42 		 "0xcafebabe 0xbeef 0x2a\n"
43 ;
44 #elif defined(CONFIG_CBPRINTF_NANO)
45 char *expected = "22 113 10000 32768 40000 22\n"
46 		 "p 112 -10000 -32768 -40000 -22\n"
47 		 "0x1 0x01 0x0001 0x00000001 0x0000000000000001\n"
48 		 "0x1 0x 1 0x   1 0x       1\n"
49 		 "42 42 0042 00000042\n"
50 		 "-42 -42 -042 -0000042\n"
51 		 "42 42   42       42\n"
52 		 "42 42 0042 00000042\n"
53 		 "255     42    abcdef        42\n"
54 		 "ERR -1 ERR ERR\n"
55 		 "0xcafebabe 0xbeef 0x2a\n"
56 ;
57 #endif
58 
59 size_t stv = 22;
60 unsigned char uc = 'q';
61 unsigned short int usi = 10000U;
62 unsigned int ui = 32768U;
63 unsigned long ul = 40000;
64 
65 /* FIXME
66  * we know printk doesn't have full support for 64-bit values.
67  * at least show it can print uint64_t values less than 32-bits wide
68  */
69 unsigned long long ull = 22;
70 
71 char c = 'p';
72 signed short int ssi = -10000;
73 signed int si = -32768;
74 signed long sl = -40000;
75 signed long long sll = -22;
76 
77 uint32_t hex = 0xCAFEBABE;
78 
79 void *ptr = (void *)0xBEEF;
80 
ram_console_out(int character)81 static int ram_console_out(int character)
82 {
83 	pk_console[pos] = (char)character;
84 	pos = (pos + 1) % BUF_SZ;
85 	return _old_char_out(character);
86 }
87 /**
88  * @addtogroup kernel_common_tests
89  * @{
90  */
91 
92 /**
93  * @brief Test printk() functionality
94  *
95  * @see printk(), __printk_get_hook(),
96  * __printk_hook_install(), snprintk()
97  *
98  */
test_printk(void)99 void test_printk(void)
100 {
101 	int count;
102 
103 	if (IS_ENABLED(CONFIG_LOG_PRINTK)) {
104 		ztest_test_skip();
105 	}
106 
107 	_old_char_out = __printk_get_hook();
108 	__printk_hook_install(ram_console_out);
109 
110 	printk("%zu %hhu %hu %u %lu %llu\n", stv, uc, usi, ui, ul, ull);
111 	printk("%c %hhd %hd %d %ld %lld\n", c, c, ssi, si, sl, sll);
112 	printk("0x%x 0x%02x 0x%04x 0x%08x 0x%016x\n", 1, 1, 1, 1, 1);
113 	printk("0x%x 0x%2x 0x%4x 0x%8x\n", 1, 1, 1, 1);
114 	printk("%d %02d %04d %08d\n", 42, 42, 42, 42);
115 	printk("%d %02d %04d %08d\n", -42, -42, -42, -42);
116 	printk("%u %2u %4u %8u\n", 42, 42, 42, 42);
117 	printk("%u %02u %04u %08u\n", 42, 42, 42, 42);
118 	printk("%-8u%-6d%-4x  %8d\n", 0xFF, 42, 0xABCDEF, 42);
119 	printk("%lld %lld %llu %llx\n", 0xFFFFFFFFFULL, -1LL, -1ULL, -1ULL);
120 	printk("0x%x %p %-2p\n", hex, ptr, (char *)42);
121 
122 	pk_console[pos] = '\0';
123 	zassert_true((strcmp(pk_console, expected) == 0), "printk failed");
124 
125 	(void)memset(pk_console, 0, sizeof(pk_console));
126 	count = 0;
127 
128 	count += snprintk(pk_console + count, sizeof(pk_console) - count,
129 			  "%zu %hhu %hu %u %lu %llu\n",
130 			  stv, uc, usi, ui, ul, ull);
131 	count += snprintk(pk_console + count, sizeof(pk_console) - count,
132 			  "%c %hhd %hd %d %ld %lld\n", c, c, ssi, si, sl, sll);
133 	count += snprintk(pk_console + count, sizeof(pk_console) - count,
134 			  "0x%x 0x%02x 0x%04x 0x%08x 0x%016x\n", 1, 1, 1, 1, 1);
135 	count += snprintk(pk_console + count, sizeof(pk_console) - count,
136 			  "0x%x 0x%2x 0x%4x 0x%8x\n", 1, 1, 1, 1);
137 	count += snprintk(pk_console + count, sizeof(pk_console) - count,
138 			  "%d %02d %04d %08d\n", 42, 42, 42, 42);
139 	count += snprintk(pk_console + count, sizeof(pk_console) - count,
140 			  "%d %02d %04d %08d\n", -42, -42, -42, -42);
141 	count += snprintk(pk_console + count, sizeof(pk_console) - count,
142 			  "%u %2u %4u %8u\n", 42, 42, 42, 42);
143 	count += snprintk(pk_console + count, sizeof(pk_console) - count,
144 			  "%u %02u %04u %08u\n", 42, 42, 42, 42);
145 	count += snprintk(pk_console + count, sizeof(pk_console) - count,
146 			  "%-8u%-6d%-4x  %8d\n",
147 			  0xFF, 42, 0xABCDEF, 42);
148 	count += snprintk(pk_console + count, sizeof(pk_console) - count,
149 			  "%lld %lld %llu %llx\n",
150 			  0xFFFFFFFFFULL, -1LL, -1ULL, -1ULL);
151 	count += snprintk(pk_console + count, sizeof(pk_console) - count,
152 			  "0x%x %p %-2p\n", hex, ptr, (char *)42);
153 	pk_console[count] = '\0';
154 	zassert_true((strcmp(pk_console, expected) == 0), "snprintk failed");
155 }
156 /**
157  * @}
158  */
159