// test_parse_int.c // PUBLIC DOMAIN // http:///www.purposeful.co.uk/software/yabaplib /* I, Tom Vajzovic, am the author of this software and its documentation. I permanently abandon all intellectual property rights in them, including copyright, trademarks, design rights, database right, patents, and the right to be identified as the author. I am fairly certain that the software does what the documentation says it does, but I do not guarantee that it does, or that it does what you think it should. I do not guarantee that it will not have undesirable side effects. You are free to use, modify and distribute this software as you please, but you do so at your own risk. If you do not pass on this warning then you may be responsible for any problems encountered by those who obtain the software through you. */ #include #include #include #include "parse_int.h" static uint32_t rand32 (void) { static uint64_t state = 0; uint64_t new_state = ((state * 6364136223846793005uLL) + 1442695040888963407uLL); state = new_state; return (new_state >> 32); } static uint64_t rand64 (void) { uint64_t high = rand32(); return ((high << 32) | rand32()); } void lwsp (char *buf, unsigned int size) { unsigned int i; for (i = 0; i < (size - 1); i++) { buf[i] = "\t "[rand32() % 3]; } buf[i] = 0; } void wsp (char *buf, unsigned int size) { unsigned int i; for (i = 0; i < (size - 1); i++) { buf[i] = "\f\v\n\r\t "[rand32() % 7]; } buf[i] = 0; } void test_u64 (uint64_t t) { char line [80]; char space1[20]; char space2[20]; lwsp (space1, sizeof space1); wsp (space2, sizeof space2); sprintf (line, "%s%llu%s", space1, (unsigned long long int)t, space2); uint64_t r; if (decimal_only_to_u64(line, &r) || (r != t)) { fprintf (stderr, "'%s' %llu %llu\n", line, (unsigned long long int)t, (unsigned long long int)r); exit (EXIT_FAILURE); } } void test_u32 (uint32_t t) { char line [80]; char space1[20]; char space2[20]; lwsp (space1, sizeof space1); wsp (space2, sizeof space2); sprintf (line, "%s%llu%s", space1, (unsigned long long int)t, space2); uint32_t r; if (decimal_only_to_u32(line, &r) || (r != t)) { fprintf (stderr, "'%s' %llu %llu\n", line, (unsigned long long int)t, (unsigned long long int)r); exit (EXIT_FAILURE); } } int main (void) { unsigned int i; for (i = 0; i < 1000; i++) { test_u32(0uL); test_u32(1uL); test_u32(0x7FFFFFFFuL); test_u32(0x80000000uL); test_u32(0x80000001uL); test_u32(0xFFFFFFFEuL); test_u32(0xFFFFFFFFuL); test_u64(0uLL); test_u64(1uLL); test_u64(0x7FFFFFFFuLL); test_u64(0x80000000uLL); test_u64(0x80000001uLL); test_u64(0xFFFFFFFEuLL); test_u64(0xFFFFFFFFuLL); } for (;;) { test_u32(rand32()); test_u64(rand64()); } } // test leading zeros // test signed versions with + - no sign // test overflows // test no digits // test suffix detection