diff options
| author | Thomas Vanbesien <tvanbesi@proton.me> | 2026-02-27 12:39:03 +0100 |
|---|---|---|
| committer | Thomas Vanbesien <tvanbesi@proton.me> | 2026-02-27 12:39:03 +0100 |
| commit | 9d2ca1ee92a13d6e276d74536ebf295f2331dc08 (patch) | |
| tree | 62ec50e236d6ff6586f948fd8940d4e7f7a94788 /tests | |
| parent | c950a0a70d37f962bbef66221ff0a23cad450195 (diff) | |
| download | malloc-master.tar.gz malloc-master.zip | |
Clarify purpose of helper functions and label the allocate/verify/free
phases in _s_test_category and _s_test_realloc. Remove _s_atoi in
favor of stdlib atoi.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/src/test_random.c | 34 | ||||
| -rw-r--r-- | tests/src/test_show.c | 1 |
2 files changed, 19 insertions, 16 deletions
diff --git a/tests/src/test_random.c b/tests/src/test_random.c index 89284a0..c608d4f 100644 --- a/tests/src/test_random.c +++ b/tests/src/test_random.c @@ -16,6 +16,7 @@ #define SMALL_MAX 1024 #define LARGE_MAX 8192 +/* Write a null-terminated string to stdout. */ static void _s_put_str (const char *str) { @@ -27,6 +28,8 @@ _s_put_str (const char *str) write (1, str, (size_t)(p - str)); } +/* Write a non-negative int to stdout. sprintf is avoided because it + calls malloc internally, which breaks under LD_PRELOAD. */ static void _s_put_nbr (int n) { @@ -44,18 +47,23 @@ _s_put_nbr (int n) write (1, buf + i, sizeof (buf) - i); } +/* Deterministic byte value for a given allocation and offset. */ static unsigned char _s_pattern (int seed, int alloc_index, int byte_index) { return ((unsigned char)((seed * 31 + alloc_index * 7 + byte_index) & 0xFF)); } +/* Return a random size in [min, max]. */ static size_t _s_rand_range (size_t min, size_t max) { return (min + (size_t)rand () % (max - min + 1)); } +/* Allocate count blocks with random sizes in [min, max], fill each + with a deterministic pattern, then verify every byte. Returns 1 + on any failure, 0 if all checks pass. */ static int _s_test_category (const char *label, size_t min, size_t max, int seed, int base_index, int count) @@ -67,6 +75,7 @@ _s_test_category (const char *label, size_t min, size_t max, int seed, int i; int j; + /* Allocate and fill. */ fail = 0; i = 0; while (i < count) @@ -92,6 +101,7 @@ _s_test_category (const char *label, size_t min, size_t max, int seed, } i++; } + /* Verify pattern. */ i = 0; while (i < count) { @@ -128,6 +138,7 @@ _s_test_category (const char *label, size_t min, size_t max, int seed, } i++; } + /* Free all. */ i = 0; while (i < count) { @@ -137,6 +148,8 @@ _s_test_category (const char *label, size_t min, size_t max, int seed, return (fail); } +/* Same as _s_test_category, but after filling, realloc each block to + a new random size and verify the preserved portion. */ static int _s_test_realloc (const char *label, size_t min, size_t max, int seed, int base_index, int count) @@ -150,6 +163,7 @@ _s_test_realloc (const char *label, size_t min, size_t max, int seed, int i; int j; + /* Allocate and fill. */ fail = 0; i = 0; while (i < count) @@ -170,6 +184,7 @@ _s_test_realloc (const char *label, size_t min, size_t max, int seed, } i++; } + /* Realloc to a new random size and verify preserved bytes. */ i = 0; while (i < count) { @@ -224,6 +239,7 @@ _s_test_realloc (const char *label, size_t min, size_t max, int seed, sizes[i] = new_size; i++; } + /* Free all. */ i = 0; while (i < count) { @@ -233,20 +249,6 @@ _s_test_realloc (const char *label, size_t min, size_t max, int seed, return (fail); } -static int -_s_atoi (const char *str) -{ - int n; - - n = 0; - while (*str) - { - n = n * 10 + (*str - '0'); - str++; - } - return (n); -} - int main (int argc, char **argv) { @@ -259,8 +261,8 @@ main (int argc, char **argv) _s_put_str ("usage: test_random <seed> <count>\n"); return (1); } - seed = _s_atoi (argv[1]); - count = _s_atoi (argv[2]); + seed = atoi (argv[1]); + count = atoi (argv[2]); srand ((unsigned int)seed); fail = 0; fail |= _s_test_category ("TINY", 1, TINY_MAX, seed, 0, count); diff --git a/tests/src/test_show.c b/tests/src/test_show.c index e5e7dca..877d39e 100644 --- a/tests/src/test_show.c +++ b/tests/src/test_show.c @@ -8,6 +8,7 @@ int main (void) { + /* One allocation per category: TINY, TINY, SMALL, LARGE. */ void *a = malloc (42); void *b = malloc (100); void *c = malloc (500); |
