seabios-hppa: update to latest version
[qemu/ar7.git] / tests / test-xbzrle.c
blobf5e08de91ecf94426d691d66d7b8006bfcaff651
1 /*
2 * Xor Based Zero Run Length Encoding unit tests.
4 * Copyright 2013 Red Hat, Inc. and/or its affiliates
6 * Authors:
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 PAGE_SIZE 4096
20 static void test_uleb(void)
22 uint32_t i, val;
23 uint8_t buf[2];
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);
30 g_assert(i == val);
33 /* decode invalid value */
34 buf[0] = 0x80;
35 buf[1] = 0x80;
37 decode_ret = uleb128_decode_small(&buf[0], &val);
38 g_assert(decode_ret == -1);
39 g_assert(val == 0);
42 static void test_encode_decode_zero(void)
44 uint8_t *buffer = g_malloc0(PAGE_SIZE);
45 uint8_t *compressed = g_malloc0(PAGE_SIZE);
46 int i = 0;
47 int dlen = 0;
48 int diff_len = g_test_rand_int_range(0, PAGE_SIZE - 1006);
50 for (i = diff_len; i > 0; i--) {
51 buffer[1000 + i] = 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, PAGE_SIZE, compressed,
59 PAGE_SIZE);
60 g_assert(dlen == 0);
62 g_free(buffer);
63 g_free(compressed);
66 static void test_encode_decode_unchanged(void)
68 uint8_t *compressed = g_malloc0(PAGE_SIZE);
69 uint8_t *test = g_malloc0(PAGE_SIZE);
70 int i = 0;
71 int dlen = 0;
72 int diff_len = g_test_rand_int_range(0, 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, PAGE_SIZE, compressed,
83 PAGE_SIZE);
84 g_assert(dlen == 0);
86 g_free(test);
87 g_free(compressed);
90 static void test_encode_decode_1_byte(void)
92 uint8_t *buffer = g_malloc0(PAGE_SIZE);
93 uint8_t *test = g_malloc0(PAGE_SIZE);
94 uint8_t *compressed = g_malloc(PAGE_SIZE);
95 int dlen = 0, rc = 0;
96 uint8_t buf[2];
98 test[PAGE_SIZE - 1] = 1;
100 dlen = xbzrle_encode_buffer(buffer, test, PAGE_SIZE, compressed,
101 PAGE_SIZE);
102 g_assert(dlen == (uleb128_encode_small(&buf[0], 4095) + 2));
104 rc = xbzrle_decode_buffer(compressed, dlen, buffer, PAGE_SIZE);
105 g_assert(rc == PAGE_SIZE);
106 g_assert(memcmp(test, buffer, PAGE_SIZE) == 0);
108 g_free(buffer);
109 g_free(compressed);
110 g_free(test);
113 static void test_encode_decode_overflow(void)
115 uint8_t *compressed = g_malloc0(PAGE_SIZE);
116 uint8_t *test = g_malloc0(PAGE_SIZE);
117 uint8_t *buffer = g_malloc0(PAGE_SIZE);
118 int i = 0, rc = 0;
120 for (i = 0; i < PAGE_SIZE / 2 - 1; i++) {
121 test[i * 2] = 1;
124 /* encode overflow */
125 rc = xbzrle_encode_buffer(buffer, test, PAGE_SIZE, compressed,
126 PAGE_SIZE);
127 g_assert(rc == -1);
129 g_free(buffer);
130 g_free(compressed);
131 g_free(test);
134 static void encode_decode_range(void)
136 uint8_t *buffer = g_malloc0(PAGE_SIZE);
137 uint8_t *compressed = g_malloc(PAGE_SIZE);
138 uint8_t *test = g_malloc0(PAGE_SIZE);
139 int i = 0, rc = 0;
140 int dlen = 0;
142 int diff_len = g_test_rand_int_range(0, 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, PAGE_SIZE, compressed,
157 PAGE_SIZE);
159 rc = xbzrle_decode_buffer(compressed, dlen, test, PAGE_SIZE);
160 g_assert(rc < PAGE_SIZE);
161 g_assert(memcmp(test, buffer, PAGE_SIZE) == 0);
163 g_free(buffer);
164 g_free(compressed);
165 g_free(test);
168 static void test_encode_decode(void)
170 int i;
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);
180 g_test_rand_int();
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);
190 return g_test_run();