diff options
author | gatecat <gatecat@ds0.me> | 2021-02-24 17:01:19 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-24 17:01:19 +0000 |
commit | 32db04a11077e7a32adc6f3d473e5cbefe83ff0a (patch) | |
tree | b8ecd0a3838c230a91492d5622c8de2003fc4c69 /fpga_interchange | |
parent | 0162fd062f58600c46919356fc7aac21339fcf80 (diff) | |
parent | 9b7fc3ea52a20dd6a05d90b6f332fa001dd1c615 (diff) | |
download | nextpnr-tests-32db04a11077e7a32adc6f3d473e5cbefe83ff0a.tar.gz nextpnr-tests-32db04a11077e7a32adc6f3d473e5cbefe83ff0a.tar.bz2 nextpnr-tests-32db04a11077e7a32adc6f3d473e5cbefe83ff0a.zip |
Merge pull request #6 from litghost/format_and_bitarray
Add tests for dynamic bitarray and format bits.c
Diffstat (limited to 'fpga_interchange')
-rw-r--r-- | fpga_interchange/bits.cc | 17 | ||||
-rw-r--r-- | fpga_interchange/dynamic_bitarray.cc | 100 |
2 files changed, 109 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<unsigned int>::digits; ++i) { + 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); + 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) { + 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); + 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); } } -}; +}; // namespace nextpnr diff --git a/fpga_interchange/dynamic_bitarray.cc b/fpga_interchange/dynamic_bitarray.cc new file mode 100644 index 0000000..6925769 --- /dev/null +++ b/fpga_interchange/dynamic_bitarray.cc @@ -0,0 +1,100 @@ +/* + * 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" +#include <climits> + +namespace nextpnr { + +class DynamicBitarrayTest : public ::testing::Test +{ +}; + +TEST_F(DynamicBitarrayTest, oneshot) +{ + for (size_t i = 0; i < 100; ++i) { + std::vector<uint8_t> 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) { + 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<uint8_t> 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 |