test_common.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*********************************************************************
  2. Blosc - Blocked Shuffling and Compression Library
  3. Unit tests for basic features in Blosc.
  4. Creation date: 2010-06-07
  5. Author: Francesc Alted <francesc@blosc.org>
  6. See LICENSES/BLOSC.txt for details about copyright and rights to use.
  7. **********************************************************************/
  8. #ifndef BLOSC_TEST_COMMON_H
  9. #define BLOSC_TEST_COMMON_H
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #include <fcntl.h>
  16. #if defined(_WIN32) && !defined(__MINGW32__)
  17. #include <time.h>
  18. #include "win32/stdint-windows.h"
  19. #else
  20. #include <stdint.h>
  21. #include <unistd.h>
  22. #include <sys/time.h>
  23. #endif
  24. #include <math.h>
  25. #include "../blosc/blosc.h"
  26. #if defined(_WIN32) && !defined(__MINGW32__)
  27. /* MSVC does not have setenv */
  28. #define setenv(name, value, overwrite) do {_putenv_s(name, value);} while(0)
  29. #endif
  30. /* This is MinUnit in action (http://www.jera.com/techinfo/jtns/jtn002.html) */
  31. #define mu_assert(message, test) do { if (!(test)) return message; } while (0)
  32. #define mu_run_test(test) do \
  33. { char *message = test(); tests_run++; \
  34. if (message) { printf("%c", 'F'); return message;} \
  35. else printf("%c", '.'); } while (0)
  36. extern int tests_run;
  37. #define KB 1024
  38. #define MB (1024*KB)
  39. #define GB (1024*MB)
  40. /*
  41. Memory functions.
  42. */
  43. /** Allocates a block of memory with the specified size and alignment.
  44. The allocated memory is 'cleaned' before returning to avoid
  45. accidental re-use of data within or between tests.
  46. */
  47. static void* blosc_test_malloc(const size_t alignment, const size_t size)
  48. {
  49. const int32_t clean_value = 0x99;
  50. void *block = NULL;
  51. int32_t res = 0;
  52. #if _ISOC11_SOURCE
  53. /* C11 aligned allocation. 'size' must be a multiple of the alignment. */
  54. block = aligned_alloc(alignment, size);
  55. #elif defined(_WIN32)
  56. /* A (void *) cast needed for avoiding a warning with MINGW :-/ */
  57. block = (void *)_aligned_malloc(size, alignment);
  58. #elif _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600
  59. /* Platform does have an implementation of posix_memalign */
  60. res = posix_memalign(&block, alignment, size);
  61. #elif defined(__APPLE__)
  62. /* Mac OS X guarantees 16-byte alignment in small allocs */
  63. block = malloc(size);
  64. #else
  65. #error Cannot determine how to allocate aligned memory on the target platform.
  66. #endif
  67. if (block == NULL || res != 0) {
  68. fprintf(stderr, "Error allocating memory!");
  69. return NULL;
  70. }
  71. /* Clean the allocated memory before returning. */
  72. memset(block, clean_value, size);
  73. return block;
  74. }
  75. /** Frees memory allocated by blosc_test_malloc. */
  76. static void blosc_test_free(void* ptr)
  77. {
  78. #if defined(_WIN32)
  79. _aligned_free(ptr);
  80. #else
  81. free(ptr);
  82. #endif /* _WIN32 */
  83. }
  84. /** Fills a buffer with random values. */
  85. static void blosc_test_fill_random(void* const ptr, const size_t size)
  86. {
  87. size_t k;
  88. uint8_t* const byte_ptr = (uint8_t*)ptr;
  89. for (k = 0; k < size; k++) {
  90. byte_ptr[k] = rand();
  91. }
  92. }
  93. /*
  94. Argument parsing.
  95. */
  96. /** Parse a `int32_t` value from a string, checking for overflow. */
  97. static int blosc_test_parse_uint32_t(const char* const str, uint32_t* value)
  98. {
  99. char* str_end;
  100. int32_t signed_value = strtol(str, &str_end, 10);
  101. if (signed_value < 0 || *str_end)
  102. {
  103. return 0;
  104. }
  105. else
  106. {
  107. *value = (uint32_t)signed_value;
  108. return 1;
  109. }
  110. }
  111. /*
  112. Error message functions.
  113. */
  114. /** Print an error message when a test program has been invoked
  115. with an invalid number of arguments. */
  116. static void blosc_test_print_bad_argcount_msg(
  117. const int32_t num_expected_args, const int32_t num_actual_args)
  118. {
  119. fprintf(stderr, "Invalid number of arguments specified.\nExpected %d arguments but was given %d.",
  120. num_expected_args, num_actual_args);
  121. }
  122. /** Print an error message when a test program has been invoked
  123. with an invalid argument value. */
  124. static void blosc_test_print_bad_arg_msg(const int32_t arg_index)
  125. {
  126. fprintf(stderr, "Invalid value specified for argument at index %d.\n", arg_index);
  127. }
  128. #endif /* !defined(BLOSC_TEST_COMMON_H) */