void TestResize()
{
    Vector* vector = VectorInit(DEFAULT_CAPACITY);

    /* Implicitly trigger the resize function. */
    unsigned i;
    for (i = 0 ; i < SIZE_MID_TEST ; ++i)
       vector->push_back(vector, (void*)(intptr_t)i);

    /* Explicitly trigger the resize function. */
    CU_ASSERT(vector->resize(vector, SIZE_MID_TEST << 1) == true);
    CU_ASSERT(vector->resize(vector, SIZE_TNY_TEST) == true);
    VectorDeinit(vector);

    /* Test the garbage collection of resize function. */
    vector = VectorInit(DEFAULT_CAPACITY);
    vector->set_clean(vector, CleanElement);
    for (i = 0 ; i < SIZE_MID_TEST ; ++i) {
        Tuple* tuple = (Tuple*)malloc(sizeof(Tuple));
        tuple->first = i;
        tuple->second = i;
        vector->push_back(vector, tuple);
    }
    CU_ASSERT(vector->resize(vector, SIZE_TNY_TEST) == true);
    VectorDeinit(vector);
}
void TestSort()
{
    srand(time(NULL));

    void* tuples[SIZE_SML_TEST];
    unsigned i;
    for (i = 0 ; i < SIZE_SML_TEST ; ++i) {
        Tuple* tuple = (Tuple*)malloc(sizeof(Tuple));
        tuple->first = i;
        tuple->second = i;
        tuples[i] = tuple;
    }
    for (i = 0 ; i < SIZE_MID_TEST ; ++i) {
        int src = rand() % SIZE_SML_TEST;
        int tge = rand() % SIZE_SML_TEST;
        void* tuple = tuples[src];
        tuples[src] = tuples[tge];
        tuples[tge] = tuple;
    }

    Vector* vector = VectorInit(DEFAULT_CAPACITY);
    vector->set_clean(vector, CleanElement);

    for (i = 0 ; i < SIZE_SML_TEST ; ++i)
        vector->push_back(vector, tuples[i]);

    vector->sort(vector, SortElement);
    for (i = 0 ; i < SIZE_SML_TEST ; ++i) {
        void* tuple;
        vector->get(vector, i, &tuple);
        CU_ASSERT_EQUAL(((Tuple*)tuple)->first, i);
    }

    VectorDeinit(vector);
}
void TestIterator()
{
    Vector* vector = VectorInit(DEFAULT_CAPACITY);
    vector->set_clean(vector, CleanElement);

    unsigned i;
    for (i = 0 ; i < SIZE_SML_TEST ; ++i) {
        Tuple* tuple = (Tuple*)malloc(sizeof(Tuple));
        tuple->first = i;
        tuple->second = i;
        vector->push_back(vector, tuple);
    }

    void* tuple;
    i = 0;
    vector->first(vector, false);
    while (vector->next(vector, &tuple)) {
        CU_ASSERT_EQUAL(((Tuple*)tuple)->first, i);
        ++i;
    }

    i = SIZE_SML_TEST - 1;
    vector->first(vector, true);
    while (vector->reverse_next(vector, &tuple)) {
        CU_ASSERT_EQUAL(((Tuple*)tuple)->first, i);
        --i;
    }

    VectorDeinit(vector);
}
/*-----------------------------------------------------------------------------*
 *            Unit tests relevant to basic structure verification              *
 *-----------------------------------------------------------------------------*/
void TestNewDelete()
{
    Vector* vector;
    CU_ASSERT((vector = VectorInit(DEFAULT_CAPACITY)) != NULL);

    /* Enlarge the vector size to test the destructor. */
    unsigned i;
    for (i = 0 ; i < SIZE_SML_TEST ; ++i)
        CU_ASSERT(vector->push_back(vector, (void*)(intptr_t)i) == true);

    VectorDeinit(vector);
}
void TestPopAndRemove()
{
    Vector* vector = VectorInit(DEFAULT_CAPACITY);
    vector->set_clean(vector, CleanElement);
    void* dummy;

    CU_ASSERT(vector->pop_back(vector) == false);

    unsigned i;
    for (i = 0 ; i < SIZE_MID_TEST ; ++i) {
        Tuple* tuple = (Tuple*)malloc(sizeof(Tuple));
        tuple->first = i;
        tuple->second = i;
        vector->push_back(vector, tuple);
    }

    /* Pop elements ranging from (SML * 3) to MID from the vector tail. */
    for (i = 0 ; i < SIZE_SML_TEST ; ++i)
        CU_ASSERT(vector->pop_back(vector) == true);
    CU_ASSERT(vector->get(vector, SIZE_SML_TEST * 3, &dummy) == false);
    CU_ASSERT(vector->get(vector, SIZE_MID_TEST, &dummy) == false);

    /* Remove elements ranging from (SML * 2) to (SML * 3) from the vector tail. */
    for (i = SIZE_SML_TEST * 3 - 1 ; i >= SIZE_SML_TEST << 1 ; --i)
        CU_ASSERT(vector->remove(vector, i) == true);
    CU_ASSERT(vector->get(vector, SIZE_SML_TEST << 1, &dummy) == false);
    CU_ASSERT(vector->remove(vector, SIZE_SML_TEST << 1) == false);
    CU_ASSERT(vector->remove(vector, SIZE_MID_TEST) == false);

    /* Remove elements ranging from 0 to SML from the vector head. */
    for (i = 0 ; i < SIZE_SML_TEST ; ++i)
        CU_ASSERT(vector->remove(vector, 0) == true);
    CU_ASSERT(vector->get(vector, SIZE_SML_TEST, &dummy) == false);

    unsigned num = SIZE_SML_TEST;
    for (i = 0 ; i < SIZE_SML_TEST ; ++i) {
        void* tuple;
        CU_ASSERT(vector->get(vector, i, &tuple) == true);
        CU_ASSERT_EQUAL(((Tuple*)tuple)->first, num);
        ++num;
    }

    CU_ASSERT_EQUAL(vector->size(vector), SIZE_SML_TEST);
    CU_ASSERT_EQUAL(vector->capacity(vector), SIZE_MID_TEST);

    VectorDeinit(vector);
}
void TestReplace()
{
    Vector* vector = VectorInit(DEFAULT_CAPACITY);
    vector->set_clean(vector, CleanElement);
    void* dummy;

    unsigned i;
    for (i = 0 ; i < SIZE_SML_TEST ; ++i) {
        Tuple* tuple = (Tuple*)malloc(sizeof(Tuple));
        tuple->first = i;
        tuple->second = i;
        vector->push_back(vector, tuple);
    }
    CU_ASSERT(vector->set(vector, SIZE_SML_TEST, dummy) == false);

    unsigned num = SIZE_SML_TEST;
    for (i = 0 ; i < SIZE_SML_TEST ; ++i) {
        Tuple* tuple = (Tuple*)malloc(sizeof(Tuple));
        tuple->first = num;
        tuple->second = num;
        CU_ASSERT(vector->set(vector, i, tuple) == true);
        ++num;
    }
    num = SIZE_SML_TEST;
    for (i = 0 ; i < SIZE_SML_TEST ; ++i) {
        void* tuple;
        vector->get(vector, i, &tuple);
        CU_ASSERT_EQUAL(((Tuple*)tuple)->first, num);
        ++num;
    }

    CU_ASSERT_EQUAL(vector->size(vector), SIZE_SML_TEST);
    CU_ASSERT_EQUAL(vector->capacity(vector), SIZE_SML_TEST);

    VectorDeinit(vector);
}
void TestPushAndInsert()
{
    Vector* vector = VectorInit(DEFAULT_CAPACITY);
    vector->set_clean(vector, CleanElement);
    void* dummy;

    /* Push elements ranging from SML to (SML * 2) to the vector tail. */
    unsigned num = SIZE_SML_TEST;
    unsigned i;
    for (i = 0 ; i < SIZE_SML_TEST ; ++i) {
        Tuple* tuple = (Tuple*)malloc(sizeof(Tuple));
        tuple->first = num;
        tuple->second = num;
        CU_ASSERT(vector->push_back(vector, tuple) == true);
        ++num;
    }
    num = SIZE_SML_TEST;
    for (i = 0 ; i < SIZE_SML_TEST ; ++i) {
        void* tuple;
        CU_ASSERT(vector->get(vector, i, &tuple) == true);
        CU_ASSERT_EQUAL(((Tuple*)tuple)->first, num);
        ++num;
    }
    CU_ASSERT(vector->get(vector, SIZE_SML_TEST << 1, &dummy) == false);
    CU_ASSERT(vector->get(vector, SIZE_MID_TEST, &dummy) == false);

    /* Insert elements ranging from 0 to SML to the vector head. */
    num = SIZE_SML_TEST - 1;
    for (i = 0 ; i < SIZE_SML_TEST ; ++i) {
        Tuple* tuple = (Tuple*)malloc(sizeof(Tuple));
        tuple->first = num;
        tuple->second = num;
        CU_ASSERT(vector->insert(vector, 0, tuple) == true);
        --num;
    }
    for (i = 0 ; i < SIZE_SML_TEST ; ++i) {
        void* tuple;
        CU_ASSERT(vector->get(vector, i, &tuple) == true);
        CU_ASSERT_EQUAL(((Tuple*)tuple)->first, i);
    }
    CU_ASSERT(vector->insert(vector, SIZE_MID_TEST, dummy) == false);

    /* Insert elements ranging from (SML * 2) to (SML * 3) to the vector tail. */
    num = SIZE_SML_TEST << 1;
    for (i = 0 ; i < SIZE_SML_TEST ; ++i) {
        Tuple* tuple = (Tuple*)malloc(sizeof(Tuple));
        tuple->first = num;
        tuple->second = num;
        CU_ASSERT(vector->insert(vector, num, tuple) == true);
        ++num;
    }
    for (i = 0 ; i < SIZE_SML_TEST * 3 ; ++i) {
        void* tuple;
        CU_ASSERT(vector->get(vector, i, &tuple) == true);
        CU_ASSERT_EQUAL(((Tuple*)tuple)->first, i);
    }

    CU_ASSERT_EQUAL(vector->size(vector), SIZE_SML_TEST * 3);
    CU_ASSERT_EQUAL(vector->capacity(vector), SIZE_MID_TEST);

    VectorDeinit(vector);
}
示例#8
0
void ManipulateNumerics()
{
    /* We should initialize the container before any operations. */
    Vector* vector = VectorInit(DEFAULT_CAPACITY);

    /* Push the integer elements. */
    VectorPushBack(vector, (void*)(intptr_t)3);
    VectorPushBack(vector, (void*)(intptr_t)4);

    /* Insert the elements at the specified indexes. */
    VectorInsert(vector, 0, (void*)(intptr_t)1);
    VectorInsert(vector, 1, (void*)(intptr_t)2);

    /*---------------------------------------------------------------*
     * Now the vector should be: [1] | [2] | [3] | [4]               *
     *---------------------------------------------------------------*/

    /* Iterate through the vector. */
    int num = 1;
    void* elem;
    VectorFirst(vector, false);
    while (VectorNext(vector, &elem)) {
        assert((intptr_t)(void*)elem == num);
        ++num;
    }

    /* Reversely iterate through the vector. */
    num = 4;
    VectorFirst(vector, true);
    while (VectorReverseNext(vector, &elem)) {
        assert((intptr_t)(void*)elem == num);
        --num;
    }

    /* Get the elements from the specified indexes. */
    VectorGet(vector, 0, &elem);
    assert((intptr_t)(void*)elem == 1);
    VectorGet(vector, 3, &elem);
    assert((intptr_t)(void*)elem == 4);

    /* Replace the elements at the specified indexes. */
    VectorSet(vector, 0, (void*)(intptr_t)10);
    VectorSet(vector, 3, (void*)(intptr_t)40);

    /*---------------------------------------------------------------*
     * Now the vector should be: [10] | [2] | [3] | [40]             *
     *---------------------------------------------------------------*/

    /* Get the number of stored elements. */
    unsigned size = VectorSize(vector);
    assert(size == 4);

    /* Sort the integer elements. */
    VectorSort(vector, CompareNumber);

    /*---------------------------------------------------------------*
     * Now the vector should be: [2] | [3] | [10] | [40]             *
     *---------------------------------------------------------------*/

    /* Remove the elements at the specified indexes. */
    VectorRemove(vector, 3);
    VectorRemove(vector, 0);

    /*---------------------------------------------------------------*
     * Now the vector should be: [3] | [10]                          *
     *---------------------------------------------------------------*/

    VectorGet(vector, 0, &elem);
    assert((int)(intptr_t)elem == 3);
    VectorGet(vector, 1, &elem);
    assert((int)(intptr_t)elem == 10);

    /* Pop the elements. */
    VectorPopBack(vector);
    VectorPopBack(vector);
    assert(VectorSize(vector) == 0);

    VectorDeinit(vector);
}
示例#9
0
void ManipulateObjects()
{
    /* We should initialize the container before any operations. */
    Vector* vector = VectorInit(DEFAULT_CAPACITY);
    VectorSetClean(vector, CleanObject);

    /* Push the object elements. */
    Tuple* tuple = (Tuple*)malloc(sizeof(Tuple));
    tuple->first = 3;
    tuple->second = -3;
    VectorPushBack(vector, tuple);
    tuple = (Tuple*)malloc(sizeof(Tuple));
    tuple->first = 4;
    tuple->second = -4;
    VectorPushBack(vector, tuple);

    /* Insert the elements at the specified indexes. */
    tuple = (Tuple*)malloc(sizeof(Tuple));
    tuple->first = 1;
    tuple->second = -1;
    VectorInsert(vector, 0, tuple);
    tuple = (Tuple*)malloc(sizeof(Tuple));
    tuple->first = 2;
    tuple->second = -2;
    VectorInsert(vector, 1, tuple);

    /*---------------------------------------------------------------*
     * Now the vector should be: [1] | [2] | [3] | [4]               *
     *---------------------------------------------------------------*/

    /* Iterate through the vector. */
    int num = 1;
    void* elem;
    VectorFirst(vector, false);
    while (VectorNext(vector, &elem)) {
        assert(((Tuple*)elem)->first == num);
        ++num;
    }

    /* Reversely iterate through the vector. */
    num = 4;
    VectorFirst(vector, true);
    while (VectorReverseNext(vector, &elem)) {
        assert(((Tuple*)elem)->first == num);
        --num;
    }

    /* Get the elements from the specified indexes. */
    VectorGet(vector, 0, &elem);
    assert(((Tuple*)elem)->first == 1);
    VectorGet(vector, 3, &elem);
    assert(((Tuple*)elem)->first == 4);

    /* Replace the elements at the specified indexes. */
    tuple = (Tuple*)malloc(sizeof(Tuple));
    tuple->first = 10;
    tuple->second = -10;
    VectorSet(vector, 0, tuple);
    tuple = (Tuple*)malloc(sizeof(Tuple));
    tuple->first = 40;
    tuple->second = -40;
    VectorSet(vector, 3, tuple);

    /*---------------------------------------------------------------*
     * Now the vector should be: [10] | [2] | [3] | [40]             *
     *---------------------------------------------------------------*/

    /* Get the number of stored elements. */
    unsigned size = VectorSize(vector);
    assert(size == 4);

    /* Sort the integer elements. */
    VectorSort(vector, CompareObject);

    /*---------------------------------------------------------------*
     * Now the vector should be: [2] | [3] | [10] | [40]             *
     *---------------------------------------------------------------*/

    /* Remove the elements at the specified indexes. */
    VectorRemove(vector, 3);
    VectorRemove(vector, 0);

    /*---------------------------------------------------------------*
     * Now the vector should be: [3] | [10]                          *
     *---------------------------------------------------------------*/

    VectorGet(vector, 0, &elem);
    assert(((Tuple*)elem)->first == 3);
    VectorGet(vector, 1, &elem);
    assert(((Tuple*)elem)->first == 10);

    /* Pop the elements. */
    VectorPopBack(vector);
    VectorPopBack(vector);
    assert(VectorSize(vector) == 0);

    VectorDeinit(vector);
}