test_compress_roundtrip.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*********************************************************************
  2. Blosc - Blocked Shuffling and Compression Library
  3. Roundtrip compression/decompression tests.
  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. #include "test_common.h"
  9. #include "../blosc/shuffle.h"
  10. #include "../blosc/shuffle-generic.h"
  11. /** Perform a compress + decompress round trip. */
  12. static int test_compress_roundtrip(size_t type_size, size_t num_elements,
  13. size_t buffer_alignment, int compression_level, int do_shuffle)
  14. {
  15. size_t buffer_size = type_size * num_elements;
  16. int exit_code;
  17. /* Allocate memory for the test. */
  18. void* original = blosc_test_malloc(buffer_alignment, buffer_size);
  19. void* intermediate = blosc_test_malloc(buffer_alignment, buffer_size + BLOSC_MAX_OVERHEAD);
  20. void* result = blosc_test_malloc(buffer_alignment, buffer_size);
  21. /* Fill the input data buffer with random values. */
  22. blosc_test_fill_random(original, buffer_size);
  23. /* Compress the input data and store it in an intermediate buffer.
  24. Decompress the data from the intermediate buffer into a result buffer. */
  25. blosc_compress(compression_level, do_shuffle, type_size, buffer_size,
  26. original, intermediate, buffer_size + BLOSC_MAX_OVERHEAD);
  27. blosc_decompress(intermediate, result, buffer_size);
  28. /* The round-tripped data matches the original data when the
  29. result of memcmp is 0. */
  30. exit_code = memcmp(original, result, buffer_size) ?
  31. EXIT_FAILURE : EXIT_SUCCESS;
  32. /* Free allocated memory. */
  33. blosc_test_free(original);
  34. blosc_test_free(intermediate);
  35. blosc_test_free(result);
  36. return exit_code;
  37. }
  38. /** Required number of arguments to this test, including the executable name. */
  39. #define TEST_ARG_COUNT 7
  40. int main(int argc, char **argv)
  41. {
  42. int shuffle_enabled;
  43. uint32_t blosc_thread_count;
  44. uint32_t type_size;
  45. uint32_t num_elements;
  46. uint32_t buffer_align_size;
  47. uint32_t compression_level;
  48. int result;
  49. /* argv[1]: sizeof(element type)
  50. argv[2]: number of elements
  51. argv[3]: buffer alignment
  52. argv[4]: compression level
  53. argv[5]: shuffle enabled
  54. argv[6]: thread count
  55. */
  56. /* Verify the correct number of command-line args have been specified. */
  57. if (TEST_ARG_COUNT != argc)
  58. {
  59. blosc_test_print_bad_argcount_msg(TEST_ARG_COUNT, argc);
  60. return EXIT_FAILURE;
  61. }
  62. /* Parse arguments */
  63. if (!blosc_test_parse_uint32_t(argv[1], &type_size) || (type_size < 1))
  64. {
  65. blosc_test_print_bad_arg_msg(1);
  66. return EXIT_FAILURE;
  67. }
  68. if (!blosc_test_parse_uint32_t(argv[2], &num_elements) || (num_elements < 1))
  69. {
  70. blosc_test_print_bad_arg_msg(2);
  71. return EXIT_FAILURE;
  72. }
  73. if (!blosc_test_parse_uint32_t(argv[3], &buffer_align_size)
  74. || (buffer_align_size & (buffer_align_size - 1))
  75. || (buffer_align_size < sizeof(void*)))
  76. {
  77. blosc_test_print_bad_arg_msg(3);
  78. return EXIT_FAILURE;
  79. }
  80. if (!blosc_test_parse_uint32_t(argv[4], &compression_level) || (compression_level > 9))
  81. {
  82. blosc_test_print_bad_arg_msg(4);
  83. return EXIT_FAILURE;
  84. }
  85. {
  86. uint32_t shuffle_enabled_raw;
  87. if (!blosc_test_parse_uint32_t(argv[5], &shuffle_enabled_raw) || (shuffle_enabled_raw > 1))
  88. {
  89. blosc_test_print_bad_arg_msg(5);
  90. return EXIT_FAILURE;
  91. }
  92. shuffle_enabled = shuffle_enabled_raw == 0 ? 0 : 1;
  93. }
  94. if (!blosc_test_parse_uint32_t(argv[6], &blosc_thread_count) || (blosc_thread_count < 1))
  95. {
  96. blosc_test_print_bad_arg_msg(6);
  97. return EXIT_FAILURE;
  98. }
  99. /* Initialize blosc before running tests. */
  100. blosc_init();
  101. blosc_set_nthreads(blosc_thread_count);
  102. /* Run the test. */
  103. result = test_compress_roundtrip(type_size, num_elements, buffer_align_size,
  104. compression_level, shuffle_enabled);
  105. /* Cleanup blosc resources. */
  106. blosc_destroy();
  107. return result;
  108. }