static void test_iterating(void) { hashset_t set = hashset_create(); hashset_itr_t iter = hashset_iterator(set); int step; /* fill the hashset */ hashset_add(set, (void *)"Bob"); hashset_add(set, (void *)"Steve"); hashset_add(set, (void *)"Karen"); hashset_add(set, (void *)"Ellen"); step = 0; while(hashset_iterator_has_next(iter)) { if(step == 0) { assert(strncmp((char *)hashset_iterator_value(iter), "Karen", 5) == 0); } if(step == 1) { assert(strncmp((char *)hashset_iterator_value(iter), "Steve", 5) == 0); } if(step == 2) { assert(strncmp((char *)hashset_iterator_value(iter), "Bob", 3) == 0); } if(step == 3) { assert(strncmp((char *)hashset_iterator_value(iter), "Ellen", 5) == 0); } hashset_iterator_next(iter); step++; } assert(hashset_iterator_has_next(iter) == 0); assert(hashset_iterator_next(iter) == -1); }
int hashset_iterator_next(hashset_itr_t itr) { if(hashset_iterator_has_next(itr) == 0) return -1; /* Can't advance */ itr->index++; while (itr->set->items[(itr->index)] == 0 && itr->index < itr->set->capacity) { itr->index++; } return itr->index; }