Mercurial > ecos
comparison packages/language/c/libc/stdio/current/tests/sprintf2.c @ 115:6ed91473a1cd ecos-sw-2000-08-21
Merge from eCos master repository on 2000-08-21-22:40:54-BST
| author | jlarmour |
|---|---|
| date | Fri, 25 Aug 2000 17:32:38 +0000 |
| parents | |
| children | e0c0827131d1 |
comparison
equal
deleted
inserted
replaced
| 114:5ad2b71d525e | 115:6ed91473a1cd |
|---|---|
| 1 //================================================================= | |
| 2 // | |
| 3 // sprintf2.c | |
| 4 // | |
| 5 // Testcase for C library sprintf() | |
| 6 // | |
| 7 //================================================================= | |
| 8 //####COPYRIGHTBEGIN#### | |
| 9 // | |
| 10 // ------------------------------------------- | |
| 11 // The contents of this file are subject to the Red Hat eCos Public License | |
| 12 // Version 1.1 (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://www.redhat.com/ | |
| 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 Configurable Operating System, | |
| 22 // released September 30, 1998. | |
| 23 // | |
| 24 // The Initial Developer of the Original Code is Red Hat. | |
| 25 // Portions created by Red Hat are | |
| 26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc. | |
| 27 // All Rights Reserved. | |
| 28 // ------------------------------------------- | |
| 29 // | |
| 30 //####COPYRIGHTEND#### | |
| 31 //================================================================= | |
| 32 //#####DESCRIPTIONBEGIN#### | |
| 33 // | |
| 34 // Author(s): ctarpy, jlarmour | |
| 35 // Contributors: | |
| 36 // Date: 2000-04-20 | |
| 37 // Description: Contains testcode for C library sprintf() function | |
| 38 // | |
| 39 // | |
| 40 //####DESCRIPTIONEND#### | |
| 41 | |
| 42 // CONFIGURATION | |
| 43 | |
| 44 #include <pkgconf/libc_stdio.h> // Configuration header | |
| 45 | |
| 46 // INCLUDES | |
| 47 | |
| 48 #include <stdio.h> | |
| 49 #include <cyg/infra/testcase.h> | |
| 50 | |
| 51 // FUNCTIONS | |
| 52 | |
| 53 // Functions to avoid having to use libc strings | |
| 54 | |
| 55 static int | |
| 56 my_strlen(const char *s) | |
| 57 { | |
| 58 const char *ptr; | |
| 59 | |
| 60 ptr = s; | |
| 61 for ( ptr=s ; *ptr != '\0' ; ptr++ ) | |
| 62 ; | |
| 63 | |
| 64 return (int)(ptr-s); | |
| 65 } // my_strlen() | |
| 66 | |
| 67 | |
| 68 static int | |
| 69 my_strcmp(const char *s1, const char *s2) | |
| 70 { | |
| 71 for ( ; *s1 == *s2 ; s1++,s2++ ) | |
| 72 { | |
| 73 if ( *s1 == '\0' ) | |
| 74 break; | |
| 75 } // for | |
| 76 | |
| 77 return (*s1 - *s2); | |
| 78 } // my_strcmp() | |
| 79 | |
| 80 | |
| 81 static char * | |
| 82 my_strcpy(char *s1, const char *s2) | |
| 83 { | |
| 84 while (*s2 != '\0') { | |
| 85 *(s1++) = *(s2++); | |
| 86 } | |
| 87 *s1 = '\0'; | |
| 88 | |
| 89 return s1; | |
| 90 } // my_strcpy() | |
| 91 | |
| 92 | |
| 93 | |
| 94 static void | |
| 95 test( CYG_ADDRWORD data ) | |
| 96 { | |
| 97 static char x[200]; | |
| 98 static char y[200]; | |
| 99 static char z[200]; | |
| 100 int ret; | |
| 101 int tmp; | |
| 102 int *ptr; | |
| 103 | |
| 104 // Check 1 | |
| 105 my_strcpy(x, "I'm afraid the shield generator"); | |
| 106 ptr = &tmp; | |
| 107 ret = sprintf(y, "%s%n will be quite operational - %5d%%%c%05X", x, | |
| 108 ptr, 13, '5', 0x89ab); | |
| 109 my_strcpy( z, "I'm afraid the shield generator will be " | |
| 110 "quite operational - 13%5089AB" ); | |
| 111 CYG_TEST_PASS_FAIL(my_strcmp(y,z) == 0, "%s%n%d%%%c%0X test"); | |
| 112 | |
| 113 CYG_TEST_PASS_FAIL(ret == my_strlen(z), | |
| 114 "%s%n%d%%%c%0X test return code" ); | |
| 115 | |
| 116 CYG_TEST_PASS_FAIL(tmp==31, "%n test"); | |
| 117 | |
| 118 // Check 2 | |
| 119 ret = sprintf(y, "|%5d|%10s|%03d|%c|%o|", 2, "times", 6, '=', 10 ); | |
| 120 my_strcpy(z, "| 2| times|006|=|12|"); | |
| 121 | |
| 122 CYG_TEST_PASS_FAIL(my_strcmp(y,z) == 0, "|%5d|%10s|%03d|%c|%o| test"); | |
| 123 | |
| 124 CYG_TEST_PASS_FAIL(ret == my_strlen(z), | |
| 125 "|%5d|%10s|%03d|%c|%o| test return code" ); | |
| 126 | |
| 127 // Check 3 | |
| 128 ret = snprintf(y, 19, "print up to here >< and not this bit" ); | |
| 129 my_strcpy(z, "print up to here >"); | |
| 130 CYG_TEST_PASS_FAIL(my_strcmp(y,z) == 0, "simple snprintf test #1"); | |
| 131 CYG_TEST_PASS_FAIL(ret == my_strlen(z), | |
| 132 "simple snprintf test #1 return code" ); | |
| 133 | |
| 134 // Check 4 | |
| 135 ret = snprintf(y, 31, "print a bit of this number: %05d nyer", 1234); | |
| 136 my_strcpy(z, "print a bit of this number: 01"); | |
| 137 CYG_TEST_PASS_FAIL(my_strcmp(y,z) == 0, "simple snprintf test #2"); | |
| 138 CYG_TEST_PASS_FAIL(ret == my_strlen(z), | |
| 139 "simple snprintf test #2 return code" ); | |
| 140 | |
| 141 #ifdef CYGSEM_LIBC_STDIO_PRINTF_FLOATING_POINT | |
| 142 | |
| 143 CYG_TEST_INFO("Starting floating point specific tests"); | |
| 144 | |
| 145 // Check 5 | |
| 146 ret = sprintf(y, "|%5f|%10s|%03d|%c|%+-5.2G|%010.3G|", | |
| 147 2.0, "times", 6, '=', 12.0, -2.3451e-6 ); | |
| 148 my_strcpy(z, "|2.000000| times|006|=|+12 |-02.35E-06|"); | |
| 149 | |
| 150 CYG_TEST_PASS_FAIL(my_strcmp(y,z) == 0, | |
| 151 "|%5f|%10s|%03d|%c|%+-5.2G|%010.3G| test"); | |
| 152 | |
| 153 CYG_TEST_PASS_FAIL(ret == my_strlen(z), | |
| 154 "|%5f|%10s|%03d|%c|%+-5.2G|%010.3G| test " | |
| 155 "return code" ); | |
| 156 | |
| 157 // Check 6 | |
| 158 ret = snprintf(y, 20, "bit of this: %g double", 6.431e8); | |
| 159 my_strcpy(z, "bit of this: 6.431e"); | |
| 160 CYG_TEST_PASS_FAIL(my_strcmp(y,z) == 0, | |
| 161 "snprintf double test #1"); | |
| 162 | |
| 163 CYG_TEST_PASS_FAIL(ret == my_strlen(z), | |
| 164 "snprintf double test #1 return code"); | |
| 165 | |
| 166 #endif // ifdef CYGSEM_LIBC_STDIO_PRINTF_FLOATING_POINT | |
| 167 | |
| 168 CYG_TEST_FINISH("Finished tests from testcase " __FILE__ | |
| 169 " for C library sprintf() function"); | |
| 170 | |
| 171 } // test() | |
| 172 | |
| 173 int | |
| 174 main(int argc, char *argv[]) | |
| 175 { | |
| 176 CYG_TEST_INIT(); | |
| 177 | |
| 178 CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C " | |
| 179 "library sprintf() function"); | |
| 180 CYG_TEST_INFO("These test combinations of sprintf() features"); | |
| 181 | |
| 182 test(0); | |
| 183 | |
| 184 return 0; | |
| 185 } // main() | |
| 186 | |
| 187 // EOF sprintf2.c |
