vhost: move virtio 1.0 check to cross-endian helper
[qemu/kevin.git] / tests / test-xbzrle.c
blob1cd8cb7d788bc8a121f2cf1d2909908f8477af48
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 <stdint.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/time.h>
18 #include "qemu-common.h"
19 #include "include/migration/migration.h"
21 #define PAGE_SIZE 4096
23 static void test_uleb(void)
25 uint32_t i, val;
26 uint8_t buf[2];
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);
33 g_assert(i == val);
36 /* decode invalid value */
37 buf[0] = 0x80;
38 buf[1] = 0x80;
40 decode_ret = uleb128_decode_small(&buf[0], &val);
41 g_assert(decode_ret == -1);
42 g_assert(val == 0);
45 static void test_encode_decode_zero(void)
47 uint8_t *buffer = g_malloc0(PAGE_SIZE);
48 uint8_t *compressed = g_malloc0(PAGE_SIZE);
49 int i = 0;
50 int dlen = 0;
51 int diff_len = g_test_rand_int_range(0, PAGE_SIZE - 1006);
53 for (i = diff_len; i > 0; i--) {
54 buffer[1000 + i] = 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,
62 PAGE_SIZE);
63 g_assert(dlen == 0);
65 g_free(buffer);
66 g_free(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);
73 int i = 0;
74 int dlen = 0;
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,
86 PAGE_SIZE);
87 g_assert(dlen == 0);
89 g_free(test);
90 g_free(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);
98 int dlen = 0, rc = 0;
99 uint8_t buf[2];
101 test[PAGE_SIZE - 1] = 1;
103 dlen = xbzrle_encode_buffer(buffer, test, PAGE_SIZE, compressed,
104 PAGE_SIZE);
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);
111 g_free(buffer);
112 g_free(compressed);
113 g_free(test);
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);
121 int i = 0, rc = 0;
123 for (i = 0; i < PAGE_SIZE / 2 - 1; i++) {
124 test[i * 2] = 1;
127 /* encode overflow */
128 rc = xbzrle_encode_buffer(buffer, test, PAGE_SIZE, compressed,
129 PAGE_SIZE);
130 g_assert(rc == -1);
132 g_free(buffer);
133 g_free(compressed);
134 g_free(test);
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);
142 int i = 0, rc = 0;
143 int dlen = 0;
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,
160 PAGE_SIZE);
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);
166 g_free(buffer);
167 g_free(compressed);
168 g_free(test);
171 static void test_encode_decode(void)
173 int i;
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);
183 g_test_rand_int();
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);
193 return g_test_run();