From 88b110479259277a10a4a049394c79e23635d2b2 Mon Sep 17 00:00:00 2001 From: Rhodri James Date: Mon, 31 Oct 2022 14:07:54 +0000 Subject: [PATCH] Move the first tranche of misc tests out of runtests.c --- expat/tests/misc_tests.c | 62 ++++++++++++++++++++++++++++++++++++++++ expat/tests/runtests.c | 61 --------------------------------------- 2 files changed, 62 insertions(+), 61 deletions(-) diff --git a/expat/tests/misc_tests.c b/expat/tests/misc_tests.c index aee1fa5a..1c282c10 100644 --- a/expat/tests/misc_tests.c +++ b/expat/tests/misc_tests.c @@ -46,6 +46,63 @@ #include "common.h" #include "misc_tests.h" +/* Test that a failure to allocate the parser structure fails gracefully */ +START_TEST(test_misc_alloc_create_parser) { + XML_Memory_Handling_Suite memsuite = {duff_allocator, realloc, free}; + unsigned int i; + const unsigned int max_alloc_count = 10; + + /* Something this simple shouldn't need more than 10 allocations */ + for (i = 0; i < max_alloc_count; i++) { + g_allocation_count = i; + g_parser = XML_ParserCreate_MM(NULL, &memsuite, NULL); + if (g_parser != NULL) + break; + } + if (i == 0) + fail("Parser unexpectedly ignored failing allocator"); + else if (i == max_alloc_count) + fail("Parser not created with max allocation count"); +} +END_TEST + +/* Test memory allocation failures for a parser with an encoding */ +START_TEST(test_misc_alloc_create_parser_with_encoding) { + XML_Memory_Handling_Suite memsuite = {duff_allocator, realloc, free}; + unsigned int i; + const unsigned int max_alloc_count = 10; + + /* Try several levels of allocation */ + for (i = 0; i < max_alloc_count; i++) { + g_allocation_count = i; + g_parser = XML_ParserCreate_MM(XCS("us-ascii"), &memsuite, NULL); + if (g_parser != NULL) + break; + } + if (i == 0) + fail("Parser ignored failing allocator"); + else if (i == max_alloc_count) + fail("Parser not created with max allocation count"); +} +END_TEST + +/* Test that freeing a NULL parser doesn't cause an explosion. + * (Not actually tested anywhere else) + */ +START_TEST(test_misc_null_parser) { + XML_ParserFree(NULL); +} +END_TEST + +/* Test that XML_ErrorString rejects out-of-range codes */ +START_TEST(test_misc_error_string) { + if (XML_ErrorString((enum XML_Error) - 1) != NULL) + fail("Negative error code not rejected"); + if (XML_ErrorString((enum XML_Error)100) != NULL) + fail("Large error code not rejected"); +} +END_TEST + TCase * make_miscellaneous_test_case(Suite *s) { TCase *tc_misc = tcase_create("miscellaneous tests"); @@ -53,5 +110,10 @@ make_miscellaneous_test_case(Suite *s) { suite_add_tcase(s, tc_misc); tcase_add_checked_fixture(tc_misc, NULL, basic_teardown); + tcase_add_test(tc_misc, test_misc_alloc_create_parser); + tcase_add_test(tc_misc, test_misc_alloc_create_parser_with_encoding); + tcase_add_test(tc_misc, test_misc_null_parser); + tcase_add_test(tc_misc, test_misc_error_string); + return tc_misc; /* TEMPORARY; this will become a void function */ } diff --git a/expat/tests/runtests.c b/expat/tests/runtests.c index 659c0533..a576ade0 100644 --- a/expat/tests/runtests.c +++ b/expat/tests/runtests.c @@ -76,63 +76,6 @@ XML_Parser g_parser = NULL; -/* Test that a failure to allocate the parser structure fails gracefully */ -START_TEST(test_misc_alloc_create_parser) { - XML_Memory_Handling_Suite memsuite = {duff_allocator, realloc, free}; - unsigned int i; - const unsigned int max_alloc_count = 10; - - /* Something this simple shouldn't need more than 10 allocations */ - for (i = 0; i < max_alloc_count; i++) { - g_allocation_count = i; - g_parser = XML_ParserCreate_MM(NULL, &memsuite, NULL); - if (g_parser != NULL) - break; - } - if (i == 0) - fail("Parser unexpectedly ignored failing allocator"); - else if (i == max_alloc_count) - fail("Parser not created with max allocation count"); -} -END_TEST - -/* Test memory allocation failures for a parser with an encoding */ -START_TEST(test_misc_alloc_create_parser_with_encoding) { - XML_Memory_Handling_Suite memsuite = {duff_allocator, realloc, free}; - unsigned int i; - const unsigned int max_alloc_count = 10; - - /* Try several levels of allocation */ - for (i = 0; i < max_alloc_count; i++) { - g_allocation_count = i; - g_parser = XML_ParserCreate_MM(XCS("us-ascii"), &memsuite, NULL); - if (g_parser != NULL) - break; - } - if (i == 0) - fail("Parser ignored failing allocator"); - else if (i == max_alloc_count) - fail("Parser not created with max allocation count"); -} -END_TEST - -/* Test that freeing a NULL parser doesn't cause an explosion. - * (Not actually tested anywhere else) - */ -START_TEST(test_misc_null_parser) { - XML_ParserFree(NULL); -} -END_TEST - -/* Test that XML_ErrorString rejects out-of-range codes */ -START_TEST(test_misc_error_string) { - if (XML_ErrorString((enum XML_Error) - 1) != NULL) - fail("Negative error code not rejected"); - if (XML_ErrorString((enum XML_Error)100) != NULL) - fail("Large error code not rejected"); -} -END_TEST - /* Test the version information is consistent */ /* Since we are working in XML_LChars (potentially 16-bits), we @@ -4537,10 +4480,6 @@ make_suite(void) { TCase *tc_accounting = tcase_create("accounting tests"); #endif - tcase_add_test(tc_misc, test_misc_alloc_create_parser); - tcase_add_test(tc_misc, test_misc_alloc_create_parser_with_encoding); - tcase_add_test(tc_misc, test_misc_null_parser); - tcase_add_test(tc_misc, test_misc_error_string); tcase_add_test(tc_misc, test_misc_version); tcase_add_test(tc_misc, test_misc_features); tcase_add_test(tc_misc, test_misc_attribute_leak);