From 1bf6e4d92dc58d53ab2e60809b13ce15295c5f4b Mon Sep 17 00:00:00 2001 From: Keith Rothman <537074+litghost@users.noreply.github.com> Date: Tue, 23 Feb 2021 15:44:47 -0800 Subject: Add DynamicBitarray unit tests. Signed-off-by: Keith Rothman <537074+litghost@users.noreply.github.com> --- fpga_interchange/dynamic_bitarray.cc | 96 ++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 fpga_interchange/dynamic_bitarray.cc diff --git a/fpga_interchange/dynamic_bitarray.cc b/fpga_interchange/dynamic_bitarray.cc new file mode 100644 index 0000000..c2e4d7a --- /dev/null +++ b/fpga_interchange/dynamic_bitarray.cc @@ -0,0 +1,96 @@ +/* + * 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 "dynamic_bitarray.h" + +namespace nextpnr { + +class DynamicBitarrayTest : public ::testing::Test +{ +}; + +TEST_F(DynamicBitarrayTest, oneshot) +{ + for (size_t i = 0; i < 100; ++i) { + std::vector simple_storage; + nextpnr::DynamicBitarray<> bitarray; + + simple_storage.resize(i); + bitarray.resize(i); + + for (size_t k = 0; k < 3; ++k) { + for (size_t j = 0; j < i; ++j) { + int value = rand() % 2; + simple_storage[j] = value == 1; + bitarray.set(j, value == 1); + } + + for (size_t j = 0; j < i; ++j) { + ASSERT_EQ(simple_storage[j], bitarray.get(j)); + } + } + } +} + +TEST_F(DynamicBitarrayTest, resize) +{ + std::vector simple_storage; + nextpnr::DynamicBitarray<> bitarray; + + for (size_t i = 0; i < 100; ++i) { + + simple_storage.resize(i); + bitarray.resize(i); + + for (size_t k = 0; k < 3; ++k) { + for (size_t j = 0; j < i; ++j) { + int value = rand() % 2; + simple_storage[j] = value == 1; + bitarray.set(j, value == 1); + } + + for (size_t j = 0; j < i; ++j) { + ASSERT_EQ(simple_storage[j], bitarray.get(j)); + } + } + } +} + +TEST_F(DynamicBitarrayTest, fill) +{ + nextpnr::DynamicBitarray<> bitarray; + + for (size_t i = 0; i < 100; ++i) { + bitarray.resize(i); + + bitarray.fill(true); + for (size_t j = 0; j < i; ++j) { + ASSERT_TRUE(bitarray.get(j)); + } + + bitarray.fill(false); + for (size_t j = 0; j < i; ++j) { + ASSERT_FALSE(bitarray.get(j)); + } + } +} + +}; // namespace nextpnr -- cgit v1.2.3 From cf3fb0b3b76a852c72b77a97aa201d198e8b6601 Mon Sep 17 00:00:00 2001 From: Keith Rothman <537074+litghost@users.noreply.github.com> Date: Tue, 23 Feb 2021 15:45:00 -0800 Subject: Run "make clangformat" on test files. Signed-off-by: Keith Rothman <537074+litghost@users.noreply.github.com> --- fpga_interchange/bits.cc | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/fpga_interchange/bits.cc b/fpga_interchange/bits.cc index 49a4c28..ede058c 100644 --- a/fpga_interchange/bits.cc +++ b/fpga_interchange/bits.cc @@ -19,8 +19,8 @@ #include "gtest/gtest.h" -#include "nextpnr.h" #include "bits.h" +#include "nextpnr.h" namespace nextpnr { @@ -32,26 +32,27 @@ 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::digits; ++i) { + for (size_t i = 0; i < std::numeric_limits::digits; ++i) { ASSERT_EQ(Bits::popcount(1 << i), 1); ASSERT_EQ(Bits::generic_popcount(1 << i), 1); } ASSERT_EQ(Bits::popcount(std::numeric_limits::max()), std::numeric_limits::digits); - ASSERT_EQ(Bits::generic_popcount(std::numeric_limits::max()), std::numeric_limits::digits); + ASSERT_EQ(Bits::generic_popcount(std::numeric_limits::max()), + std::numeric_limits::digits); } TEST_F(BitsTest, ctz) { - for(size_t i = 0; i < std::numeric_limits::digits; ++i) { + for (size_t i = 0; i < std::numeric_limits::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::digits-1; ++i) { - ASSERT_EQ(Bits::ctz((1 << i) | (1 << (i+1))), i); - ASSERT_EQ(Bits::generic_ctz((1 << i) | (1 << (i+1))), i); + for (size_t i = 0; i < std::numeric_limits::digits - 1; ++i) { + ASSERT_EQ(Bits::ctz((1 << i) | (1 << (i + 1))), i); + ASSERT_EQ(Bits::generic_ctz((1 << i) | (1 << (i + 1))), i); } } -}; +}; // namespace nextpnr -- cgit v1.2.3 From 9b7fc3ea52a20dd6a05d90b6f332fa001dd1c615 Mon Sep 17 00:00:00 2001 From: Keith Rothman <537074+litghost@users.noreply.github.com> Date: Wed, 24 Feb 2021 08:56:55 -0800 Subject: Add some tests around resizing and bits_per_value. Signed-off-by: Keith Rothman <537074+litghost@users.noreply.github.com> --- fpga_interchange/dynamic_bitarray.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fpga_interchange/dynamic_bitarray.cc b/fpga_interchange/dynamic_bitarray.cc index c2e4d7a..6925769 100644 --- a/fpga_interchange/dynamic_bitarray.cc +++ b/fpga_interchange/dynamic_bitarray.cc @@ -20,6 +20,7 @@ #include "gtest/gtest.h" #include "dynamic_bitarray.h" +#include namespace nextpnr { @@ -32,9 +33,12 @@ TEST_F(DynamicBitarrayTest, oneshot) for (size_t i = 0; i < 100; ++i) { std::vector simple_storage; nextpnr::DynamicBitarray<> bitarray; + ASSERT_EQ(bitarray.bits_per_value(), CHAR_BIT); simple_storage.resize(i); bitarray.resize(i); + ASSERT_LE(i, bitarray.size()); + ASSERT_LT(bitarray.size() - i, CHAR_BIT); for (size_t k = 0; k < 3; ++k) { for (size_t j = 0; j < i; ++j) { -- cgit v1.2.3