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.
13 #include "qemu/osdep.h"
14 #include "qemu-common.h"
15 #include "qemu/cutils.h"
16 #include "../migration/xbzrle.h"
18 #define XBZRLE_PAGE_SIZE 4096
20 static void test_uleb(void)
24 int encode_ret
, decode_ret
;
26 for (i
= 0; i
<= 0x3fff; i
++) {
27 encode_ret
= uleb128_encode_small(&buf
[0], i
);
28 decode_ret
= uleb128_decode_small(&buf
[0], &val
);
29 g_assert(encode_ret
== decode_ret
);
33 /* decode invalid value */
37 decode_ret
= uleb128_decode_small(&buf
[0], &val
);
38 g_assert(decode_ret
== -1);
42 static void test_encode_decode_zero(void)
44 uint8_t *buffer
= g_malloc0(XBZRLE_PAGE_SIZE
);
45 uint8_t *compressed
= g_malloc0(XBZRLE_PAGE_SIZE
);
48 int diff_len
= g_test_rand_int_range(0, XBZRLE_PAGE_SIZE
- 1006);
50 for (i
= diff_len
; i
> 0; i
--) {
54 buffer
[1000 + diff_len
+ 3] = 103;
55 buffer
[1000 + diff_len
+ 5] = 105;
57 /* encode zero page */
58 dlen
= xbzrle_encode_buffer(buffer
, buffer
, XBZRLE_PAGE_SIZE
, compressed
,
66 static void test_encode_decode_unchanged(void)
68 uint8_t *compressed
= g_malloc0(XBZRLE_PAGE_SIZE
);
69 uint8_t *test
= g_malloc0(XBZRLE_PAGE_SIZE
);
72 int diff_len
= g_test_rand_int_range(0, XBZRLE_PAGE_SIZE
- 1006);
74 for (i
= diff_len
; i
> 0; i
--) {
75 test
[1000 + i
] = i
+ 4;
78 test
[1000 + diff_len
+ 3] = 107;
79 test
[1000 + diff_len
+ 5] = 109;
81 /* test unchanged buffer */
82 dlen
= xbzrle_encode_buffer(test
, test
, XBZRLE_PAGE_SIZE
, compressed
,
90 static void test_encode_decode_1_byte(void)
92 uint8_t *buffer
= g_malloc0(XBZRLE_PAGE_SIZE
);
93 uint8_t *test
= g_malloc0(XBZRLE_PAGE_SIZE
);
94 uint8_t *compressed
= g_malloc(XBZRLE_PAGE_SIZE
);
98 test
[XBZRLE_PAGE_SIZE
- 1] = 1;
100 dlen
= xbzrle_encode_buffer(buffer
, test
, XBZRLE_PAGE_SIZE
, compressed
,
102 g_assert(dlen
== (uleb128_encode_small(&buf
[0], 4095) + 2));
104 rc
= xbzrle_decode_buffer(compressed
, dlen
, buffer
, XBZRLE_PAGE_SIZE
);
105 g_assert(rc
== XBZRLE_PAGE_SIZE
);
106 g_assert(memcmp(test
, buffer
, XBZRLE_PAGE_SIZE
) == 0);
113 static void test_encode_decode_overflow(void)
115 uint8_t *compressed
= g_malloc0(XBZRLE_PAGE_SIZE
);
116 uint8_t *test
= g_malloc0(XBZRLE_PAGE_SIZE
);
117 uint8_t *buffer
= g_malloc0(XBZRLE_PAGE_SIZE
);
120 for (i
= 0; i
< XBZRLE_PAGE_SIZE
/ 2 - 1; i
++) {
124 /* encode overflow */
125 rc
= xbzrle_encode_buffer(buffer
, test
, XBZRLE_PAGE_SIZE
, compressed
,
134 static void encode_decode_range(void)
136 uint8_t *buffer
= g_malloc0(XBZRLE_PAGE_SIZE
);
137 uint8_t *compressed
= g_malloc(XBZRLE_PAGE_SIZE
);
138 uint8_t *test
= g_malloc0(XBZRLE_PAGE_SIZE
);
142 int diff_len
= g_test_rand_int_range(0, XBZRLE_PAGE_SIZE
- 1006);
144 for (i
= diff_len
; i
> 0; i
--) {
145 buffer
[1000 + i
] = i
;
146 test
[1000 + i
] = i
+ 4;
149 buffer
[1000 + diff_len
+ 3] = 103;
150 test
[1000 + diff_len
+ 3] = 107;
152 buffer
[1000 + diff_len
+ 5] = 105;
153 test
[1000 + diff_len
+ 5] = 109;
155 /* test encode/decode */
156 dlen
= xbzrle_encode_buffer(test
, buffer
, XBZRLE_PAGE_SIZE
, compressed
,
159 rc
= xbzrle_decode_buffer(compressed
, dlen
, test
, XBZRLE_PAGE_SIZE
);
160 g_assert(rc
< XBZRLE_PAGE_SIZE
);
161 g_assert(memcmp(test
, buffer
, XBZRLE_PAGE_SIZE
) == 0);
168 static void test_encode_decode(void)
172 for (i
= 0; i
< 10000; i
++) {
173 encode_decode_range();
177 int main(int argc
, char **argv
)
179 g_test_init(&argc
, &argv
, NULL
);
181 g_test_add_func("/xbzrle/uleb", test_uleb
);
182 g_test_add_func("/xbzrle/encode_decode_zero", test_encode_decode_zero
);
183 g_test_add_func("/xbzrle/encode_decode_unchanged",
184 test_encode_decode_unchanged
);
185 g_test_add_func("/xbzrle/encode_decode_1_byte", test_encode_decode_1_byte
);
186 g_test_add_func("/xbzrle/encode_decode_overflow",
187 test_encode_decode_overflow
);
188 g_test_add_func("/xbzrle/encode_decode", test_encode_decode
);