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.
18 #include "qemu-common.h"
19 #include "include/migration/migration.h"
21 #define PAGE_SIZE 4096
23 static void test_uleb(void)
27 int encode_ret
, decode_ret
;
29 for (i
= 0; i
<= 0x3fff; i
++) {
30 encode_ret
= uleb128_encode_small(&buf
[0], i
);
31 decode_ret
= uleb128_decode_small(&buf
[0], &val
);
32 g_assert(encode_ret
== decode_ret
);
36 /* decode invalid value */
40 decode_ret
= uleb128_decode_small(&buf
[0], &val
);
41 g_assert(decode_ret
== -1);
45 static void test_encode_decode_zero(void)
47 uint8_t *buffer
= g_malloc0(PAGE_SIZE
);
48 uint8_t *compressed
= g_malloc0(PAGE_SIZE
);
51 int diff_len
= g_test_rand_int_range(0, PAGE_SIZE
- 1006);
53 for (i
= diff_len
; i
> 0; i
--) {
57 buffer
[1000 + diff_len
+ 3] = 103;
58 buffer
[1000 + diff_len
+ 5] = 105;
60 /* encode zero page */
61 dlen
= xbzrle_encode_buffer(buffer
, buffer
, PAGE_SIZE
, compressed
,
69 static void test_encode_decode_unchanged(void)
71 uint8_t *compressed
= g_malloc0(PAGE_SIZE
);
72 uint8_t *test
= g_malloc0(PAGE_SIZE
);
75 int diff_len
= g_test_rand_int_range(0, PAGE_SIZE
- 1006);
77 for (i
= diff_len
; i
> 0; i
--) {
78 test
[1000 + i
] = i
+ 4;
81 test
[1000 + diff_len
+ 3] = 107;
82 test
[1000 + diff_len
+ 5] = 109;
84 /* test unchanged buffer */
85 dlen
= xbzrle_encode_buffer(test
, test
, PAGE_SIZE
, compressed
,
93 static void test_encode_decode_1_byte(void)
95 uint8_t *buffer
= g_malloc0(PAGE_SIZE
);
96 uint8_t *test
= g_malloc0(PAGE_SIZE
);
97 uint8_t *compressed
= g_malloc(PAGE_SIZE
);
101 test
[PAGE_SIZE
- 1] = 1;
103 dlen
= xbzrle_encode_buffer(buffer
, test
, PAGE_SIZE
, compressed
,
105 g_assert(dlen
== (uleb128_encode_small(&buf
[0], 4095) + 2));
107 rc
= xbzrle_decode_buffer(compressed
, dlen
, buffer
, PAGE_SIZE
);
108 g_assert(rc
== PAGE_SIZE
);
109 g_assert(memcmp(test
, buffer
, PAGE_SIZE
) == 0);
116 static void test_encode_decode_overflow(void)
118 uint8_t *compressed
= g_malloc0(PAGE_SIZE
);
119 uint8_t *test
= g_malloc0(PAGE_SIZE
);
120 uint8_t *buffer
= g_malloc0(PAGE_SIZE
);
123 for (i
= 0; i
< PAGE_SIZE
/ 2 - 1; i
++) {
127 /* encode overflow */
128 rc
= xbzrle_encode_buffer(buffer
, test
, PAGE_SIZE
, compressed
,
137 static void encode_decode_range(void)
139 uint8_t *buffer
= g_malloc0(PAGE_SIZE
);
140 uint8_t *compressed
= g_malloc(PAGE_SIZE
);
141 uint8_t *test
= g_malloc0(PAGE_SIZE
);
145 int diff_len
= g_test_rand_int_range(0, PAGE_SIZE
- 1006);
147 for (i
= diff_len
; i
> 0; i
--) {
148 buffer
[1000 + i
] = i
;
149 test
[1000 + i
] = i
+ 4;
152 buffer
[1000 + diff_len
+ 3] = 103;
153 test
[1000 + diff_len
+ 3] = 107;
155 buffer
[1000 + diff_len
+ 5] = 105;
156 test
[1000 + diff_len
+ 5] = 109;
158 /* test encode/decode */
159 dlen
= xbzrle_encode_buffer(test
, buffer
, PAGE_SIZE
, compressed
,
162 rc
= xbzrle_decode_buffer(compressed
, dlen
, test
, PAGE_SIZE
);
163 g_assert(rc
< PAGE_SIZE
);
164 g_assert(memcmp(test
, buffer
, PAGE_SIZE
) == 0);
171 static void test_encode_decode(void)
175 for (i
= 0; i
< 10000; i
++) {
176 encode_decode_range();
180 int main(int argc
, char **argv
)
182 g_test_init(&argc
, &argv
, NULL
);
184 g_test_add_func("/xbzrle/uleb", test_uleb
);
185 g_test_add_func("/xbzrle/encode_decode_zero", test_encode_decode_zero
);
186 g_test_add_func("/xbzrle/encode_decode_unchanged",
187 test_encode_decode_unchanged
);
188 g_test_add_func("/xbzrle/encode_decode_1_byte", test_encode_decode_1_byte
);
189 g_test_add_func("/xbzrle/encode_decode_overflow",
190 test_encode_decode_overflow
);
191 g_test_add_func("/xbzrle/encode_decode", test_encode_decode
);