hw/lm32/milkymist.c: Fix misusing qemu_allocate_irqs for single irq
[qemu/ar7.git] / tests / test-xbzrle.c
blobdb93b0a3d2a20192c1f675ff6b9dc64329d16bac
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 <strings.h>
17 #include <string.h>
18 #include <sys/time.h>
19 #include <assert.h>
20 #include "qemu-common.h"
21 #include "include/migration/migration.h"
23 #define PAGE_SIZE 4096
25 static void test_uleb(void)
27 uint32_t i, val;
28 uint8_t buf[2];
29 int encode_ret, decode_ret;
31 for (i = 0; i <= 0x3fff; i++) {
32 encode_ret = uleb128_encode_small(&buf[0], i);
33 decode_ret = uleb128_decode_small(&buf[0], &val);
34 g_assert(encode_ret == decode_ret);
35 g_assert(i == val);
38 /* decode invalid value */
39 buf[0] = 0x80;
40 buf[1] = 0x80;
42 decode_ret = uleb128_decode_small(&buf[0], &val);
43 g_assert(decode_ret == -1);
44 g_assert(val == 0);
47 static void test_encode_decode_zero(void)
49 uint8_t *buffer = g_malloc0(PAGE_SIZE);
50 uint8_t *compressed = g_malloc0(PAGE_SIZE);
51 int i = 0;
52 int dlen = 0;
53 int diff_len = g_test_rand_int_range(0, PAGE_SIZE - 1006);
55 for (i = diff_len; i > 0; i--) {
56 buffer[1000 + i] = i;
59 buffer[1000 + diff_len + 3] = 103;
60 buffer[1000 + diff_len + 5] = 105;
62 /* encode zero page */
63 dlen = xbzrle_encode_buffer(buffer, buffer, PAGE_SIZE, compressed,
64 PAGE_SIZE);
65 g_assert(dlen == 0);
67 g_free(buffer);
68 g_free(compressed);
71 static void test_encode_decode_unchanged(void)
73 uint8_t *compressed = g_malloc0(PAGE_SIZE);
74 uint8_t *test = g_malloc0(PAGE_SIZE);
75 int i = 0;
76 int dlen = 0;
77 int diff_len = g_test_rand_int_range(0, PAGE_SIZE - 1006);
79 for (i = diff_len; i > 0; i--) {
80 test[1000 + i] = i + 4;
83 test[1000 + diff_len + 3] = 107;
84 test[1000 + diff_len + 5] = 109;
86 /* test unchanged buffer */
87 dlen = xbzrle_encode_buffer(test, test, PAGE_SIZE, compressed,
88 PAGE_SIZE);
89 g_assert(dlen == 0);
91 g_free(test);
92 g_free(compressed);
95 static void test_encode_decode_1_byte(void)
97 uint8_t *buffer = g_malloc0(PAGE_SIZE);
98 uint8_t *test = g_malloc0(PAGE_SIZE);
99 uint8_t *compressed = g_malloc(PAGE_SIZE);
100 int dlen = 0, rc = 0;
101 uint8_t buf[2];
103 test[PAGE_SIZE - 1] = 1;
105 dlen = xbzrle_encode_buffer(buffer, test, PAGE_SIZE, compressed,
106 PAGE_SIZE);
107 g_assert(dlen == (uleb128_encode_small(&buf[0], 4095) + 2));
109 rc = xbzrle_decode_buffer(compressed, dlen, buffer, PAGE_SIZE);
110 g_assert(rc == PAGE_SIZE);
111 g_assert(memcmp(test, buffer, PAGE_SIZE) == 0);
113 g_free(buffer);
114 g_free(compressed);
115 g_free(test);
118 static void test_encode_decode_overflow(void)
120 uint8_t *compressed = g_malloc0(PAGE_SIZE);
121 uint8_t *test = g_malloc0(PAGE_SIZE);
122 uint8_t *buffer = g_malloc0(PAGE_SIZE);
123 int i = 0, rc = 0;
125 for (i = 0; i < PAGE_SIZE / 2 - 1; i++) {
126 test[i * 2] = 1;
129 /* encode overflow */
130 rc = xbzrle_encode_buffer(buffer, test, PAGE_SIZE, compressed,
131 PAGE_SIZE);
132 g_assert(rc == -1);
134 g_free(buffer);
135 g_free(compressed);
136 g_free(test);
139 static void encode_decode_range(void)
141 uint8_t *buffer = g_malloc0(PAGE_SIZE);
142 uint8_t *compressed = g_malloc(PAGE_SIZE);
143 uint8_t *test = g_malloc0(PAGE_SIZE);
144 int i = 0, rc = 0;
145 int dlen = 0;
147 int diff_len = g_test_rand_int_range(0, PAGE_SIZE - 1006);
149 for (i = diff_len; i > 0; i--) {
150 buffer[1000 + i] = i;
151 test[1000 + i] = i + 4;
154 buffer[1000 + diff_len + 3] = 103;
155 test[1000 + diff_len + 3] = 107;
157 buffer[1000 + diff_len + 5] = 105;
158 test[1000 + diff_len + 5] = 109;
160 /* test encode/decode */
161 dlen = xbzrle_encode_buffer(test, buffer, PAGE_SIZE, compressed,
162 PAGE_SIZE);
164 rc = xbzrle_decode_buffer(compressed, dlen, test, PAGE_SIZE);
165 g_assert(rc < PAGE_SIZE);
166 g_assert(memcmp(test, buffer, PAGE_SIZE) == 0);
168 g_free(buffer);
169 g_free(compressed);
170 g_free(test);
173 static void test_encode_decode(void)
175 int i;
177 for (i = 0; i < 10000; i++) {
178 encode_decode_range();
182 int main(int argc, char **argv)
184 g_test_init(&argc, &argv, NULL);
185 g_test_rand_int();
186 g_test_add_func("/xbzrle/uleb", test_uleb);
187 g_test_add_func("/xbzrle/encode_decode_zero", test_encode_decode_zero);
188 g_test_add_func("/xbzrle/encode_decode_unchanged",
189 test_encode_decode_unchanged);
190 g_test_add_func("/xbzrle/encode_decode_1_byte", test_encode_decode_1_byte);
191 g_test_add_func("/xbzrle/encode_decode_overflow",
192 test_encode_decode_overflow);
193 g_test_add_func("/xbzrle/encode_decode", test_encode_decode);
195 return g_test_run();