tests: Add test-bitops.c with some sextract tests

Add some simple test cases for the new sextract32
and sextract64 functions.

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Richard Henderson <rth@twiddle.net>
Message-id: 1372419632-5521-3-git-send-email-peter.maydell@linaro.org
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
This commit is contained in:
Peter Maydell 2013-06-28 12:40:32 +01:00 committed by Anthony Liguori
parent 2dc6bebde9
commit 3464700f6a
2 changed files with 77 additions and 0 deletions

View File

@ -47,6 +47,7 @@ gcov-files-test-mul64-y = util/host-utils.c
check-unit-y += tests/test-int128$(EXESUF)
# all code tested by test-int128 is inside int128.h
gcov-files-test-int128-y =
check-unit-y += tests/test-bitops$(EXESUF)
check-block-$(CONFIG_POSIX) += tests/qemu-iotests-quick.sh
@ -125,6 +126,7 @@ tests/test-qmp-commands$(EXESUF): tests/test-qmp-commands.o tests/test-qmp-marsh
tests/test-visitor-serialization$(EXESUF): tests/test-visitor-serialization.o $(test-qapi-obj-y) libqemuutil.a libqemustub.a
tests/test-mul64$(EXESUF): tests/test-mul64.o libqemuutil.a
tests/test-bitops$(EXESUF): tests/test-bitops.o libqemuutil.a
libqos-obj-y = tests/libqos/pci.o tests/libqos/fw_cfg.o
libqos-obj-y += tests/libqos/i2c.o

75
tests/test-bitops.c Normal file
View File

@ -0,0 +1,75 @@
/*
* Test bitops routines
*
* This work is licensed under the terms of the GNU LGPL, version 2 or later.
* See the COPYING.LIB file in the top-level directory.
*
*/
#include <glib.h>
#include <stdint.h>
#include "qemu/bitops.h"
typedef struct {
uint32_t value;
int start;
int length;
int32_t result;
} S32Test;
typedef struct {
uint64_t value;
int start;
int length;
int64_t result;
} S64Test;
static const S32Test test_s32_data[] = {
{ 0x38463983, 4, 4, -8 },
{ 0x38463983, 12, 8, 0x63 },
{ 0x38463983, 0, 32, 0x38463983 },
};
static const S64Test test_s64_data[] = {
{ 0x8459826734967223, 60, 4, -8 },
{ 0x8459826734967223, 0, 64, 0x8459826734967223 },
};
static void test_sextract32(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(test_s32_data); i++) {
const S32Test *test = &test_s32_data[i];
int32_t r = sextract32(test->value, test->start, test->length);
g_assert_cmpint(r, ==, test->result);
}
}
static void test_sextract64(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(test_s32_data); i++) {
const S32Test *test = &test_s32_data[i];
int64_t r = sextract64(test->value, test->start, test->length);
g_assert_cmpint(r, ==, test->result);
}
for (i = 0; i < ARRAY_SIZE(test_s64_data); i++) {
const S64Test *test = &test_s64_data[i];
int64_t r = sextract64(test->value, test->start, test->length);
g_assert_cmpint(r, ==, test->result);
}
}
int main(int argc, char **argv)
{
g_test_init(&argc, &argv, NULL);
g_test_add_func("/bitops/sextract32", test_sextract32);
g_test_add_func("/bitops/sextract64", test_sextract64);
return g_test_run();
}