Mercurial > lbo > hg > ylisp
changeset 28:bcc2670db1a6
chore: indent
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Mon, 19 Aug 2019 22:16:19 +0200 |
parents | f6cb12bf2d09 |
children | 3e38ca5d5feb |
files | src/base/str.c src/base/str.h src/base/str_test.c src/base/vec.c src/base/vec.h src/base/vec_test.c |
diffstat | 6 files changed, 314 insertions(+), 309 deletions(-) [+] |
line wrap: on
line diff
--- a/src/base/str.c Mon Aug 19 22:16:07 2019 +0200 +++ b/src/base/str.c Mon Aug 19 22:16:19 2019 +0200 @@ -8,181 +8,181 @@ static inline bool ystr_is_inline(ystr_t *s) { return 0 == s->inner.big.size; } void ystr_init(ystr_t *s, const char *src) { - memset(s, 0, sizeof(ystr_t)); - ystr_set(s, src); + memset(s, 0, sizeof(ystr_t)); + ystr_set(s, src); } void ystr_set_owned(ystr_t *s, char *src) { - if (src == NULL) { - memset(s, 0, sizeof(ystr_t)); - return; - } - // Free previous memory. - if (!ystr_is_inline(s)) - ystr_destroy(s); - size_t len = strlen(src) + 1; - s->inner.big.len = len; - s->inner.big.cap = len; - s->inner.big.size = 1; - s->inner.big.base = src; + if (src == NULL) { + memset(s, 0, sizeof(ystr_t)); + return; + } + // Free previous memory. + if (!ystr_is_inline(s)) + ystr_destroy(s); + size_t len = strlen(src) + 1; + s->inner.big.len = len; + s->inner.big.cap = len; + s->inner.big.size = 1; + s->inner.big.base = src; } void ystr_set(ystr_t *s, const char *src) { - if (src == NULL) { - if (!ystr_is_inline(s)) - ystr_destroy(s); - memset(s, 0, sizeof(ystr_t)); - return; - } - ystr_set_n(s, src, strlen(src)); + if (src == NULL) { + if (!ystr_is_inline(s)) + ystr_destroy(s); + memset(s, 0, sizeof(ystr_t)); + return; + } + ystr_set_n(s, src, strlen(src)); } void ystr_set_n(ystr_t *s, const char *src, size_t srclen) { - if (src == NULL) { - ystr_destroy(s); - return; - } - if (srclen < YSTR_SMALL_THRESHOLD) { - if (!ystr_is_inline(s)) - yvec_destroy(&s->inner.big); - memset(s, 0, - sizeof(ystr_t)); // Marks as inline because inner.big.size == 0. - memcpy(s->inner.small, src, srclen); - } else { - // +1 for terminating NULL byte. - if (ystr_is_inline(s)) - yvec_init(&s->inner.big, sizeof(char), srclen + 1); - yvec_resize(&s->inner.big, srclen + 1); - memcpy(s->inner.big.base, src, srclen + 1); - } + if (src == NULL) { + ystr_destroy(s); + return; + } + if (srclen < YSTR_SMALL_THRESHOLD) { + if (!ystr_is_inline(s)) + yvec_destroy(&s->inner.big); + memset(s, 0, + sizeof(ystr_t)); // Marks as inline because inner.big.size == 0. + memcpy(s->inner.small, src, srclen); + } else { + // +1 for terminating NULL byte. + if (ystr_is_inline(s)) + yvec_init(&s->inner.big, sizeof(char), srclen + 1); + yvec_resize(&s->inner.big, srclen + 1); + memcpy(s->inner.big.base, src, srclen + 1); + } } void ystr_destroy(ystr_t *s) { - if (!ystr_is_inline(s)) { - free(s->inner.big.base); - } - memset(s, 0, sizeof(ystr_t)); + if (!ystr_is_inline(s)) { + free(s->inner.big.base); + } + memset(s, 0, sizeof(ystr_t)); } size_t ystr_len(ystr_t *s) { - if (ystr_is_inline(s)) - return strlen(s->inner.small); - return s->inner.big.len - 1; + if (ystr_is_inline(s)) + return strlen(s->inner.small); + return s->inner.big.len - 1; } char ystr_at(ystr_t *s, size_t index) { - if (index >= ystr_len(s)) - return -1; - if (ystr_is_inline(s)) { - return s->inner.small[index]; - } - return *YVEC_AT(&s->inner.big, index, char); + if (index >= ystr_len(s)) + return -1; + if (ystr_is_inline(s)) { + return s->inner.small[index]; + } + return *YVEC_AT(&s->inner.big, index, char); } const char *ystr_str(ystr_t *s) { - if (ystr_is_inline(s)) { - return s->inner.small; - } - return (const char *)s->inner.big.base; + if (ystr_is_inline(s)) { + return s->inner.small; + } + return (const char *)s->inner.big.base; } int ystr_cmp(ystr_t *s1, ystr_t *s2) { - return strcmp(ystr_str(s1), ystr_str(s2)); + return strcmp(ystr_str(s1), ystr_str(s2)); } int ystr_cmp_str(ystr_t *s1, const char *s2) { - return strcmp(ystr_str(s1), s2); + return strcmp(ystr_str(s1), s2); } void ystr_split(ystr_t *s, char limit, yvec_t *dst) { - size_t parts = 0; - size_t len = ystr_len(s); - for (int i = 0; i < len; i++) { - if (ystr_at(s, i) == limit) - parts++; - } - parts += 1; - yvec_init(dst, sizeof(ystr_t), parts); - size_t index = 0; - while (index <= len) { - ystr_t part; - ystr_init(&part, NULL); - size_t i = 0; - for (i = index; i < len && ystr_at(s, i) != limit; i++) - ; - ystr_set_n(&part, ystr_str(s) + index, i - index); - YVEC_PUSH(dst, &part); - index = i + 1; - } + size_t parts = 0; + size_t len = ystr_len(s); + for (int i = 0; i < len; i++) { + if (ystr_at(s, i) == limit) + parts++; + } + parts += 1; + yvec_init(dst, sizeof(ystr_t), parts); + size_t index = 0; + while (index <= len) { + ystr_t part; + ystr_init(&part, NULL); + size_t i = 0; + for (i = index; i < len && ystr_at(s, i) != limit; i++) + ; + ystr_set_n(&part, ystr_str(s) + index, i - index); + YVEC_PUSH(dst, &part); + index = i + 1; + } } void ystr_resize(ystr_t *s, size_t new) { - if (ystr_is_inline(s)) { - size_t oldlen = strlen(s->inner.small); - if (new < oldlen) { - // Truncate small string. - memset(s->inner.small + new, 0, YSTR_SMALL_THRESHOLD - new); - } else if (new > YSTR_SMALL_THRESHOLD) { - // Move to external representation. - yvec_t ext; - YVEC_INIT(&ext, new + 1, char); - yvec_push_multi(&ext, s->inner.small, oldlen + 1); - yvec_resize(&ext, new + 1); - s->inner.big = ext; - } else { - // Size is between current size and threshold; do nothing. + if (ystr_is_inline(s)) { + size_t oldlen = strlen(s->inner.small); + if (new < oldlen) { + // Truncate small string. + memset(s->inner.small + new, 0, YSTR_SMALL_THRESHOLD - new); + } else if (new > YSTR_SMALL_THRESHOLD) { + // Move to external representation. + yvec_t ext; + YVEC_INIT(&ext, new + 1, char); + yvec_push_multi(&ext, s->inner.small, oldlen + 1); + yvec_resize(&ext, new + 1); + s->inner.big = ext; + } else { + // Size is between current size and threshold; do nothing. + } + return; } - return; - } - // +1 for NULL byte. - yvec_resize(&s->inner.big, new + 1); + // +1 for NULL byte. + yvec_resize(&s->inner.big, new + 1); } void ystr_append(ystr_t *s1, ystr_t *s2) { - size_t oldlen = ystr_len(s1); - ystr_resize(s1, ystr_len(s1) + ystr_len(s2)); + size_t oldlen = ystr_len(s1); + ystr_resize(s1, ystr_len(s1) + ystr_len(s2)); - // Still inline after resize? - if (ystr_is_inline(s1)) { - memcpy(s1->inner.small + strlen(s1->inner.small), ystr_str(s2), - ystr_len(s2)); - return; - } - // String has external vec with length of new string. Resize to length of - // old string without NULL before appending to it (append starts at current - // length, we want to skip the NULL byte) - yvec_resize(&s1->inner.big, oldlen); - yvec_push_multi(&s1->inner.big, ystr_str(s2), ystr_len(s2) + 1); + // Still inline after resize? + if (ystr_is_inline(s1)) { + memcpy(s1->inner.small + strlen(s1->inner.small), ystr_str(s2), + ystr_len(s2)); + return; + } + // String has external vec with length of new string. Resize to length of + // old string without NULL before appending to it (append starts at current + // length, we want to skip the NULL byte) + yvec_resize(&s1->inner.big, oldlen); + yvec_push_multi(&s1->inner.big, ystr_str(s2), ystr_len(s2) + 1); } #define YSTR_BUILD_BUF_SIZE 128 void ystr_build(ystr_t *s, const char *fmt, ...) { - va_list args; - char buf[YSTR_BUILD_BUF_SIZE]; - memset(buf, 0, YSTR_BUILD_BUF_SIZE); + va_list args; + char buf[YSTR_BUILD_BUF_SIZE]; + memset(buf, 0, YSTR_BUILD_BUF_SIZE); - va_start(args, fmt); - int written = vsnprintf(buf, YSTR_BUILD_BUF_SIZE, fmt, args); - va_end(args); - - // If truncated, make more space. - if (written >= YSTR_BUILD_BUF_SIZE) { - char *buf2 = calloc(written + 1, sizeof(char)); va_start(args, fmt); - vsnprintf(buf, written + 1, fmt, args); + int written = vsnprintf(buf, YSTR_BUILD_BUF_SIZE, fmt, args); va_end(args); + // If truncated, make more space. + if (written >= YSTR_BUILD_BUF_SIZE) { + char *buf2 = calloc(written + 1, sizeof(char)); + va_start(args, fmt); + vsnprintf(buf, written + 1, fmt, args); + va_end(args); + + ystr_t fmtd; + ystr_set_owned(&fmtd, buf2); + ystr_append(s, &fmtd); + ystr_destroy(&fmtd); + return; + } ystr_t fmtd; - ystr_set_owned(&fmtd, buf2); + ystr_init(&fmtd, NULL); + ystr_set_owned(&fmtd, buf); ystr_append(s, &fmtd); - ystr_destroy(&fmtd); - return; - } - ystr_t fmtd; - ystr_init(&fmtd, NULL); - ystr_set_owned(&fmtd, buf); - ystr_append(s, &fmtd); - // No destroy, fmtd points to buffer on stack. + // No destroy, fmtd points to buffer on stack. }
--- a/src/base/str.h Mon Aug 19 22:16:07 2019 +0200 +++ b/src/base/str.h Mon Aug 19 22:16:19 2019 +0200 @@ -15,17 +15,17 @@ static const size_t YSTR_SMALL_THRESHOLD = sizeof(yvec_t) - 2; typedef struct { - union { - /// A string in an array, also NULL-terminated for easy conversion to C - /// strings. - yvec_t big; - /// The last two bytes will be zero if this is an inline - /// string; otherwise they will be 1, because the size - /// field of the yvec_t will be 1. - /// - /// This field is NULL-terminated. - char small[sizeof(yvec_t) - 2]; - } inner; + union { + /// A string in an array, also NULL-terminated for easy conversion to C + /// strings. + yvec_t big; + /// The last two bytes will be zero if this is an inline + /// string; otherwise they will be 1, because the size + /// field of the yvec_t will be 1. + /// + /// This field is NULL-terminated. + char small[sizeof(yvec_t) - 2]; + } inner; } ystr_t; /**
--- a/src/base/str_test.c Mon Aug 19 22:16:07 2019 +0200 +++ b/src/base/str_test.c Mon Aug 19 22:16:19 2019 +0200 @@ -5,132 +5,132 @@ #include <stdio.h> void test_inline_string() { - ystr_t str, str2; + ystr_t str, str2; - ystr_init(&str, "short"); - assert(0 == str.inner.big.size); - assert(0 == strcmp("short", ystr_str(&str))); + ystr_init(&str, "short"); + assert(0 == str.inner.big.size); + assert(0 == strcmp("short", ystr_str(&str))); - ystr_init(&str2, "looooooooooooooooooooooooooong"); - assert(0 != str2.inner.big.size); - assert(0 == strcmp("looooooooooooooooooooooooooong", ystr_str(&str2))); + ystr_init(&str2, "looooooooooooooooooooooooooong"); + assert(0 != str2.inner.big.size); + assert(0 == strcmp("looooooooooooooooooooooooooong", ystr_str(&str2))); - assert(5 == ystr_len(&str)); + assert(5 == ystr_len(&str)); - ystr_set(&str2, "short, too"); - assert(str.inner.big.size == 0); // is inline. + ystr_set(&str2, "short, too"); + assert(str.inner.big.size == 0); // is inline. - ystr_destroy(&str); - ystr_destroy(&str2); - assert(0 == ystr_len(&str)); + ystr_destroy(&str); + ystr_destroy(&str2); + assert(0 == ystr_len(&str)); } void test_str_set_owned() { - ystr_t str; - ystr_init(&str, NULL); - char *s = strdup("Hello"); - ystr_set_owned(&str, s); - assert(s == ystr_str(&str)); - ystr_destroy(&str); + ystr_t str; + ystr_init(&str, NULL); + char *s = strdup("Hello"); + ystr_set_owned(&str, s); + assert(s == ystr_str(&str)); + ystr_destroy(&str); } void test_str_set() { - ystr_t str, str2; - ystr_init(&str, "ab"); - ystr_init(&str2, "ab"); - assert(0 == ystr_cmp(&str, &str2)); + ystr_t str, str2; + ystr_init(&str, "ab"); + ystr_init(&str2, "ab"); + assert(0 == ystr_cmp(&str, &str2)); } void test_str_at() { - ystr_t s; - ystr_init(&s, "Hello"); + ystr_t s; + ystr_init(&s, "Hello"); - assert('H' == ystr_at(&s, 0)); - assert('o' == ystr_at(&s, 4)); - assert(-1 == ystr_at(&s, 5)); - ystr_destroy(&s); + assert('H' == ystr_at(&s, 0)); + assert('o' == ystr_at(&s, 4)); + assert(-1 == ystr_at(&s, 5)); + ystr_destroy(&s); } void test_str_cmp() { - ystr_t s1, s2; - ystr_init(&s1, "aaaaaaaaaaaaaaaaaaaabc"); - ystr_init(&s2, "aaaaaaaaaaaaaaaaaaaabd"); - assert(-1 == ystr_cmp(&s1, &s2)); - assert(1 == ystr_cmp(&s2, &s1)); - assert(0 == ystr_cmp(&s1, &s1)); - ystr_destroy(&s1); - ystr_destroy(&s2); + ystr_t s1, s2; + ystr_init(&s1, "aaaaaaaaaaaaaaaaaaaabc"); + ystr_init(&s2, "aaaaaaaaaaaaaaaaaaaabd"); + assert(-1 == ystr_cmp(&s1, &s2)); + assert(1 == ystr_cmp(&s2, &s1)); + assert(0 == ystr_cmp(&s1, &s1)); + ystr_destroy(&s1); + ystr_destroy(&s2); } void test_str_append() { - ystr_t s1, s2, s3; - ystr_init(&s1, "xyz"); - ystr_init(&s2, "abcdefghijklmnop"); - ystr_init(&s3, "0123"); + ystr_t s1, s2, s3; + ystr_init(&s1, "xyz"); + ystr_init(&s2, "abcdefghijklmnop"); + ystr_init(&s3, "0123"); - ystr_append(&s1, &s2); - assert(0 == ystr_cmp_str(&s1, "xyzabcdefghijklmnop")); + ystr_append(&s1, &s2); + assert(0 == ystr_cmp_str(&s1, "xyzabcdefghijklmnop")); - ystr_append(&s1, &s3); - assert(0 == ystr_cmp_str(&s1, "xyzabcdefghijklmnop0123")); + ystr_append(&s1, &s3); + assert(0 == ystr_cmp_str(&s1, "xyzabcdefghijklmnop0123")); - ystr_append(&s1, &s3); - assert(0 == ystr_cmp_str(&s1, "xyzabcdefghijklmnop01230123")); + ystr_append(&s1, &s3); + assert(0 == ystr_cmp_str(&s1, "xyzabcdefghijklmnop01230123")); - ystr_set(&s1, "xyz"); - ystr_build(&s1, " hello %s %d", "world", 13); - assert(0 == ystr_cmp_str(&s1, "xyz hello world 13")); + ystr_set(&s1, "xyz"); + ystr_build(&s1, " hello %s %d", "world", 13); + assert(0 == ystr_cmp_str(&s1, "xyz hello world 13")); - ystr_destroy(&s1); - ystr_destroy(&s2); - ystr_destroy(&s3); + ystr_destroy(&s1); + ystr_destroy(&s2); + ystr_destroy(&s3); } void test_string_split() { - ystr_t str; - yvec_t parts; - ystr_init(&str, "hello,world,this,is,me"); - ystr_split(&str, ',', &parts); + ystr_t str; + yvec_t parts; + ystr_init(&str, "hello,world,this,is,me"); + ystr_split(&str, ',', &parts); - assert(5 == parts.len); - assert(5 == parts.cap); - assert(0 == strcmp("world", ystr_str(YVEC_AT(&parts, 1, ystr_t)))); + assert(5 == parts.len); + assert(5 == parts.cap); + assert(0 == strcmp("world", ystr_str(YVEC_AT(&parts, 1, ystr_t)))); - ystr_destroy(&str); + ystr_destroy(&str); - for (size_t i = 0; i < parts.len; i++) { - ystr_destroy(YVEC_AT(&parts, i, ystr_t)); - } - yvec_destroy(&parts); + for (size_t i = 0; i < parts.len; i++) { + ystr_destroy(YVEC_AT(&parts, i, ystr_t)); + } + yvec_destroy(&parts); } void test_string_resize() { - ystr_t s; - ystr_init(&s, "hello"); + ystr_t s; + ystr_init(&s, "hello"); - // No-op in small buffer. - assert(5 == ystr_len(&s)); - ystr_resize(&s, 10); - assert(5 == ystr_len(&s)); - // Truncate - ystr_resize(&s, 4); - assert(0 == ystr_cmp_str(&s, "hell")); - assert(4 == ystr_len(&s)); - // Extend far - ystr_resize(&s, 20); - assert(20 == ystr_len(&s)); + // No-op in small buffer. + assert(5 == ystr_len(&s)); + ystr_resize(&s, 10); + assert(5 == ystr_len(&s)); + // Truncate + ystr_resize(&s, 4); + assert(0 == ystr_cmp_str(&s, "hell")); + assert(4 == ystr_len(&s)); + // Extend far + ystr_resize(&s, 20); + assert(20 == ystr_len(&s)); - ystr_destroy(&s); + ystr_destroy(&s); } int main(int argc, char **argv) { - test_inline_string(); - test_str_set_owned(); - test_str_set(); - test_str_at(); - test_str_cmp(); - test_str_append(); - test_string_split(); - test_string_resize(); - return 0; + test_inline_string(); + test_str_set_owned(); + test_str_set(); + test_str_at(); + test_str_cmp(); + test_str_append(); + test_string_split(); + test_string_resize(); + return 0; }
--- a/src/base/vec.c Mon Aug 19 22:16:07 2019 +0200 +++ b/src/base/vec.c Mon Aug 19 22:16:19 2019 +0200 @@ -10,77 +10,82 @@ */ static const unsigned int growth_factor = 2; +static const size_t initial_cap = 4; /** * Grow a vector's capacity to a new value. It is unchanged if `new_cap` is * smaller than the current capacity. */ static void yvec_growcap(yvec_t *vec, size_t new_cap) { - if (new_cap > vec->cap) { - vec->base = realloc(vec->base, new_cap * vec->size); - assert(vec->base != NULL); - vec->cap = new_cap; - } - assert(vec->len <= vec->cap); + if (new_cap > vec->cap) { + vec->base = realloc(vec->base, new_cap * vec->size); + assert(vec->base != NULL); + vec->cap = new_cap; + } + assert(vec->len <= vec->cap); } /** * Grow a vector by `growth_factor`. */ static void yvec_grow(yvec_t *vec) { - yvec_growcap(vec, growth_factor * vec->cap); - assert(vec->base != NULL); - assert(vec->len <= vec->cap); + if (vec->cap == 0) { + yvec_growcap(vec, initial_cap); + } else { + yvec_growcap(vec, growth_factor * vec->cap); + } + assert(vec->base != NULL); + assert(vec->len <= vec->cap); } void yvec_init(yvec_t *vec, size_t size, size_t n) { - assert(vec); - vec->size = size; - vec->len = 0; - if (n == 0) { - vec->base = NULL; - vec->cap = 0; - } else { - vec->base = calloc(n, size); - vec->cap = n; - } + assert(vec); + vec->size = size; + vec->len = 0; + if (n == 0) { + vec->base = NULL; + vec->cap = 0; + } else { + vec->base = calloc(n, size); + vec->cap = n; + } } static inline void *yvec_at_nocheck(yvec_t *vec, size_t index) { - return (void *)((uint8_t *)vec->base + (index * vec->size)); + return (void *)((uint8_t *)vec->base + (index * vec->size)); } void *yvec_at(yvec_t *vec, size_t index) { - if (index >= vec->len) - return NULL; - return yvec_at_nocheck(vec, index); + if (index >= vec->len) + return NULL; + return yvec_at_nocheck(vec, index); } void yvec_resize(yvec_t *vec, size_t new_size) { - if (new_size > vec->cap) { - yvec_growcap(vec, new_size); - memset(yvec_at_nocheck(vec, vec->len), 0, (new_size - vec->len)*vec->size); - vec->len = new_size; - } else { - vec->len = new_size; - } - assert(vec->len <= vec->cap); + if (new_size > vec->cap) { + yvec_growcap(vec, new_size); + memset(yvec_at_nocheck(vec, vec->len), 0, (new_size - vec->len)*vec->size); + vec->len = new_size; + } else { + vec->len = new_size; + } + assert(vec->len <= vec->cap); } void yvec_push(yvec_t *vec, const void *element) { - if (vec->len >= vec->cap) { - yvec_grow(vec); - } - memcpy(yvec_at_nocheck(vec, vec->len), element, vec->size); - vec->len += 1; - assert(vec->len <= vec->cap); + if (vec->len >= vec->cap) { + yvec_grow(vec); + } + memcpy(yvec_at_nocheck(vec, vec->len), element, vec->size); + vec->len += 1; + assert(vec->len <= vec->cap); } void yvec_push_multi(yvec_t *vec, const void *elements, size_t n) { - yvec_growcap(vec, vec->len + n); // Grow capacity to at least len+n. - memcpy(yvec_at_nocheck(vec, vec->len), elements, n * vec->size); - vec->len += n; - assert(vec->len <= vec->cap); + yvec_growcap(vec, vec->len + n); // Grow capacity to at least len+n. + memcpy(yvec_at_nocheck(vec, vec->len), elements, n * vec->size); + vec->len += n; + assert(vec->len <= vec->cap); } void yvec_append(yvec_t *v1, yvec_t *v2) { @@ -91,14 +96,14 @@ } void yvec_copy(yvec_t *src, yvec_t *dst) { - dst->size = src->size; - dst->len = src->len; - dst->cap = src->len; - dst->base = calloc(src->len, src->size); - memcpy(dst->base, src->base, src->len * src->size); + dst->size = src->size; + dst->len = src->len; + dst->cap = src->len; + dst->base = calloc(src->len, src->size); + memcpy(dst->base, src->base, src->len * src->size); } void yvec_destroy(yvec_t *vec) { - free(vec->base); - memset(vec, 0, sizeof(yvec_t)); + free(vec->base); + memset(vec, 0, sizeof(yvec_t)); }
--- a/src/base/vec.h Mon Aug 19 22:16:07 2019 +0200 +++ b/src/base/vec.h Mon Aug 19 22:16:19 2019 +0200 @@ -18,14 +18,14 @@ * into 16 bytes. */ typedef struct { - /// Start of field. - void *base; - /// Total capacity. - size_t cap : 24; - /// Current length. - size_t len : 24; - /// Size of each element. - size_t size : 16; + /// Start of field. + void *base; + /// Total capacity. + size_t cap : 24; + /// Current length. + size_t len : 24; + /// Size of each element. + size_t size : 16; } yvec_t; typedef const char yconstchar; @@ -41,7 +41,7 @@ /// a pointer to the vector), asserting that the size of the element type is /// the same as the one expected by the vector. #define YVEC_PUSH(vecp, elemp) \ - (assert((vecp)->size == sizeof(*(elemp))), yvec_push(vecp, (void *)(elemp))) + (assert((vecp)->size == sizeof(*(elemp))), yvec_push(vecp, (void *)(elemp))) /** * @brief Initialize a vector with `n` elements of `size` bytes each. See also
--- a/src/base/vec_test.c Mon Aug 19 22:16:07 2019 +0200 +++ b/src/base/vec_test.c Mon Aug 19 22:16:19 2019 +0200 @@ -4,37 +4,37 @@ #include "vec.h" void test_vec_init_use(void) { - yvec_t vec; - YVEC_INIT(&vec, 16, int); - assert(NULL == YVEC_AT(&vec, 4, int)); + yvec_t vec; + YVEC_INIT(&vec, 16, int); + assert(NULL == YVEC_AT(&vec, 4, int)); - yvec_resize(&vec, 16); - assert(0 == *YVEC_AT(&vec, 4, int)); + yvec_resize(&vec, 16); + assert(0 == *YVEC_AT(&vec, 4, int)); - *YVEC_AT(&vec, 5, int) = 3; - assert(3 == *YVEC_AT(&vec, 5, int)); + *YVEC_AT(&vec, 5, int) = 3; + assert(3 == *YVEC_AT(&vec, 5, int)); - yvec_resize(&vec, 0); + yvec_resize(&vec, 0); - for (int i = 0; i < 100; i++) - YVEC_PUSH(&vec, &i); + for (int i = 0; i < 100; i++) + YVEC_PUSH(&vec, &i); - // Depends on growth_factor! - assert(128 == vec.cap); - fprintf(stderr, "%ld %ld %ld %d\n", vec.cap, vec.len, vec.size, - *YVEC_AT(&vec, 98, int)); - assert(98 == *YVEC_AT(&vec, 98, int)); + // Depends on growth_factor! + assert(128 == vec.cap); + fprintf(stderr, "%ld %ld %ld %d\n", vec.cap, vec.len, vec.size, + *YVEC_AT(&vec, 98, int)); + assert(98 == *YVEC_AT(&vec, 98, int)); - // Copy vector. - yvec_t other; - yvec_copy(&vec, &other); + // Copy vector. + yvec_t other; + yvec_copy(&vec, &other); - assert(98 == *YVEC_AT(&other, 98, int)); - assert(100 == other.len); - assert(100 == other.cap); + assert(98 == *YVEC_AT(&other, 98, int)); + assert(100 == other.len); + assert(100 == other.cap); - yvec_destroy(&vec); - yvec_destroy(&other); + yvec_destroy(&vec); + yvec_destroy(&other); } void test_vec_append(void) { @@ -73,7 +73,7 @@ int main(void) { - test_vec_init_use(); - test_vec_append(); - return 0; + test_vec_init_use(); + test_vec_append(); + return 0; }