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