2 * Xor Based Zero Run Length Encoding unit tests.
4 * Copyright 2013 Red Hat, Inc. and/or its affiliates
7 * Orit Wasserman <owasserm@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
20 #include "qemu-common.h"
21 #include "include/migration/migration.h"
23 #define PAGE_SIZE 4096
25 static void test_uleb(void)
29 int encode_ret
, decode_ret
;
31 for (i
= 0; i
<= 0x3fff; i
++) {
32 encode_ret
= uleb128_encode_small(&buf
[0], i
);
33 decode_ret
= uleb128_decode_small(&buf
[0], &val
);
34 g_assert(encode_ret
== decode_ret
);
38 /* decode invalid value */
42 decode_ret
= uleb128_decode_small(&buf
[0], &val
);
43 g_assert(decode_ret
== -1);
47 static void test_encode_decode_zero(void)
49 uint8_t *buffer
= g_malloc0(PAGE_SIZE
);
50 uint8_t *compressed
= g_malloc0(PAGE_SIZE
);
53 int diff_len
= g_test_rand_int_range(0, PAGE_SIZE
- 1006);
55 for (i
= diff_len
; i
> 0; i
--) {
59 buffer
[1000 + diff_len
+ 3] = 103;
60 buffer
[1000 + diff_len
+ 5] = 105;
62 /* encode zero page */
63 dlen
= xbzrle_encode_buffer(buffer
, buffer
, PAGE_SIZE
, compressed
,
71 static void test_encode_decode_unchanged(void)
73 uint8_t *compressed
= g_malloc0(PAGE_SIZE
);
74 uint8_t *test
= g_malloc0(PAGE_SIZE
);
77 int diff_len
= g_test_rand_int_range(0, PAGE_SIZE
- 1006);
79 for (i
= diff_len
; i
> 0; i
--) {
80 test
[1000 + i
] = i
+ 4;
83 test
[1000 + diff_len
+ 3] = 107;
84 test
[1000 + diff_len
+ 5] = 109;
86 /* test unchanged buffer */
87 dlen
= xbzrle_encode_buffer(test
, test
, PAGE_SIZE
, compressed
,
95 static void test_encode_decode_1_byte(void)
97 uint8_t *buffer
= g_malloc0(PAGE_SIZE
);
98 uint8_t *test
= g_malloc0(PAGE_SIZE
);
99 uint8_t *compressed
= g_malloc(PAGE_SIZE
);
100 int dlen
= 0, rc
= 0;
103 test
[PAGE_SIZE
- 1] = 1;
105 dlen
= xbzrle_encode_buffer(buffer
, test
, PAGE_SIZE
, compressed
,
107 g_assert(dlen
== (uleb128_encode_small(&buf
[0], 4095) + 2));
109 rc
= xbzrle_decode_buffer(compressed
, dlen
, buffer
, PAGE_SIZE
);
110 g_assert(rc
== PAGE_SIZE
);
111 g_assert(memcmp(test
, buffer
, PAGE_SIZE
) == 0);
118 static void test_encode_decode_overflow(void)
120 uint8_t *compressed
= g_malloc0(PAGE_SIZE
);
121 uint8_t *test
= g_malloc0(PAGE_SIZE
);
122 uint8_t *buffer
= g_malloc0(PAGE_SIZE
);
125 for (i
= 0; i
< PAGE_SIZE
/ 2 - 1; i
++) {
129 /* encode overflow */
130 rc
= xbzrle_encode_buffer(buffer
, test
, PAGE_SIZE
, compressed
,
139 static void encode_decode_range(void)
141 uint8_t *buffer
= g_malloc0(PAGE_SIZE
);
142 uint8_t *compressed
= g_malloc(PAGE_SIZE
);
143 uint8_t *test
= g_malloc0(PAGE_SIZE
);
147 int diff_len
= g_test_rand_int_range(0, PAGE_SIZE
- 1006);
149 for (i
= diff_len
; i
> 0; i
--) {
150 buffer
[1000 + i
] = i
;
151 test
[1000 + i
] = i
+ 4;
154 buffer
[1000 + diff_len
+ 3] = 103;
155 test
[1000 + diff_len
+ 3] = 107;
157 buffer
[1000 + diff_len
+ 5] = 105;
158 test
[1000 + diff_len
+ 5] = 109;
160 /* test encode/decode */
161 dlen
= xbzrle_encode_buffer(test
, buffer
, PAGE_SIZE
, compressed
,
164 rc
= xbzrle_decode_buffer(compressed
, dlen
, test
, PAGE_SIZE
);
165 g_assert(rc
< PAGE_SIZE
);
166 g_assert(memcmp(test
, buffer
, PAGE_SIZE
) == 0);
173 static void test_encode_decode(void)
177 for (i
= 0; i
< 10000; i
++) {
178 encode_decode_range();
182 int main(int argc
, char **argv
)
184 g_test_init(&argc
, &argv
, NULL
);
186 g_test_add_func("/xbzrle/uleb", test_uleb
);
187 g_test_add_func("/xbzrle/encode_decode_zero", test_encode_decode_zero
);
188 g_test_add_func("/xbzrle/encode_decode_unchanged",
189 test_encode_decode_unchanged
);
190 g_test_add_func("/xbzrle/encode_decode_1_byte", test_encode_decode_1_byte
);
191 g_test_add_func("/xbzrle/encode_decode_overflow",
192 test_encode_decode_overflow
);
193 g_test_add_func("/xbzrle/encode_decode", test_encode_decode
);