test_getitem.c 3.7 KB

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