test_compressor.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 const char *test_compressor(void) {
  19. const 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 const char *test_compress_decompress(void) {
  39. const 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 const char *test_clevel(void) {
  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 const char *test_noshuffle(void) {
  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 const char *test_shuffle(void) {
  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 const char *test_bitshuffle(void) {
  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 * 1.5);
  126. /* Reset env var */
  127. unsetenv("BLOSC_SHUFFLE");
  128. return 0;
  129. }
  130. /* Check typesize */
  131. static const char *test_typesize(void) {
  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. /* Check splitmode */
  147. static char *test_splitmode() {
  148. int cbytes2;
  149. /* Get a compressed buffer */
  150. cbytes = blosc_compress(clevel, doshuffle, typesize, size, src,
  151. dest, size + 16);
  152. mu_assert("ERROR: cbytes is not correct", cbytes < size);
  153. /* Deactivate the split */
  154. blosc_set_splitmode(BLOSC_NEVER_SPLIT);
  155. cbytes2 = blosc_compress(clevel, doshuffle, typesize, size, src,
  156. dest, size + 16);
  157. mu_assert("ERROR: blosc_set_splitmode does not work correctly", cbytes2 > cbytes);
  158. /* Reset the splitmode */
  159. blosc_set_splitmode(BLOSC_FORWARD_COMPAT_SPLIT);
  160. return 0;
  161. }
  162. /* Check splitmode with an environment variable */
  163. static char *test_splitmode_envvar() {
  164. int cbytes2;
  165. /* Get a compressed buffer */
  166. cbytes = blosc_compress(clevel, doshuffle, typesize, size, src,
  167. dest, size + 16);
  168. mu_assert("ERROR: cbytes is not correct", cbytes < size);
  169. /* Deactivate the split */
  170. setenv("BLOSC_SPLITMODE", "NEVER", 0);
  171. cbytes2 = blosc_compress(clevel, doshuffle, typesize, size, src,
  172. dest, size + 16);
  173. mu_assert("ERROR: BLOSC_SPLITMODE envvar does not work correctly", cbytes2 > cbytes);
  174. return 0;
  175. }
  176. static const char *all_tests(void) {
  177. mu_run_test(test_compressor);
  178. mu_run_test(test_compress_decompress);
  179. mu_run_test(test_clevel);
  180. mu_run_test(test_noshuffle);
  181. mu_run_test(test_shuffle);
  182. mu_run_test(test_bitshuffle);
  183. mu_run_test(test_typesize);
  184. mu_run_test(test_splitmode);
  185. mu_run_test(test_splitmode_envvar);
  186. return 0;
  187. }
  188. #define BUFFER_ALIGN_SIZE 32
  189. int main(int argc, char **argv) {
  190. int64_t *_src;
  191. const char *result;
  192. size_t i;
  193. printf("STARTING TESTS for %s", argv[0]);
  194. blosc_init();
  195. blosc_set_compressor("blosclz");
  196. /* Initialize buffers */
  197. src = blosc_test_malloc(BUFFER_ALIGN_SIZE, size);
  198. srccpy = blosc_test_malloc(BUFFER_ALIGN_SIZE, size);
  199. dest = blosc_test_malloc(BUFFER_ALIGN_SIZE, size + 16);
  200. dest2 = blosc_test_malloc(BUFFER_ALIGN_SIZE, size);
  201. _src = (int64_t *)src;
  202. for (i=0; i < (size / sizeof(int64_t)); i++) {
  203. _src[i] = (int64_t)i;
  204. }
  205. memcpy(srccpy, src, size);
  206. /* Run all the suite */
  207. result = all_tests();
  208. if (result != 0) {
  209. printf(" (%s)\n", result);
  210. }
  211. else {
  212. printf(" ALL TESTS PASSED");
  213. }
  214. printf("\tTests run: %d\n", tests_run);
  215. blosc_test_free(src);
  216. blosc_test_free(srccpy);
  217. blosc_test_free(dest);
  218. blosc_test_free(dest2);
  219. blosc_destroy();
  220. return result != 0;
  221. }