nvdimm acpi: let qemu handle _DSM method
[qemu/kevin.git] / tests / test-xbzrle.c
blobabd309d4181143faa690ab649c6f02b7d3de095f
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 "include/migration/migration.h"
17 #define PAGE_SIZE 4096
19 static void test_uleb(void)
21 uint32_t i, val;
22 uint8_t buf[2];
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);
29 g_assert(i == val);
32 /* decode invalid value */
33 buf[0] = 0x80;
34 buf[1] = 0x80;
36 decode_ret = uleb128_decode_small(&buf[0], &val);
37 g_assert(decode_ret == -1);
38 g_assert(val == 0);
41 static void test_encode_decode_zero(void)
43 uint8_t *buffer = g_malloc0(PAGE_SIZE);
44 uint8_t *compressed = g_malloc0(PAGE_SIZE);
45 int i = 0;
46 int dlen = 0;
47 int diff_len = g_test_rand_int_range(0, PAGE_SIZE - 1006);
49 for (i = diff_len; i > 0; i--) {
50 buffer[1000 + i] = 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,
58 PAGE_SIZE);
59 g_assert(dlen == 0);
61 g_free(buffer);
62 g_free(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);
69 int i = 0;
70 int dlen = 0;
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,
82 PAGE_SIZE);
83 g_assert(dlen == 0);
85 g_free(test);
86 g_free(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);
94 int dlen = 0, rc = 0;
95 uint8_t buf[2];
97 test[PAGE_SIZE - 1] = 1;
99 dlen = xbzrle_encode_buffer(buffer, test, PAGE_SIZE, compressed,
100 PAGE_SIZE);
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);
107 g_free(buffer);
108 g_free(compressed);
109 g_free(test);
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);
117 int i = 0, rc = 0;
119 for (i = 0; i < PAGE_SIZE / 2 - 1; i++) {
120 test[i * 2] = 1;
123 /* encode overflow */
124 rc = xbzrle_encode_buffer(buffer, test, PAGE_SIZE, compressed,
125 PAGE_SIZE);
126 g_assert(rc == -1);
128 g_free(buffer);
129 g_free(compressed);
130 g_free(test);
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);
138 int i = 0, rc = 0;
139 int dlen = 0;
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,
156 PAGE_SIZE);
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);
162 g_free(buffer);
163 g_free(compressed);
164 g_free(test);
167 static void test_encode_decode(void)
169 int i;
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);
179 g_test_rand_int();
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);
189 return g_test_run();