Skip to content

Commit

Permalink
rtlil: unit tests for Const
Browse files Browse the repository at this point in the history
  • Loading branch information
widlarizer committed Sep 30, 2024
1 parent 005a6fe commit 91f4cc4
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -916,8 +916,8 @@ ystests: $(TARGETS) $(EXTRA_TARGETS)

# Unit test
unit-test: libyosys.so
@$(MAKE) -C $(UNITESTPATH) CXX="$(CXX)" CPPFLAGS="$(CPPFLAGS)" \
CXXFLAGS="$(CXXFLAGS)" LIBS="$(LIBS)" ROOTPATH="$(CURDIR)"
@$(MAKE) -C $(UNITESTPATH) CXX="$(CXX)" CC="$(CC)" CPPFLAGS="$(CPPFLAGS)" \
CXXFLAGS="$(CXXFLAGS)" LINKFLAGS="$(LINKFLAGS)" LIBS="$(LIBS)" ROOTPATH="$(CURDIR)"

clean-unit-test:
@$(MAKE) -C $(UNITESTPATH) clean
Expand Down
2 changes: 2 additions & 0 deletions kernel/rtlil.h
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,8 @@ struct RTLIL::Const
{
short flags;
private:
friend class KernelRtlilTest;
FRIEND_TEST(KernelRtlilTest, ConstStr);
using bitvectype = std::vector<RTLIL::State>;
enum class backing_tag: bool { bits, string };
// Do not access the union or tag even in Const methods unless necessary
Expand Down
2 changes: 2 additions & 0 deletions kernel/yosys_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
#include <sys/stat.h>
#include <errno.h>

#include <gtest/gtest_prod.h>

Check failure on line 53 in kernel/yosys_common.h

View workflow job for this annotation

GitHub Actions / test-compile (ubuntu-latest, clang-14)

'gtest/gtest_prod.h' file not found

Check failure on line 53 in kernel/yosys_common.h

View workflow job for this annotation

GitHub Actions / test-compile (ubuntu-latest, gcc-10)

gtest/gtest_prod.h: No such file or directory

Check failure on line 53 in kernel/yosys_common.h

View workflow job for this annotation

GitHub Actions / test-compile (ubuntu-latest, gcc-10)

gtest/gtest_prod.h: No such file or directory

Check failure on line 53 in kernel/yosys_common.h

View workflow job for this annotation

GitHub Actions / test-compile (ubuntu-latest, gcc-10)

gtest/gtest_prod.h: No such file or directory

Check failure on line 53 in kernel/yosys_common.h

View workflow job for this annotation

GitHub Actions / test-compile (ubuntu-latest, gcc-10)

gtest/gtest_prod.h: No such file or directory

Check failure on line 53 in kernel/yosys_common.h

View workflow job for this annotation

GitHub Actions / test-compile (ubuntu-latest, clang)

'gtest/gtest_prod.h' file not found

Check failure on line 53 in kernel/yosys_common.h

View workflow job for this annotation

GitHub Actions / test-compile (ubuntu-latest, gcc)

gtest/gtest_prod.h: No such file or directory

Check failure on line 53 in kernel/yosys_common.h

View workflow job for this annotation

GitHub Actions / test-compile (ubuntu-latest, gcc)

gtest/gtest_prod.h: No such file or directory

Check failure on line 53 in kernel/yosys_common.h

View workflow job for this annotation

GitHub Actions / test-compile (ubuntu-latest, gcc)

gtest/gtest_prod.h: No such file or directory

Check failure on line 53 in kernel/yosys_common.h

View workflow job for this annotation

GitHub Actions / test-compile (ubuntu-latest, gcc)

gtest/gtest_prod.h: No such file or directory

Check failure on line 53 in kernel/yosys_common.h

View workflow job for this annotation

GitHub Actions / test-compile (macos-13, clang)

'gtest/gtest_prod.h' file not found

Check failure on line 53 in kernel/yosys_common.h

View workflow job for this annotation

GitHub Actions / test-compile (macos-13, clang)

'gtest/gtest_prod.h' file not found

Check failure on line 53 in kernel/yosys_common.h

View workflow job for this annotation

GitHub Actions / test-compile (macos-13, clang)

'gtest/gtest_prod.h' file not found

Check failure on line 53 in kernel/yosys_common.h

View workflow job for this annotation

GitHub Actions / test-compile (macos-13, clang)

'gtest/gtest_prod.h' file not found

Check failure on line 53 in kernel/yosys_common.h

View workflow job for this annotation

GitHub Actions / test-compile (ubuntu-20.04, clang-10)

'gtest/gtest_prod.h' file not found

Check failure on line 53 in kernel/yosys_common.h

View workflow job for this annotation

GitHub Actions / test-compile (ubuntu-20.04, clang-10)

'gtest/gtest_prod.h' file not found

Check failure on line 53 in kernel/yosys_common.h

View workflow job for this annotation

GitHub Actions / test-compile (ubuntu-20.04, clang-10)

'gtest/gtest_prod.h' file not found

Check failure on line 53 in kernel/yosys_common.h

View workflow job for this annotation

GitHub Actions / test-compile (ubuntu-20.04, clang-10)

'gtest/gtest_prod.h' file not found

#ifdef WITH_PYTHON
#include <Python.h>
#endif
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ TESTS := $(addprefix $(BINTEST)/, $(basename $(ALLTESTFILE:%Test.cc=%Test.o)))
all: prepare $(TESTS) run-tests

$(BINTEST)/%: $(OBJTEST)/%.o
$(CXX) -L$(ROOTPATH) $(RPATH)=$(ROOTPATH) -o $@ $^ $(LIBS) \
$(CXX) -L$(ROOTPATH) $(RPATH)=$(ROOTPATH) $(LINKFLAGS) -o $@ $^ $(LIBS) \
$(GTESTFLAG) $(EXTRAFLAGS)

$(OBJTEST)/%.o: $(basename $(subst $(OBJTEST),.,%)).cc
Expand Down
77 changes: 71 additions & 6 deletions tests/unit/kernel/rtlilTest.cc
Original file line number Diff line number Diff line change
@@ -1,14 +1,79 @@
#include <gtest/gtest.h>

#include "kernel/yosys.h"
#include "kernel/rtlil.h"

YOSYS_NAMESPACE_BEGIN

TEST(KernelRtlilTest, getReferenceValid)
{
//TODO: Implement rtlil test
EXPECT_EQ(33, 33);
namespace RTLIL {

class KernelRtlilTest : public testing::Test {};

TEST_F(KernelRtlilTest, ConstAssignCompare)
{
Const c1;
Const c2;
c2 = c1;
Const c3(c2);
EXPECT_TRUE(c2 == c3);
EXPECT_FALSE(c2 < c3);
}

TEST_F(KernelRtlilTest, ConstStr) {
// We have multiple distinct sections since it's annoying
// to list multiple testcases as friends of Const in kernel/rtlil.h
{
std::string foo = "foo";
Const c1 = foo;
Const c2;
c2 = c1;
Const c3(c2);
EXPECT_TRUE(c1.is_str());
EXPECT_TRUE(c2.is_str());
EXPECT_TRUE(c3.is_str());
}

{
// A binary constant is bitvec backed
Const cb1(0, 10);
Const cb2(1, 10);
Const cb3(cb2);
std::vector<bool> v1 {false, true};
std::vector<State> v2 {State::S0, State::S1};
Const cb4(v1);
Const cb5(v2);
EXPECT_TRUE(cb4 == cb5);
EXPECT_TRUE(cb1.is_bits());
EXPECT_TRUE(cb2.is_bits());
EXPECT_TRUE(cb3.is_bits());
EXPECT_TRUE(cb4.is_bits());
EXPECT_TRUE(cb5.is_bits());
EXPECT_EQ(cb1.size(), 10);
EXPECT_EQ(cb2.size(), 10);
EXPECT_EQ(cb3.size(), 10);
}

{
// A string constructed Const starts off packed
std::string foo = "foo";
Const cs1 = foo;
EXPECT_TRUE(cs1.is_str());

// It can be iterated without mutating
int i = 0;
for (auto bit : cs1) {
i += bit;
}
EXPECT_EQ(i, 16);
EXPECT_TRUE(cs1.is_str());

// It can be mutated with the bits() view
// and decays into unpacked
for (auto& bit : cs1.bits()) {
bit = State::Sx;
}
EXPECT_TRUE(cs1.is_bits());
}

}
}

YOSYS_NAMESPACE_END

0 comments on commit 91f4cc4

Please sign in to comment.