test_compressor.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*********************************************************************
  2. Blosc - Blocked Shuffling and Compression Library
  3. Unit tests for BLOSC_COMPRESSOR 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 = 8;
  16. size_t size = 8 * 1000 * 1000; /* must be divisible by typesize */
  17. /* Check compressor */
  18. static char *test_compressor() {
  19. char* compressor;
  20. /* Before any blosc_compress() the compressor must be blosclz */
  21. compressor = blosc_get_compressor();
  22. mu_assert("ERROR: get_compressor (compress, before) incorrect",
  23. strcmp(compressor, "blosclz") == 0);
  24. /* Activate the BLOSC_COMPRESSOR variable */
  25. setenv("BLOSC_COMPRESSOR", "lz4", 0);
  26. /* Get a compressed buffer */
  27. cbytes = blosc_compress(clevel, doshuffle, typesize, size, src,
  28. dest, size + 16);
  29. mu_assert("ERROR: cbytes is not correct", cbytes < size);
  30. compressor = blosc_get_compressor();
  31. mu_assert("ERROR: get_compressor (compress, after) incorrect",
  32. strcmp(compressor, "lz4") == 0);
  33. /* Reset envvar */
  34. unsetenv("BLOSC_COMPRESSOR");
  35. return 0;
  36. }
  37. /* Check compressing + decompressing */
  38. static char *test_compress_decompress() {
  39. char* compressor;
  40. /* Activate the BLOSC_COMPRESSOR variable */
  41. setenv("BLOSC_COMPRESSOR", "lz4", 0);
  42. compressor = blosc_get_compressor();
  43. mu_assert("ERROR: get_compressor incorrect",
  44. strcmp(compressor, "lz4") == 0);
  45. /* Get a compressed buffer */
  46. cbytes = blosc_compress(clevel, doshuffle, typesize, size, src,
  47. dest, size + 16);
  48. mu_assert("ERROR: cbytes is not correct", cbytes < size);
  49. compressor = blosc_get_compressor();
  50. mu_assert("ERROR: get_compressor incorrect",
  51. strcmp(compressor, "lz4") == 0);
  52. /* Decompress the buffer */
  53. nbytes = blosc_decompress(dest, dest2, size);
  54. mu_assert("ERROR: nbytes incorrect(1)", nbytes == size);
  55. compressor = blosc_get_compressor();
  56. mu_assert("ERROR: get_compressor incorrect",
  57. strcmp(compressor, "lz4") == 0);
  58. /* Reset envvar */
  59. unsetenv("BLOSC_COMPRESSOR");
  60. return 0;
  61. }
  62. /* Check compression level */
  63. static char *test_clevel() {
  64. int cbytes2;
  65. /* Get a compressed buffer */
  66. cbytes = blosc_compress(clevel, doshuffle, typesize, size, src,
  67. dest, size + 16);
  68. mu_assert("ERROR: cbytes is not correct", cbytes < size);
  69. /* Activate the BLOSC_CLEVEL variable */
  70. setenv("BLOSC_CLEVEL", "9", 0);
  71. cbytes2 = blosc_compress(clevel, doshuffle, typesize, size, src,
  72. dest, size + 16);
  73. mu_assert("ERROR: BLOSC_CLEVEL does not work correctly", cbytes2 < cbytes);
  74. /* Reset envvar */
  75. unsetenv("BLOSC_CLEVEL");
  76. return 0;
  77. }
  78. /* Check noshuffle */
  79. static char *test_noshuffle() {
  80. int cbytes2;
  81. /* Get a compressed buffer */
  82. cbytes = blosc_compress(clevel, doshuffle, typesize, size, src,
  83. dest, size + 16);
  84. mu_assert("ERROR: cbytes is not correct", cbytes < size);
  85. /* Activate the BLOSC_SHUFFLE variable */
  86. setenv("BLOSC_SHUFFLE", "NOSHUFFLE", 0);
  87. cbytes2 = blosc_compress(clevel, doshuffle, typesize, size, src,
  88. dest, size + 16);
  89. mu_assert("ERROR: BLOSC_SHUFFLE=NOSHUFFLE does not work correctly",
  90. cbytes2 > cbytes);
  91. /* Reset env var */
  92. unsetenv("BLOSC_SHUFFLE");
  93. return 0;
  94. }
  95. /* Check regular shuffle */
  96. static char *test_shuffle() {
  97. int cbytes2;
  98. /* Get a compressed buffer */
  99. cbytes = blosc_compress(clevel, doshuffle, typesize, size, src,
  100. dest, size + 16);
  101. mu_assert("ERROR: cbytes is not 0", cbytes < size);
  102. /* Activate the BLOSC_SHUFFLE variable */
  103. setenv("BLOSC_SHUFFLE", "SHUFFLE", 0);
  104. cbytes2 = blosc_compress(clevel, doshuffle, typesize, size, src,
  105. dest, size + 16);
  106. mu_assert("ERROR: BLOSC_SHUFFLE=SHUFFLE does not work correctly",
  107. cbytes2 == cbytes);
  108. /* Reset env var */
  109. unsetenv("BLOSC_SHUFFLE");
  110. return 0;
  111. }
  112. /* Check bitshuffle */
  113. static char *test_bitshuffle() {
  114. int cbytes2;
  115. /* Get a compressed buffer */
  116. blosc_set_compressor("blosclz"); /* avoid lz4 here for now (see #168) */
  117. cbytes = blosc_compress(clevel, doshuffle, typesize, size, src,
  118. dest, size + 16);
  119. mu_assert("ERROR: cbytes is not 0", cbytes < size);
  120. /* Activate the BLOSC_BITSHUFFLE variable */
  121. setenv("BLOSC_SHUFFLE", "BITSHUFFLE", 0);
  122. cbytes2 = blosc_compress(clevel, doshuffle, typesize, size, src,
  123. dest, size + 16);
  124. mu_assert("ERROR: BLOSC_SHUFFLE=BITSHUFFLE does not work correctly",
  125. cbytes2 < cbytes);
  126. /* Reset env var */
  127. unsetenv("BLOSC_SHUFFLE");
  128. return 0;
  129. }
  130. /* Check typesize */
  131. static char *test_typesize() {
  132. int cbytes2;
  133. /* Get a compressed buffer */
  134. cbytes = blosc_compress(clevel, doshuffle, typesize, size, src,
  135. dest, size + 16);
  136. mu_assert("ERROR: cbytes is not correct", cbytes < size);
  137. /* Activate the BLOSC_TYPESIZE variable */
  138. setenv("BLOSC_TYPESIZE", "9", 0);
  139. cbytes2 = blosc_compress(clevel, doshuffle, typesize, size, src,
  140. dest, size + 16);
  141. mu_assert("ERROR: BLOSC_TYPESIZE does not work correctly", cbytes2 > cbytes);
  142. /* Reset envvar */
  143. unsetenv("BLOSC_TYPESIZE");
  144. return 0;
  145. }
  146. static char *all_tests() {
  147. mu_run_test(test_compressor);
  148. mu_run_test(test_compress_decompress);
  149. mu_run_test(test_clevel);
  150. mu_run_test(test_noshuffle);
  151. mu_run_test(test_shuffle);
  152. mu_run_test(test_bitshuffle);
  153. mu_run_test(test_typesize);
  154. return 0;
  155. }
  156. #define BUFFER_ALIGN_SIZE 32
  157. int main(int argc, char **argv) {
  158. int64_t *_src;
  159. char *result;
  160. size_t i;
  161. printf("STARTING TESTS for %s", argv[0]);
  162. blosc_init();
  163. blosc_set_compressor("blosclz");
  164. /* Initialize buffers */
  165. src = blosc_test_malloc(BUFFER_ALIGN_SIZE, size);
  166. srccpy = blosc_test_malloc(BUFFER_ALIGN_SIZE, size);
  167. dest = blosc_test_malloc(BUFFER_ALIGN_SIZE, size + 16);
  168. dest2 = blosc_test_malloc(BUFFER_ALIGN_SIZE, size);
  169. _src = (int64_t *)src;
  170. for (i=0; i < (size / sizeof(int64_t)); i++) {
  171. _src[i] = (int64_t)i;
  172. }
  173. memcpy(srccpy, src, size);
  174. /* Run all the suite */
  175. result = all_tests();
  176. if (result != 0) {
  177. printf(" (%s)\n", result);
  178. }
  179. else {
  180. printf(" ALL TESTS PASSED");
  181. }
  182. printf("\tTests run: %d\n", tests_run);
  183. blosc_test_free(src);
  184. blosc_test_free(srccpy);
  185. blosc_test_free(dest);
  186. blosc_test_free(dest2);
  187. blosc_destroy();
  188. return result != 0;
  189. }