diff options
| author | David M. Syzdek <david@syzdek.net> | 2022-03-28 18:07:56 -0800 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2022-03-28 18:07:56 -0800 |
| commit | 4bae67fe2ddff0f7d281b42f6f920dde3bc23598 (patch) | |
| tree | 1157defb034a261fd8d403ccee3d6f6c1e9577ea /tests/test-str-split.c | |
| parent | bffc5bc5b791264e8407dfff4c79ccd422a6927c (diff) | |
| download | tinyrad-4bae67fe2ddff0f7d281b42f6f920dde3bc23598.tar.gz tinyrad-4bae67fe2ddff0f7d281b42f6f920dde3bc23598.tar.bz2 tinyrad-4bae67fe2ddff0f7d281b42f6f920dde3bc23598.tar.xz tinyrad-4bae67fe2ddff0f7d281b42f6f920dde3bc23598.zip | |
tests: adding test-str-split
Diffstat (limited to '')
| -rw-r--r-- | tests/test-str-split.c | 299 |
1 files changed, 299 insertions, 0 deletions
diff --git a/tests/test-str-split.c b/tests/test-str-split.c new file mode 100644 index 0000000..9823cd4 --- /dev/null +++ b/tests/test-str-split.c | |||
| @@ -0,0 +1,299 @@ | |||
| 1 | /* | ||
| 2 | * Tiny RADIUS Client Library | ||
| 3 | * Copyright (C) 2022 David M. Syzdek <david@syzdek.net>. | ||
| 4 | * All rights reserved. | ||
| 5 | * | ||
| 6 | * Redistribution and use in source and binary forms, with or without | ||
| 7 | * modification, are permitted provided that the following conditions are | ||
| 8 | * met: | ||
| 9 | * | ||
| 10 | * * Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * * Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * * Neither the name of David M. Syzdek nor the | ||
| 16 | * names of its contributors may be used to endorse or promote products | ||
| 17 | * derived from this software without specific prior written permission. | ||
| 18 | * | ||
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | ||
| 20 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
| 21 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID M. SYZDEK BE LIABLE FOR | ||
| 23 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
| 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
| 26 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 29 | * SUCH DAMAGE. | ||
| 30 | */ | ||
| 31 | #define _TESTS_TINYRAD_STR_SPLIT_C 1 | ||
| 32 | |||
| 33 | |||
| 34 | /////////////// | ||
| 35 | // // | ||
| 36 | // Headers // | ||
| 37 | // // | ||
| 38 | /////////////// | ||
| 39 | #pragma mark - Headers | ||
| 40 | |||
| 41 | #include <tinyrad_utils.h> | ||
| 42 | |||
| 43 | #include <stdio.h> | ||
| 44 | #include <stdarg.h> | ||
| 45 | #include <stdlib.h> | ||
| 46 | #include <string.h> | ||
| 47 | #include <strings.h> | ||
| 48 | #include <unistd.h> | ||
| 49 | #include <getopt.h> | ||
| 50 | #include <assert.h> | ||
| 51 | |||
| 52 | #include <tinyrad.h> | ||
| 53 | |||
| 54 | |||
| 55 | /////////////////// | ||
| 56 | // // | ||
| 57 | // Definitions // | ||
| 58 | // // | ||
| 59 | /////////////////// | ||
| 60 | #pragma mark - Definitions | ||
| 61 | |||
| 62 | #undef PROGRAM_NAME | ||
| 63 | #define PROGRAM_NAME "tinyrad-str-split" | ||
| 64 | |||
| 65 | |||
| 66 | ///////////////// | ||
| 67 | // // | ||
| 68 | // Variables // | ||
| 69 | // // | ||
| 70 | ///////////////// | ||
| 71 | #pragma mark - Variables | ||
| 72 | |||
| 73 | static const char * test_delim[] = | ||
| 74 | { | ||
| 75 | ":", | ||
| 76 | "-", | ||
| 77 | ",", | ||
| 78 | "_", | ||
| 79 | " ", | ||
| 80 | NULL | ||
| 81 | }; | ||
| 82 | |||
| 83 | static const char * const * test_strings[] = | ||
| 84 | { | ||
| 85 | (const char * const[]) { "test", "string", "1", NULL }, | ||
| 86 | (const char * const[]) { "test", "empty", "", "string", NULL }, | ||
| 87 | (const char * const[]) { "test", NULL }, | ||
| 88 | (const char * const[]) { "", "test", NULL }, | ||
| 89 | (const char * const[]) { "test", "", NULL }, | ||
| 90 | (const char * const[]) { "", NULL }, | ||
| 91 | (const char * const[]) { "", "", NULL }, | ||
| 92 | (const char * const[]) { "", "", "", NULL }, | ||
| 93 | NULL | ||
| 94 | }; | ||
| 95 | |||
| 96 | |||
| 97 | ////////////////// | ||
| 98 | // // | ||
| 99 | // Prototypes // | ||
| 100 | // // | ||
| 101 | ////////////////// | ||
| 102 | #pragma mark - Prototypes | ||
| 103 | |||
| 104 | int | ||
| 105 | main( | ||
| 106 | int argc, | ||
| 107 | char * argv[] ); | ||
| 108 | |||
| 109 | |||
| 110 | int | ||
| 111 | my_test_array( | ||
| 112 | unsigned opts, | ||
| 113 | const char * const * argv, | ||
| 114 | const char * delim ); | ||
| 115 | |||
| 116 | |||
| 117 | ///////////////// | ||
| 118 | // // | ||
| 119 | // Functions // | ||
| 120 | // // | ||
| 121 | ///////////////// | ||
| 122 | #pragma mark - Functions | ||
| 123 | |||
| 124 | int | ||
| 125 | main( | ||
| 126 | int argc, | ||
| 127 | char * argv[] ) | ||
| 128 | { | ||
| 129 | int opt; | ||
| 130 | int c; | ||
| 131 | int opt_index; | ||
| 132 | int x; | ||
| 133 | int y; | ||
| 134 | int rc; | ||
| 135 | unsigned opts; | ||
| 136 | static const char * const * delims; | ||
| 137 | const char * user_delim[2]; | ||
| 138 | |||
| 139 | // getopt options | ||
| 140 | static char short_opt[] = "dD:hVvq"; | ||
| 141 | static struct option long_opt[] = | ||
| 142 | { | ||
| 143 | {"debug", no_argument, NULL, 'd' }, | ||
| 144 | {"help", no_argument, NULL, 'h' }, | ||
| 145 | {"quiet", no_argument, NULL, 'q' }, | ||
| 146 | {"silent", no_argument, NULL, 'q' }, | ||
| 147 | {"version", no_argument, NULL, 'V' }, | ||
| 148 | {"verbose", no_argument, NULL, 'v' }, | ||
| 149 | { NULL, 0, NULL, 0 } | ||
| 150 | }; | ||
| 151 | |||
| 152 | trutils_initialize(PROGRAM_NAME); | ||
| 153 | |||
| 154 | delims = test_delim; | ||
| 155 | opts = 0; | ||
| 156 | |||
| 157 | while((c = getopt_long(argc, argv, short_opt, long_opt, &opt_index)) != -1) | ||
| 158 | { | ||
| 159 | switch(c) | ||
| 160 | { | ||
| 161 | case -1: /* no more arguments */ | ||
| 162 | case 0: /* long options toggles */ | ||
| 163 | break; | ||
| 164 | |||
| 165 | case 'D': | ||
| 166 | user_delim[0] = optarg; | ||
| 167 | user_delim[1] = NULL; | ||
| 168 | delims = user_delim; | ||
| 169 | break; | ||
| 170 | |||
| 171 | case 'd': | ||
| 172 | opt = TRAD_DEBUG_ANY; | ||
| 173 | tinyrad_set_option(NULL, TRAD_OPT_DEBUG_LEVEL, &opt); | ||
| 174 | break; | ||
| 175 | |||
| 176 | case 'h': | ||
| 177 | printf("Usage: %s [OPTIONS]\n", PROGRAM_NAME); | ||
| 178 | printf(" %s [OPTIONS] str1 [ str2 [ ... [ strN ] ] ]\n", PROGRAM_NAME); | ||
| 179 | printf("OPTIONS:\n"); | ||
| 180 | printf(" -D character use 'character' as deliminator\n"); | ||
| 181 | printf(" -d, --debug print debug messages\n"); | ||
| 182 | printf(" -h, --help print this help and exit\n"); | ||
| 183 | printf(" -q, --quiet, --silent do not print messages\n"); | ||
| 184 | printf(" -V, --version print version number and exit\n"); | ||
| 185 | printf(" -v, --verbose print verbose messages\n"); | ||
| 186 | printf("\n"); | ||
| 187 | return(0); | ||
| 188 | |||
| 189 | case 'q': | ||
| 190 | opts |= TRUTILS_OPT_QUIET; | ||
| 191 | opts &= ~TRUTILS_OPT_VERBOSE; | ||
| 192 | break; | ||
| 193 | |||
| 194 | case 'V': | ||
| 195 | trutils_version(); | ||
| 196 | return(0); | ||
| 197 | |||
| 198 | case 'v': | ||
| 199 | opts |= TRUTILS_OPT_VERBOSE; | ||
| 200 | opts &= ~TRUTILS_OPT_QUIET; | ||
| 201 | break; | ||
| 202 | |||
| 203 | case '?': | ||
| 204 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | ||
| 205 | return(1); | ||
| 206 | |||
| 207 | default: | ||
| 208 | fprintf(stderr, "%s: unrecognized option `--%c'\n", PROGRAM_NAME, c); | ||
| 209 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | ||
| 210 | return(1); | ||
| 211 | }; | ||
| 212 | }; | ||
| 213 | |||
| 214 | if (optind < argc) | ||
| 215 | { | ||
| 216 | for(y = 0; ((delims[y])); y++) | ||
| 217 | if ((rc = my_test_array(opts, (const char *const *)&argv[optind], delims[y])) != 0) | ||
| 218 | return(1); | ||
| 219 | return(0); | ||
| 220 | }; | ||
| 221 | |||
| 222 | for(x = 0; ((test_strings[x])); x++) | ||
| 223 | for(y = 0; ((delims[y])); y++) | ||
| 224 | if ((rc = my_test_array(opts, test_strings[x], delims[y])) != 0) | ||
| 225 | return(1); | ||
| 226 | |||
| 227 | return(0); | ||
| 228 | } | ||
| 229 | |||
| 230 | |||
| 231 | int | ||
| 232 | my_test_array( | ||
| 233 | unsigned opts, | ||
| 234 | const char * const * argv, | ||
| 235 | const char * delim ) | ||
| 236 | { | ||
| 237 | int rc; | ||
| 238 | int len; | ||
| 239 | int pos; | ||
| 240 | char buff[1024]; | ||
| 241 | char ** strs; | ||
| 242 | |||
| 243 | strs = NULL; | ||
| 244 | buff[0] = '\0'; | ||
| 245 | delim = ((delim)) ? delim : ":"; | ||
| 246 | |||
| 247 | // build test string | ||
| 248 | for(pos = 0; ((argv[pos])); pos++) | ||
| 249 | { | ||
| 250 | if ((pos)) | ||
| 251 | tinyrad_strlcat(buff, delim, sizeof(buff)); | ||
| 252 | tinyrad_strlcat(buff, argv[pos], sizeof(buff)); | ||
| 253 | }; | ||
| 254 | |||
| 255 | trutils_verbose(opts, "string(%c): \"%s\"", delim[0], buff); | ||
| 256 | |||
| 257 | if ((rc = tinyrad_strsplit(buff, delim[0], &strs, &len)) != TRAD_SUCCESS) | ||
| 258 | { | ||
| 259 | trutils_error(opts, NULL, "tinyrad_strsplit(): %s", tinyrad_strerror(rc)); | ||
| 260 | return(1); | ||
| 261 | }; | ||
| 262 | if (!(strs)) | ||
| 263 | { | ||
| 264 | trutils_error(opts, NULL, "tinyrad_strsplit(): did not return data"); | ||
| 265 | return(1); | ||
| 266 | }; | ||
| 267 | |||
| 268 | // print result | ||
| 269 | for(pos = 0; ((strs[pos])); pos++) | ||
| 270 | trutils_verbose(opts, " arg %i: \"%s\"", pos, strs[pos]); | ||
| 271 | |||
| 272 | // check result against original | ||
| 273 | if (len != pos) | ||
| 274 | { | ||
| 275 | trutils_error(opts, NULL, "tinyrad_strsplit(): returned data length did not patch returned length"); | ||
| 276 | return(1); | ||
| 277 | }; | ||
| 278 | for(pos = 0; ((argv[pos])); pos++); | ||
| 279 | if (len != pos) | ||
| 280 | { | ||
| 281 | trutils_error(opts, NULL, "tinyrad_strsplit(): returned %i strings, expected %i strings", len, pos); | ||
| 282 | return(1); | ||
| 283 | }; | ||
| 284 | for(pos = 0; ((argv[pos])); pos++) | ||
| 285 | { | ||
| 286 | if ((strcmp(argv[pos], strs[pos]))) | ||
| 287 | { | ||
| 288 | trutils_error(opts, NULL, "tinyrad_strsplit(): strings do not match originals "); | ||
| 289 | return(1); | ||
| 290 | }; | ||
| 291 | }; | ||
| 292 | |||
| 293 | tinyrad_free(strs); | ||
| 294 | |||
| 295 | return(TRAD_SUCCESS); | ||
| 296 | } | ||
| 297 | |||
| 298 | /* end of source */ | ||
| 299 | |||
