void TestarrayQueue_iterator_iterates( CuTest * tc ) { char *item = "testitem"; char *item2 = "TESTITEM"; arrayqueue_t *qu = aqueue_new(1, 8); CuAssertTrue(tc, 0 == aqueue_offerensure(&qu, item)); CuAssertTrue(tc, 0 == aqueue_offerensure(&qu, item2)); arrayqueue_iter_t iter; aqueue_iter(qu, &iter); CuAssertTrue(tc, 1 == aqueue_iter_has_next(qu, &iter)); CuAssertTrue(tc, 0 == strncmp(item, aqueue_iter_next(qu, &iter), 8)); CuAssertTrue(tc, 1 == aqueue_iter_has_next(qu, &iter)); CuAssertTrue(tc, 0 == strncmp(item2, aqueue_iter_next(qu, &iter), 8)); CuAssertTrue(tc, 0 == aqueue_iter_has_next(qu, &iter)); aqueue_free(qu); }
static int __load(dqueue_t* me) { unsigned int pos = 0; while (pos < me->size) { int e, start_pos; header_t h; start_pos = pos; /* 1. read first header */ e = read(me->fd, &h, sizeof(header_t)); if (e < (int)sizeof(header_t)) return -1; pos += sizeof(header_t); /* check header is ok */ if (h.type != HEADER || 0 != memcmp(h.stamp, STAMP, strlen(STAMP))) continue; unsigned int check_id = h.id; /* 2. read 2nd header */ /* put on sizeof(header_t) offset */ size_t offset = sizeof(header_t) - ntohl(h.len) % sizeof(header_t); e = lseek(me->fd, ntohl(h.len) + offset, SEEK_CUR); pos += ntohl(h.len); e = read(me->fd, &h, sizeof(header_t)); if (e < (int)sizeof(header_t)) { perror("couldn't read file\n"); return -1; } pos += __padding_required(ntohl(h.len)); pos += sizeof(header_t); if (h.id != check_id || ntohl(h.type) != FOOTER || 0 != memcmp(h.stamp, STAMP, strlen(STAMP))) continue; /* found a valid queue item */ item_t item; item.pos = start_pos; item.len = ntohl(h.len); item.space_used = ntohl(h.len) + ITEM_METADATA_SIZE + __padding_required(ntohl(h.len)); item.id = h.id; aqueue_offerensure((void*)&me->items, &item); h.id = ntohl(h.id); } if (0 == aqueue_count(me->items)) return 0; /* get lowest */ unsigned int lowest_id = UINT_MAX; unsigned int highest_id = 0; arrayqueue_iter_t iter; for (aqueue_iter(me->items, &iter); aqueue_iter_has_next(me->items, &iter); ) { item_t* item = aqueue_iter_next(me->items, &iter); if (item->id < lowest_id) { lowest_id = item->id; me->head = item->pos; } /* set tail */ if (highest_id < item->id) { highest_id = item->id; me->tail = item->pos + item->len + ITEM_METADATA_SIZE + __padding_required(item->len); me->item_id = item->id + 1; } } arrayqueue_t* stowaway = aqueue_new(16, sizeof(item_t)); /* put lowest at front of queue */ while (!aqueue_is_empty(me->items)) { item_t* item = aqueue_peek(me->items); if (item->id == lowest_id) break; aqueue_offerensure(&stowaway, aqueue_peek(me->items)); aqueue_poll(me->items); } /* empty out stowaway */ while (!aqueue_is_empty(stowaway)) { aqueue_offerensure((void*)&me->items, aqueue_peek(stowaway)); aqueue_poll(stowaway); } aqueue_free(stowaway); return 0; }