Imported Upstream version 1.2.4
[archive/platform/upstream/libvirt.git] / tests / virendiantest.c
1 /*
2  * Copyright (C) 2013 Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library.  If not, see
16  * <http://www.gnu.org/licenses/>.
17  *
18  */
19
20 #include <config.h>
21
22 #include "testutils.h"
23
24 #include "virendian.h"
25
26 static int
27 test1(const void *data ATTRIBUTE_UNUSED)
28 {
29     /* Regular char should work, even if signed, and even with
30      * unaligned access.  */
31     char array[] = { 1, 2, 3, 4, 5, 6, 7, 8,
32                      0x89, 0x8a, 0x8b, 0x8c, 0x8d };
33     int ret = -1;
34
35     if (virReadBufInt64BE(array) != 0x0102030405060708ULL)
36         goto cleanup;
37     if (virReadBufInt64BE(array + 5) != 0x060708898a8b8c8dULL)
38         goto cleanup;
39     if (virReadBufInt64LE(array) != 0x0807060504030201ULL)
40         goto cleanup;
41     if (virReadBufInt64LE(array + 5) != 0x8d8c8b8a89080706ULL)
42         goto cleanup;
43
44     if (virReadBufInt32BE(array) != 0x01020304U)
45         goto cleanup;
46     if (virReadBufInt32BE(array + 9) != 0x8a8b8c8dU)
47         goto cleanup;
48     if (virReadBufInt32LE(array) != 0x04030201U)
49         goto cleanup;
50     if (virReadBufInt32LE(array + 9) != 0x8d8c8b8aU)
51         goto cleanup;
52
53     ret = 0;
54  cleanup:
55     return ret;
56 }
57
58 static int
59 test2(const void *data ATTRIBUTE_UNUSED)
60 {
61     /* Unsigned char should work without cast, even if unaligned access.  */
62     unsigned char array[] = { 1, 2, 3, 4, 5, 6, 7, 8,
63                               0x89, 0x8a, 0x8b, 0x8c, 0x8d };
64     int ret = -1;
65
66     if (virReadBufInt64BE(array) != 0x0102030405060708ULL)
67         goto cleanup;
68     if (virReadBufInt64BE(array + 5) != 0x060708898a8b8c8dULL)
69         goto cleanup;
70     if (virReadBufInt64LE(array) != 0x0807060504030201ULL)
71         goto cleanup;
72     if (virReadBufInt64LE(array + 5) != 0x8d8c8b8a89080706ULL)
73         goto cleanup;
74
75     if (virReadBufInt32BE(array) != 0x01020304U)
76         goto cleanup;
77     if (virReadBufInt32BE(array + 9) != 0x8a8b8c8dU)
78         goto cleanup;
79     if (virReadBufInt32LE(array) != 0x04030201U)
80         goto cleanup;
81     if (virReadBufInt32LE(array + 9) != 0x8d8c8b8aU)
82         goto cleanup;
83
84     ret = 0;
85  cleanup:
86     return ret;
87 }
88
89 static int
90 mymain(void)
91 {
92     int ret = 0;
93
94     if (virtTestRun("test1", test1, NULL) < 0)
95         ret = -1;
96     if (virtTestRun("test2", test2, NULL) < 0)
97         ret = -1;
98
99     return ret;
100 }
101
102 VIRT_TEST_MAIN(mymain)