Beispiel #1
0
void TestarrayQueue_new_is_empty(
    CuTest * tc
    )
{
    void *qu = aqueue_new(10, 1);
    CuAssertTrue(tc, aqueue_is_empty(qu));
    aqueue_free(qu);
}
Beispiel #2
0
void TestarrayQueue_offer_adds_new_item(
    CuTest * tc
    )
{
    char *item = "testitem";
    void *qu = aqueue_new(10, 8);

    aqueue_offer(qu, item);
    CuAssertTrue(tc, 1 == aqueue_count(qu));
    aqueue_free(qu);
}
Beispiel #3
0
void TestarrayQueue_offer_and_poll_item(
    CuTest * tc
    )
{
    char *item = "testitem";
    void *qu = aqueue_new(10, 8);

    aqueue_offer(qu, item);
    CuAssertTrue(tc, 0 == strncmp(item, aqueue_peek(qu), 8));
    CuAssertTrue(tc, 0 == aqueue_poll(qu));
    aqueue_free(qu);
}
Beispiel #4
0
void TestarrayQueue_cant_poll_with_no_contents(
    CuTest * tc
    )
{
    char *item = "testitem";
    void *qu = aqueue_new(10, 8);
    aqueue_offer(qu, item);
    CuAssertTrue(tc, 0 == strncmp(item, aqueue_peek(qu), 8));
    CuAssertTrue(tc, 0 == aqueue_poll(qu));
    CuAssertTrue(tc, 0 == aqueue_count(qu));
    aqueue_free(qu);
}
Beispiel #5
0
void TestarrayQueue_cannot_offer_over_capacity(
    CuTest * tc
    )
{
    char *item = "testitem";
    void *qu = aqueue_new(1, 8);

    aqueue_offer(qu, item);
    CuAssertTrue(tc, -1 == aqueue_offer(qu, item));
    CuAssertTrue(tc, 1 == aqueue_count(qu));
    aqueue_free(qu);
}
Beispiel #6
0
void TestarrayQueue_empty_empties_queue(
    CuTest * tc
    )
{
    char *item = "testitem";
    void *qu = aqueue_new(10, 8);

    aqueue_offer(qu, item);
    aqueue_empty(qu);
    CuAssertTrue(tc, 0 == aqueue_count(qu));
    CuAssertTrue(tc, aqueue_is_empty(qu));
    aqueue_free(qu);
}
Beispiel #7
0
void TestarrayQueue_peek_gets_head(
    CuTest * tc
    )
{
    char *item1 = "testitem1";
    char *item2 = "testitem2";
    void *qu = aqueue_new(10, 9);

    aqueue_offer(qu, item1);
    aqueue_offer(qu, item2);
    CuAssertTrue(tc, 0 == strncmp(item1, aqueue_peek(qu), 9));
    aqueue_free(qu);
}
Beispiel #8
0
void exampleArrayQueue()
{
	ArrayQueue* Q = newArrayQueue(8);

	// A queue with strings
	aqueue_offer(Q, "First");
	aqueue_offer(Q, "In");
	aqueue_offer(Q, "First");
	aqueue_offer(Q, "Out.");
	
	// Peek at the head of the queue
	printf("%s\n", (char*)aqueue_peek(Q));

	// Traverse through the queue polling each string
	while (!aqueue_isEmpty(Q))
		printf("%s ", (char*)aqueue_poll(Q));
	printf("\n");

	// A queue with integers, primitive data types require some trickyness
	aqueue_clear(Q);
	int x[] = {1, 2};
	int y = 3;
	aqueue_offer(Q, &x[0]);
	aqueue_offer(Q, &x[1]);
	aqueue_offer(Q, &y);
	
	while (!aqueue_isEmpty(Q))
		// You first need to cast it using (int*) and since its a pointer to
		// an integer you need to get the value of the pointer using *
		// You could similarly use:
		// 	int* z = queue_poll(Q);
		//		printf("%d ", *z);
		printf("%d ", *((int*)aqueue_poll(Q)));

	printf("\n");
	
	// Fill it up and resize it if an offer has failed
	char* letters[] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K"};
	for (y = 0; y < 11; y++)
		aqueue_offerf(Q, letters[y]);

	while (!aqueue_isEmpty(Q))
		printf("%s ", (char*)aqueue_poll(Q));
	printf("\n");

	// This will clear the queue of any nodes and pool them and then free
	// the queue itself from memory
	aqueue_free(Q);
}
Beispiel #9
0
void TestarrayQueue_fifo(
    CuTest * tc
    )
{
    char *item = "testitem1", *item2 = "testitem2";
    void *qu = aqueue_new(10, 9);

    aqueue_offer(qu, item);
    aqueue_offer(qu, item2);
    CuAssertTrue(tc, 0 == strncmp(item, aqueue_peek(qu), 9));
    CuAssertTrue(tc, 0 == aqueue_poll(qu));
    CuAssertTrue(tc, 0 == strncmp(item2, aqueue_peek(qu), 9));
    CuAssertTrue(tc, 0 == aqueue_poll(qu));
    aqueue_free(qu);
}
Beispiel #10
0
void TestarrayQueue_offerensure_ensures_capacity(
    CuTest * tc
    )
{
    char *item = "testitem";
    arrayqueue_t *qu = aqueue_new(1, 8);

    CuAssertTrue(tc, 0 == aqueue_offerensure(&qu, item));
    CuAssertTrue(tc, 1 == aqueue_count(qu));
    CuAssertTrue(tc, 1 == aqueue_size(qu));
    CuAssertTrue(tc, 0 == aqueue_offerensure(&qu, item));
    CuAssertTrue(tc, 2 == aqueue_count(qu));
    CuAssertTrue(tc, 2 == aqueue_size(qu));
    aqueue_free(qu);
}
Beispiel #11
0
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);
}
Beispiel #12
0
void TestarrayQueue_poll_offer_past_boundary(
    CuTest * tc
    )
{
    char *item1 = "testitem1";
    char *item2 = "testitem2";
    char *item3 = "testitem3";
    void *qu = aqueue_new(2, 9);

    aqueue_offer(qu, item1);
    aqueue_offer(qu, item2);
    CuAssertTrue(tc, 0 == strncmp(item1, aqueue_peek(qu), 9));
    CuAssertTrue(tc, 0 == aqueue_poll(qu));
    CuAssertTrue(tc, 0 == strncmp(item2, aqueue_peek(qu), 9));
    aqueue_offer(qu, item3);
    CuAssertTrue(tc, 0 == strncmp(item2, aqueue_peek(qu), 9));
    CuAssertTrue(tc, 0 == aqueue_poll(qu));
    CuAssertTrue(tc, 0 == strncmp(item3, aqueue_peek(qu), 9));
    CuAssertTrue(tc, 0 == aqueue_poll(qu));
    aqueue_free(qu);
}
Beispiel #13
0
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;
}