test_shuffle_roundtrip_generic.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*********************************************************************
  2. Blosc - Blocked Shuffling and Compression Library
  3. Roundtrip 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. /** Roundtrip tests for the generic shuffle/unshuffle. */
  12. static int test_shuffle_roundtrip_generic(size_t type_size, size_t num_elements,
  13. size_t buffer_alignment)
  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* shuffled = blosc_test_malloc(buffer_alignment, buffer_size);
  20. void* unshuffled = 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. /* Generic shuffle, then generic unshuffle. */
  24. shuffle_generic(type_size, buffer_size, original, shuffled);
  25. unshuffle_generic(type_size, buffer_size, shuffled, unshuffled);
  26. /* The round-tripped data matches the original data when the
  27. result of memcmp is 0. */
  28. exit_code = memcmp(original, unshuffled, buffer_size) ?
  29. EXIT_FAILURE : EXIT_SUCCESS;
  30. /* Free allocated memory. */
  31. blosc_test_free(original);
  32. blosc_test_free(shuffled);
  33. blosc_test_free(unshuffled);
  34. return exit_code;
  35. }
  36. /** Required number of arguments to this test, including the executable name. */
  37. #define TEST_ARG_COUNT 4
  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. /* argv[1]: sizeof(element type)
  44. argv[2]: number of elements
  45. argv[3]: buffer alignment
  46. */
  47. /* Verify the correct number of command-line args have been specified. */
  48. if (TEST_ARG_COUNT != argc)
  49. {
  50. blosc_test_print_bad_argcount_msg(TEST_ARG_COUNT, argc);
  51. return EXIT_FAILURE;
  52. }
  53. /* Parse arguments */
  54. if (!blosc_test_parse_uint32_t(argv[1], &type_size) || (type_size < 1))
  55. {
  56. blosc_test_print_bad_arg_msg(1);
  57. return EXIT_FAILURE;
  58. }
  59. if (!blosc_test_parse_uint32_t(argv[2], &num_elements) || (num_elements < 1))
  60. {
  61. blosc_test_print_bad_arg_msg(2);
  62. return EXIT_FAILURE;
  63. }
  64. if (!blosc_test_parse_uint32_t(argv[3], &buffer_align_size)
  65. || (buffer_align_size & (buffer_align_size - 1))
  66. || (buffer_align_size < sizeof(void*)))
  67. {
  68. blosc_test_print_bad_arg_msg(3);
  69. return EXIT_FAILURE;
  70. }
  71. /* Run the test. */
  72. return test_shuffle_roundtrip_generic(type_size, num_elements, buffer_align_size);
  73. }