universe@103: /* universe@103: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@103: * universe@250: * Copyright 2017 Olaf Wintermann. All rights reserved. universe@103: * universe@103: * Redistribution and use in source and binary forms, with or without universe@103: * modification, are permitted provided that the following conditions are met: universe@103: * universe@103: * 1. Redistributions of source code must retain the above copyright universe@103: * notice, this list of conditions and the following disclaimer. universe@26: * universe@103: * 2. Redistributions in binary form must reproduce the above copyright universe@103: * notice, this list of conditions and the following disclaimer in the universe@103: * documentation and/or other materials provided with the distribution. universe@103: * universe@103: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@103: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@103: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@103: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@103: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@103: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@103: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@103: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@103: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@103: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@103: * POSSIBILITY OF SUCH DAMAGE. universe@26: */ universe@26: universe@251: #include "ucx/test.h" universe@26: universe@26: UcxTestSuite* ucx_test_suite_new() { universe@26: UcxTestSuite* suite = (UcxTestSuite*) malloc(sizeof(UcxTestSuite)); universe@26: if (suite != NULL) { universe@26: suite->success = 0; universe@26: suite->failure = 0; universe@26: suite->tests = NULL; universe@26: } universe@134: universe@26: return suite; universe@26: } universe@26: universe@26: void ucx_test_suite_free(UcxTestSuite* suite) { universe@83: UcxTestList *l = suite->tests; universe@83: while (l != NULL) { universe@83: UcxTestList *e = l; universe@83: l = l->next; universe@83: free(e); universe@83: } universe@26: free(suite); universe@26: } universe@26: universe@83: int ucx_test_register(UcxTestSuite* suite, UcxTest test) { universe@83: if (suite->tests) { universe@97: UcxTestList *newelem = (UcxTestList*) malloc(sizeof(UcxTestList)); universe@97: if (newelem) { universe@97: newelem->test = test; universe@97: newelem->next = NULL; universe@97: universe@97: UcxTestList *last = suite->tests; universe@97: while (last->next) { universe@97: last = last->next; universe@97: } universe@97: last->next = newelem; universe@83: universe@83: return EXIT_SUCCESS; universe@83: } else { universe@83: return EXIT_FAILURE; universe@83: } universe@83: } else { universe@83: suite->tests = (UcxTestList*) malloc(sizeof(UcxTestList)); universe@83: if (suite->tests) { universe@83: suite->tests->test = test; universe@83: suite->tests->next = NULL; universe@83: universe@83: return EXIT_SUCCESS; universe@83: } else { universe@83: return EXIT_FAILURE; universe@83: } universe@83: } universe@26: } universe@26: universe@26: void ucx_test_run(UcxTestSuite* suite, FILE* output) { universe@26: suite->success = 0; universe@26: suite->failure = 0; universe@121: for (UcxTestList* elem = suite->tests ; elem ; elem = elem->next) { universe@121: elem->test(suite, output); universe@26: } universe@26: fwrite("\nAll test completed.\n", 1, 21, output); universe@247: fprintf(output, " Total: %u\n Success: %u\n Failure: %u\n", universe@26: suite->success+suite->failure, suite->success, suite->failure); universe@26: }