1 |
786 |
skrzyp |
//=================================================================
|
2 |
|
|
//
|
3 |
|
|
// snprintf.c
|
4 |
|
|
//
|
5 |
|
|
// Testcase for C library snprintf() implementation
|
6 |
|
|
//
|
7 |
|
|
//=================================================================
|
8 |
|
|
// ####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
// -------------------------------------------
|
10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2009 Free Software Foundation, Inc.
|
12 |
|
|
//
|
13 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
14 |
|
|
// the terms of the GNU General Public License as published by the Free
|
15 |
|
|
// Software Foundation; either version 2 or (at your option) any later
|
16 |
|
|
// version.
|
17 |
|
|
//
|
18 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
19 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
20 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
21 |
|
|
// for more details.
|
22 |
|
|
//
|
23 |
|
|
// You should have received a copy of the GNU General Public License
|
24 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
25 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
26 |
|
|
//
|
27 |
|
|
// As a special exception, if other files instantiate templates or use
|
28 |
|
|
// macros or inline functions from this file, or you compile this file
|
29 |
|
|
// and link it with other works to produce a work based on this file,
|
30 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
31 |
|
|
// the GNU General Public License. However the source code for this file
|
32 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
33 |
|
|
// General Public License v2.
|
34 |
|
|
//
|
35 |
|
|
// This exception does not invalidate any other reasons why a work based
|
36 |
|
|
// on this file might be covered by the GNU General Public License.
|
37 |
|
|
// -------------------------------------------
|
38 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
39 |
|
|
//=================================================================
|
40 |
|
|
//#####DESCRIPTIONBEGIN####
|
41 |
|
|
//
|
42 |
|
|
// Author(s): Uwe Kindler
|
43 |
|
|
// Contributors:
|
44 |
|
|
// Date: 2009-08-05
|
45 |
|
|
// Description: Contains testcode for C library snprintf() function
|
46 |
|
|
//
|
47 |
|
|
//
|
48 |
|
|
//####DESCRIPTIONEND####
|
49 |
|
|
|
50 |
|
|
// CONFIGURATION
|
51 |
|
|
|
52 |
|
|
#include <pkgconf/libc_stdio.h> // Configuration header
|
53 |
|
|
|
54 |
|
|
// INCLUDES
|
55 |
|
|
|
56 |
|
|
#include <stdio.h>
|
57 |
|
|
#include <cyg/infra/testcase.h>
|
58 |
|
|
|
59 |
|
|
static int my_strnlen(const char *s, size_t maxlen)
|
60 |
|
|
{
|
61 |
|
|
const char *ptr;
|
62 |
|
|
const char *endptr;
|
63 |
|
|
|
64 |
|
|
ptr = s;
|
65 |
|
|
endptr = s + maxlen;
|
66 |
|
|
while ((*ptr != '\0') && (ptr != endptr))
|
67 |
|
|
{
|
68 |
|
|
ptr++;
|
69 |
|
|
}
|
70 |
|
|
|
71 |
|
|
return (int)(ptr-s);
|
72 |
|
|
} // my_strlen()
|
73 |
|
|
|
74 |
|
|
static void
|
75 |
|
|
test( CYG_ADDRWORD data )
|
76 |
|
|
{
|
77 |
|
|
static char x[32];
|
78 |
|
|
static char y[4];
|
79 |
|
|
int xret;
|
80 |
|
|
int yret;
|
81 |
|
|
int xstrlen;
|
82 |
|
|
int ystrlen;
|
83 |
|
|
|
84 |
|
|
// fill buffer to ensure that there are no zeros in the buffers
|
85 |
|
|
memset(x, 0xFF, sizeof(x));
|
86 |
|
|
memset(y, 0xFF, sizeof(y));
|
87 |
|
|
|
88 |
|
|
// print into a buffer with sufficient size
|
89 |
|
|
xret = snprintf(x, sizeof(x), "%d:%d:%d:%d", 1, 2, 3, 4);
|
90 |
|
|
xstrlen = my_strnlen(x, sizeof(x));
|
91 |
|
|
|
92 |
|
|
// print into a buffer that is too small
|
93 |
|
|
yret = snprintf(y, sizeof(y), "%d:%d:%d:%d", 1, 2, 3, 4);
|
94 |
|
|
ystrlen = my_strnlen(y, sizeof(y));
|
95 |
|
|
|
96 |
|
|
CYG_TEST_PASS_FAIL(xret == xstrlen, "[buffer > strlen] return code");
|
97 |
|
|
|
98 |
|
|
#ifdef CYGIMP_LIBC_STDIO_C99_SNPRINTF
|
99 |
|
|
// C99 compliant implementation returns the number of characters that
|
100 |
|
|
// would have been written had size been sufficiently large,
|
101 |
|
|
// not counting the terminating nul character
|
102 |
|
|
CYG_TEST_PASS_FAIL(xret == yret, "[buffer < strlen] return code");
|
103 |
|
|
CYG_TEST_INFO("C99 compliant implementation of snprintf()");
|
104 |
|
|
#else
|
105 |
|
|
// default eCos implementation returns number of bytes written into
|
106 |
|
|
// the buffer without terminating nul character
|
107 |
|
|
CYG_TEST_PASS_FAIL(yret == ystrlen, "[buffer < strlen] return code");
|
108 |
|
|
CYG_TEST_INFO("Default implementation of snprintf() (no C99 compliance)");
|
109 |
|
|
#endif
|
110 |
|
|
|
111 |
|
|
|
112 |
|
|
CYG_TEST_FINISH("Finished tests from testcase " __FILE__
|
113 |
|
|
" for C library snprintf() function return values");
|
114 |
|
|
|
115 |
|
|
} // test()
|
116 |
|
|
|
117 |
|
|
int
|
118 |
|
|
main(int argc, char *argv[])
|
119 |
|
|
{
|
120 |
|
|
CYG_TEST_INIT();
|
121 |
|
|
|
122 |
|
|
CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C "
|
123 |
|
|
"library snprintf() function return values");
|
124 |
|
|
CYG_TEST_INFO("These test return values of snprinf() family of functions");
|
125 |
|
|
|
126 |
|
|
test(0);
|
127 |
|
|
|
128 |
|
|
return 0;
|
129 |
|
|
} // main()
|
130 |
|
|
|
131 |
|
|
// EOF snprintf.c
|