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/dynamic_bitarray.cc | |
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/dynamic_bitarray.cc')
-rw-r--r-- | fpga_interchange/dynamic_bitarray.cc | 100 |
1 files changed, 100 insertions, 0 deletions
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 |