Skip to content

Commit 6682d0a

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 a240813 commit 6682d0a

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,5 +1,6 @@
11
#include <stdio.h>
22
#include <stdlib.h>
3+
#include <string.h>
34
#include <assert.h>
45
#include <unistd.h>
56
#include <fcntl.h>
@@ -9,6 +10,8 @@
910
#include "util.h"
1011
#include "criu-log.h"
1112
#include "bfd.h"
13+
#include "compression.h"
14+
#include "page.h"
1215

1316
int parse_statement(int i, char *line, char **configuration);
1417

@@ -92,6 +95,72 @@ static void test_bwrite(void)
9295
free(read_buf);
9396
}
9497

98+
#ifdef CONFIG_LZ4
99+
static void test_compress_roundtrip(const char *page, int acceleration)
100+
{
101+
char compressed[PAGE_COMPRESSED_SIZE_BOUND];
102+
char decompressed[PAGE_SIZE];
103+
int cs;
104+
105+
cs = compress_data(page, PAGE_SIZE, compressed,
106+
PAGE_COMPRESSED_SIZE_BOUND, acceleration);
107+
assert(cs > 0);
108+
assert(cs <= PAGE_COMPRESSED_SIZE_BOUND);
109+
assert(decompress_data(compressed, cs, PAGE_SIZE, decompressed) == 0);
110+
assert(memcmp(page, decompressed, PAGE_SIZE) == 0);
111+
}
112+
113+
static void test_compression(void)
114+
{
115+
char cbuf[PAGE_COMPRESSED_SIZE_BOUND];
116+
char page[PAGE_SIZE];
117+
int cs;
118+
int accels[] = { 1, 4, 100 };
119+
120+
/* Zero-page detection */
121+
memset(page, 0, PAGE_SIZE);
122+
assert(page_is_all_zero(page) == true);
123+
page[PAGE_SIZE - 1] = 1;
124+
assert(page_is_all_zero(page) == false);
125+
page[PAGE_SIZE - 1] = 0;
126+
page[0] = 0x42;
127+
assert(page_is_all_zero(page) == false);
128+
129+
130+
for (int a = 0; a < 3; a++) {
131+
int accel = accels[a];
132+
133+
/* Zero-filled page: should compress well */
134+
memset(cbuf, 0, sizeof(cbuf));
135+
memset(page, 0, PAGE_SIZE);
136+
cs = compress_data(page, PAGE_SIZE,
137+
cbuf, PAGE_COMPRESSED_SIZE_BOUND, accel);
138+
assert(cs > 0 && cs < PAGE_SIZE);
139+
test_compress_roundtrip(page, accel);
140+
141+
/* Repeating pattern */
142+
for (int i = 0; i < PAGE_SIZE; i++)
143+
page[i] = i & 0xff;
144+
test_compress_roundtrip(page, accel);
145+
146+
/* Pseudo-random data (incompressible) */
147+
srand(42);
148+
for (int i = 0; i < PAGE_SIZE; i++)
149+
page[i] = rand() & 0xff;
150+
test_compress_roundtrip(page, accel);
151+
152+
/* Single non-zero byte */
153+
memset(page, 0, PAGE_SIZE);
154+
page[0] = 0x42;
155+
cs = compress_data(page, PAGE_SIZE,
156+
(char[PAGE_COMPRESSED_SIZE_BOUND]){0},
157+
PAGE_COMPRESSED_SIZE_BOUND, accel);
158+
assert(cs > 0 && cs < PAGE_SIZE);
159+
test_compress_roundtrip(page, accel);
160+
}
161+
}
162+
#endif
163+
95164
int main(int argc, char *argv[], char *envp[])
96165
{
97166
char **configuration;
@@ -230,6 +299,10 @@ int main(int argc, char *argv[], char *envp[])
230299
/* leaves punctuation in returned string as is */
231300
assert(!strcmp(get_relative_path("./a////.///./b//././c", "a"), "b//././c"));
232301

302+
#ifdef CONFIG_LZ4
303+
test_compression();
304+
#endif
305+
233306
pr_msg("OK\n");
234307
return 0;
235308
}

0 commit comments

Comments
 (0)