Exemplo n.º 1
0
TEST_C(DequeTests, DequeCopyShallow)
{
    int a = 1;
    int b = 2;
    int c = 3;

    deque_add_last(deque, &a);
    deque_add_last(deque, &b);
    deque_add_last(deque, &c);

    Deque *copy;
    deque_copy_shallow(deque, &copy);

    int size = deque_size(copy);
    CHECK_EQUAL_C_INT(3, size);

    int *ca;
    deque_get_at(copy, 0, (void*)&ca);

    int *cb;
    deque_get_at(copy, 1, (void*)&cb);

    int *cc;
    deque_get_at(copy, 2, (void*)&cc);

    CHECK_EQUAL_C_INT(a, *ca);
    CHECK_EQUAL_C_INT(b, *cb);
    CHECK_EQUAL_C_INT(c, *cc);
    deque_destroy(copy);
};
Exemplo n.º 2
0
TEST_C(DequeTests, DequeCopyDeep)
{
    int *a = malloc(sizeof(int));
    int *b = malloc(sizeof(int));
    int *c = malloc(sizeof(int));

    *a = 1;
    *b = 2;
    *c = 3;

    deque_add_last(deque, a);
    deque_add_last(deque, b);
    deque_add_last(deque, c);

    Deque *copy;
    deque_copy_deep(deque, cpy, &copy);

    int size = deque_size(copy);
    CHECK_EQUAL_C_INT(3, size);

    int *ca;
    deque_get_at(copy, 0, (void*)&ca);
    int *cb;
    deque_get_at(copy, 1, (void*)&cb);
    int *cc;
    deque_get_at(copy, 2, (void*)&cc);

    CHECK_EQUAL_C_INT(1, *ca);
    CHECK_EQUAL_C_INT(2, *cb);
    CHECK_EQUAL_C_INT(3, *cc);
    deque_destroy_cb(copy, free);
    free(a);
    free(b);
    free(c);
}
Exemplo n.º 3
0
TEST_C(DequeTests, DequeAddAt4)
{
    /* index < size / 2 && index_raw < last_raw*/

    int a = 1;
    int b = 2;
    int c = 3;
    int d = 4;
    int e = 5;
    int f = 6;
    int g = 999;

    deque_add_last(deque, &a);
    deque_add_last(deque, &b);
    deque_add_last(deque, &c);
    deque_add_last(deque, &d);
    deque_add_last(deque, &e);
    deque_add_first(deque, &f);

    deque_add_at(deque, &g, 1);

    const void * const*buff = deque_get_buffer(deque);
    const int elem = *((int*) buff[0]);

    CHECK_EQUAL_C_INT(elem, g);

    const int elem1 = *((int*) buff[4]);
    CHECK_EQUAL_C_INT(elem1, e);

    const int elem2 = *((int*) buff[6]);
    CHECK_EQUAL_C_INT(elem2, f);

    const int elem3 = *((int*) buff[7]);
    CHECK_EQUAL_C_INT(elem3, a);
};
Exemplo n.º 4
0
TEST_C(DequeTests, DequeAddAt2)
{
    /* index < deque->size / 2 && index_raw > first_raw */

    int a = 1;
    int b = 2;
    int c = 3;
    int d = 4;
    int e = 5;
    int f = 6;
    int g = 999;

    deque_add_last(deque, &a);
    deque_add_last(deque, &b);
    deque_add_first(deque, &c);
    deque_add_first(deque, &d);
    deque_add_first(deque, &e);
    deque_add_first(deque, &f);

    deque_add_at(deque, &g, 1);

    const void * const *buff = deque_get_buffer(deque);
    const void *elem = buff[5];

    CHECK_EQUAL_C_POINTER(elem, &g);

    const void *elem1 = buff[0];
    CHECK_EQUAL_C_POINTER(elem1, &a);

    const void *elem2 = buff[7];
    CHECK_EQUAL_C_POINTER(elem2, &c);

    const void *elem3 = buff[3];
    CHECK_EQUAL_C_POINTER(elem3, &f);
};
Exemplo n.º 5
0
TEST_C(DequeTestsConf, DequeBufferExpansion)
{
    int a = 1;
    int b = 2;
    int c = 3;
    int d = 4;
    int e = 5;
    int f = 6;

    deque_add_first(deque, &a);
    deque_add_last(deque, &b);
    deque_add_first(deque, &c);
    deque_add_last(deque, &d);

    size_t capacity = deque_capacity(deque);

    CHECK_EQUAL_C_INT(4, capacity);

    /* Current layout:
       _________________
       | b | d | c | a |
       -----------------
             L   F
     */
    /* This line should trigger the buffer expansion */
    deque_add_first(deque, &e);

    capacity = deque_capacity(deque);
    CHECK_EQUAL_C_INT(8, capacity);

    /* The expansion should align the elements.*/
    const void * const* buff = deque_get_buffer(deque);
    const int elem = *((int*) buff[0]);

    CHECK_EQUAL_C_INT(elem, c);

    const int elem1 = *((int*) buff[1]);
    CHECK_EQUAL_C_INT(elem1, a);

    const int elem2 = *((int*) buff[2]);
    CHECK_EQUAL_C_INT(elem2, b);

    const int elem3 = *((int*) buff[3]);
    CHECK_EQUAL_C_INT(elem3, d);

    const int elem4 = *((int*) buff[7]);
    CHECK_EQUAL_C_INT(elem4, e);

    deque_add_last(deque, &f);

    const int elem5 = *((int*) buff[4]);
    CHECK_EQUAL_C_INT(elem5, f);
};
Exemplo n.º 6
0
TEST_C(DequeTests, DequeFilterMut1)
{
    int a = 1;
    int b = 2;
    int c = 3;
    int d = 4;
    int e = 5;
    int f = 6;

    deque_add_last(deque, &a);
    deque_add_last(deque, &b);
    deque_add_last(deque, &c);
    deque_add_last(deque, &d);
    deque_add_last(deque, &e);
    deque_add_last(deque, &f);
    CHECK_EQUAL_C_INT(6, deque_size(deque));

    deque_filter_mut(deque, pred1);
    CHECK_EQUAL_C_INT(3, deque_size(deque));

    int *removed = NULL;
    deque_remove_first(deque, (void*) &removed);
    CHECK_EQUAL_C_INT(a, *removed);

    deque_remove_first(deque, (void*) &removed);
    CHECK_EQUAL_C_INT(b, *removed);

    deque_remove_first(deque, (void*) &removed);
    CHECK_EQUAL_C_INT(c, *removed);
};
Exemplo n.º 7
0
TEST_C(DequeTests, DequeFilter3)
{
    int a = 1;
    int b = 2;
    int c = 3;
    int d = 4;
    int e = 5;
    int f = 6;

    deque_add_last(deque, &a);
    deque_add_last(deque, &b);
    deque_add_last(deque, &c);
    deque_add_last(deque, &d);
    deque_add_last(deque, &e);
    deque_add_last(deque, &f);
    CHECK_EQUAL_C_INT(6, deque_size(deque));

    Deque *filter = NULL;
    deque_filter(deque, pred3, &filter);
    const void * const* buff = deque_get_buffer(filter);

    CHECK_EQUAL_C_INT(1, deque_size(filter));
    CHECK_EQUAL_C_POINTER(buff[0], &f);

    free(filter);
};
Exemplo n.º 8
0
TEST_C(DequeTests, DequeAddAt1)
{
    /* index < (size / 2) && index_raw > first_raw */

    int a = 1;
    int b = 2;
    int c = 3;
    int d = 4;
    int e = 5;
    int f = 6;
    int g = 999;

    deque_add_last(deque, &a);
    deque_add_last(deque, &b);
    deque_add_last(deque, &c);
    deque_add_last(deque, &d);
    deque_add_last(deque, &e);
    deque_add_last(deque, &f);

    deque_add_at(deque, &g, 4);

    const void * const* buff = deque_get_buffer(deque);

    CHECK_EQUAL_C_POINTER(buff[4], &g);

    CHECK_EQUAL_C_POINTER(buff[5], &e);

    const void *elem = buff[6];
    CHECK_EQUAL_C_POINTER(elem, &f);
};
Exemplo n.º 9
0
TEST_C(DequeTests, DequeAddAt5)
{
    /* f == 0*/

    int a = 1;
    int b = 2;
    int c = 3;
    int d = 4;
    int e = 5;
    int f = 6;
    int g = 7;

    deque_add_last(deque, &a);
    deque_add_last(deque, &b);
    deque_add_last(deque, &c);
    deque_add_last(deque, &d);
    deque_add_last(deque, &e);
    deque_add_last(deque, &f);

    deque_add_at(deque, &g, 1);

    const void * const* buff = deque_get_buffer(deque);
    const int elem = *((int*) buff[7]);

    CHECK_EQUAL_C_INT(elem, a);

    const int elem1 = *((int*) buff[0]);
    CHECK_EQUAL_C_INT(elem1, b);

    const int elem2 = *((int*) buff[5]);
    CHECK_EQUAL_C_INT(elem2, f);

    const int elem3 = *((int*) buff[1]);
    CHECK_EQUAL_C_INT(elem3, g);
};
Exemplo n.º 10
0
TEST_C(DequeTests, DequeAddLast)
{
    int a = 1;
    int b = 2;
    int c = 3;

    deque_add_last(deque, &a);
    deque_add_last(deque, &b);
    deque_add_last(deque, &c);

    CHECK_EQUAL_C_INT(3, deque_size(deque));

    const void * const* u= deque_get_buffer(deque);
    const void *e = u[0];

    CHECK_EQUAL_C_POINTER(e, &a);

    e = u[1];
    CHECK_EQUAL_C_POINTER(e, &b);

    e = u[2];
    CHECK_EQUAL_C_POINTER(e, &c);
};
Exemplo n.º 11
0
TEST_C(DequeTests, DequeRemoveLast)
{
    int a = 1;
    int b = 2;
    int c = 3;
    int d = 4;

    deque_add_first(deque, &a);
    deque_add_first(deque, &b);
    deque_add_last(deque, &c);
    deque_add_last(deque, &d);

    int *last;
    deque_get_last(deque, (void*) &last);
    CHECK_EQUAL_C_INT(d, *last);

    int *removed;
    deque_remove_last(deque, (void*) &removed);
    CHECK_EQUAL_C_INT(d, *removed);

    deque_get_last(deque, (void*) &last);
    CHECK_EQUAL_C_INT(c, *last);
};
Exemplo n.º 12
0
TEST_C(DequeTests, DequeGetLast)
{
    int a = 1;
    int b = 2;
    int c = 3;

    deque_add_first(deque, &a);
    deque_add_last(deque, &b);
    deque_add_first(deque, &c);

    int *last;
    deque_get_last(deque, (void*) &last);

    CHECK_EQUAL_C_INT(b, *last);
};
Exemplo n.º 13
0
/**
 * Inserts a new element at the specified index within the deque. The index
 * must be within the range of the Deque.
 *
 * @param[in] deque Deque to which this new element is being added
 * @param[in] element element that is being added
 * @param[in] index position within the Deque at which this new element is
 *                  is being added
 *
 * @return CC_OK if the element was successfully added, CC_ERR_OUT_OF_RANGE if
 * the specified index was not in range, or CC_ERR_ALLOC if the memory
 * allocation for the new element failed.
 */
enum cc_stat deque_add_at(Deque *deque, void *element, size_t index)
{
    if (index >= deque->size)
        return CC_ERR_OUT_OF_RANGE;

    if (deque->capacity == deque->size && expand_capacity(deque) != CC_OK)
        return CC_ERR_ALLOC;

    const size_t c = deque->capacity - 1;
    const size_t l = deque->last & c;
    const size_t f = deque->first & c;
    const size_t p = (deque->first + index) & c;

    if (index == 0)
        return deque_add_first(deque, element);

    if (index == c)
        return deque_add_last(deque, element);

    if (index <= (deque->size / 2) - 1) {
        if (p < f || f == 0) {
            /* _________________________________
             * | 1 | 2 | 3 | 4 | 5 | . | . | 6 |
             * ---------------------------------
             *  (p) <--          L           F
             *
             * Left circular shift from (p)
             */
            const size_t r_move = (f != 0) ? c - f + 1 : 0;
            const size_t l_move = p;

            void *e_first = deque->buffer[0];

            if (f != 0) {
                memmove(&(deque->buffer[f - 1]),
                        &(deque->buffer[f]),
                        r_move * sizeof(void*));
            }
            if (p != 0) {
                memmove(&(deque->buffer[0]),
                        &(deque->buffer[1]),
                        l_move * sizeof(void*));
            }
            deque->buffer[c] = e_first;
        } else {
            memmove(&(deque->buffer[f - 1]),
                    &(deque->buffer[f]),
                    index * sizeof(void*));
        }
        deque->first = (deque->first - 1) & c;
    } else {
        if (p > l || l == c) {
            /* _________________________________
             * | 1 | . | . | 6 | 5 | 4 | 3 | 2 |
             * ---------------------------------
             *   L           F          (p) -->
             *
             * Circular right shift from (p)
             */
            void* e_last = deque->buffer[c];

            if (p != c) {
                memmove(&(deque->buffer[p + 1]),
                        &(deque->buffer[p]),
                        (c - p) * sizeof(void*));
            }
            if (l != c) {
                memmove(&(deque->buffer[1]),
                        &(deque->buffer[0]),
                        (l + 1) * sizeof(void*));
            }
            deque->buffer[0] = e_last;
        } else {
            memmove(&(deque->buffer[p + 1]),
                    &(deque->buffer[p]),
                    (deque->size - index) * sizeof(void*));
        }
        deque->last = (deque->last + 1) & c;
    }
    deque->buffer[p] = element;
    deque->size++;

    return CC_OK;
}
Exemplo n.º 14
0
/**
 * Adds a new element to the deque. The element is appended to the deque making
 * it the last element (the one with the highest index) of the Deque.
 *
 * @param[in] deque Deque to which the element is being added
 * @param[in] element element that is being added
 *
 * @return CC_OK if the element was successfully added, or CC_ERR_ALLOC if the
 * memory allocation for the new element has failed.
 */
enum cc_stat deque_add(Deque *deque, void *element)
{
    return deque_add_last(deque, element);
}
Exemplo n.º 15
0
/**
 * Adds a new element to the deque. The element is appended to the tail of the
 * deque. This function returns a <code>bool</code> based on whether or not the
 * space allocation for the new element was successful or not.
 *
 * @param[in] deque the deque to which the element is being added
 * @param[in] element the element that is being added
 *
 * @return true if the operation was successful
 */
bool deque_add(Deque *deque, void *element)
{
    return deque_add_last(deque, element);
}