test_api.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 char *test_cbuffer_sizes() {
  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 char *test_cbuffer_metainfo() {
  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 char *test_cbuffer_versions() {
  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 char *test_cbuffer_complib() {
  43. char *complib;
  44. complib = blosc_cbuffer_complib(dest);
  45. mu_assert("ERROR: complib incorrect", strcmp(complib, "BloscLZ") == 0);
  46. return 0;
  47. }
  48. static char *test_nthreads() {
  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 char *test_blocksize() {
  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 *all_tests() {
  66. mu_run_test(test_cbuffer_sizes);
  67. mu_run_test(test_cbuffer_metainfo);
  68. mu_run_test(test_cbuffer_versions);
  69. mu_run_test(test_cbuffer_complib);
  70. mu_run_test(test_nthreads);
  71. mu_run_test(test_blocksize);
  72. return 0;
  73. }
  74. #define BUFFER_ALIGN_SIZE 8
  75. int main(int argc, char **argv) {
  76. char *result;
  77. printf("STARTING TESTS for %s", argv[0]);
  78. blosc_init();
  79. blosc_set_nthreads(1);
  80. /* Initialize buffers */
  81. src = blosc_test_malloc(BUFFER_ALIGN_SIZE, size);
  82. srccpy = blosc_test_malloc(BUFFER_ALIGN_SIZE, size);
  83. dest = blosc_test_malloc(BUFFER_ALIGN_SIZE, size);
  84. dest2 = blosc_test_malloc(BUFFER_ALIGN_SIZE, size);
  85. memset(src, 0, size);
  86. memcpy(srccpy, src, size);
  87. /* Get a compressed buffer */
  88. cbytes = blosc_compress(clevel, doshuffle, typesize, size, src, dest, size);
  89. /* Get a decompressed buffer */
  90. nbytes = blosc_decompress(dest, dest2, size);
  91. /* Run all the suite */
  92. result = all_tests();
  93. if (result != 0) {
  94. printf(" (%s)\n", result);
  95. }
  96. else {
  97. printf(" ALL TESTS PASSED");
  98. }
  99. printf("\tTests run: %d\n", tests_run);
  100. blosc_test_free(src);
  101. blosc_test_free(srccpy);
  102. blosc_test_free(dest);
  103. blosc_test_free(dest2);
  104. blosc_destroy();
  105. return result != 0;
  106. }