Statistics
| Branch: | Revision:

root / tests / test-bitops.c @ 164a101f

History | View | Annotate | Download (1.7 kB)

1 3464700f Peter Maydell
/*
2 3464700f Peter Maydell
 * Test bitops routines
3 3464700f Peter Maydell
 *
4 3464700f Peter Maydell
 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
5 3464700f Peter Maydell
 * See the COPYING.LIB file in the top-level directory.
6 3464700f Peter Maydell
 *
7 3464700f Peter Maydell
 */
8 3464700f Peter Maydell
9 3464700f Peter Maydell
#include <glib.h>
10 3464700f Peter Maydell
#include <stdint.h>
11 3464700f Peter Maydell
#include "qemu/bitops.h"
12 3464700f Peter Maydell
13 3464700f Peter Maydell
typedef struct {
14 3464700f Peter Maydell
    uint32_t value;
15 3464700f Peter Maydell
    int start;
16 3464700f Peter Maydell
    int length;
17 3464700f Peter Maydell
    int32_t result;
18 3464700f Peter Maydell
} S32Test;
19 3464700f Peter Maydell
20 3464700f Peter Maydell
typedef struct {
21 3464700f Peter Maydell
    uint64_t value;
22 3464700f Peter Maydell
    int start;
23 3464700f Peter Maydell
    int length;
24 3464700f Peter Maydell
    int64_t result;
25 3464700f Peter Maydell
} S64Test;
26 3464700f Peter Maydell
27 3464700f Peter Maydell
static const S32Test test_s32_data[] = {
28 3464700f Peter Maydell
    { 0x38463983, 4, 4, -8 },
29 3464700f Peter Maydell
    { 0x38463983, 12, 8, 0x63 },
30 3464700f Peter Maydell
    { 0x38463983, 0, 32, 0x38463983 },
31 3464700f Peter Maydell
};
32 3464700f Peter Maydell
33 3464700f Peter Maydell
static const S64Test test_s64_data[] = {
34 3464700f Peter Maydell
    { 0x8459826734967223, 60, 4, -8 },
35 3464700f Peter Maydell
    { 0x8459826734967223, 0, 64, 0x8459826734967223 },
36 3464700f Peter Maydell
};
37 3464700f Peter Maydell
38 3464700f Peter Maydell
static void test_sextract32(void)
39 3464700f Peter Maydell
{
40 3464700f Peter Maydell
    int i;
41 3464700f Peter Maydell
42 3464700f Peter Maydell
    for (i = 0; i < ARRAY_SIZE(test_s32_data); i++) {
43 3464700f Peter Maydell
        const S32Test *test = &test_s32_data[i];
44 3464700f Peter Maydell
        int32_t r = sextract32(test->value, test->start, test->length);
45 3464700f Peter Maydell
46 3464700f Peter Maydell
        g_assert_cmpint(r, ==, test->result);
47 3464700f Peter Maydell
    }
48 3464700f Peter Maydell
}
49 3464700f Peter Maydell
50 3464700f Peter Maydell
static void test_sextract64(void)
51 3464700f Peter Maydell
{
52 3464700f Peter Maydell
    int i;
53 3464700f Peter Maydell
54 3464700f Peter Maydell
    for (i = 0; i < ARRAY_SIZE(test_s32_data); i++) {
55 3464700f Peter Maydell
        const S32Test *test = &test_s32_data[i];
56 3464700f Peter Maydell
        int64_t r = sextract64(test->value, test->start, test->length);
57 3464700f Peter Maydell
58 3464700f Peter Maydell
        g_assert_cmpint(r, ==, test->result);
59 3464700f Peter Maydell
    }
60 3464700f Peter Maydell
61 3464700f Peter Maydell
    for (i = 0; i < ARRAY_SIZE(test_s64_data); i++) {
62 3464700f Peter Maydell
        const S64Test *test = &test_s64_data[i];
63 3464700f Peter Maydell
        int64_t r = sextract64(test->value, test->start, test->length);
64 3464700f Peter Maydell
65 3464700f Peter Maydell
        g_assert_cmpint(r, ==, test->result);
66 3464700f Peter Maydell
    }
67 3464700f Peter Maydell
}
68 3464700f Peter Maydell
69 3464700f Peter Maydell
int main(int argc, char **argv)
70 3464700f Peter Maydell
{
71 3464700f Peter Maydell
    g_test_init(&argc, &argv, NULL);
72 3464700f Peter Maydell
    g_test_add_func("/bitops/sextract32", test_sextract32);
73 3464700f Peter Maydell
    g_test_add_func("/bitops/sextract64", test_sextract64);
74 3464700f Peter Maydell
    return g_test_run();
75 3464700f Peter Maydell
}