| 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 * POSSIBILITY OF SUCH DAMAGE. |
26 * POSSIBILITY OF SUCH DAMAGE. |
| 27 */ |
27 */ |
| 28 |
28 |
| |
29 #include "util_allocator.h" |
| 29 #include "cx/common.h" |
30 #include "cx/common.h" |
| 30 #include "cx/test.h" |
31 #include "cx/test.h" |
| 31 |
32 |
| 32 CxTestSuite *cx_test_suite_testing_allocator(void); |
33 CxTestSuite *cx_test_suite_testing_allocator(void); |
| 33 CxTestSuite *cx_test_suite_szmul(void); |
34 CxTestSuite *cx_test_suite_szmul(void); |
| 50 CxTestSuite *cx_test_suite_properties(void); |
51 CxTestSuite *cx_test_suite_properties(void); |
| 51 CxTestSuite *cx_test_suite_json(void); |
52 CxTestSuite *cx_test_suite_json(void); |
| 52 CxTestSuite *cx_test_suite_printf(void); |
53 CxTestSuite *cx_test_suite_printf(void); |
| 53 CxTestSuite *cx_test_suite_mempool(void); |
54 CxTestSuite *cx_test_suite_mempool(void); |
| 54 |
55 |
| 55 #define run_tests(suite) cx_test_run_stdout(suite); success += (suite)->success; failure += (suite)->failure |
56 #define run_tests(suite) cx_test_run_stdout(suite); success += (suite)->success; failure += (suite)->failure; \ |
| |
57 if (!cx_testing_allocator_verify(&testing_allocator)) break; |
| 56 #define execute_test_suites(...) unsigned success = 0, failure = 0; CxTestSuite* test_suites[] = {__VA_ARGS__}; \ |
58 #define execute_test_suites(...) unsigned success = 0, failure = 0; CxTestSuite* test_suites[] = {__VA_ARGS__}; \ |
| 57 for (size_t i = 0; i < cx_nmemb(test_suites) ; i++) {run_tests(test_suites[i]);} (void)0 |
59 for (size_t i = 0; i < cx_nmemb(test_suites) ; i++) {run_tests(test_suites[i]);} (void)0 |
| 58 #define free_test_suites for (size_t i = 0 ; i < cx_nmemb(test_suites) ; i++) {cx_test_suite_free(test_suites[i]);} (void)0 |
60 #define free_test_suites for (size_t i = 0 ; i < cx_nmemb(test_suites) ; i++) {cx_test_suite_free(test_suites[i]);} (void)0 |
| 59 |
61 |
| |
62 static int verify_testing_allocator() { |
| |
63 printf("Verify Testing Allocator\n"); |
| |
64 CxTestSuite *suite_ta = cx_test_suite_testing_allocator(); |
| |
65 int result = suite_ta->failure > 0; |
| |
66 cx_test_suite_free(suite_ta); |
| |
67 return result; |
| |
68 } |
| |
69 |
| 60 int main(void) { |
70 int main(void) { |
| |
71 if (verify_testing_allocator()) { |
| |
72 fprintf(stderr, "Testing Allocator is not working - cannot perform tests!\n"); |
| |
73 return EXIT_FAILURE; |
| |
74 } |
| |
75 |
| |
76 // Replace the default allocator with the testing allocator |
| |
77 CxTestingAllocator testing_allocator; |
| |
78 cx_testing_allocator_init(&testing_allocator); |
| |
79 cxDefaultAllocator = &testing_allocator.base; |
| |
80 |
| |
81 // Run tests |
| 61 printf("UCX Tests\n---------\n"); |
82 printf("UCX Tests\n---------\n"); |
| 62 |
83 |
| 63 execute_test_suites( |
84 execute_test_suites( |
| 64 cx_test_suite_testing_allocator(), |
|
| 65 cx_test_suite_szmul(), |
85 cx_test_suite_szmul(), |
| 66 cx_test_suite_allocator(), |
86 cx_test_suite_allocator(), |
| 67 cx_test_suite_streams(), |
87 cx_test_suite_streams(), |
| 68 cx_test_suite_compare(), |
88 cx_test_suite_compare(), |
| 69 cx_test_suite_string(), |
89 cx_test_suite_string(), |
| 87 printf("=== OVERALL RESULT ===\n"); |
107 printf("=== OVERALL RESULT ===\n"); |
| 88 printf(" Total: %u\n Success: %u\n Failure: %u\n", |
108 printf(" Total: %u\n Success: %u\n Failure: %u\n", |
| 89 success + failure, success, failure); |
109 success + failure, success, failure); |
| 90 free_test_suites; |
110 free_test_suites; |
| 91 |
111 |
| |
112 if (cx_testing_allocator_verify(&testing_allocator)) { |
| |
113 printf("\nAllocations verified successfully.\n"); |
| |
114 } else { |
| |
115 printf("\nVerifying allocations failed!\nSome cxMalloc() might not match a cxFree().\n" |
| |
116 "Aborting test execution.\nError must be in a function tested by the last executed test suite.\n" |
| |
117 "Total allocations: %u\n" |
| |
118 "Total frees: %u\n", testing_allocator.alloc_total, testing_allocator.free_total); |
| |
119 failure++; |
| |
120 } |
| |
121 cx_testing_allocator_destroy(&testing_allocator); |
| |
122 |
| 92 return failure > 0 ? 1 : 0; |
123 return failure > 0 ? 1 : 0; |
| 93 } |
124 } |
| 94 |
125 |