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/osdep.h" 12 #include "qemu/bitops.h" 13 14 typedef struct { 15 uint32_t value; 16 int start; 17 int length; 18 int32_t result; 19 } S32Test; 20 21 typedef struct { 22 uint64_t value; 23 int start; 24 int length; 25 int64_t result; 26 } S64Test; 27 28 static const S32Test test_s32_data[] = { 29 { 0x38463983, 4, 4, -8 }, 30 { 0x38463983, 12, 8, 0x63 }, 31 { 0x38463983, 0, 32, 0x38463983 }, 32 }; 33 34 static const S64Test test_s64_data[] = { 35 { 0x8459826734967223ULL, 60, 4, -8 }, 36 { 0x8459826734967223ULL, 0, 64, 0x8459826734967223LL }, 37 }; 38 39 static void test_sextract32(void) 40 { 41 int i; 42 43 for (i = 0; i < ARRAY_SIZE(test_s32_data); i++) { 44 const S32Test *test = &test_s32_data[i]; 45 int32_t r = sextract32(test->value, test->start, test->length); 46 47 g_assert_cmpint(r, ==, test->result); 48 } 49 } 50 51 static void test_sextract64(void) 52 { 53 int i; 54 55 for (i = 0; i < ARRAY_SIZE(test_s32_data); i++) { 56 const S32Test *test = &test_s32_data[i]; 57 int64_t r = sextract64(test->value, test->start, test->length); 58 59 g_assert_cmpint(r, ==, test->result); 60 } 61 62 for (i = 0; i < ARRAY_SIZE(test_s64_data); i++) { 63 const S64Test *test = &test_s64_data[i]; 64 int64_t r = sextract64(test->value, test->start, test->length); 65 66 g_assert_cmpint(r, ==, test->result); 67 } 68 } 69 70 int main(int argc, char **argv) 71 { 72 g_test_init(&argc, &argv, NULL); 73 g_test_add_func("/bitops/sextract32", test_sextract32); 74 g_test_add_func("/bitops/sextract64", test_sextract64); 75 return g_test_run(); 76 } 77