| 16 | /* ── Helper: compress + decompress round-trip ──────────────────── */ |
| 17 | |
| 18 | static int lz4_roundtrip(const char *data, int len) { |
| 19 | if (len == 0) { |
| 20 | /* Empty input: Go wrapper returns nil without calling LZ4. |
| 21 | * LZ4_compress_HC actually returns 1 (end-of-stream marker) for empty input. |
| 22 | * Match Go behavior: empty in → empty out is correct. */ |
| 23 | return 0; |
| 24 | } |
| 25 | |
| 26 | int bound = cbm_lz4_bound(len); |
| 27 | if (bound <= 0) |
| 28 | return -1; |
| 29 | |
| 30 | char *cbuf = malloc(bound); |
| 31 | if (!cbuf) |
| 32 | return -1; |
| 33 | |
| 34 | int clen = cbm_lz4_compress_hc(data, len, cbuf, bound); |
| 35 | if (clen <= 0) { |
| 36 | free(cbuf); |
| 37 | return -1; |
| 38 | } |
| 39 | |
| 40 | char *dbuf = malloc(len); |
| 41 | if (!dbuf) { |
| 42 | free(cbuf); |
| 43 | return -1; |
| 44 | } |
| 45 | |
| 46 | int dlen = cbm_lz4_decompress(cbuf, clen, dbuf, len); |
| 47 | int ok = (dlen == len && memcmp(dbuf, data, len) == 0) ? 0 : -1; |
| 48 | |
| 49 | free(cbuf); |
| 50 | free(dbuf); |
| 51 | return ok; |
| 52 | } |
| 53 | |
| 54 | /* ── Tests ─────────────────────────────────────────────────────── */ |
| 55 |
no test coverage detected