aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/test-bitops.c
diff options
context:
space:
mode:
authorPeter Maydell <peter.maydell@linaro.org>2021-03-14 15:13:53 +0000
committerPeter Maydell <peter.maydell@linaro.org>2021-03-14 15:13:53 +0000
commit757acb9a8295e8be4a37b2cfc1cd947e357fd29c (patch)
tree881fdcb812a8b8d067d5cb59832b3bb31ce9bcf9 /tests/unit/test-bitops.c
parent6f8a81fc296535f73c48cf9563862e088cc71c57 (diff)
parent33bf47291ed575847d7de26b503c50e72f5aa6c3 (diff)
Merge remote-tracking branch 'remotes/thuth-gitlab/tags/pull-request-2021-03-12' into staging
* Move unit and bench tests into separate directories * Clean-up and improve gitlab-ci jobs * Drop the non-working "check-speed" makefile target * Minor documentation updates # gpg: Signature made Fri 12 Mar 2021 17:18:45 GMT # gpg: using RSA key 27B88847EEE0250118F3EAB92ED9D774FE702DB5 # gpg: issuer "thuth@redhat.com" # gpg: Good signature from "Thomas Huth <th.huth@gmx.de>" [full] # gpg: aka "Thomas Huth <thuth@redhat.com>" [full] # gpg: aka "Thomas Huth <huth@tuxfamily.org>" [full] # gpg: aka "Thomas Huth <th.huth@posteo.de>" [unknown] # Primary key fingerprint: 27B8 8847 EEE0 2501 18F3 EAB9 2ED9 D774 FE70 2DB5 * remotes/thuth-gitlab/tags/pull-request-2021-03-12: README: Add Documentation blurb MAINTAINERS: Merge the Gitlab-CI section into the generic CI section tests: remove "make check-speed" in favor of "make bench" gitlab-ci.yml: Merge check-crypto-old jobs into the build-crypto-old jobs gitlab-ci.yml: Merge one of the coroutine jobs with the tcg-disabled job gitlab-ci.yml: Add some missing dependencies to the jobs gitlab-ci.yml: Move build-tools-and-docs-debian to a better place tests: Move benchmarks into a separate folder tests: Move unit tests into a separate directory Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Diffstat (limited to 'tests/unit/test-bitops.c')
-rw-r--r--tests/unit/test-bitops.c146
1 files changed, 146 insertions, 0 deletions
diff --git a/tests/unit/test-bitops.c b/tests/unit/test-bitops.c
new file mode 100644
index 0000000000..5a791d2e17
--- /dev/null
+++ b/tests/unit/test-bitops.c
@@ -0,0 +1,146 @@
+/*
+ * 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 "qemu/osdep.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[] = {
+ { 0x8459826734967223ULL, 60, 4, -8 },
+ { 0x8459826734967223ULL, 0, 64, 0x8459826734967223LL },
+};
+
+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);
+ }
+}
+
+typedef struct {
+ uint32_t unshuffled;
+ uint32_t shuffled;
+} Shuffle32Test;
+
+typedef struct {
+ uint64_t unshuffled;
+ uint64_t shuffled;
+} Shuffle64Test;
+
+static const Shuffle32Test test_shuffle32_data[] = {
+ { 0x0000FFFF, 0x55555555 },
+ { 0x000081C5, 0x40015011 },
+};
+
+static const Shuffle64Test test_shuffle64_data[] = {
+ { 0x00000000FFFFFFFFULL, 0x5555555555555555ULL },
+ { 0x00000000493AB02CULL, 0x1041054445000450ULL },
+};
+
+static void test_half_shuffle32(void)
+{
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(test_shuffle32_data); i++) {
+ const Shuffle32Test *test = &test_shuffle32_data[i];
+ uint32_t r = half_shuffle32(test->unshuffled);
+
+ g_assert_cmpint(r, ==, test->shuffled);
+ }
+}
+
+static void test_half_shuffle64(void)
+{
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(test_shuffle64_data); i++) {
+ const Shuffle64Test *test = &test_shuffle64_data[i];
+ uint64_t r = half_shuffle64(test->unshuffled);
+
+ g_assert_cmpint(r, ==, test->shuffled);
+ }
+}
+
+static void test_half_unshuffle32(void)
+{
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(test_shuffle32_data); i++) {
+ const Shuffle32Test *test = &test_shuffle32_data[i];
+ uint32_t r = half_unshuffle32(test->shuffled);
+
+ g_assert_cmpint(r, ==, test->unshuffled);
+ }
+}
+
+static void test_half_unshuffle64(void)
+{
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(test_shuffle64_data); i++) {
+ const Shuffle64Test *test = &test_shuffle64_data[i];
+ uint64_t r = half_unshuffle64(test->shuffled);
+
+ g_assert_cmpint(r, ==, test->unshuffled);
+ }
+}
+
+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);
+ g_test_add_func("/bitops/half_shuffle32", test_half_shuffle32);
+ g_test_add_func("/bitops/half_shuffle64", test_half_shuffle64);
+ g_test_add_func("/bitops/half_unshuffle32", test_half_unshuffle32);
+ g_test_add_func("/bitops/half_unshuffle64", test_half_unshuffle64);
+ return g_test_run();
+}