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; }
END_TEST START_TEST (test_find_second_item) { kvpair_t *pair1 = mk_kvpair("some_key", NULL); pair = mk_kvpair("some_other_key", NULL); pair->next = pair1; fail_unless(find_kvpair(pair, "some_key") == pair1, "Depth search failed."); }
END_TEST START_TEST (test_find_missing_item) { kvpair_t *pair1 = mk_kvpair("some_key", NULL); pair = mk_kvpair("some_other_key", NULL); pair->next = pair1; fail_unless(find_kvpair(pair, "missing_key") == NULL, "Negative search failed."); }
END_TEST START_TEST (test_simple_find_second_item) { char *val[2] = { "someval", NULL }; kvpair_t *pair1 = mk_kvpair("some_key", val ); pair = mk_kvpair("some_other_key", NULL); pair->next = pair1; fail_unless(strcmp(get_simple_kvpair_val(pair, "some_key"), "someval") == 0, "Depth search failed."); }
END_TEST START_TEST (test_find_first_item) { pair = mk_kvpair("some_key", NULL); fail_unless(find_kvpair(pair, "some_key") == pair, "Identity search failed."); }
END_TEST START_TEST (test_kvpair_storage) { char* args[] = {"arg1", "arg2", NULL}; kvpair_t *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(strcmp(pair->values[0], "arg1") == 0, "First value is broken."); fail_unless(strcmp(pair->values[1], "arg2") == 0, "Second value is broken."); fail_unless(pair->used_values == 2, "Wrong number of used 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."); fail_unless(save_kvpairs(handle, pair, db_loc), "Failed to save kv pairs."); kvpair_t *dbpair = load_kvpairs(handle, db_loc); check_pair_equality(pair, dbpair); free_kvpair(pair); free_kvpair(dbpair); }
END_TEST START_TEST (test_simple_find_first_item) { char *val[2] = { "someval", NULL }; pair = mk_kvpair("some_key", val); fail_unless(strcmp(get_simple_kvpair_val(pair, "some_key"), "someval") == 0, "Identity search failed."); }
kvpair_t *dup_kvpair(kvpair_t *pair) { assert(pair); kvpair_t *copy = mk_kvpair(pair->key, pair->values); if (pair->next) { copy->next = dup_kvpair(pair->next); } return copy; }
END_TEST START_TEST (test_copy_pair) { char *args1[] = {"arg1", "arg2", NULL}; char *args2[] = {"other", NULL}; kvpair_t *pair1 = mk_kvpair("some_key", args1); pair = mk_kvpair("some_other_key", args2); pair->next = pair1; kvpair_t *copy = dup_kvpair(pair); fail_if(copy == NULL, "Copy failed."); fail_if(copy == pair, "Copy something not an identity."); fail_unless(strcmp(copy->key, pair->key) == 0, "Keys don't match."); fail_if(copy->key == pair->key, "Keys were identical."); check_pair_equality(pair, copy); free_kvpair(copy); }
END_TEST START_TEST (test_mk_pair_without_arg) { 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."); }
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; }
static kvpair_t *grok_form(xmpp_stanza_t *fields) { kvpair_t *rv = NULL; /* walk the field peers and grab their values and stuff */ while (fields) { if (xmpp_stanza_is_tag(fields)) { char* k = xmpp_stanza_get_attribute(fields, "var"); char **vals = get_specific_form_values(fields, k); kvpair_t* thispair = mk_kvpair(k, vals); free_string_list(vals); thispair->next = rv; rv = thispair; } fields = xmpp_stanza_get_next(fields); } return rv; }
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"); }
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"); }