补充
This commit is contained in:
78
Test/TestProject/App/__zlib/test_debug.c
Normal file
78
Test/TestProject/App/__zlib/test_debug.c
Normal file
@@ -0,0 +1,78 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include "pyzlib.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
printf("=== Full Self-Implemented zlib Test ===\n\n");
|
||||
|
||||
printf("1. Adler-32:\n");
|
||||
unsigned long a = zlib_adler32((const unsigned char *)"Hello", 5, 1);
|
||||
printf(" adler32(\"Hello\") = 0x%08lX\n", a);
|
||||
|
||||
printf("\n2. CRC-32:\n");
|
||||
unsigned long c = zlib_crc32((const unsigned char *)"Hello", 5, 0);
|
||||
printf(" crc32(\"Hello\") = 0x%08lX\n", c);
|
||||
|
||||
const char *tests[] = {"Hello", "Hello, World!", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", ""};
|
||||
int levels[] = {2, 6, 0};
|
||||
|
||||
for (int t = 0; t < 4; t++) {
|
||||
const char *input = tests[t];
|
||||
size_t input_len = strlen(input);
|
||||
printf("\n--- Test: \"%s\" (%zu bytes) ---\n", input_len > 0 ? input : "(empty)", input_len);
|
||||
|
||||
for (int li = 0; li < 3; li++) {
|
||||
int level = levels[li];
|
||||
printf("\n Level %d, zlib format:\n", level);
|
||||
size_t comp_len = 0;
|
||||
unsigned char *comp = zlib_compress((const unsigned char *)input, input_len,
|
||||
level, MAX_WBITS, &comp_len);
|
||||
if (comp) {
|
||||
printf(" Compressed: %zu bytes\n", comp_len);
|
||||
size_t dec_len = 0;
|
||||
unsigned char *dec = zlib_decompress(comp, comp_len, MAX_WBITS, DEF_BUF_SIZE, &dec_len);
|
||||
if (dec) {
|
||||
int match = (dec_len == input_len && memcmp(dec, input, dec_len) == 0);
|
||||
printf(" Decompressed: %zu bytes, Match: %s\n", dec_len, match ? "YES" : "NO");
|
||||
if (!match) {
|
||||
printf(" Expected: \"%s\"\n", input_len > 0 ? input : "(empty)");
|
||||
printf(" Got: \"%.*s\"\n", (int)dec_len, (char *)dec);
|
||||
}
|
||||
free(dec);
|
||||
} else {
|
||||
printf(" Decompress FAILED: %s (code %d)\n", zlib_get_error(), zlib_get_error_code());
|
||||
}
|
||||
free(comp);
|
||||
} else {
|
||||
printf(" Compress FAILED: %s (code %d)\n", zlib_get_error(), zlib_get_error_code());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printf("\n3. Raw deflate:\n");
|
||||
{
|
||||
const char *input = "Raw deflate test";
|
||||
size_t comp_len = 0;
|
||||
unsigned char *comp = zlib_compress((const unsigned char *)input, strlen(input),
|
||||
Z_DEFAULT_COMPRESSION, -MAX_WBITS, &comp_len);
|
||||
if (comp) {
|
||||
printf(" Compressed: %zu bytes\n", comp_len);
|
||||
size_t dec_len = 0;
|
||||
unsigned char *dec = zlib_decompress(comp, comp_len, -MAX_WBITS, DEF_BUF_SIZE, &dec_len);
|
||||
if (dec) {
|
||||
printf(" Decompressed: \"%.*s\" %s\n", (int)dec_len, (char *)dec,
|
||||
dec_len == strlen(input) && memcmp(dec, input, dec_len) == 0 ? "OK" : "FAIL");
|
||||
free(dec);
|
||||
} else {
|
||||
printf(" Decompress FAILED: %s\n", zlib_get_error());
|
||||
}
|
||||
free(comp);
|
||||
}
|
||||
}
|
||||
|
||||
printf("\nDone.\n");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user