Exemplo n.º 1
0
static kvpair_t *mk_kvpairs(char *spec[]) {
  kvpair_t *last = NULL;
  kvpair_t *head = NULL;

  int i = 0;
  while (spec[i]) {
    if (head == NULL) {
      assert(last == NULL);
      head = last = mk_kvpair(spec[i], empty);
    } else {
      last->next = mk_kvpair(spec[i], empty);
      last = last->next;
    }

    i++;

    while(spec[i]) {
      add_kvpair_value(last, spec[i]);
      i++;
    }

    i++;
  }

  return head;
}
Exemplo n.º 2
0
END_TEST

START_TEST (test_add_value_to_empty_values)
{
    pair = mk_kvpair("some_key", NULL);

    fail_if(pair == NULL, "Didn't create a pair.");
    fail_unless(strcmp(pair->key, "some_key") == 0, "Key is broken.");
    fail_unless(pair->used_values == 0, "Has values?");
    fail_unless(pair->allocated_values >= pair->used_values,
                "Allocated values can't be smaller than used values.");
    fail_unless(pair->values[0] == NULL, "First value isn't null.");
    fail_unless(pair->next == NULL, "Next pointer is non-null.");

    add_kvpair_value(pair, "newvalue1");
    fail_unless(pair->used_values == 1, "Value at 1");
    fail_unless(strcmp(pair->values[0], "newvalue1") == 0, "Unexpected value at 0");

    add_kvpair_value(pair, "newvalue2");
    fail_unless(pair->used_values == 2, "Value at 2");
    fail_unless(strcmp(pair->values[1], "newvalue2") == 0, "Unexpected value at 1");
}
Exemplo n.º 3
0
END_TEST

START_TEST (test_add_value_to_existing_values)
{
    char* args[] = {"arg1", "arg2", NULL};
    pair = mk_kvpair("some_key", args);

    fail_if(pair == NULL, "Didn't create a pair.");
    fail_unless(strcmp(pair->key, "some_key") == 0, "Key is broken.");
    fail_unless(pair->used_values == 2, "Has values?");
    fail_unless(pair->allocated_values >= pair->used_values,
                "Allocated values can't be smaller than used values.");
    fail_unless(pair->next == NULL, "Next pointer is non-null.");

    add_kvpair_value(pair, "newvalue1");
    fail_unless(pair->used_values == 3, "Value at 3");
    add_kvpair_value(pair, "newvalue2");
    fail_unless(pair->used_values == 4, "Value at 4");

    fail_unless(strcmp(pair->values[0], "arg1") == 0, "Unexpected value at 0");
    fail_unless(strcmp(pair->values[1], "arg2") == 0, "Unexpected value at 1");
    fail_unless(strcmp(pair->values[2], "newvalue1") == 0, "Unexpected value at 2");
    fail_unless(strcmp(pair->values[3], "newvalue2") == 0, "Unexpected value at 3");
}
Exemplo n.º 4
0
static int append_kvpair_from_db(void* arg, int n, char **vals, char **cols)
{
    (void)n;
    (void)cols;
    kvpair_t** pairs = (kvpair_t**)arg;
    kvpair_t* pair = find_kvpair(*pairs, vals[0]);

    /* If such a list doesn't already exist, one will be assigned to you */
    if (!pair) {
        pair = mk_kvpair(vals[0], NULL);
        pair->next = *pairs;
        *pairs = pair;
    }

    add_kvpair_value(pair, vals[1]);

    return SQLITE_OK;
}
Exemplo n.º 5
0
kvpair_t* mk_kvpair(const char* k, char** v)
{
    kvpair_t* rv = calloc(1, sizeof(kvpair_t));
    assert(rv);

    rv->key = safe_strdup(k);
    if (v) {
        int i = 0;
        for (i = 0; v[i]; i++) {
            add_kvpair_value(rv, v[i]);
        }
    } else {
        rv->allocated_values = 4;
        rv->values = calloc(4, sizeof(char*));
        assert(rv->values);
    }

    return rv;
}