comparison packages/language/c/libc/current/tests/string/strncpy1.c @ 0:3111d98ba7b3 ecos-v1_1-release

Initial commit of eCos version 1.1
author jlarmour
date Tue, 11 May 1999 11:16:07 +0000
parents
children 443894e2e912
comparison
equal deleted inserted replaced
-1:000000000000 0:3111d98ba7b3
1 //=================================================================
2 //
3 // strncpy1.c
4 //
5 // Testcase for C library strncpy()
6 //
7 //=================================================================
8 //####COPYRIGHTBEGIN####
9 //
10 // -------------------------------------------
11 // The contents of this file are subject to the Cygnus eCos Public License
12 // Version 1.0 (the "License"); you may not use this file except in
13 // compliance with the License. You may obtain a copy of the License at
14 // http://sourceware.cygnus.com/ecos
15 //
16 // Software distributed under the License is distributed on an "AS IS"
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
18 // License for the specific language governing rights and limitations under
19 // the License.
20 //
21 // The Original Code is eCos - Embedded Cygnus Operating System, released
22 // September 30, 1998.
23 //
24 // The Initial Developer of the Original Code is Cygnus. Portions created
25 // by Cygnus are Copyright (C) 1998 Cygnus Solutions. All Rights Reserved.
26 // -------------------------------------------
27 //
28 //####COPYRIGHTEND####
29 //=================================================================
30 //#####DESCRIPTIONBEGIN####
31 //
32 // Author(s): ctarpy@cygnus.co.uk, jlarmour@cygnus.co.uk
33 // Contributors: jlarmour@cygnus.co.uk
34 // Date: 1998/6/3
35 // Description: Contains testcode for C library strncpy() function
36 //
37 //
38 //####DESCRIPTIONEND####
39
40 // Declarations for test system:
41 //
42 // TESTCASE_TYPE=CYG_TEST_MODULE
43 // COMPOUND_TESTCASE
44
45
46 // CONFIGURATION
47
48 #include <pkgconf/libc.h> // Configuration header
49
50 // INCLUDES
51
52 #include <string.h>
53 #include <cyg/infra/testcase.h>
54 #include <sys/cstartup.h> // C library initialisation
55
56
57 // FUNCTIONS
58
59
60 externC void
61 cyg_package_start( void )
62 {
63 #ifdef CYGPKG_LIBC
64 cyg_iso_c_start();
65 #else
66 (void)main(0, NULL);
67 #endif
68 } // cyg_package_start()
69
70
71
72 // Functions to avoid having to use libc strings
73
74 #ifdef CYGPKG_LIBC
75 static int my_strlen(const char *s)
76 {
77 const char *ptr;
78
79 ptr = s;
80 for ( ptr=s ; *ptr != '\0' ; ptr++ )
81 ;
82
83 return (int)(ptr-s);
84 } // my_strlen()
85
86
87 static int my_strcmp(const char *s1, const char *s2)
88 {
89 for ( ; *s1 == *s2 ; s1++,s2++ )
90 {
91 if ( *s1 == '\0' )
92 break;
93 } // for
94
95 return (*s1 - *s2);
96 } // my_strcmp()
97
98
99 static char *my_strcpy(char *s1, const char *s2)
100 {
101 while (*s2 != '\0') {
102 *(s1++) = *(s2++);
103 }
104 *s1 = '\0';
105
106 return s1;
107 } // my_strcpy()
108 #endif // ifdef CYGPKG_LIBC
109
110
111 int main( int argc, char *argv[] )
112 {
113 #ifdef CYGPKG_LIBC
114 char x[300];
115 char y[300];
116 char *ret;
117 #endif
118
119 CYG_TEST_INIT();
120
121 CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library "
122 "strncpy() function");
123 CYG_TEST_INFO("This testcase provides simple basic tests");
124
125 #ifdef CYGPKG_LIBC
126
127 // Check 1
128 my_strcpy(x, "Nine rings for men doomed to die");
129 ret = strncpy(y, "Nine rings for men doomed to die", my_strlen(x));
130 CYG_TEST_PASS_FAIL( (my_strcmp(x, y) == 0), "Simple copy" );
131 // Check return value
132 CYG_TEST_PASS_FAIL( (y == ret), "Simple copy return value" );
133
134
135 // Check 2
136 my_strcpy(x, "");
137 my_strcpy(y, "Seven rings for the dwarves in their halls of stone");
138 ret = strncpy(y, x, 1);
139 CYG_TEST_PASS_FAIL( (my_strcmp(y, "") == 0), "Copy empty string" );
140 // Check return value
141 CYG_TEST_PASS_FAIL( (y == ret), "Copy empty string return value" );
142
143 // Check 3
144 my_strcpy(x, "");
145 my_strcpy(y, "Seven rings for the dwarves in their halls of stone");
146 ret = strncpy(y, x, 0);
147 if (my_strcmp(y, "Seven rings for the dwarves in their halls of stone") == 0)
148 CYG_TEST_PASS("Copy 0 characters");
149 else
150 CYG_TEST_FAIL("Copy 0 characters");
151 // Check return value
152 CYG_TEST_PASS_FAIL( (y == ret), "Copy empty string return value" );
153
154
155 #else // ifndef CYGPKG_LIBC
156 CYG_TEST_PASS("Testing is not applicable to this configuration");
157 #endif // ifndef CYGPKG_LIBC
158
159 CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for C library "
160 "strncpy() function");
161
162 } // main()
163
164 // EOF strncpy1.c