summaryrefslogtreecommitdiff
path: root/src/tc-string.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/tc-string.c')
-rw-r--r--src/tc-string.c228
1 files changed, 228 insertions, 0 deletions
diff --git a/src/tc-string.c b/src/tc-string.c
new file mode 100644
index 0000000..826a9a0
--- /dev/null
+++ b/src/tc-string.c
@@ -0,0 +1,228 @@
+/* long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing */
+/*
+ * Copyright Jan Engelhardt
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the WTF Public License version 2 or
+ * (at your option) any later version.
+ */
+#ifndef __cplusplus
+# include <assert.h>
+# include <errno.h>
+# include <stddef.h>
+# include <stdio.h>
+# include <stdlib.h>
+#else
+# include <cassert>
+# include <cerrno>
+# include <cstddef>
+# include <cstdio>
+# include <cstdlib>
+#endif
+#include <libHX/defs.h>
+#include <libHX/init.h>
+#include <libHX/misc.h>
+#include <libHX/string.h>
+#include "internal.h"
+
+static void t_mc(void)
+{
+ hxmc_t *s, *old_s;
+
+ s = HXmc_meminit(NULL, 4096);
+ printf("%" HX_SIZET_FMT "u\n", HXmc_length(s));
+ if (HXmc_length(s) != 0)
+ abort();
+ old_s = s;
+ HXmc_trunc(&s, 8192);
+ if (old_s != s)
+ fprintf(stderr, "INFO: HXmc: no reallocation took place.\n");
+ printf("Length is now %" HX_SIZET_FMT "u\n", HXmc_length(s));
+ HXmc_setlen(&s, 16384);
+ printf("Length is now %" HX_SIZET_FMT "u\n", HXmc_length(s));
+ HXmc_free(s);
+}
+
+static void t_path(void)
+{
+ static const char *const paths[] = {
+ ".", "..", "/", "//", "/.", "/./", "/.//", "/./.",
+ "/mnt", "//mnt", "/mnt/", "//mnt/", "//mnt//", "//mnt//root",
+ "/mnt/.", "/mnt/./", "mnt", "mnt/", "mnt/root", "mnt/root/",
+ "mnt//root", NULL
+ };
+ const char *const *iter;
+
+ printf("#\tname\tbn\tbne\tdn\n");
+ for (iter = paths; *iter != NULL; ++iter) {
+ char *bn = HX_basename(*iter);
+ char *bne = HX_basename_exact(*iter);
+ char *dn = HX_dirname(*iter);
+ printf("\t%s\t%s\t%s\t%s\n", *iter, bn, bne, dn);
+ free(bne);
+ free(dn);
+ }
+}
+
+static void t_strcpy(void)
+{
+ hxmc_t *vp = NULL;
+
+ HXmc_strcpy(&vp, NULL);
+ if (vp != NULL)
+ abort();
+}
+
+static void t_strdup(void)
+{
+ char *a;
+ a = HX_strndup("DATA", 2);
+ printf(">%s<\n", a);
+ free(a);
+ a = HX_strndup("DATA", 10);
+ printf(">%s<\n", a);
+ free(a);
+}
+
+static void t_strncat(void)
+{
+ char data[5] = "DATA";
+
+ if (snprintf(data, sizeof(data), "12345678") >=
+ static_cast(ssize_t, sizeof(data)))
+ printf("Not enough space\n");
+ printf("String: >%s<\n", data);
+
+ HX_strlcat(data, "pqrstuv__", 2);
+ printf("String: >%s<\n", data);
+
+ *data = '\0';
+ HX_strlcat(data, "123456789", sizeof(data));
+ printf("String: >%s<\n", data);
+
+ *data = '\0';
+ HX_strlncat(data, "123456789", sizeof(data), 9);
+ printf("String: >%s<\n", data);
+}
+
+static void t_strnlen(void)
+{
+ static const char s[] = "Hello world";
+ printf("# strnlen: %" HX_SIZET_FMT "u %" HX_SIZET_FMT "u "
+ "%" HX_SIZET_FMT "u %" HX_SIZET_FMT "u %" HX_SIZET_FMT "u\n",
+ HX_strnlen(s, -1), HX_strnlen(s, 0), HX_strnlen(s, 1),
+ HX_strnlen(s, strlen(s)), HX_strnlen(s, 999));
+}
+
+static void t_strsep(void)
+{
+ char b[] = "jengelh:x:1500:100:Jan Engelhardt:/home/jengelh:/bin/bash";
+ char *wp = b, *ret;
+
+ printf("# strsep\n");
+ while ((ret = HX_strsep2(&wp, ":")) != NULL)
+ printf("%s\n", ret);
+}
+
+static void t_strtrim(void)
+{
+ char a[] = " a and b ", aexp[] = "a and b ";
+ char b[] = " a and b ", bexp[] = " a and b";
+ char c[] = "a&b", cexp[] = "a&b";
+ const char *r;
+
+ r = HX_stpltrim(a);
+ printf("HX_stpltrim(\"%s\") = \"%s\"\n", a, r);
+ assert(strcmp(r, aexp) == 0);
+
+ printf("HX_strltrim(\"%s\") = ", a);
+ printf("\"%s\"\n", (HX_strltrim(a), a));
+ assert(strcmp(a, aexp) == 0);
+
+ printf("HX_strrtrim(\"%s\") = ", b);
+ printf("\"%s\"\n", (HX_strrtrim(b), b));
+ assert(strcmp(b, bexp) == 0);
+
+ assert(strcmp(cexp, HX_stpltrim(c)) == 0);
+ assert(strcmp(cexp, (HX_strltrim(c), c)) == 0);
+ assert(strcmp(cexp, (HX_strrtrim(c), c)) == 0);
+}
+
+static void t_split(void)
+{
+ char t1[] = "root:x:0:0:root:/root:/bin/bash";
+ char t2[sizeof(t1)];
+ int f0, f1, f2;
+ char **a0, **a1, *a2[10];
+ char *const *wp;
+
+ memcpy(t2, t1, sizeof(t1));
+ a0 = HX_split(t1, ":", &f0, 0);
+ a1 = HX_split4(t1, ":", &f1, 0);
+ f2 = HX_split5(t2, ":", ARRAY_SIZE(a2), a2);
+
+ /* complete allocation */
+ printf("HX_split1: a0[%p]:", a0);
+ for (wp = a0; *wp != NULL; ++wp)
+ printf(" %s[%p]", *wp, *wp);
+ printf("\n");
+
+ /* array allocated */
+ printf("HX_split4: a1[%p]:", a1);
+ for (wp = a1; *wp != NULL; ++wp)
+ printf(" %s[%p]", *wp, *wp);
+ printf("\n");
+
+ /* nothing allocated */
+ printf("HX_split5: a2[%p]:", a2);
+ for (wp = a2; f2 > 0; --f2, ++wp)
+ printf(" %s[%p]", *wp, *wp);
+ printf("\n");
+
+ HX_zvecfree(a0);
+ free(a1);
+}
+
+static void t_split2(void)
+{
+ static const char tmp[] = "";
+ int c = 0;
+ char **a;
+
+ a = HX_split(tmp, " ", &c, 6);
+ printf("Got %d fields\n", c);
+ HX_zvecfree(a);
+}
+
+int main(int argc, const char **argv)
+{
+ hxmc_t *tx = NULL;
+ const char *file = (argc >= 2) ? argv[1] : "tx-string.cpp";
+ FILE *fp;
+
+ if (HX_init() <= 0)
+ abort();
+
+ fp = fopen(file, "r");
+ if (fp == NULL) {
+ fprintf(stderr, "Cannot open %s: %s\n", file, strerror(errno));
+ } else {
+ while (HX_getl(&tx, fp) != NULL)
+ printf("%s", tx);
+ fclose(fp);
+ }
+
+ t_mc();
+ t_path();
+ t_strcpy();
+ t_strncat();
+ t_strnlen();
+ t_strdup();
+ t_strsep();
+ t_strtrim();
+ t_split();
+ t_split2();
+ HXmc_free(tx);
+ HX_exit();
+ return EXIT_SUCCESS;
+}