From 22eddf10710b7d1ecb8527a5e9f6dc3810b7c55c Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Sat, 4 Jan 2020 10:35:02 +0100 Subject: [PATCH] C64 Test files written in the same location as other test files --- src/tests/testformats.c | 18 +++++++----------- src/tests/testmain.c | 36 ++++++++++++++++++++++++++++++++++-- src/tests/tests.h | 5 +++++ 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/src/tests/testformats.c b/src/tests/testformats.c index 4d7b8cfb..77fa5a4b 100644 --- a/src/tests/testformats.c +++ b/src/tests/testformats.c @@ -34,6 +34,7 @@ #include "../fileformats.h" #include "../gfx2log.h" #include "../gfx2mem.h" +#include "tests.h" // Load_IFF/Save_IFF does for both LBM and PBM (and Load_IFF also loads ACBM format) #define Load_ACBM Load_IFF @@ -232,7 +233,6 @@ int Test_Save(void) { T_IO_Context context; char path[256]; - char tmpdir[40]; int i; int ok = 0; T_GFX2_Surface * testpic256 = NULL; @@ -277,13 +277,6 @@ int Test_Save(void) context.Surface = NULL; memcpy(testpiccpco->palette, context.Palette, sizeof(T_Palette)); - snprintf(tmpdir, sizeof(tmpdir), "/tmp/grafx2-test.XXXXXX"); - if (mkdtemp(tmpdir) == NULL) - { - perror("mkdtemp"); - goto ret; - } - printf("temp dir : %s\n", tmpdir); ok = 1; for (i = 0; ok && formats[i].name != NULL; i++) { @@ -388,8 +381,6 @@ int Test_Save(void) } } } - if (rmdir(tmpdir) < 0) - perror("rmdir"); ret: if (testpic16) Free_GFX2_Surface(testpic16); @@ -458,7 +449,7 @@ int Test_C64_Formats(void) ref = testpichires; context.Ratio = PIXEL_SIMPLE; } - snprintf(path, sizeof(path), "/tmp/%s-%d.%s", "test", j, formats[i].name); + snprintf(path, sizeof(path), "%s/%s-%d.%s", tmpdir, "test", j, formats[i].name); context_set_file_path(&context, path); // save the reference picture @@ -513,6 +504,11 @@ int Test_C64_Formats(void) GFX2_Log(GFX2_ERROR, "Save_%s/Load_%s: Pixels mismatch\n", formats[i].name, formats[i].name); ok = 0; } + else + { + if (unlink(path) < 0) + perror("unlink"); + } Free_GFX2_Surface(context.Surface); context.Surface = NULL; } diff --git a/src/tests/testmain.c b/src/tests/testmain.c index ae89bdec..db67ca3c 100644 --- a/src/tests/testmain.c +++ b/src/tests/testmain.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include "../struct.h" #include "../global.h" @@ -68,6 +69,8 @@ byte Windows_open; dword Key; +char tmpdir[40]; + static const struct { int (*test_func)(void); const char * test_name; @@ -81,7 +84,7 @@ static const struct { /** * Initializations for test program */ -void init(void) +int init(void) { srandom(time(NULL)); #ifdef ENABLE_FILENAMES_ICONV @@ -96,6 +99,29 @@ void init(void) cd_utf16_inv = iconv_open(FROMCODE, "UTF-16LE"); // From UTF16 to UTF8 #endif #endif /* ENABLE_FILENAMES_ICONV */ + snprintf(tmpdir, sizeof(tmpdir), "/tmp/grafx2-test.XXXXXX"); + if (mkdtemp(tmpdir) == NULL) + { + perror("mkdtemp"); + return -1; + } + printf("temp dir : %s\n", tmpdir); + return 0; +} + +/** + * Releases resources + */ +void finish(void) +{ +#ifdef ENABLE_FILENAMES_ICONV + iconv_close(cd); + iconv_close(cd_inv); + iconv_close(cd_utf16); + iconv_close(cd_utf16_inv); +#endif /* ENABLE_FILENAMES_ICONV */ + if (rmdir(tmpdir) < 0) + fprintf(stderr, "Failed to rmdir(\"%s\"): %s\n", tmpdir, strerror(errno)); } #define ESC_GREEN "\033[32m" @@ -113,7 +139,11 @@ int main(int argc, char * * argv) (void)argv; GFX2_verbosity_level = GFX2_DEBUG; - init(); + if (init() < 0) + { + fprintf(stderr, "Failed to init.\n"); + return 1; + } for (i = 0; tests[i].test_func != 0; i++) { @@ -127,6 +157,8 @@ int main(int argc, char * * argv) } } + finish(); + if (fail == 0) { printf(ESC_GREEN "All tests succesfull" ESC_RESET "\n"); diff --git a/src/tests/tests.h b/src/tests/tests.h index 79ff97c9..edf4174a 100644 --- a/src/tests/tests.h +++ b/src/tests/tests.h @@ -26,4 +26,9 @@ #include "testlist.h" #undef TEST +/** + * path to directory where tests can write files + */ +extern char tmpdir[]; + #endif