test_miniz.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdint.h>
  5. #include "sdkconfig.h"
  6. #include "unity.h"
  7. // compression/decompression will take off a bunch of memory
  8. // test it only with PSRAM enabled
  9. #ifdef CONFIG_SPIRAM
  10. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32)
  11. // miniz unit test can't pass on ESP32 non-ECO3 version IDF-1807
  12. #if CONFIG_IDF_TARGET_ESP32
  13. #include "esp32/rom/miniz.h"
  14. #elif CONFIG_IDF_TARGET_ESP32S2
  15. #include "esp32s2/rom/miniz.h"
  16. #else
  17. #error "unsupported target"
  18. #endif
  19. #define DATASIZE (1024 * 64)
  20. TEST_CASE("Test miniz compression/decompression", "[rom][miniz]")
  21. {
  22. size_t inbytes = 0, outbytes = 0, inpos = 0, outpos = 0, compsz;
  23. printf("Allocating data buffer and filling it with semi-random data\r\n");
  24. uint8_t *inbuf = calloc(1, DATASIZE);
  25. TEST_ASSERT_NOT_NULL_MESSAGE(inbuf, "allocate input buffer failed");
  26. srand(0); // semi random data
  27. for (int i = 0; i < DATASIZE; i++) {
  28. inbuf[i] = (i & 0x01) ? rand() & 0xff : 0;
  29. }
  30. printf("Allocating compressor\r\n");
  31. tdefl_compressor *comp = calloc(1, sizeof(tdefl_compressor));
  32. TEST_ASSERT_NOT_NULL_MESSAGE(comp, "allocate tdefl_compressor failed");
  33. uint8_t *outbuf = calloc(1, DATASIZE);
  34. TEST_ASSERT_NOT_NULL_MESSAGE(outbuf, "allocate output buffer failed");
  35. printf("Compressing...\r\n");
  36. tdefl_status comp_status = tdefl_init(comp, NULL, NULL, TDEFL_WRITE_ZLIB_HEADER | 1500);
  37. TEST_ASSERT_EQUAL_MESSAGE(TDEFL_STATUS_OKAY, comp_status, "tdefl_init failed");
  38. while (1) {
  39. outbytes = DATASIZE - outpos;
  40. inbytes = DATASIZE - inpos;
  41. comp_status = tdefl_compress(comp, &inbuf[inpos], &inbytes, &outbuf[outpos], &outbytes, TDEFL_FINISH);
  42. inpos += inbytes;
  43. outpos += outbytes;
  44. printf("...Compressed %d into %d bytes\r\n", inpos, outpos);
  45. if (comp_status == TDEFL_STATUS_DONE) {
  46. break;
  47. } else if (comp_status != TDEFL_STATUS_OKAY) {
  48. TEST_ASSERT_MESSAGE(0, "tdefl_compress failed");
  49. }
  50. }
  51. compsz = outpos;
  52. free(comp);
  53. free(inbuf);
  54. inbuf = outbuf;
  55. outbuf = calloc(1, DATASIZE);
  56. TEST_ASSERT_NOT_NULL_MESSAGE(outbuf, "allocate output buffer failed");
  57. printf("Decompressing...\r\n");
  58. tinfl_decompressor *decomp = calloc(1, sizeof(tinfl_decompressor));
  59. TEST_ASSERT_NOT_NULL_MESSAGE(decomp, "allocate tinfl_decompressor failed");
  60. tinfl_init(decomp);
  61. inpos = 0;
  62. outpos = 0;
  63. while (1) {
  64. outbytes = DATASIZE - outpos;
  65. inbytes = compsz - inpos;
  66. tinfl_status decomp_status = tinfl_decompress(decomp, &inbuf[inpos], &inbytes, outbuf, &outbuf[outpos], &outbytes, TINFL_FLAG_PARSE_ZLIB_HEADER);
  67. inpos += inbytes;
  68. outpos += outbytes;
  69. printf("...Decompressed %d into %d bytes\r\n", inpos, outpos);
  70. if (decomp_status == TINFL_STATUS_DONE) {
  71. break;
  72. } else if (decomp_status < TINFL_STATUS_DONE) {
  73. printf("decomp status=%d\r\n", decomp_status);
  74. TEST_ASSERT_MESSAGE(0, "tinfl_decompress failed");
  75. }
  76. }
  77. printf("Verifying data between compression and decompression...\r\n");
  78. srand(0); // semi random data
  79. for (int i = 0; i < DATASIZE; i++) {
  80. uint8_t original = (i & 1) ? rand() & 0xff : 0;
  81. TEST_ASSERT_EQUAL_MESSAGE(original, outbuf[i], "data after decompression doesn't match the original one");
  82. }
  83. printf("Great Success!\n");
  84. free(inbuf);
  85. free(outbuf);
  86. free(decomp);
  87. }
  88. #endif //#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32)
  89. #endif // CONFIG_SPIRAM