summaryrefslogtreecommitdiffstats
path: root/tests/lib/test_darr.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lib/test_darr.c')
-rw-r--r--tests/lib/test_darr.c145
1 files changed, 144 insertions, 1 deletions
diff --git a/tests/lib/test_darr.c b/tests/lib/test_darr.c
index 9150aed..74aedac 100644
--- a/tests/lib/test_darr.c
+++ b/tests/lib/test_darr.c
@@ -14,7 +14,8 @@
* [x] - darr_append_n
* [x] - darr_append_nz
* [x] - darr_cap
- * [-] - darr_ensure_cap
+ * [x] - darr_ensure_avail
+ * [x] - darr_ensure_cap
* [x] - darr_ensure_i
* [x] - darr_foreach_i
* [x] - darr_foreach_p
@@ -23,6 +24,15 @@
* [ ] - darr_insertz
* [x] - darr_insert_n
* [x] - darr_insert_nz
+ * [x] - darr_in_sprintf
+ * [x] - darr_in_strcat
+ * [x] - darr_in_strcat_tail
+ * [ ] - darr_in_strcatf
+ * [ ] - darr_in_vstrcatf
+ * [x] - darr_in_strdup
+ * [x] - darr_in_strdup_cap
+ * [-] - darr_in_vsprintf
+ * [x] - darr_lasti
* [x] - darr_maxi
* [x] - darr_pop
* [x] - darr_push
@@ -31,6 +41,13 @@
* [x] - darr_remove_n
* [x] - darr_reset
* [x] - darr_setlen
+ * [x] - darr_set_strlen
+ * [x] - darr_sprintf
+ * [x] - darr_strdup
+ * [x] - darr_strdup_cap
+ * [x] - darr_strlen
+ * [x] - darr_strnul
+ * [ ] - darr_vsprintf
*/
static void test_int(void)
@@ -43,6 +60,11 @@ static void test_int(void)
int *dap;
uint i;
+ assert(darr_len(da1) == 0);
+ assert(darr_lasti(da1) == -1);
+ assert(darr_last(da1) == NULL);
+ assert(darr_end(da1) == NULL);
+
darr_ensure_i(da1, 0);
da1[0] = 0;
assert(darr_len(da1) == 1);
@@ -57,9 +79,11 @@ static void test_int(void)
da1[i] = i;
assert(darr_len(da1) == 5);
+ assert(darr_lasti(da1) == 4);
/* minimum non-pow2 array size for long long and smaller */
assert(darr_cap(da1) == 8);
assert(!memcmp(da1, a1, sizeof(a1)));
+ assert(&da1[darr_lasti(da1)] == darr_last(da1));
/* reverse the numbers */
darr_foreach_p (da1, dap)
@@ -185,6 +209,20 @@ static void test_struct(void)
assert(darr_cap(da1) == 8);
assert(!memcmp(da1, a1, sizeof(a1)));
+ assert(darr_cap(da1) - darr_len(da1) == 3);
+ darr_ensure_avail(da1, 2);
+ assert(darr_cap(da1) == 8);
+ darr_ensure_avail(da1, 3);
+ assert(darr_cap(da1) == 8);
+ darr_ensure_avail(da1, 4);
+ assert(darr_cap(da1) == 16);
+
+ darr_ensure_cap(da1, 16);
+ assert(darr_cap(da1) == 16);
+
+ darr_ensure_cap(da1, 20);
+ assert(darr_cap(da1) == 32);
+
darr_append_n(da1, 100);
assert(darr_len(da1) == 105);
@@ -272,8 +310,113 @@ static void test_struct(void)
darr_free(da2);
}
+static void test_string(void)
+{
+ const char *src = "ABCDE";
+ const char *add = "FGHIJ";
+ uint srclen = strlen(src);
+ uint addlen = strlen(add);
+ char *da1 = NULL;
+ char *da2 = NULL;
+
+ assert(darr_strlen(da1) == 0);
+
+ da1 = darr_strdup(src);
+ assert(darr_strlen(da1) == strlen(da1));
+ assert(darr_strlen(da1) == srclen);
+ assert(darr_len(da1) == srclen + 1);
+ assert(darr_ilen(da1) == (int)srclen + 1);
+ assert(darr_cap(da1) >= 8);
+ assert(darr_last(da1) == darr_strnul(da1));
+ assert(darr_strnul(da1) == da1 + darr_strlen(da1));
+
+ da2 = da1;
+ darr_in_strdup(da1, src);
+ assert(da1 == da2);
+ assert(darr_strlen(da1) == strlen(da1));
+ assert(darr_strlen(da1) == srclen);
+ assert(darr_len(da1) == srclen + 1);
+ darr_free(da1);
+ assert(da1 == NULL);
+
+ da1 = darr_strdup_cap(src, 128);
+ assert(darr_strlen(da1) == srclen);
+ assert(darr_cap(da1) >= 128);
+
+ darr_in_strdup_cap(da1, src, 256);
+ assert(darr_strlen(da1) == srclen);
+ assert(darr_cap(da1) >= 256);
+ darr_free(da1);
+
+ da1 = darr_strdup_cap(add, 2);
+ assert(darr_strlen(da1) == addlen);
+ assert(darr_cap(da1) >= 8);
+
+ darr_in_strdup(da1, "ab");
+ darr_in_strcat(da1, "/");
+ darr_in_strcat(da1, "foo");
+ assert(!strcmp("ab/foo", da1));
+ darr_free(da1);
+
+ da1 = darr_in_strcat(da1, "ab");
+ darr_in_strcat(da1, "/");
+ darr_in_strcat(da1, "foo");
+ assert(!strcmp("ab/foo", da1));
+
+ darr_set_strlen(da1, 5);
+ assert(!strcmp("ab/fo", da1));
+ darr_set_strlen(da1, 1);
+ assert(!strcmp("a", da1));
+
+ darr_in_strdup(da1, "ab");
+ da2 = darr_strdup(add);
+ darr_in_strcat_tail(da1, da2);
+ assert(!strcmp("abHIJ", da1));
+ assert(darr_strlen(da1) == 5);
+ assert(darr_len(da1) == 6);
+ darr_free(da1);
+ darr_free(da2);
+
+ da1 = darr_strdup("abcde");
+ da2 = darr_strdup(add);
+ darr_in_strcat_tail(da1, da2);
+ assert(!strcmp("abcde", da1));
+ assert(darr_strlen(da1) == 5);
+ assert(darr_len(da1) == 6);
+ darr_free(da1);
+ darr_free(da2);
+
+ da1 = darr_sprintf("0123456789: %08X", 0xDEADBEEF);
+ assert(!strcmp(da1, "0123456789: DEADBEEF"));
+ assert(darr_strlen(da1) == 20);
+ assert(darr_cap(da1) == 128);
+ da2 = da1;
+ darr_in_sprintf(da1, "9876543210: %08x", 0x0BADF00D);
+ assert(da1 == da2);
+ assert(!strcmp("9876543210: 0badf00d", da2));
+ darr_free(da1);
+ da2 = NULL;
+
+ da1 = NULL;
+ darr_in_sprintf(da1, "0123456789: %08X", 0xDEADBEEF);
+ assert(!strcmp(da1, "0123456789: DEADBEEF"));
+ assert(darr_strlen(da1) == 20);
+ assert(darr_cap(da1) == 128);
+ darr_free(da1);
+
+ da1 = darr_sprintf("0123456789: %08x", 0xDEADBEEF);
+ darr_in_strcatf(da1, " 9876543210: %08x", 0x0BADF00D);
+ assert(!strcmp("0123456789: deadbeef 9876543210: 0badf00d", da1));
+ darr_free(da1);
+
+ da1 = darr_in_strcatf(da1, "0123456789: %08x", 0xDEADBEEF);
+ assert(!strcmp("0123456789: deadbeef", da1));
+ darr_free(da1);
+}
+
int main(int argc, char **argv)
{
test_int();
test_struct();
+ test_string();
}