aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorgatecat <gatecat@ds0.me>2021-02-17 20:01:44 +0000
committerGitHub <noreply@github.com>2021-02-17 20:01:44 +0000
commit0162fd062f58600c46919356fc7aac21339fcf80 (patch)
tree92b8e4d9a3aa960ae51a60d8c9456237f1c925ae
parent2aa02a28d4078c616d96400973d3b5e4cf1e454c (diff)
parent7abc346be25775ea4b00003c364bb36533960097 (diff)
downloadnextpnr-tests-0162fd062f58600c46919356fc7aac21339fcf80.tar.gz
nextpnr-tests-0162fd062f58600c46919356fc7aac21339fcf80.tar.bz2
nextpnr-tests-0162fd062f58600c46919356fc7aac21339fcf80.zip
Merge pull request #5 from litghost/add_bits_test
Add tests for new Bits library.
-rw-r--r--fpga_interchange/bits.cc57
1 files changed, 57 insertions, 0 deletions
diff --git a/fpga_interchange/bits.cc b/fpga_interchange/bits.cc
new file mode 100644
index 0000000..49a4c28
--- /dev/null
+++ b/fpga_interchange/bits.cc
@@ -0,0 +1,57 @@
+/*
+ * nextpnr -- Next Generation Place and Route
+ *
+ * Copyright (C) 2021 Symbiflow Authors
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ */
+
+#include "gtest/gtest.h"
+
+#include "nextpnr.h"
+#include "bits.h"
+
+namespace nextpnr {
+
+class BitsTest : public ::testing::Test
+{
+};
+
+TEST_F(BitsTest, popcount)
+{
+ ASSERT_EQ(Bits::popcount(0), 0);
+ ASSERT_EQ(Bits::generic_popcount(0), 0);
+ for(size_t i = 0; i < std::numeric_limits<unsigned int>::digits; ++i) {
+ ASSERT_EQ(Bits::popcount(1 << i), 1);
+ ASSERT_EQ(Bits::generic_popcount(1 << i), 1);
+ }
+
+ ASSERT_EQ(Bits::popcount(std::numeric_limits<unsigned int>::max()), std::numeric_limits<unsigned int>::digits);
+ ASSERT_EQ(Bits::generic_popcount(std::numeric_limits<unsigned int>::max()), std::numeric_limits<unsigned int>::digits);
+}
+
+TEST_F(BitsTest, ctz)
+{
+ for(size_t i = 0; i < std::numeric_limits<unsigned int>::digits; ++i) {
+ ASSERT_EQ(Bits::ctz(1 << i), i);
+ ASSERT_EQ(Bits::generic_ctz(1 << i), i);
+ }
+
+ for(size_t i = 0; i < std::numeric_limits<unsigned int>::digits-1; ++i) {
+ ASSERT_EQ(Bits::ctz((1 << i) | (1 << (i+1))), i);
+ ASSERT_EQ(Bits::generic_ctz((1 << i) | (1 << (i+1))), i);
+ }
+}
+
+};