|
1 | 1 | #include <stdio.h> |
2 | 2 | #include <stdlib.h> |
| 3 | +#include <string.h> |
3 | 4 | #include <assert.h> |
4 | 5 |
|
5 | 6 | #include "log.h" |
6 | 7 | #include "util.h" |
7 | 8 | #include "criu-log.h" |
| 9 | +#include "compression.h" |
| 10 | +#include "page.h" |
8 | 11 |
|
9 | 12 | int parse_statement(int i, char *line, char **configuration); |
10 | 13 |
|
| 14 | +#ifdef CONFIG_LZ4 |
| 15 | +static void test_compress_roundtrip(const char *page, int acceleration) |
| 16 | +{ |
| 17 | + char compressed[PAGE_COMPRESSED_SIZE_BOUND]; |
| 18 | + char decompressed[PAGE_SIZE]; |
| 19 | + int cs; |
| 20 | + |
| 21 | + cs = compress_data(page, PAGE_SIZE, compressed, |
| 22 | + PAGE_COMPRESSED_SIZE_BOUND, acceleration); |
| 23 | + assert(cs > 0); |
| 24 | + assert(cs <= PAGE_COMPRESSED_SIZE_BOUND); |
| 25 | + assert(decompress_data(compressed, cs, PAGE_SIZE, decompressed) == 0); |
| 26 | + assert(memcmp(page, decompressed, PAGE_SIZE) == 0); |
| 27 | +} |
| 28 | + |
| 29 | +static void test_compression(void) |
| 30 | +{ |
| 31 | + char cbuf[PAGE_COMPRESSED_SIZE_BOUND]; |
| 32 | + char page[PAGE_SIZE]; |
| 33 | + int cs; |
| 34 | + int accels[] = { 1, 4, 100 }; |
| 35 | + |
| 36 | + /* Zero-page detection */ |
| 37 | + memset(page, 0, PAGE_SIZE); |
| 38 | + assert(page_is_all_zero(page) == true); |
| 39 | + page[PAGE_SIZE - 1] = 1; |
| 40 | + assert(page_is_all_zero(page) == false); |
| 41 | + page[PAGE_SIZE - 1] = 0; |
| 42 | + page[0] = 0x42; |
| 43 | + assert(page_is_all_zero(page) == false); |
| 44 | + |
| 45 | + |
| 46 | + for (int a = 0; a < 3; a++) { |
| 47 | + int accel = accels[a]; |
| 48 | + |
| 49 | + /* Zero-filled page: should compress well */ |
| 50 | + memset(cbuf, 0, sizeof(cbuf)); |
| 51 | + memset(page, 0, PAGE_SIZE); |
| 52 | + cs = compress_data(page, PAGE_SIZE, |
| 53 | + cbuf, PAGE_COMPRESSED_SIZE_BOUND, accel); |
| 54 | + assert(cs > 0 && cs < PAGE_SIZE); |
| 55 | + test_compress_roundtrip(page, accel); |
| 56 | + |
| 57 | + /* Repeating pattern */ |
| 58 | + for (int i = 0; i < PAGE_SIZE; i++) |
| 59 | + page[i] = i & 0xff; |
| 60 | + test_compress_roundtrip(page, accel); |
| 61 | + |
| 62 | + /* Pseudo-random data (incompressible) */ |
| 63 | + srand(42); |
| 64 | + for (int i = 0; i < PAGE_SIZE; i++) |
| 65 | + page[i] = rand() & 0xff; |
| 66 | + test_compress_roundtrip(page, accel); |
| 67 | + |
| 68 | + /* Single non-zero byte */ |
| 69 | + memset(page, 0, PAGE_SIZE); |
| 70 | + page[0] = 0x42; |
| 71 | + cs = compress_data(page, PAGE_SIZE, |
| 72 | + (char[PAGE_COMPRESSED_SIZE_BOUND]){0}, |
| 73 | + PAGE_COMPRESSED_SIZE_BOUND, accel); |
| 74 | + assert(cs > 0 && cs < PAGE_SIZE); |
| 75 | + test_compress_roundtrip(page, accel); |
| 76 | + } |
| 77 | +} |
| 78 | +#endif |
| 79 | + |
11 | 80 | int main(int argc, char *argv[], char *envp[]) |
12 | 81 | { |
13 | 82 | char **configuration; |
@@ -143,6 +212,10 @@ int main(int argc, char *argv[], char *envp[]) |
143 | 212 | /* leaves punctuation in returned string as is */ |
144 | 213 | assert(!strcmp(get_relative_path("./a////.///./b//././c", "a"), "b//././c")); |
145 | 214 |
|
| 215 | +#ifdef CONFIG_LZ4 |
| 216 | + test_compression(); |
| 217 | +#endif |
| 218 | + |
146 | 219 | pr_msg("OK\n"); |
147 | 220 | return 0; |
148 | 221 | } |
0 commit comments