Skip to content

Commit 8b1ef89

Browse files
committed
unittest: add compression round-trip tests
Test compress_data()/decompress_data() with zero-filled, repeating pattern, pseudo-random, and single-byte pages across three LZ4 acceleration levels. Also test page_is_all_zero() edge cases. Signed-off-by: Radostin Stoyanov <rstoyanov@fedoraproject.org>
1 parent fc682f1 commit 8b1ef89

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

criu/unittest/unit.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,82 @@
11
#include <stdio.h>
22
#include <stdlib.h>
3+
#include <string.h>
34
#include <assert.h>
45

56
#include "log.h"
67
#include "util.h"
78
#include "criu-log.h"
9+
#include "compression.h"
10+
#include "page.h"
811

912
int parse_statement(int i, char *line, char **configuration);
1013

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+
1180
int main(int argc, char *argv[], char *envp[])
1281
{
1382
char **configuration;
@@ -143,6 +212,10 @@ int main(int argc, char *argv[], char *envp[])
143212
/* leaves punctuation in returned string as is */
144213
assert(!strcmp(get_relative_path("./a////.///./b//././c", "a"), "b//././c"));
145214

215+
#ifdef CONFIG_LZ4
216+
test_compression();
217+
#endif
218+
146219
pr_msg("OK\n");
147220
return 0;
148221
}

0 commit comments

Comments
 (0)