1 1.1 christos /* Test program for strtol family of funtions, 2 1.8 christos Copyright (C) 2014-2024 Free Software Foundation, Inc. 3 1.1 christos Written by Yury Gribov <y.gribov (at) samsung.com> 4 1.1 christos 5 1.1 christos This file is part of the libiberty library, which is part of GCC. 6 1.1 christos 7 1.1 christos This file is free software; you can redistribute it and/or modify 8 1.1 christos it under the terms of the GNU General Public License as published by 9 1.1 christos the Free Software Foundation; either version 2 of the License, or 10 1.1 christos (at your option) any later version. 11 1.1 christos 12 1.1 christos In addition to the permissions in the GNU General Public License, the 13 1.1 christos Free Software Foundation gives you unlimited permission to link the 14 1.1 christos compiled version of this file into combinations with other programs, 15 1.1 christos and to distribute those combinations without any restriction coming 16 1.1 christos from the use of this file. (The General Public License restrictions 17 1.1 christos do apply in other respects; for example, they cover modification of 18 1.1 christos the file, and distribution when not linked into a combined 19 1.1 christos executable.) 20 1.1 christos 21 1.1 christos This program is distributed in the hope that it will be useful, 22 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 23 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 24 1.1 christos GNU General Public License for more details. 25 1.1 christos 26 1.1 christos You should have received a copy of the GNU General Public License 27 1.1 christos along with this program; if not, write to the Free Software 28 1.1 christos Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. 29 1.1 christos */ 30 1.1 christos 31 1.1 christos #ifdef HAVE_CONFIG_H 32 1.1 christos #include "config.h" 33 1.1 christos #endif 34 1.1 christos #include "libiberty.h" 35 1.1 christos #include <stdio.h> 36 1.1 christos #include <errno.h> 37 1.1 christos #ifdef HAVE_STDLIB_H 38 1.1 christos #include <stdlib.h> 39 1.1 christos #endif 40 1.1 christos #ifdef HAVE_STRING_H 41 1.1 christos #include <string.h> 42 1.1 christos #endif 43 1.1 christos #ifdef HAVE_UNISTD_H 44 1.1 christos #include <unistd.h> 45 1.1 christos #endif 46 1.1 christos 47 1.1 christos #ifndef EXIT_SUCCESS 48 1.1 christos #define EXIT_SUCCESS 0 49 1.1 christos #endif 50 1.1 christos 51 1.1 christos #ifndef EXIT_FAILURE 52 1.1 christos #define EXIT_FAILURE 1 53 1.1 christos #endif 54 1.1 christos 55 1.1 christos 56 1.1 christos /* Test input data. */ 57 1.1 christos 58 1.1 christos enum conversion_fun 59 1.1 christos { 60 1.1 christos STRTOL, 61 1.1 christos STRTOLL, 62 1.1 christos STRTOUL, 63 1.1 christos STRTOULL, 64 1.1 christos }; 65 1.1 christos 66 1.1 christos #ifdef HAVE_LONG_LONG 67 1.1 christos typedef unsigned long long integer_type; 68 1.1 christos #else 69 1.1 christos typedef unsigned long integer_type; 70 1.1 christos #endif 71 1.1 christos 72 1.1 christos struct test_data_t 73 1.1 christos { 74 1.1 christos enum conversion_fun fun; 75 1.1 christos const char *nptr; 76 1.1 christos int base; 77 1.1 christos integer_type res; 78 1.1 christos int errnum; 79 1.1 christos }; 80 1.1 christos 81 1.1 christos const struct test_data_t test_data[] = { 82 1.1 christos { STRTOL, "0x123", 0, 0x123L, 0 }, 83 1.1 christos { STRTOL, "123", 0, 123L, 0 }, 84 1.1 christos { STRTOL, "0123", 0, 0123L, 0 }, 85 1.1 christos { STRTOL, "0x7FFFFFFF", 0, 0x7fffffffL, 0 }, 86 1.1 christos { STRTOL, "-0x80000000", 0, -0x80000000L, 0 }, 87 1.1 christos { STRTOUL, "0x123", 0, 0x123UL, 0 }, 88 1.1 christos { STRTOUL, "123", 0, 123UL, 0 }, 89 1.1 christos { STRTOUL, "0123", 0, 0123UL, 0 }, 90 1.1 christos { STRTOUL, "0xFFFFFFFF", 0, 0xffffffffUL, 0 }, 91 1.1 christos #if SIZEOF_LONG == 4 92 1.1 christos { STRTOL, "0x80000000", 0, 0x7fffffffL, ERANGE }, 93 1.1 christos { STRTOL, "-0x80000001", 0, -0x80000000L, ERANGE }, 94 1.1 christos { STRTOUL, "0x100000000", 0, 0xffffffffUL, ERANGE }, 95 1.1 christos #endif 96 1.1 christos #ifdef HAVE_LONG_LONG 97 1.1 christos { STRTOLL, "0x123", 0, 0x123LL, 0 }, 98 1.1 christos { STRTOLL, "123", 0, 123LL, 0 }, 99 1.1 christos { STRTOLL, "0123", 0, 0123LL, 0 }, 100 1.1 christos { STRTOLL, "0x7FFFFFFFFFFFFFFF", 0, 0x7fffffffffffffffLL, 0 }, 101 1.1 christos { STRTOLL, "-0x8000000000000000", 0, -0x8000000000000000LL, 0 }, 102 1.1 christos { STRTOULL, "0x123", 0, 0x123ULL, 0 }, 103 1.1 christos { STRTOULL, "123", 0, 123ULL, 0 }, 104 1.1 christos { STRTOULL, "0123", 0, 0123ULL, 0 }, 105 1.1 christos { STRTOULL, "0xFFFFFFFFFFFFFFFF", 0, 0xffffffffffffffffULL, 0 }, 106 1.1 christos #if SIZEOF_LONG_LONG == 8 107 1.1 christos { STRTOLL, "0x8000000000000000", 0, 0x7fffffffffffffffLL, ERANGE }, 108 1.1 christos { STRTOLL, "-0x8000000000000001", 0, -0x8000000000000000LL, ERANGE }, 109 1.1 christos { STRTOULL, "0x10000000000000000", 0, 0xffffffffffffffffULL, ERANGE }, 110 1.1 christos #endif 111 1.1 christos #endif 112 1.1 christos }; 113 1.1 christos 114 1.1 christos /* run_tests: 115 1.1 christos Run conversion function 116 1.1 christos Compare results 117 1.1 christos Return number of fails */ 118 1.1 christos 119 1.1 christos int 120 1.1 christos run_tests (const struct test_data_t *test_data, size_t ntests) 121 1.1 christos { 122 1.1 christos int fails = 0, failed; 123 1.1 christos size_t i; 124 1.1 christos 125 1.1 christos for (i = 0; i < ntests; ++i) 126 1.1 christos { 127 1.1 christos integer_type res; 128 1.1 christos int saved_errno; 129 1.1 christos 130 1.1 christos errno = 0; 131 1.1 christos 132 1.1 christos switch (test_data[i].fun) 133 1.1 christos { 134 1.1 christos case STRTOL: 135 1.1 christos res = (unsigned long) strtol (test_data[i].nptr, 136 1.1 christos 0, test_data[i].base); 137 1.1 christos break; 138 1.1 christos case STRTOUL: 139 1.1 christos res = strtoul (test_data[i].nptr, 0, test_data[i].base); 140 1.1 christos break; 141 1.1 christos #ifdef HAVE_LONG_LONG 142 1.1 christos case STRTOLL: 143 1.1 christos res = strtoll (test_data[i].nptr, 0, test_data[i].base); 144 1.1 christos break; 145 1.1 christos case STRTOULL: 146 1.1 christos res = strtoull (test_data[i].nptr, 0, test_data[i].base); 147 1.1 christos break; 148 1.1 christos #endif 149 1.1 christos } 150 1.1 christos 151 1.1 christos saved_errno = errno; 152 1.1 christos 153 1.1 christos failed = 0; 154 1.1 christos 155 1.1 christos /* Compare result */ 156 1.1 christos if (res != test_data[i].res) 157 1.1 christos { 158 1.1 christos printf ("FAIL: test-strtol-%zd. Results don't match.\n", i); 159 1.1 christos failed++; 160 1.1 christos } 161 1.1 christos 162 1.1 christos /* Compare errno */ 163 1.1 christos if (saved_errno != test_data[i].errnum) 164 1.1 christos { 165 1.1 christos printf ("FAIL: test-strtol-%zd. Errnos don't match.\n", i); 166 1.1 christos failed++; 167 1.1 christos } 168 1.1 christos 169 1.1 christos if (!failed) 170 1.1 christos printf ("PASS: test-strtol-%zd.\n", i); 171 1.1 christos else 172 1.1 christos fails++; 173 1.1 christos } 174 1.1 christos 175 1.1 christos return fails; 176 1.1 christos } 177 1.1 christos 178 1.1 christos int 179 1.1 christos main(int argc, char **argv) 180 1.1 christos { 181 1.1 christos int fails; 182 1.1 christos fails = run_tests (test_data, sizeof (test_data) / sizeof (test_data[0])); 183 1.1 christos exit (fails ? EXIT_FAILURE : EXIT_SUCCESS); 184 1.1 christos } 185 1.1 christos 186