diff options
| author | David M. Syzdek <david@syzdek.net> | 2022-01-20 21:32:42 -0900 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2022-01-20 21:32:42 -0900 |
| commit | 5fb67e26f2f86e66c83555704ed092479394c2ad (patch) | |
| tree | d03d6330280c5196a4534d56de0096d7086e3a5e /tests | |
| parent | df94df154b58376fdc34e96181f332e341979312 (diff) | |
| download | tinyrad-5fb67e26f2f86e66c83555704ed092479394c2ad.tar.gz tinyrad-5fb67e26f2f86e66c83555704ed092479394c2ad.tar.bz2 tinyrad-5fb67e26f2f86e66c83555704ed092479394c2ad.tar.xz tinyrad-5fb67e26f2f86e66c83555704ed092479394c2ad.zip | |
tests: moving random string function to common.c
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/common.c | 36 | ||||
| -rw-r--r-- | tests/common.h | 10 | ||||
| -rw-r--r-- | tests/test-array.c | 37 |
3 files changed, 47 insertions, 36 deletions
diff --git a/tests/common.c b/tests/common.c index e1b0401..68ab788 100644 --- a/tests/common.c +++ b/tests/common.c | |||
| @@ -39,9 +39,11 @@ | |||
| 39 | #pragma mark - Headers | 39 | #pragma mark - Headers |
| 40 | 40 | ||
| 41 | #include <stdio.h> | 41 | #include <stdio.h> |
| 42 | #include <stdlib.h> | ||
| 42 | #include <stdarg.h> | 43 | #include <stdarg.h> |
| 43 | #include <string.h> | 44 | #include <string.h> |
| 44 | #include <strings.h> | 45 | #include <strings.h> |
| 46 | #include <assert.h> | ||
| 45 | 47 | ||
| 46 | #include "common.h" | 48 | #include "common.h" |
| 47 | 49 | ||
| @@ -534,4 +536,38 @@ void our_verbose(int opts, const char * fmt, ...) | |||
| 534 | return; | 536 | return; |
| 535 | } | 537 | } |
| 536 | 538 | ||
| 539 | |||
| 540 | //------------------// | ||
| 541 | // string functions // | ||
| 542 | //------------------// | ||
| 543 | #pragma mark string functions | ||
| 544 | |||
| 545 | char * our_random_str(size_t min, size_t max) | ||
| 546 | { | ||
| 547 | char * str; | ||
| 548 | size_t len; | ||
| 549 | size_t pos; | ||
| 550 | static int count = 0; | ||
| 551 | |||
| 552 | assert(min <= max); | ||
| 553 | |||
| 554 | len = ((max - min)) ? ((unsigned)random() % (max - min)) : 0; | ||
| 555 | len += min; | ||
| 556 | if (len < sizeof(int)) | ||
| 557 | len = sizeof(int); | ||
| 558 | |||
| 559 | if ((str = malloc(len+1)) == NULL) | ||
| 560 | return(NULL); | ||
| 561 | str[len] = '\0'; | ||
| 562 | |||
| 563 | for(pos = 0; (pos < (len-sizeof(int))); pos++) | ||
| 564 | str[pos] = (random() % 26) + 'a'; | ||
| 565 | |||
| 566 | snprintf(&str[pos], (len-pos+1), "%04xu", (~count)&0xffff); | ||
| 567 | |||
| 568 | count++; | ||
| 569 | |||
| 570 | return(str); | ||
| 571 | } | ||
| 572 | |||
| 537 | /* end of source */ | 573 | /* end of source */ |
diff --git a/tests/common.h b/tests/common.h index a298d73..1e83957 100644 --- a/tests/common.h +++ b/tests/common.h | |||
| @@ -116,4 +116,14 @@ void our_print(int opts, const char * fmt, ...); | |||
| 116 | void our_verbose(int opts, const char * fmt, ...); | 116 | void our_verbose(int opts, const char * fmt, ...); |
| 117 | 117 | ||
| 118 | 118 | ||
| 119 | //------------------// | ||
| 120 | // string functions // | ||
| 121 | //------------------// | ||
| 122 | #pragma mark string functions | ||
| 123 | |||
| 124 | char * | ||
| 125 | our_random_str( | ||
| 126 | size_t min, | ||
| 127 | size_t max ); | ||
| 128 | |||
| 119 | #endif /* end of header */ | 129 | #endif /* end of header */ |
diff --git a/tests/test-array.c b/tests/test-array.c index 283d71f..e8c9068 100644 --- a/tests/test-array.c +++ b/tests/test-array.c | |||
| @@ -183,12 +183,6 @@ my_test_search( | |||
| 183 | size_t len ); | 183 | size_t len ); |
| 184 | 184 | ||
| 185 | 185 | ||
| 186 | char * | ||
| 187 | my_random_str( | ||
| 188 | size_t min, | ||
| 189 | size_t max ); | ||
| 190 | |||
| 191 | |||
| 192 | ///////////////// | 186 | ///////////////// |
| 193 | // // | 187 | // // |
| 194 | // Functions // | 188 | // Functions // |
| @@ -299,7 +293,7 @@ int main( int argc, char * argv[] ) | |||
| 299 | { | 293 | { |
| 300 | data[pos].value = (int)(~pos); | 294 | data[pos].value = (int)(~pos); |
| 301 | data[pos].opts = (int)pos; | 295 | data[pos].opts = (int)pos; |
| 302 | data[pos].name = (pos < len) ? strdup(test_strs[pos]) : my_random_str(8, 8); | 296 | data[pos].name = (pos < len) ? strdup(test_strs[pos]) : our_random_str(8, 8); |
| 303 | if (!(data[pos].name)) | 297 | if (!(data[pos].name)) |
| 304 | { | 298 | { |
| 305 | for(; (pos > 0); pos--) | 299 | for(; (pos > 0); pos--) |
| @@ -587,35 +581,6 @@ my_compar_opts( | |||
| 587 | } | 581 | } |
| 588 | 582 | ||
| 589 | 583 | ||
| 590 | char * my_random_str(size_t min, size_t max) | ||
| 591 | { | ||
| 592 | char * str; | ||
| 593 | size_t len; | ||
| 594 | size_t pos; | ||
| 595 | static int count = 0; | ||
| 596 | |||
| 597 | assert(min <= max); | ||
| 598 | |||
| 599 | len = ((max - min)) ? ((unsigned)random() % (max - min)) : 0; | ||
| 600 | len += min; | ||
| 601 | if (len < sizeof(int)) | ||
| 602 | len = sizeof(int); | ||
| 603 | |||
| 604 | if ((str = malloc(len+1)) == NULL) | ||
| 605 | return(NULL); | ||
| 606 | str[len] = '\0'; | ||
| 607 | |||
| 608 | for(pos = 0; (pos < (len-sizeof(int))); pos++) | ||
| 609 | str[pos] = (random() % 26) + 'a'; | ||
| 610 | |||
| 611 | snprintf(&str[pos], (len-pos+1), "%04xu", (~count)&0xffff); | ||
| 612 | |||
| 613 | count++; | ||
| 614 | |||
| 615 | return(str); | ||
| 616 | } | ||
| 617 | |||
| 618 | |||
| 619 | int | 584 | int |
| 620 | my_test_insert( | 585 | my_test_insert( |
| 621 | int opts, | 586 | int opts, |
