source: rtems/testsuites/sptests/spprintk/init.c @ 1082798

5
Last change on this file since 1082798 was 1082798, checked in by Sebastian Huber <sebastian.huber@…>, on 10/23/17 at 11:30:44

score: Add _IO_Printf() and _IO_Vprintf()

The previous vprintk() implementation had a questionable licence header,
lacks support for the 'z' and 'j' format specifiers, is not robust
against invalid format specifiers, uses a global variable for output.
Replace it with a stripped down version of the FreeBSD kernel kvprintf()
function.

The new implementation allows a low overhead rtems_snprintf() if
necessary.

Update #3199.
Close #3216.

  • Property mode set to 100644
File size: 5.8 KB
Line 
1/*
2 *  Exercise putk, printk, and getchark
3 *
4 *  COPYRIGHT (c) 1989-2010.
5 *  On-Line Applications Research Corporation (OAR).
6 *
7 *  The license and distribution terms for this file may be
8 *  found in the file LICENSE in this distribution or at
9 *  http://www.rtems.org/license/LICENSE.
10 */
11
12#ifdef HAVE_CONFIG_H
13#include "config.h"
14#endif
15
16#include <rtems/score/io.h>
17
18/*
19 * Undefined the RTEMS_PRINTFLIKE and make it nothing. The test code
20 * contained in the file is suppose to be wrong.
21 */
22#undef RTEMS_PRINTFLIKE
23#define RTEMS_PRINTFLIKE(_a, _b)
24
25#include <rtems/bspIo.h>
26#include <tmacros.h>
27
28const char rtems_test_name[] = "SPPRINTK";
29
30static int test_getchar(void)
31{
32  return 0x35;
33}
34
35static void do_getchark(void)
36{
37  int                                sc;
38  BSP_polling_getchar_function_type  poll_char;
39
40  poll_char = BSP_poll_char;
41
42  BSP_poll_char = NULL;
43
44  putk( "getchark - NULL getchar method - return -1" );
45  sc = getchark();
46  rtems_test_assert( sc == -1 );
47
48  putk( "getchark - test getchar method - returns 0x35" );
49  BSP_poll_char = test_getchar;
50  sc = getchark();
51  rtems_test_assert( sc == 0x35 );
52
53  BSP_poll_char = poll_char;
54}
55
56static void do_putk(void)
57{
58  putk( "This is a test of putk" );
59}
60
61static void do_printk(void)
62{
63  long lm = 2147483647L;
64  unsigned long ulm = 4294967295UL;
65  long long llm = 9223372036854775807LL;
66  long long ullm = 18446744073709551615ULL;
67
68  printk( "bad format                   -- %%q in parentheses (%q)\n" );
69
70  printk( "bad format                   -- %%lq in parentheses (%lq)\n" );
71
72  printk( "%%o of 16                     -- %o\n", 16 );
73  printk( "%%i of 16                     -- %i\n", 16 );
74  printk( "%%d of 16                     -- %d\n", 16 );
75  printk( "'%%-3d' of 16                 -- '%-3d'\n", 16 );
76  printk( "'%%3d' of 16                  -- '%3d'\n", 16 );
77  printk( "%%u of 16                     -- %u\n", 16 );
78  printk( "%%X of 16                     -- %X\n", 16 );
79  printk( "%%x of 16                     -- %x\n", 16 );
80  printk( "%%p of 0x1234                 -- %p\n", (void *)0x1234 );
81
82  /* long */
83  printk( "%%lo of 2147483647            -- %lo\n", lm );
84  printk( "%%li of 2147483647            -- %li\n", lm );
85  printk( "%%lu of 2147483647            -- %lu\n", lm );
86  printk( "%%lx of 2147483647            -- %lx\n", lm );
87  printk( "%%lo of -2147483648           -- %lo\n", -lm - 1L );
88  printk( "%%li of -2147483648           -- %li\n", -lm - 1L );
89  printk( "%%lx of -2147483648           -- %lx\n", -lm - 1L );
90  printk( "%%lo of 4294967295            -- %lo\n", ulm );
91  printk( "%%lu of 4294967295            -- %lu\n", ulm );
92  printk( "%%lx of 4294967295            -- %lx\n", ulm );
93
94  /* long long */
95  printk( "%%llo of 9223372036854775807  -- %llo\n", llm );
96  printk( "%%lli of 9223372036854775807  -- %lli\n", llm );
97  printk( "%%llu of 9223372036854775807  -- %llu\n", llm );
98  printk( "%%llx of 9223372036854775807  -- %llx\n", llm );
99  printk( "%%llo of -9223372036854775808 -- %llo\n", -llm - 1LL );
100  printk( "%%lli of -9223372036854775808 -- %lli\n", -llm - 1LL );
101  printk( "%%llx of -9223372036854775808 -- %llx\n", -llm - 1LL );
102  printk( "%%llo of 18446744073709551615 -- %llo\n", ullm );
103  printk( "%%llu of 18446744073709551615 -- %llu\n", ullm );
104  printk( "%%llx of 18446744073709551615 -- %llx\n", ullm );
105
106  /* negative numbers */
107  printk( "%%d of -16                    -- %d\n", -16 );
108  printk( "%%d of -16                    -- %-3d\n", -16 );
109  printk( "%%u of -16                    -- %u\n", -16 );
110
111  /* string formats */
112  printk( "%%s of Mary Had a Little Lamb -- '%s'\n",
113          "Mary Had a Little Lamb" );
114  printk( "%%s of NULL                   -- '%s'\n", NULL );
115  printk( "%%12s of joel                 -- '%20s'\n", "joel" );
116  printk( "%%4s of joel                  -- '%4s'\n", "joel" );
117  printk( "%%-12s of joel                -- '%-20s'\n", "joel" );
118  printk( "%%-4s of joel                 -- '%-4s'\n", "joel" );
119  printk( "%%c of X                      -- '%c'\n", 'X' );
120  printk( "%%hhu of X                    -- %hhu\n", 'X' );
121}
122
123typedef struct {
124  char buf[128];
125  size_t i;
126} test_context;
127
128static test_context test_instance;
129
130static void clear( test_context *ctx )
131{
132  ctx->i = 0;
133  memset( ctx->buf, 0, sizeof( ctx->buf ) );
134}
135
136static void put_char( int c, void *arg )
137{
138  test_context *ctx;
139
140  ctx = arg;
141
142  if ( ctx->i < sizeof( ctx->buf ) ) {
143    ctx->buf[ ctx->i ] = (char) c;
144    ++ctx->i;
145  }
146}
147
148static test_context test_instance;
149
150static void test_io_printf( test_context *ctx )
151{
152  int i;
153  intmax_t j;
154  long long ll;
155  long l;
156  size_t z;
157  ptrdiff_t t;
158
159  clear( ctx );
160  i = 123;
161  _IO_Printf( put_char, ctx, "%i", i );
162  rtems_test_assert( strcmp( ctx->buf, "123" ) == 0 );
163
164  clear( ctx );
165  j = 456;
166  _IO_Printf( put_char, ctx, "%ji", j );
167  rtems_test_assert( strcmp( ctx->buf, "456" ) == 0 );
168
169  clear( ctx );
170  ll = 789;
171  _IO_Printf( put_char, ctx, "%lli", ll );
172  rtems_test_assert( strcmp( ctx->buf, "789" ) == 0 );
173
174  clear( ctx );
175  l = 101112;
176  _IO_Printf( put_char, ctx, "%li", l );
177  rtems_test_assert( strcmp( ctx->buf, "101112" ) == 0 );
178
179  clear( ctx );
180  z = 131415;
181  _IO_Printf( put_char, ctx, "%zi", z );
182  rtems_test_assert( strcmp( ctx->buf, "131415" ) == 0 );
183
184  clear( ctx );
185  t = 161718;
186  _IO_Printf( put_char, ctx, "%ti", t );
187  rtems_test_assert( strcmp( ctx->buf, "161718" ) == 0 );
188}
189
190static rtems_task Init(
191  rtems_task_argument argument
192)
193{
194  TEST_BEGIN();
195
196  do_putk();
197  putk("");
198
199  do_printk();
200  putk("");
201
202  do_getchark();
203  test_io_printf(&test_instance);
204
205  TEST_END();
206  rtems_test_exit( 0 );
207}
208
209/* configuration information */
210
211#define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
212
213#define CONFIGURE_MAXIMUM_TASKS           1
214
215#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
216
217#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
218
219#define CONFIGURE_INIT
220
221#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.