use_lz4.c (1014B)
1 /* use_lz4.c — exercise LZ4 block compress / decompress round-trips. 2 * Deterministic: fixed inputs, reports sizes and exact-match flags. */ 3 #include "lz4.h" 4 #include <stdio.h> 5 #include <string.h> 6 7 static int roundtrip(const char *label, const char *in, int n) { 8 char comp[4096]; 9 char dec[4096]; 10 int cl = LZ4_compress_default(in, comp, n, (int)sizeof comp); 11 if (cl <= 0) { 12 printf("%s compress-fail\n", label); 13 return 0; 14 } 15 int dl = LZ4_decompress_safe(comp, dec, cl, (int)sizeof dec); 16 int ok = (dl == n) && (memcmp(in, dec, (size_t)n) == 0); 17 printf("%s in=%d comp=%d out=%d match=%d\n", label, n, cl, dl, ok); 18 return ok; 19 } 20 21 int main(void) { 22 const char *a = "the quick brown fox jumps over the lazy dog"; 23 /* Highly compressible repeated pattern. */ 24 char b[1024]; 25 for (int i = 0; i < (int)sizeof b; i++) b[i] = (char)('A' + (i % 8)); 26 27 roundtrip("text", a, (int)strlen(a)); 28 roundtrip("repeat", b, (int)sizeof b); 29 30 printf("versionstr=%s\n", LZ4_versionString()); 31 return 0; 32 }