1
0
Fork 0
mirror of https://git.tukaani.org/xz.git synced 2024-04-04 12:36:23 +02:00

Tests: tuktest.h: Rename file_from_* and use tuktest_malloc there.

This commit is contained in:
Lasse Collin 2022-06-16 13:29:59 +03:00
parent d8b63a0ad6
commit b339892668
2 changed files with 25 additions and 31 deletions

View file

@ -79,7 +79,7 @@ test_empty_block(void)
{ {
// An empty file with one Block using PowerPC BCJ and LZMA2. // An empty file with one Block using PowerPC BCJ and LZMA2.
size_t in_size; size_t in_size;
uint8_t *empty_bcj_lzma2 = file_from_srcdir( uint8_t *empty_bcj_lzma2 = tuktest_file_from_srcdir(
"files/good-1-empty-bcj-lzma2.xz", &in_size); "files/good-1-empty-bcj-lzma2.xz", &in_size);
// Decompress without giving any output space. // Decompress without giving any output space.
@ -92,8 +92,6 @@ test_empty_block(void)
LZMA_OK); LZMA_OK);
assert_uint_eq(in_pos, in_size); assert_uint_eq(in_pos, in_size);
assert_uint_eq(out_pos, 0); assert_uint_eq(out_pos, 0);
free(empty_bcj_lzma2);
} }

View file

@ -2,7 +2,7 @@
// //
/// \file tuktest.h /// \file tuktest.h
/// \brief Helper macros for writing simple test programs /// \brief Helper macros for writing simple test programs
/// \version 2022-06-02 /// \version 2022-06-16
/// ///
/// Some inspiration was taken from STest by Keith Nicholas. /// Some inspiration was taken from STest by Keith Nicholas.
/// ///
@ -88,12 +88,17 @@
/// (The assert_CONDITION() macros depend on setup code in tuktest_run() /// (The assert_CONDITION() macros depend on setup code in tuktest_run()
/// and other use results in undefined behavior.) /// and other use results in undefined behavior.)
/// ///
/// - The tuktest_* functions and macros macros must not be used in /// - tuktest_start(), tuktest_early_skip, tuktest_error(), tuktest_run(),
/// the tests called via tuktest_run()! /// and tuktest_end() must not be used in the tests called via
/// tuktest_run()! (tuktest_end() is called more freely internally
/// by this file but such use isn't part of the API.)
/// ///
/// - file_from_* functions and macros may be used anywhere after /// - tuktest_malloc(), tuktest_free(), tuktest_file_from_srcdir(), and
/// tuktest_file_from_builddir() can be used everywhere after
/// tuktest_start() has been called. /// tuktest_start() has been called.
/// ///
/// - Everything else is for internal use only.
///
/// Footnotes: /// Footnotes:
/// ///
/// [1] As of 2022-06-02: /// [1] As of 2022-06-02:
@ -605,7 +610,7 @@ tuktest_run_test(void (*testfunc)(void), const char *testfunc_str)
} }
// Maximum allowed file size in file_from_* macros and functions. // Maximum allowed file size in tuktest_file_from_* macros and functions.
#ifndef TUKTEST_FILE_SIZE_MAX #ifndef TUKTEST_FILE_SIZE_MAX
# define TUKTEST_FILE_SIZE_MAX (64L << 20) # define TUKTEST_FILE_SIZE_MAX (64L << 20)
#endif #endif
@ -613,7 +618,7 @@ tuktest_run_test(void (*testfunc)(void), const char *testfunc_str)
/// Allocates memory and reads the specified file into a buffer. /// Allocates memory and reads the specified file into a buffer.
/// If the environment variable srcdir is set, it will be prefixed /// If the environment variable srcdir is set, it will be prefixed
/// to the filename. Otherwise the filename is used as is (and so /// to the filename. Otherwise the filename is used as is (and so
/// the behavior is identical to file_from_builddir() below). /// the behavior is identical to tuktest_file_from_builddir() below).
/// ///
/// On success the a pointer to malloc'ed memory is returned. /// On success the a pointer to malloc'ed memory is returned.
/// The size of the allocation and the file is stored in *size. /// The size of the allocation and the file is stored in *size.
@ -627,16 +632,17 @@ tuktest_run_test(void (*testfunc)(void), const char *testfunc_str)
/// This function can be called either from outside the tests (like in main()) /// This function can be called either from outside the tests (like in main())
/// or from tests run via tuktest_run(). Remember to free() the memory to /// or from tests run via tuktest_run(). Remember to free() the memory to
/// keep Valgrind happy. /// keep Valgrind happy.
#define file_from_srcdir(filename, sizeptr) \ #define tuktest_file_from_srcdir(filename, sizeptr) \
file_from_x(getenv("srcdir"), filename, sizeptr, __FILE__, __LINE__) tuktest_file_from_x(getenv("srcdir"), filename, sizeptr, \
__FILE__, __LINE__)
/// Like file_from_srcdir except this reads from the current directory. /// Like tuktest_file_from_srcdir except this reads from the current directory.
#define file_from_builddir(filename, sizeptr) \ #define tuktest_file_from_builddir(filename, sizeptr) \
file_from_x(NULL, filename, sizeptr, __FILE__, __LINE__) tuktest_file_from_x(NULL, filename, sizeptr, __FILE__, __LINE__)
// Internal helper for the macros above. // Internal helper for the macros above.
static void * static void *
file_from_x(const char *prefix, const char *filename, size_t *size, tuktest_file_from_x(const char *prefix, const char *filename, size_t *size,
const char *prog_filename, unsigned prog_line) const char *prog_filename, unsigned prog_line)
{ {
// If needed: buffer for holding prefix + '/' + filename + '\0'. // If needed: buffer for holding prefix + '/' + filename + '\0'.
@ -674,11 +680,8 @@ file_from_x(const char *prefix, const char *filename, size_t *size,
const size_t alloc_name_size const size_t alloc_name_size
= prefix_len + 1 + filename_len + 1; = prefix_len + 1 + filename_len + 1;
alloc_name = malloc(alloc_name_size); alloc_name = tuktest_malloc_impl(alloc_name_size,
if (alloc_name == NULL) { prog_filename, prog_line);
error_msg = "Memory allocation failed (alloc_name)";
goto error;
}
memcpy(alloc_name, prefix, prefix_len); memcpy(alloc_name, prefix, prefix_len);
alloc_name[prefix_len] = '/'; alloc_name[prefix_len] = '/';
@ -726,11 +729,7 @@ file_from_x(const char *prefix, const char *filename, size_t *size,
*size = (size_t)end; *size = (size_t)end;
rewind(f); rewind(f);
buf = malloc(*size); buf = tuktest_malloc_impl(*size, prog_filename, prog_line);
if (buf == NULL) {
error_msg = "Memory allocation failed (buf)";
goto error;
}
const size_t amount = fread(buf, 1, *size, f); const size_t amount = fread(buf, 1, *size, f);
if (ferror(f)) { if (ferror(f)) {
@ -750,7 +749,7 @@ file_from_x(const char *prefix, const char *filename, size_t *size,
goto error; goto error;
} }
free(alloc_name); tuktest_free(alloc_name);
return buf; return buf;
error: error:
@ -760,12 +759,9 @@ error:
tuktest_print_result_prefix(TUKTEST_ERROR, prog_filename, prog_line); tuktest_print_result_prefix(TUKTEST_ERROR, prog_filename, prog_line);
if (filename == NULL) if (filename == NULL)
printf("file_from_x: %s\n", error_msg); printf("tuktest_file_from_x: %s\n", error_msg);
else else
printf("file_from_x: %s: %s\n", filename, error_msg); printf("tuktest_file_from_x: %s: %s\n", filename, error_msg);
free(buf);
free(alloc_name);
++tuktest_stats[TUKTEST_ERROR]; ++tuktest_stats[TUKTEST_ERROR];
exit(tuktest_end()); exit(tuktest_end());