test_api.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*********************************************************************
  2. Blosc - Blocked Shuffling and Compression Library
  3. Unit tests for Blosc API.
  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. int tests_run = 0;
  10. /* Global vars */
  11. void *src, *srccpy, *dest, *dest2;
  12. size_t nbytes, cbytes;
  13. int clevel = 3;
  14. int doshuffle = 1;
  15. size_t typesize = 4;
  16. size_t size = 1*MB;
  17. static const char *test_cbuffer_sizes(void) {
  18. size_t nbytes_, cbytes_, blocksize;
  19. blosc_cbuffer_sizes(dest, &nbytes_, &cbytes_, &blocksize);
  20. mu_assert("ERROR: nbytes incorrect(1)", nbytes == size);
  21. mu_assert("ERROR: nbytes incorrect(2)", nbytes_ == nbytes);
  22. mu_assert("ERROR: cbytes incorrect", cbytes == cbytes_);
  23. mu_assert("ERROR: blocksize incorrect", blocksize >= 128);
  24. return 0;
  25. }
  26. static const char *test_cbuffer_metainfo(void) {
  27. size_t typesize_;
  28. int flags;
  29. blosc_cbuffer_metainfo(dest, &typesize_, &flags);
  30. mu_assert("ERROR: typesize incorrect", typesize_ == typesize);
  31. mu_assert("ERROR: shuffle incorrect", (flags & BLOSC_DOSHUFFLE) == doshuffle);
  32. return 0;
  33. }
  34. static const char *test_cbuffer_versions(void) {
  35. int version_;
  36. int versionlz_;
  37. blosc_cbuffer_versions(dest, &version_, &versionlz_);
  38. mu_assert("ERROR: version incorrect", version_ == BLOSC_VERSION_FORMAT);
  39. mu_assert("ERROR: versionlz incorrect", versionlz_ == BLOSC_BLOSCLZ_VERSION_FORMAT);
  40. return 0;
  41. }
  42. static const char *test_cbuffer_complib(void) {
  43. const char *complib;
  44. complib = blosc_cbuffer_complib(dest);
  45. mu_assert("ERROR: complib incorrect", strcmp(complib, "BloscLZ") == 0);
  46. return 0;
  47. }
  48. static const char *test_nthreads(void) {
  49. int nthreads;
  50. nthreads = blosc_set_nthreads(4);
  51. mu_assert("ERROR: set_nthreads incorrect", nthreads == 1);
  52. nthreads = blosc_get_nthreads();
  53. mu_assert("ERROR: get_nthreads incorrect", nthreads == 4);
  54. return 0;
  55. }
  56. static const char *test_blocksize(void) {
  57. int blocksize;
  58. blocksize = blosc_get_blocksize();
  59. mu_assert("ERROR: get_blocksize incorrect", blocksize == 0);
  60. blosc_set_blocksize(4096);
  61. blocksize = blosc_get_blocksize();
  62. mu_assert("ERROR: get_blocksize incorrect", blocksize == 4096);
  63. return 0;
  64. }
  65. static char *test_set_splitmode() {
  66. blosc_set_splitmode(BLOSC_AUTO_SPLIT);
  67. return 0;
  68. }
  69. static const char *all_tests(void) {
  70. mu_run_test(test_cbuffer_sizes);
  71. mu_run_test(test_cbuffer_metainfo);
  72. mu_run_test(test_cbuffer_versions);
  73. mu_run_test(test_cbuffer_complib);
  74. mu_run_test(test_nthreads);
  75. mu_run_test(test_blocksize);
  76. mu_run_test(test_set_splitmode);
  77. return 0;
  78. }
  79. #define BUFFER_ALIGN_SIZE 8
  80. int main(int argc, char **argv) {
  81. const char *result;
  82. printf("STARTING TESTS for %s", argv[0]);
  83. blosc_init();
  84. blosc_set_nthreads(1);
  85. /* Initialize buffers */
  86. src = blosc_test_malloc(BUFFER_ALIGN_SIZE, size);
  87. srccpy = blosc_test_malloc(BUFFER_ALIGN_SIZE, size);
  88. dest = blosc_test_malloc(BUFFER_ALIGN_SIZE, size);
  89. dest2 = blosc_test_malloc(BUFFER_ALIGN_SIZE, size);
  90. memset(src, 0, size);
  91. memcpy(srccpy, src, size);
  92. /* Get a compressed buffer */
  93. cbytes = blosc_compress(clevel, doshuffle, typesize, size, src, dest, size);
  94. /* Get a decompressed buffer */
  95. nbytes = blosc_decompress(dest, dest2, size);
  96. /* Run all the suite */
  97. result = all_tests();
  98. if (result != 0) {
  99. printf(" (%s)\n", result);
  100. }
  101. else {
  102. printf(" ALL TESTS PASSED");
  103. }
  104. printf("\tTests run: %d\n", tests_run);
  105. blosc_test_free(src);
  106. blosc_test_free(srccpy);
  107. blosc_test_free(dest);
  108. blosc_test_free(dest2);
  109. blosc_destroy();
  110. return result != 0;
  111. }