test/main.c

Fri, 05 Oct 2012 16:59:14 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 05 Oct 2012 16:59:14 +0200
changeset 52
34f50d0bada4
parent 51
1c78cd19fb6b
child 53
e533c170bfb8
permissions
-rw-r--r--

added ucx_map_copy and fixed ucx_map_rehash

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2011 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <stdlib.h>

#include "ucx/test.h"

#include "main.h"

#include "list_tests.h"
#include "dlist_tests.h"
#include "string_tests.h"
#include "mpool_tests.h"
#include "map_tests.h"

int cmp_string(void* o1, void* o2, void* data) {
    return strcmp((char*)o1, (char*)o2);
}

void* copy_string(void* e, void* data) {
    char *str = (char*) e;
    size_t n = 1+strlen(str);
    char *cpy = (char*) malloc(n);
    memcpy(cpy, str, n);
    return cpy;
}

UCX_TEST_IMPLEMENT(testTestSuitePositive) {
    UCX_TEST_BEGIN
    UCX_TEST_ASSERT(2*2 == 4, "the test framework fails");
    UCX_TEST_END
}

UCX_TEST_IMPLEMENT(testTestSuiteNegative) {
    UCX_TEST_BEGIN
    UCX_TEST_ASSERT(2*(-2) == 4, "the test framework works");
    UCX_TEST_END
}

UCX_TEST_SUBROUTINE(testTestSuiteRoutineSuccess,field) {
    int* i = (int*) field;
    *i += 2;
    UCX_TEST_ASSERT(*i==4, "the test framework fails");
}

UCX_TEST_SUBROUTINE(testTestSuiteRoutineFailure,field) {
    int* i = (int*) field;
    *i += 2;
    UCX_TEST_ASSERT(*i==4, "the test framework works");
}

UCX_TEST_IMPLEMENT(testTestSuiteRoutinePositive) {
    int i = 2;
    UCX_TEST_BEGIN
    UCX_TEST_CALL_SUBROUTINE(testTestSuiteRoutineSuccess, &i);
    UCX_TEST_ASSERT(i==4, "the test framework fails");
    UCX_TEST_END
}

UCX_TEST_IMPLEMENT(testTestSuiteRoutineNegative) {
    int i = 0;
    UCX_TEST_BEGIN
    UCX_TEST_CALL_SUBROUTINE(testTestSuiteRoutineFailure, &i);
    UCX_TEST_ASSERT(1, "the test framework fails");
    UCX_TEST_END
}

int main(int argc, char **argv) {
    printf("UCX Tests\n---------\n");

    printf("\nUcxTestSuite tests (2 failures are intended!)\n");
    UcxTestSuite* suite = ucx_test_suite_new();
    ucx_test_register(suite, testTestSuitePositive);
    ucx_test_register(suite, testTestSuiteNegative);
    ucx_test_register(suite, testTestSuiteRoutinePositive);
    ucx_test_register(suite, testTestSuiteRoutineNegative);
    ucx_test_run(suite, stdout);
    if (suite->failure == 2 && suite->success == 2) {
        ucx_test_suite_free(suite);

        printf("\nLibrary function tests\n");
        suite = ucx_test_suite_new();
        /* UcxList Tests */
        ucx_test_register(suite, test_ucx_list_append);
        ucx_test_register(suite, test_ucx_list_prepend);
        ucx_test_register(suite, test_ucx_list_equals);
        ucx_test_register(suite, test_ucx_list_concat);
        ucx_test_register(suite, test_ucx_list_size);
        ucx_test_register(suite, test_ucx_list_last);
        ucx_test_register(suite, test_ucx_list_get);
        ucx_test_register(suite, test_ucx_list_remove);
        ucx_test_register(suite, test_ucx_list_clone);
        ucx_test_register(suite, test_ucx_list_sort);
        
        /* UcxDlist Tests */
        ucx_test_register(suite, test_ucx_dlist_append);
        ucx_test_register(suite, test_ucx_dlist_prepend);
        ucx_test_register(suite, test_ucx_dlist_equals);
        ucx_test_register(suite, test_ucx_dlist_concat);
        ucx_test_register(suite, test_ucx_dlist_size);
        ucx_test_register(suite, test_ucx_dlist_first);
        ucx_test_register(suite, test_ucx_dlist_last);
        ucx_test_register(suite, test_ucx_dlist_get);
        ucx_test_register(suite, test_ucx_dlist_remove);
        ucx_test_register(suite, test_ucx_dlist_clone);
        ucx_test_register(suite, test_ucx_dlist_sort);

        /* UcxMemPool Tests */
        ucx_test_register(suite, test_ucx_mempool_new);
        ucx_test_register(suite, test_ucx_mempool_malloc);
        ucx_test_register(suite, test_ucx_mempool_malloc_with_chcap);
        ucx_test_register(suite, test_ucx_mempool_calloc);
        ucx_test_register(suite, test_ucx_mempool_set_destr);
        ucx_test_register(suite, test_ucx_mempool_reg_destr);
        ucx_test_register(suite, test_ucx_mempool_realloc);

        /* UcxMap Tests */
        ucx_test_register(suite, test_ucx_map_new);
        ucx_test_register(suite, test_ucx_key);
        ucx_test_register(suite, test_ucx_map_put);
        ucx_test_register(suite, test_ucx_map_get);
        ucx_test_register(suite, test_ucx_map_iterator);
        ucx_test_register(suite, test_ucx_map_iterator_chain);
        ucx_test_register(suite, test_ucx_map_store_load);
        ucx_test_register(suite, test_ucx_map_store_load_with_mempool);
        ucx_test_register(suite, test_ucx_map_clone);
        ucx_test_register(suite, test_ucx_map_rehash);
        
        /* sstring Tests */
        ucx_test_register(suite, test_sstr);
        ucx_test_register(suite, test_sstr_len_cat);
        ucx_test_register(suite, test_sstrsplit);

        ucx_test_run(suite, stdout);
        ucx_test_suite_free(suite);
        
        return EXIT_SUCCESS;
    } else {
        ucx_test_suite_free(suite);
        return EXIT_FAILURE;
    }
}

mercurial