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