test_nthreads.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*********************************************************************
  2. Blosc - Blocked Shuffling and Compression Library
  3. Unit tests for BLOSC_NTHREADS environment variable in Blosc.
  4. Creation date: 2016-04-25
  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. int tests_run = 0;
  10. /* Global vars */
  11. void *src, *srccpy, *dest, *dest2;
  12. int nbytes, cbytes;
  13. int clevel = 1;
  14. int doshuffle = 1;
  15. size_t typesize = 4;
  16. size_t size = 4 * 1000 * 1000; /* must be divisible by 4 */
  17. /* Check just compressing */
  18. static const char *test_compress(void) {
  19. int nthreads;
  20. /* Before any blosc_compress() or blosc_decompress() the number of
  21. threads must be 1 */
  22. nthreads = blosc_get_nthreads();
  23. mu_assert("ERROR: get_nthreads (compress, before) incorrect", nthreads == 1);
  24. /* Get a compressed buffer */
  25. cbytes = blosc_compress(clevel, doshuffle, typesize, size, src,
  26. dest, size + 16);
  27. mu_assert("ERROR: cbytes is not correct", cbytes < size);
  28. nthreads = blosc_get_nthreads();
  29. mu_assert("ERROR: get_nthreads (compress, after) incorrect", nthreads == 3);
  30. return 0;
  31. }
  32. /* Check compressing + decompressing */
  33. static const char *test_compress_decompress(void) {
  34. int nthreads;
  35. nthreads = blosc_get_nthreads();
  36. mu_assert("ERROR: get_nthreads incorrect", nthreads == 3);
  37. /* Get a compressed buffer */
  38. cbytes = blosc_compress(clevel, doshuffle, typesize, size, src,
  39. dest, size+16);
  40. mu_assert("ERROR: cbytes is not correct", cbytes < size);
  41. nthreads = blosc_get_nthreads();
  42. mu_assert("ERROR: get_nthreads incorrect", nthreads == 3);
  43. /* Decompress the buffer */
  44. nbytes = blosc_decompress(dest, dest2, size);
  45. mu_assert("ERROR: nbytes incorrect(1)", nbytes == size);
  46. nthreads = blosc_get_nthreads();
  47. mu_assert("ERROR: get_nthreads incorrect", nthreads == 3);
  48. return 0;
  49. }
  50. static const char *all_tests(void) {
  51. mu_run_test(test_compress);
  52. mu_run_test(test_compress_decompress);
  53. return 0;
  54. }
  55. #define BUFFER_ALIGN_SIZE 32
  56. int main(int argc, char **argv) {
  57. int32_t *_src;
  58. const char *result;
  59. size_t i;
  60. printf("STARTING TESTS for %s", argv[0]);
  61. /* Activate the BLOSC_NTHREADS variable */
  62. setenv("BLOSC_NTHREADS", "3", 1);
  63. blosc_init();
  64. blosc_set_nthreads(1);
  65. /* Initialize buffers */
  66. src = blosc_test_malloc(BUFFER_ALIGN_SIZE, size);
  67. srccpy = blosc_test_malloc(BUFFER_ALIGN_SIZE, size);
  68. dest = blosc_test_malloc(BUFFER_ALIGN_SIZE, size + 16);
  69. dest2 = blosc_test_malloc(BUFFER_ALIGN_SIZE, size);
  70. _src = (int32_t *)src;
  71. for (i=0; i < (size/4); i++) {
  72. _src[i] = (int32_t)i;
  73. }
  74. memcpy(srccpy, src, size);
  75. /* Run all the suite */
  76. result = all_tests();
  77. if (result != 0) {
  78. printf(" (%s)\n", result);
  79. }
  80. else {
  81. printf(" ALL TESTS PASSED");
  82. }
  83. printf("\tTests run: %d\n", tests_run);
  84. blosc_test_free(src);
  85. blosc_test_free(srccpy);
  86. blosc_test_free(dest);
  87. blosc_test_free(dest2);
  88. blosc_destroy();
  89. return result != 0;
  90. }