diff options
| author | David M. Syzdek <david@syzdek.net> | 2022-02-05 15:58:29 -0900 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2022-02-05 16:07:11 -0900 |
| commit | 9387b363b01af54ccde997eeb89e08c16323bdce (patch) | |
| tree | 09248df14d70cbcc45bb66d36a8152143430faef /tests/test-assumptions.c | |
| parent | c9c8920f8d6447e1594bb3cc65bc77e70da7a9c1 (diff) | |
| download | tinyrad-9387b363b01af54ccde997eeb89e08c16323bdce.tar.gz tinyrad-9387b363b01af54ccde997eeb89e08c16323bdce.tar.bz2 tinyrad-9387b363b01af54ccde997eeb89e08c16323bdce.tar.xz tinyrad-9387b363b01af54ccde997eeb89e08c16323bdce.zip | |
tests: adding test-developer-assumptions
Diffstat (limited to 'tests/test-assumptions.c')
| -rw-r--r-- | tests/test-assumptions.c | 311 |
1 files changed, 311 insertions, 0 deletions
diff --git a/tests/test-assumptions.c b/tests/test-assumptions.c new file mode 100644 index 0000000..224aef1 --- /dev/null +++ b/tests/test-assumptions.c | |||
| @@ -0,0 +1,311 @@ | |||
| 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_TEST_ASSUMPTIONS_C 1 | ||
| 32 | |||
| 33 | |||
| 34 | /////////////// | ||
| 35 | // // | ||
| 36 | // Headers // | ||
| 37 | // // | ||
| 38 | /////////////// | ||
| 39 | #pragma mark - Headers | ||
| 40 | |||
| 41 | #undef NDEBUG | ||
| 42 | |||
| 43 | #include "common.h" | ||
| 44 | |||
| 45 | #include <stdio.h> | ||
| 46 | #include <stdarg.h> | ||
| 47 | #include <stdlib.h> | ||
| 48 | #include <string.h> | ||
| 49 | #include <strings.h> | ||
| 50 | #include <unistd.h> | ||
| 51 | #include <getopt.h> | ||
| 52 | #include <assert.h> | ||
| 53 | #include <stdatomic.h> | ||
| 54 | |||
| 55 | #include <tinyrad.h> | ||
| 56 | |||
| 57 | |||
| 58 | /////////////////// | ||
| 59 | // // | ||
| 60 | // Definitions // | ||
| 61 | // // | ||
| 62 | /////////////////// | ||
| 63 | #pragma mark - Definitions | ||
| 64 | |||
| 65 | #undef PROGRAM_NAME | ||
| 66 | #define PROGRAM_NAME "test-assumptions" | ||
| 67 | |||
| 68 | |||
| 69 | ///////////////// | ||
| 70 | // // | ||
| 71 | // Datatypes // | ||
| 72 | // // | ||
| 73 | ///////////////// | ||
| 74 | #pragma mark - Datatypes | ||
| 75 | |||
| 76 | struct my_data8 | ||
| 77 | { | ||
| 78 | char member0; | ||
| 79 | char member1; | ||
| 80 | char member2; | ||
| 81 | uint8_t member3; | ||
| 82 | uint8_t member4; | ||
| 83 | uint8_t member5; | ||
| 84 | int8_t member6; | ||
| 85 | int8_t member7; | ||
| 86 | int8_t member8; | ||
| 87 | }; | ||
| 88 | |||
| 89 | struct my_data16 | ||
| 90 | { | ||
| 91 | uint16_t member0; | ||
| 92 | uint16_t member1; | ||
| 93 | uint16_t member2; | ||
| 94 | int16_t member3; | ||
| 95 | int16_t member4; | ||
| 96 | int16_t member5; | ||
| 97 | }; | ||
| 98 | |||
| 99 | struct my_data32 | ||
| 100 | { | ||
| 101 | uint32_t member0; | ||
| 102 | uint32_t member1; | ||
| 103 | uint32_t member2; | ||
| 104 | int32_t member3; | ||
| 105 | int32_t member4; | ||
| 106 | int32_t member5; | ||
| 107 | }; | ||
| 108 | |||
| 109 | struct my_data64 | ||
| 110 | { | ||
| 111 | uint64_t member0; | ||
| 112 | uint64_t member1; | ||
| 113 | uint64_t member2; | ||
| 114 | int64_t member3; | ||
| 115 | int64_t member4; | ||
| 116 | int64_t member5; | ||
| 117 | }; | ||
| 118 | |||
| 119 | struct my_data_mixed | ||
| 120 | { | ||
| 121 | char member0; | ||
| 122 | char member1; | ||
| 123 | char member2; | ||
| 124 | uint8_t member3; | ||
| 125 | uint8_t member4; | ||
| 126 | uint8_t member5; | ||
| 127 | int8_t member6; | ||
| 128 | int8_t member7; | ||
| 129 | |||
| 130 | uint16_t member8; | ||
| 131 | uint16_t member9; | ||
| 132 | int16_t member10; | ||
| 133 | int16_t member11; | ||
| 134 | |||
| 135 | uint32_t member12; | ||
| 136 | int32_t member13; | ||
| 137 | |||
| 138 | uint64_t member14; | ||
| 139 | |||
| 140 | int64_t member15; | ||
| 141 | |||
| 142 | int8_t member16; | ||
| 143 | int8_t member17; | ||
| 144 | int16_t member18; | ||
| 145 | int32_t member19; | ||
| 146 | }; | ||
| 147 | |||
| 148 | |||
| 149 | struct my_data_intptr | ||
| 150 | { | ||
| 151 | intptr_t member0; | ||
| 152 | intptr_t member1; | ||
| 153 | intptr_t member2; | ||
| 154 | uintptr_t member3; | ||
| 155 | uintptr_t member4; | ||
| 156 | uintptr_t member5; | ||
| 157 | }; | ||
| 158 | |||
| 159 | |||
| 160 | struct my_data_ptr | ||
| 161 | { | ||
| 162 | void * member0; | ||
| 163 | void * member1; | ||
| 164 | void * member2; | ||
| 165 | void * member3; | ||
| 166 | void * member4; | ||
| 167 | void * member5; | ||
| 168 | }; | ||
| 169 | |||
| 170 | |||
| 171 | struct my_obj | ||
| 172 | { | ||
| 173 | uint8_t magic[8]; | ||
| 174 | atomic_intptr_t ref_count; | ||
| 175 | void (*free_func)(void * ptr); | ||
| 176 | }; | ||
| 177 | |||
| 178 | |||
| 179 | struct my_data | ||
| 180 | { | ||
| 181 | struct my_obj obj; | ||
| 182 | int8_t member0; | ||
| 183 | int8_t member1; | ||
| 184 | int16_t member2; | ||
| 185 | int32_t member3; | ||
| 186 | }; | ||
| 187 | |||
| 188 | |||
| 189 | ////////////////// | ||
| 190 | // // | ||
| 191 | // Prototypes // | ||
| 192 | // // | ||
| 193 | ////////////////// | ||
| 194 | #pragma mark - Prototypes | ||
| 195 | |||
| 196 | int | ||
| 197 | main( | ||
| 198 | int argc, | ||
| 199 | char * argv[] ); | ||
| 200 | |||
| 201 | |||
| 202 | ///////////////// | ||
| 203 | // // | ||
| 204 | // Functions // | ||
| 205 | // // | ||
| 206 | ///////////////// | ||
| 207 | #pragma mark - Functions | ||
| 208 | |||
| 209 | int | ||
| 210 | main( | ||
| 211 | int argc, | ||
| 212 | char * argv[] ) | ||
| 213 | { | ||
| 214 | int c; | ||
| 215 | int opt_index; | ||
| 216 | unsigned opts; | ||
| 217 | |||
| 218 | // getopt options | ||
| 219 | static char short_opt[] = "dhVvq"; | ||
| 220 | static struct option long_opt[] = | ||
| 221 | { | ||
| 222 | {"debug", no_argument, NULL, 'd' }, | ||
| 223 | {"help", no_argument, NULL, 'h' }, | ||
| 224 | {"quiet", no_argument, NULL, 'q' }, | ||
| 225 | {"silent", no_argument, NULL, 'q' }, | ||
| 226 | {"version", no_argument, NULL, 'V' }, | ||
| 227 | {"verbose", no_argument, NULL, 'v' }, | ||
| 228 | { NULL, 0, NULL, 0 } | ||
| 229 | }; | ||
| 230 | |||
| 231 | opts = 0; | ||
| 232 | |||
| 233 | while((c = getopt_long(argc, argv, short_opt, long_opt, &opt_index)) != -1) | ||
| 234 | { | ||
| 235 | switch(c) | ||
| 236 | { | ||
| 237 | case -1: /* no more arguments */ | ||
| 238 | case 0: /* long options toggles */ | ||
| 239 | break; | ||
| 240 | |||
| 241 | case 'd': | ||
| 242 | break; | ||
| 243 | |||
| 244 | case 'h': | ||
| 245 | printf("Usage: %s [OPTIONS]\n", PROGRAM_NAME); | ||
| 246 | printf("OPTIONS:\n"); | ||
| 247 | printf(" -d, --debug print debug messages\n"); | ||
| 248 | printf(" -h, --help print this help and exit\n"); | ||
| 249 | printf(" -q, --quiet, --silent do not print messages\n"); | ||
| 250 | printf(" -V, --version print version number and exit\n"); | ||
| 251 | printf(" -v, --verbose print verbose messages\n"); | ||
| 252 | printf("\n"); | ||
| 253 | return(0); | ||
| 254 | |||
| 255 | case 'q': | ||
| 256 | opts |= TRAD_TEST_QUIET; | ||
| 257 | opts &= ~TRAD_TEST_VERBOSE; | ||
| 258 | break; | ||
| 259 | |||
| 260 | case 'V': | ||
| 261 | printf("%s (%s) %s\n", PROGRAM_NAME, PACKAGE_NAME, PACKAGE_VERSION); | ||
| 262 | return(0); | ||
| 263 | |||
| 264 | case 'v': | ||
| 265 | opts |= TRAD_TEST_VERBOSE; | ||
| 266 | opts &= ~TRAD_TEST_QUIET; | ||
| 267 | break; | ||
| 268 | |||
| 269 | case '?': | ||
| 270 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | ||
| 271 | return(1); | ||
| 272 | |||
| 273 | default: | ||
| 274 | fprintf(stderr, "%s: unrecognized option `--%c'\n", PROGRAM_NAME, c); | ||
| 275 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | ||
| 276 | return(1); | ||
| 277 | }; | ||
| 278 | }; | ||
| 279 | |||
| 280 | // data type size assumptions | ||
| 281 | assert(sizeof( int8_t ) == 1 ); | ||
| 282 | assert(sizeof( uint8_t ) == 1 ); | ||
| 283 | assert(sizeof( int16_t ) == 2 ); | ||
| 284 | assert(sizeof( uint16_t ) == 2 ); | ||
| 285 | assert(sizeof( int32_t ) == 4 ); | ||
| 286 | assert(sizeof( uint32_t ) == 4 ); | ||
| 287 | assert(sizeof( int64_t ) == 8 ); | ||
| 288 | assert(sizeof( uint64_t ) == 8 ); | ||
| 289 | assert(sizeof( intptr_t ) == sizeof(void *) ); | ||
| 290 | assert(sizeof( uintptr_t ) == sizeof(void *) ); | ||
| 291 | assert(sizeof( size_t ) == sizeof(void *) ); | ||
| 292 | assert(sizeof( ssize_t ) == sizeof(void *) ); | ||
| 293 | |||
| 294 | // struct packing assumptions | ||
| 295 | assert(sizeof( struct my_data8 ) == (1*9)); | ||
| 296 | assert(sizeof( struct my_data16 ) == (2*6)); | ||
| 297 | assert(sizeof( struct my_data32 ) == (4*6)); | ||
| 298 | assert(sizeof( struct my_data64 ) == (8*6)); | ||
| 299 | assert(sizeof( struct my_data_mixed ) == (8*6)); | ||
| 300 | assert(sizeof( struct my_data_ptr ) == sizeof( struct my_data_intptr) ); | ||
| 301 | assert(sizeof( struct my_data ) == (16 + (sizeof(void *) * 2)) ); | ||
| 302 | |||
| 303 | if ((opts & TRAD_TEST_VERBOSE)) | ||
| 304 | printf("%s: It appears that the developer's assumptions are correct.\n", PROGRAM_NAME); | ||
| 305 | |||
| 306 | return(0); | ||
| 307 | } | ||
| 308 | |||
| 309 | |||
| 310 | /* end of source */ | ||
| 311 | |||
