cDict *dict_from_slices(cList *slices) { cList *keys, *values; cDict *dict; cData *d; /* Make lists for keys and values. */ keys = list_new(list_length(slices)); values = list_new(list_length(slices)); for (d = list_first(slices); d; d = list_next(slices, d)) { if (d->type != LIST || list_length(d->u.list) != 2) { /* Invalid slice. Throw away what we had and return NULL. */ list_discard(keys); list_discard(values); return NULL; } keys = list_add(keys, list_elem(d->u.list, 0)); values = list_add(values, list_elem(d->u.list, 1)); } /* Slices were all valid; return new dict. */ dict = dict_new(keys, values); list_discard(keys); list_discard(values); return dict; }
static int check_vectors(cList * l1, cList * l2, Int * len_ret) { Int i, len; len = list_length(l1); if (list_length(l2) != len) THROW((range_id, "Arguments are not of the same length.")); for (i = 0; i < len; i++) { if (list_elem(l1, i)->type != FLOAT) THROW((type_id, "Arguments must be lists of floats.")); if (list_elem(l2, i)->type != FLOAT) THROW((type_id, "Arguments must be lists of floats.")); } *len_ret = len; RETURN_TRUE; }
static int check_one_vector(cList * l1, Int * len_ret) { Int i, len; len = list_length(l1); for (i = 0; i < len; i++) { if (list_elem(l1, i)->type != FLOAT) THROW((type_id, "Arguments must be lists of floats.")); } *len_ret = len; RETURN_TRUE; }
void testListMod(void) { yeInitMem(); GameConfig cfg; Entity *gc = yeCreateArray(NULL, NULL); Entity *e1 = yeCreateInt(0, gc, NULL); Entity *e2 = yeCreateInt(0, gc, NULL); Entity *e3 = yeCreateInt(0, gc, NULL); Entity *l; Entity *l2; Entity *l3; g_assert(!ygInitGameConfig(&cfg, NULL, NONE)); g_assert(!ygInit(&cfg)); ygLoadMod(TESTS_PATH"../modules/list/"); #define list_init_from_array(elem, father, name) ysCall(ygGetTccManager(), \ "list_init_from_array", \ elem, father, name) #define list_elem(list) ysCall(ygGetTccManager(), "list_elem", list) #define list_insert(list, elem) ysCall(ygGetTccManager(), "list_insert", \ list, elem) #define list_next(list) ysCall(ygGetTccManager(), "list_next", list) #define list_prev(list) ysCall(ygGetTccManager(), "list_prev", list) #define list_head(list) ysCall(ygGetTccManager(), "list_head", list) #define list_last(list) ysCall(ygGetTccManager(), "list_last", list) #define list_pop(list) ysCall(ygGetTccManager(), "list_pop", list) #define list_insert_before(list, elem) ysCall(ygGetTccManager(), \ "list_insert_before", \ list, elem) #define list_roll(list) ysCall(ygGetTccManager(), "list_roll", list) #define list_back_roll(list) ysCall(ygGetTccManager(), "list_back_roll", list) #define list_to_array(list, father, name) ysCall(ygGetTccManager(),\ "list_to_array", \ list, father, name) l = ysCall(ygGetTccManager(), "list_init", e1); g_assert(l); g_assert(list_elem(l) == e1); g_assert(list_prev(l) == l); g_assert(list_next(l) == l); /* test insert */ g_assert(list_insert(l, e2) == l); g_assert(list_elem(list_next(l)) == e2); g_assert(list_elem(list_prev(l)) == e2); g_assert(list_elem(list_next(list_next(l))) == e1); g_assert(list_elem(list_next(list_prev(l))) == e1); /* pop second elem */ l2 = list_pop(list_next(l)); g_assert(l2 == l); g_assert(list_elem(l) == e1); g_assert(list_prev(l) == l); g_assert(list_next(l) == l); /* insert l2 before l1 */ l2 = list_insert_before(l, e2); g_assert(l2 != l); g_assert(list_elem(l) == e1); g_assert(list_elem(l2) == e2); g_assert(list_elem(list_next(l)) == e2); g_assert(list_elem(list_prev(l)) == e2); g_assert(list_elem(list_next(list_next(l))) == e1); g_assert(list_elem(list_next(list_prev(l))) == e1); /* insert l3 before l1 */ /* l2 -> l3 -> l */ l3 = list_insert_before(l, e3); g_assert(l3 == l2); l3 = list_next(l2); g_assert(list_head(l) == l2); g_assert(l3 != l2); g_assert(list_elem(l) == e1); g_assert(list_elem(l2) == e2); g_assert(list_elem(l3) == e3); g_assert(list_elem(list_next(l)) == e2); g_assert(list_elem(list_prev(l)) == e3); g_assert(list_elem(list_next(l3)) == e1); g_assert(list_elem(list_prev(l3)) == e2); g_assert(list_elem(list_next(list_next(l))) == e3); g_assert(list_elem(list_next(list_prev(l))) == e1); l3 = list_pop(l3); g_assert(l3 == l2); g_assert(list_head(l) == l2); g_assert(list_next(l2) == l); g_assert(list_prev(l2) == l); g_assert(list_next(l) == l2); g_assert(list_prev(l) == l2); l2 = list_pop(l2); g_assert(list_head(l) == l); g_assert(l == l2); g_assert(list_next(l) == l2); g_assert(list_prev(l) == l2); g_assert(list_insert(l, e2) == l); l2 = list_last(l); g_assert(list_insert(l2, e2) == l); l3 = list_last(l); g_assert(l != l2); g_assert(l2 != l3); g_assert(l != l3); g_assert(list_prev(l) == l3); g_assert(list_next(l) == l2); g_assert(list_next(l2) == l3); g_assert(list_head(l) == l); g_assert(l2 == list_roll(l3)); g_assert(l3 == list_roll(l3)); g_assert(l == list_roll(l3)); g_assert(l3 == list_back_roll(l3)); g_assert(l2 == list_back_roll(l3)); g_assert(l == list_back_roll(l3)); ysCall(ygGetTccManager(), "list_destroy", l); l = list_init_from_array(gc, gc, "list :p"); g_assert(l); g_assert(list_elem(l) == e1); g_assert(list_elem(list_next(l)) == e2); g_assert(list_elem(list_prev(l)) == e3); g_assert(list_elem(list_next(list_next(l))) == e3); g_assert(list_elem(list_next(list_prev(l))) == e1); g_assert(yeGet(gc, "list :p") == l); Entity *ar2 = list_to_array(l, gc, "ar2"); g_assert(ar2 && ar2 == yeGet(gc, "ar2")); g_assert(yeGet(ar2, 0) == e1); g_assert(yeGet(ar2, 1) == e2); g_assert(yeGet(ar2, 2) == e3); ysCall(ygGetTccManager(), "list_destroy", l); ygCleanGameConfig(&cfg); yeDestroy(gc); ygEnd(); }
int main() { init_util(); cList *keys = list_new(2); cList *values = list_new(2); cData key1, key2; cData val1, val2; key1.type = STRING; key2.type = INTEGER; key1.u.str = string_from_chars("ab", 2); key2.u.val = 1106; val1.type = val2.type = INTEGER; val1.u.val = 10; val2.u.val = 20; list_add(keys, &key1); list_add(keys, &key2); values = list_add(values, &val1); values = list_add(values, &val2); cData keys_data, values_data; keys_data.type = values_data.type = LIST; keys_data.u.list = keys; values_data.u.list = values; cDict *mydict = dict_new(keys, values); cStr *lit = string_new(0); dict_add_literal_to_str(lit, mydict, 0); printf("%s\n", lit->s); printf("%s -> %d\n", key1.u.str->s, data_hash(&key1)); printf("%d -> %d\n", key2.u.val, data_hash(&key2)); puts("Testing dict!"); int i; for (i = 0; i < mydict->hashtab_size; i++) { printf("dict->links[%d] = %d\n", i, mydict->links[i]); } puts(""); for (i = 0; i < mydict->hashtab_size; i++) { printf("dict->hashtab[%d] = %d\n", i, mydict->hashtab[i]); } puts(""); for (i = 0; i < list_length(values); i++) { printf("dict->values[%d] = %s\n", i, data_to_literal(list_elem(mydict->values, i), 0)->s); //printf("dict->values[%d] = %d\n", i, list_elem(values, i)->u.val); } /*puts("Find some valz!"); cData ret; cData ret2; long res = dict_find(mydict, &key1, &ret); printf("%s => %d (%d)\n", key1.u.str->s, ret.u.val, res == keynf_id); res = dict_find(mydict, &key2, &ret2); printf("%s => %d (%d)\n", key2.u.str->s, ret2.u.val, res == keynf_id); */ return 0; }