+ fprintf(out, "strvec (len=%zu) [", strvec->len);
+ for (i = 0; i < ec_strvec_len(strvec); i++) {
+ if (i == 0)
+ fprintf(out, "%s", strvec->vec[i]->str);
+ else
+ fprintf(out, ", %s", strvec->vec[i]->str);
+ }
+ fprintf(out, "]\n");
+
+}
+
+/* LCOV_EXCL_START */
+static int ec_strvec_testcase(void)
+{
+ struct ec_strvec *strvec = NULL;
+ struct ec_strvec *strvec2 = NULL;
+ FILE *f = NULL;
+ char *buf = NULL;
+ size_t buflen = 0;
+ int testres = 0;
+
+ strvec = ec_strvec();
+ if (strvec == NULL) {
+ EC_TEST_ERR("cannot create strvec\n");
+ goto fail;
+ }
+ if (ec_strvec_len(strvec) != 0) {
+ EC_TEST_ERR("bad strvec len (0)\n");
+ goto fail;
+ }
+ if (ec_strvec_add(strvec, "0") < 0) {
+ EC_TEST_ERR("cannot add (0) in strvec\n");
+ goto fail;
+ }
+ if (ec_strvec_len(strvec) != 1) {
+ EC_TEST_ERR("bad strvec len (1)\n");
+ goto fail;
+ }
+ if (ec_strvec_add(strvec, "1") < 0) {
+ EC_TEST_ERR("cannot add (1) in strvec\n");
+ goto fail;
+ }
+ if (ec_strvec_len(strvec) != 2) {
+ EC_TEST_ERR("bad strvec len (2)\n");
+ goto fail;
+ }
+ if (strcmp(ec_strvec_val(strvec, 0), "0")) {
+ EC_TEST_ERR("invalid element in strvec (0)\n");
+ goto fail;
+ }
+ if (strcmp(ec_strvec_val(strvec, 1), "1")) {
+ EC_TEST_ERR("invalid element in strvec (1)\n");
+ goto fail;
+ }
+ if (ec_strvec_val(strvec, 2) != NULL) {
+ EC_TEST_ERR("strvec val should be NULL\n");
+ goto fail;
+ }
+
+ strvec2 = ec_strvec_dup(strvec);
+ if (strvec2 == NULL) {
+ EC_TEST_ERR("cannot create strvec2\n");
+ goto fail;
+ }
+ if (ec_strvec_len(strvec2) != 2) {
+ EC_TEST_ERR("bad strvec2 len (2)\n");
+ goto fail;
+ }
+ if (strcmp(ec_strvec_val(strvec2, 0), "0")) {
+ EC_TEST_ERR("invalid element in strvec2 (0)\n");
+ goto fail;
+ }
+ if (strcmp(ec_strvec_val(strvec2, 1), "1")) {
+ EC_TEST_ERR("invalid element in strvec2 (1)\n");
+ goto fail;
+ }
+ if (ec_strvec_val(strvec2, 2) != NULL) {
+ EC_TEST_ERR("strvec2 val should be NULL\n");
+ goto fail;
+ }
+ ec_strvec_free(strvec2);
+
+ strvec2 = ec_strvec_ndup(strvec, 0, 0);
+ if (strvec2 == NULL) {
+ EC_TEST_ERR("cannot create strvec2\n");
+ goto fail;
+ }
+ if (ec_strvec_len(strvec2) != 0) {
+ EC_TEST_ERR("bad strvec2 len (0)\n");
+ goto fail;
+ }
+ if (ec_strvec_val(strvec2, 0) != NULL) {
+ EC_TEST_ERR("strvec2 val should be NULL\n");
+ goto fail;
+ }
+ ec_strvec_free(strvec2);
+
+ strvec2 = ec_strvec_ndup(strvec, 1, 1);
+ if (strvec2 == NULL) {
+ EC_TEST_ERR("cannot create strvec2\n");
+ goto fail;
+ }
+ if (ec_strvec_len(strvec2) != 1) {
+ EC_TEST_ERR("bad strvec2 len (1)\n");
+ goto fail;
+ }
+ if (strcmp(ec_strvec_val(strvec2, 0), "1")) {
+ EC_TEST_ERR("invalid element in strvec2 (1)\n");
+ goto fail;
+ }
+ if (ec_strvec_val(strvec2, 1) != NULL) {
+ EC_TEST_ERR("strvec2 val should be NULL\n");
+ goto fail;
+ }
+ ec_strvec_free(strvec2);
+
+ strvec2 = ec_strvec_ndup(strvec, 3, 1);
+ if (strvec2 != NULL) {
+ EC_TEST_ERR("strvec2 should be NULL\n");
+ goto fail;
+ }
+ ec_strvec_free(strvec2);
+
+ strvec2 = EC_STRVEC("0", "1");
+ if (strvec2 == NULL) {
+ EC_TEST_ERR("cannot create strvec from array\n");
+ goto fail;
+ }
+ testres |= EC_TEST_CHECK(ec_strvec_cmp(strvec, strvec2) == 0,
+ "strvec and strvec2 should be equal\n");
+ ec_strvec_free(strvec2);
+
+ f = open_memstream(&buf, &buflen);
+ if (f == NULL)
+ goto fail;
+ ec_strvec_dump(f, strvec);
+ fclose(f);
+ f = NULL;
+ testres |= EC_TEST_CHECK(
+ strstr(buf, "strvec (len=2) [0, 1]"), "bad dump\n");
+ free(buf);
+ buf = NULL;
+
+ ec_strvec_del_last(strvec);
+ strvec2 = EC_STRVEC("0");
+ if (strvec2 == NULL) {
+ EC_TEST_ERR("cannot create strvec from array\n");
+ goto fail;
+ }
+ testres |= EC_TEST_CHECK(ec_strvec_cmp(strvec, strvec2) == 0,
+ "strvec and strvec2 should be equal\n");
+ ec_strvec_free(strvec2);
+ strvec2 = NULL;
+
+ f = open_memstream(&buf, &buflen);
+ if (f == NULL)
+ goto fail;
+ ec_strvec_dump(f, NULL);
+ fclose(f);
+ f = NULL;
+ testres |= EC_TEST_CHECK(
+ strstr(buf, "none"), "bad dump\n");
+ free(buf);
+ buf = NULL;
+
+ ec_strvec_free(strvec);
+
+ strvec = EC_STRVEC("e", "a", "f", "d", "b", "c");
+ if (strvec == NULL) {
+ EC_TEST_ERR("cannot create strvec from array\n");
+ goto fail;
+ }
+ ec_strvec_sort(strvec, NULL);
+ strvec2 = EC_STRVEC("a", "b", "c", "d", "e", "f");
+ if (strvec2 == NULL) {
+ EC_TEST_ERR("cannot create strvec from array\n");
+ goto fail;
+ }
+ testres |= EC_TEST_CHECK(ec_strvec_cmp(strvec, strvec2) == 0,
+ "strvec and strvec2 should be equal\n");
+ ec_strvec_free(strvec);
+ strvec = NULL;
+ ec_strvec_free(strvec2);
+ strvec2 = NULL;
+
+ return testres;
+
+fail:
+ if (f != NULL)
+ fclose(f);
+ ec_strvec_free(strvec);
+ ec_strvec_free(strvec2);
+ free(buf);
+
+ return -1;