xref: /openbmc/qemu/tests/unit/test-bufferiszero.c (revision 51204c2f)
1 /*
2  * QEMU buffer_is_zero test
3  *
4  * Copyright (c) 2016 Red Hat, Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #include "qemu/osdep.h"
22 #include "qemu/cutils.h"
23 
24 static char buffer[8 * 1024 * 1024];
25 
test_1(void)26 static void test_1(void)
27 {
28     size_t s, a, o;
29 
30     /* Basic positive test.  */
31     g_assert(buffer_is_zero(buffer, sizeof(buffer)));
32 
33     /* Basic negative test.  */
34     buffer[sizeof(buffer) - 1] = 1;
35     g_assert(!buffer_is_zero(buffer, sizeof(buffer)));
36     buffer[sizeof(buffer) - 1] = 0;
37 
38     /* Positive tests for size and alignment.  */
39     for (a = 1; a <= 64; a++) {
40         for (s = 1; s < 1024; s++) {
41             buffer[a - 1] = 1;
42             buffer[a + s] = 1;
43             g_assert(buffer_is_zero(buffer + a, s));
44             buffer[a - 1] = 0;
45             buffer[a + s] = 0;
46         }
47     }
48 
49     /* Negative tests for size, alignment, and the offset of the marker.  */
50     for (a = 1; a <= 64; a++) {
51         for (s = 1; s < 1024; s++) {
52             for (o = 0; o < s; ++o) {
53                 buffer[a + o] = 1;
54                 g_assert(!buffer_is_zero(buffer + a, s));
55                 buffer[a + o] = 0;
56             }
57         }
58     }
59 }
60 
test_2(void)61 static void test_2(void)
62 {
63     if (g_test_perf()) {
64         test_1();
65     } else {
66         do {
67             test_1();
68         } while (test_buffer_is_zero_next_accel());
69     }
70 }
71 
main(int argc,char ** argv)72 int main(int argc, char **argv)
73 {
74     g_test_init(&argc, &argv, NULL);
75     g_test_add_func("/cutils/bufferiszero", test_2);
76 
77     return g_test_run();
78 }
79