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 "include/migration/migration.h"
17 #define PAGE_SIZE 4096
19 static void test_uleb(void)
23 int encode_ret
, decode_ret
;
25 for (i
= 0; i
<= 0x3fff; i
++) {
26 encode_ret
= uleb128_encode_small(&buf
[0], i
);
27 decode_ret
= uleb128_decode_small(&buf
[0], &val
);
28 g_assert(encode_ret
== decode_ret
);
32 /* decode invalid value */
36 decode_ret
= uleb128_decode_small(&buf
[0], &val
);
37 g_assert(decode_ret
== -1);
41 static void test_encode_decode_zero(void)
43 uint8_t *buffer
= g_malloc0(PAGE_SIZE
);
44 uint8_t *compressed
= g_malloc0(PAGE_SIZE
);
47 int diff_len
= g_test_rand_int_range(0, PAGE_SIZE
- 1006);
49 for (i
= diff_len
; i
> 0; i
--) {
53 buffer
[1000 + diff_len
+ 3] = 103;
54 buffer
[1000 + diff_len
+ 5] = 105;
56 /* encode zero page */
57 dlen
= xbzrle_encode_buffer(buffer
, buffer
, PAGE_SIZE
, compressed
,
65 static void test_encode_decode_unchanged(void)
67 uint8_t *compressed
= g_malloc0(PAGE_SIZE
);
68 uint8_t *test
= g_malloc0(PAGE_SIZE
);
71 int diff_len
= g_test_rand_int_range(0, PAGE_SIZE
- 1006);
73 for (i
= diff_len
; i
> 0; i
--) {
74 test
[1000 + i
] = i
+ 4;
77 test
[1000 + diff_len
+ 3] = 107;
78 test
[1000 + diff_len
+ 5] = 109;
80 /* test unchanged buffer */
81 dlen
= xbzrle_encode_buffer(test
, test
, PAGE_SIZE
, compressed
,
89 static void test_encode_decode_1_byte(void)
91 uint8_t *buffer
= g_malloc0(PAGE_SIZE
);
92 uint8_t *test
= g_malloc0(PAGE_SIZE
);
93 uint8_t *compressed
= g_malloc(PAGE_SIZE
);
97 test
[PAGE_SIZE
- 1] = 1;
99 dlen
= xbzrle_encode_buffer(buffer
, test
, PAGE_SIZE
, compressed
,
101 g_assert(dlen
== (uleb128_encode_small(&buf
[0], 4095) + 2));
103 rc
= xbzrle_decode_buffer(compressed
, dlen
, buffer
, PAGE_SIZE
);
104 g_assert(rc
== PAGE_SIZE
);
105 g_assert(memcmp(test
, buffer
, PAGE_SIZE
) == 0);
112 static void test_encode_decode_overflow(void)
114 uint8_t *compressed
= g_malloc0(PAGE_SIZE
);
115 uint8_t *test
= g_malloc0(PAGE_SIZE
);
116 uint8_t *buffer
= g_malloc0(PAGE_SIZE
);
119 for (i
= 0; i
< PAGE_SIZE
/ 2 - 1; i
++) {
123 /* encode overflow */
124 rc
= xbzrle_encode_buffer(buffer
, test
, PAGE_SIZE
, compressed
,
133 static void encode_decode_range(void)
135 uint8_t *buffer
= g_malloc0(PAGE_SIZE
);
136 uint8_t *compressed
= g_malloc(PAGE_SIZE
);
137 uint8_t *test
= g_malloc0(PAGE_SIZE
);
141 int diff_len
= g_test_rand_int_range(0, PAGE_SIZE
- 1006);
143 for (i
= diff_len
; i
> 0; i
--) {
144 buffer
[1000 + i
] = i
;
145 test
[1000 + i
] = i
+ 4;
148 buffer
[1000 + diff_len
+ 3] = 103;
149 test
[1000 + diff_len
+ 3] = 107;
151 buffer
[1000 + diff_len
+ 5] = 105;
152 test
[1000 + diff_len
+ 5] = 109;
154 /* test encode/decode */
155 dlen
= xbzrle_encode_buffer(test
, buffer
, PAGE_SIZE
, compressed
,
158 rc
= xbzrle_decode_buffer(compressed
, dlen
, test
, PAGE_SIZE
);
159 g_assert(rc
< PAGE_SIZE
);
160 g_assert(memcmp(test
, buffer
, PAGE_SIZE
) == 0);
167 static void test_encode_decode(void)
171 for (i
= 0; i
< 10000; i
++) {
172 encode_decode_range();
176 int main(int argc
, char **argv
)
178 g_test_init(&argc
, &argv
, NULL
);
180 g_test_add_func("/xbzrle/uleb", test_uleb
);
181 g_test_add_func("/xbzrle/encode_decode_zero", test_encode_decode_zero
);
182 g_test_add_func("/xbzrle/encode_decode_unchanged",
183 test_encode_decode_unchanged
);
184 g_test_add_func("/xbzrle/encode_decode_1_byte", test_encode_decode_1_byte
);
185 g_test_add_func("/xbzrle/encode_decode_overflow",
186 test_encode_decode_overflow
);
187 g_test_add_func("/xbzrle/encode_decode", test_encode_decode
);