From e0152319f5ed6d99eb38bf6da40157a60fd48e04 Mon Sep 17 00:00:00 2001 From: rodrigosiqueira Date: Sun, 4 Dec 2016 11:28:25 -0200 Subject: [PATCH 1/6] Added required structure to implement unit tests Added modifications inside the main Makefile to refers the unit test Makefile. Added separated Makefile only for compiling unit tests. Added simple example of unit test. Signed-off-by: Charles Oliveira <18oliveira.charles@gmail.com> Signed-off-by: Pablo Alejandro Signed-off-by: Rodrigo Siqueira --- .gitignore | 3 +++ Makefile | 14 ++++++++++++++ tests/unit/Makefile | 28 ++++++++++++++++++++++++++++ tests/unit/kernel/logTest.cc | 14 ++++++++++++++ tests/unit/kernel/rtlilTest.cc | 14 ++++++++++++++ 5 files changed, 73 insertions(+) create mode 100644 tests/unit/Makefile create mode 100644 tests/unit/kernel/logTest.cc create mode 100644 tests/unit/kernel/rtlilTest.cc diff --git a/.gitignore b/.gitignore index 93e28cd6..cd624f23 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ *.o *.d .*.swp +*.gch /.cproject /.project /.settings @@ -27,3 +28,5 @@ /yosys-win32-vcxsrc-* /yosysjs-* /libyosys.so +/tests/unit/bintest/ +/tests/unit/objtest/ diff --git a/Makefile b/Makefile index 9bf67d34..340fe122 100644 --- a/Makefile +++ b/Makefile @@ -45,6 +45,9 @@ TARGETS = yosys$(EXE) yosys-config PRETTY = 1 SMALL = 0 +# Unit test +UNITESTPATH := tests/unit + all: top-all YOSYS_SRC := $(dir $(firstword $(MAKEFILE_LIST))) @@ -447,6 +450,17 @@ vloghtb: $(TARGETS) $(EXTRA_TARGETS) @echo " Passed \"make vloghtb\"." @echo "" +# Unit test +unit-test: libyosys.so + @$(MAKE) -C $(UNITESTPATH) CXX="$(CXX)" CPPFLAGS="$(CPPFLAGS)" \ + CXXFLAGS="$(CXXFLAGS)" LDLIBS="$(LDLIBS)" ROOTPATH="$(CURDIR)" + +run-all-unitest: + @$(MAKE) -C $(UNITESTPATH) run-tests + +clean-unit-test: + @$(MAKE) -C $(UNITESTPATH) clean + install: $(TARGETS) $(EXTRA_TARGETS) $(INSTALL_SUDO) mkdir -p $(DESTDIR)$(BINDIR) $(INSTALL_SUDO) install $(TARGETS) $(DESTDIR)$(BINDIR) diff --git a/tests/unit/Makefile b/tests/unit/Makefile new file mode 100644 index 00000000..16f65ec2 --- /dev/null +++ b/tests/unit/Makefile @@ -0,0 +1,28 @@ +GTESTFLAG := -lgtest -lgtest_main +RPATH := -Wl,-rpath +EXTRAFLAGS := -lyosys + +ALLTESTFILE := $(wildcard ./**/*Test.cc) +OBJTEST := objtest +BINTEST := bintest + +all: prepare $(ALLTESTFILE:%Test.cc=%Test.o) + +%Test.o: %Test.cc + $(CXX) -o $(OBJTEST)/$(notdir $@) -c -I$(ROOTPATH) $(CPPFLAGS) $(CXXFLAGS) $< + $(CXX) -L$(ROOTPATH) $(RPATH)=$(ROOTPATH) -o \ + $(BINTEST)/$(basename $(notdir $@)) $(OBJTEST)/$(notdir $@) $(LDLIBS) \ + $(GTESTFLAG) $(EXTRAFLAGS) + +.PHONY: prepare run-tests clean + +run-tests: + $(CURDIR)/$(BINTEST)/* + +prepare: + mkdir -p $(OBJTEST) + mkdir -p $(BINTEST) + +clean: + rm -rf $(OBJTEST) + rm -rf $(BINTEST) diff --git a/tests/unit/kernel/logTest.cc b/tests/unit/kernel/logTest.cc new file mode 100644 index 00000000..62b4f3b9 --- /dev/null +++ b/tests/unit/kernel/logTest.cc @@ -0,0 +1,14 @@ +#include + +#include "kernel/yosys.h" +#include "kernel/log.h" + +YOSYS_NAMESPACE_BEGIN + +TEST(KernelLogTest, logvValidValues) +{ + //TODO: Implement log test + EXPECT_EQ(7, 7); +} + +YOSYS_NAMESPACE_END diff --git a/tests/unit/kernel/rtlilTest.cc b/tests/unit/kernel/rtlilTest.cc new file mode 100644 index 00000000..d9eeed55 --- /dev/null +++ b/tests/unit/kernel/rtlilTest.cc @@ -0,0 +1,14 @@ +#include + +#include "kernel/yosys.h" +#include "kernel/rtlil.h" + +YOSYS_NAMESPACE_BEGIN + +TEST(KernelRtlilTest, getReferenceValid) +{ + //TODO: Implement rtlil test + EXPECT_EQ(33, 33); +} + +YOSYS_NAMESPACE_END From 3f2f64f41475550527a5621f57d0b0d2b30ee179 Mon Sep 17 00:00:00 2001 From: rodrigosiqueira Date: Sun, 4 Dec 2016 11:35:13 -0200 Subject: [PATCH 2/6] Added explanation about configure and create test Added explanation about configure unit test environment and how to add new unit tests --- CodingReadme | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/CodingReadme b/CodingReadme index cbe1fb8b..d2e975da 100644 --- a/CodingReadme +++ b/CodingReadme @@ -411,3 +411,78 @@ Updating the website: git commit -am update make push +How to add unit test +==================== + +Unit test brings some advantages, briefly, we can list some of them (reference +[1](https://en.wikipedia.org/wiki/Unit_testing)): + +* Tests reduce bugs in new features; +* Tests reduce bugs in existing features; +* Tests are good documentation; +* Tests reduce the cost of change; +* Tests allow refactoring; + +With those advantages in mind, it was required to choose a framework which fits +well with C/C++ code. Hence, it was chosen (google test) +[https://github.com/google/googletest], because it is largely used and it is +relatively easy learn. + +Install and configure google test (manually) +============================================ + +In this section, you will see a brief description of how to install google +test. However, it is strongly recommended that you take a look to the official +repository (https://github.com/google/googletest) and refers to that if you +have any problem to install it. Follow the steps below: + +* Install: cmake +* Clone google test project from: //github.com/rodrigosiqueira/logbook.git and + enter in the project directory +* Inside project directory, type: + +``` +cmake -DBUILD_SHARED_LIBS=ON . +make +``` + +* After compilation, copy all "*.so" inside directory "googlemock" and + "googlemock/gtest" to "/usr/lib/" +* Done! Now you can compile your tests. + +If you have any problem, go to the official repository to find help. + +Ps.: Some distros already have googletest packed. If your distro supports it, +you can use it instead of compile. + +Create new unit test +======================= + +If you want to add new unit tests for Yosys, just follow the steps below: + +* Go to directory "yosys/test/unit/" +* In this directory you can find something similar Yosys's directory structure. + To create your unit test file you have to follow this pattern: + fileNameToImplementUnitTest + Test.cc. E.g.: if you want to implement the + unit test for kernel/celledges.cc, you will need to create a file like this: + tests/unit/kernel/celledgesTest.cc; +* Implement your unit test +* If you want to compile your tests, just go to yosys root directory and type: +``` +make unit-test +``` + +Run unit test +============= + +To run all unit tests, you need to compile it first and then run it. Follow the +steps below (from the yosys root directory): +``` +make unit-test +make run-all-unitest +``` + +If you want to remove all unit test files, type: +``` +make clean-unit-test +``` From b932e2355de3d2d3b7a61fe86d11265ba02aba2a Mon Sep 17 00:00:00 2001 From: rodrigosiqueira Date: Sat, 10 Dec 2016 18:21:56 -0200 Subject: [PATCH 3/6] Improved unit test structure Signed-off-by: rodrigosiqueira Signed-off-by: chaws <18oliveira.charles@gmail.com> * Merged run-all-unitest inside unit-test target * Fixed Makefile dependencies * Updated documentation about unit test --- CodingReadme | 4 ++-- Makefile | 3 --- tests/unit/Makefile | 29 ++++++++++++++++++----------- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/CodingReadme b/CodingReadme index d2e975da..041f3b1f 100644 --- a/CodingReadme +++ b/CodingReadme @@ -436,8 +436,8 @@ test. However, it is strongly recommended that you take a look to the official repository (https://github.com/google/googletest) and refers to that if you have any problem to install it. Follow the steps below: -* Install: cmake -* Clone google test project from: //github.com/rodrigosiqueira/logbook.git and +* Install: cmake and pthread +* Clone google test project from: https://github.com/google/googletest and enter in the project directory * Inside project directory, type: diff --git a/Makefile b/Makefile index 340fe122..cf40309c 100644 --- a/Makefile +++ b/Makefile @@ -455,9 +455,6 @@ unit-test: libyosys.so @$(MAKE) -C $(UNITESTPATH) CXX="$(CXX)" CPPFLAGS="$(CPPFLAGS)" \ CXXFLAGS="$(CXXFLAGS)" LDLIBS="$(LDLIBS)" ROOTPATH="$(CURDIR)" -run-all-unitest: - @$(MAKE) -C $(UNITESTPATH) run-tests - clean-unit-test: @$(MAKE) -C $(UNITESTPATH) clean diff --git a/tests/unit/Makefile b/tests/unit/Makefile index 16f65ec2..447a5f61 100644 --- a/tests/unit/Makefile +++ b/tests/unit/Makefile @@ -2,26 +2,33 @@ GTESTFLAG := -lgtest -lgtest_main RPATH := -Wl,-rpath EXTRAFLAGS := -lyosys -ALLTESTFILE := $(wildcard ./**/*Test.cc) OBJTEST := objtest BINTEST := bintest -all: prepare $(ALLTESTFILE:%Test.cc=%Test.o) +ALLTESTFILE := $(shell find -name '*Test.cc' -printf '%P ') +TESTDIRS := $(sort $(dir $(ALLTESTFILE))) +TESTS := $(addprefix $(BINTEST)/, $(basename $(ALLTESTFILE:%Test.cc=%Test.o))) -%Test.o: %Test.cc - $(CXX) -o $(OBJTEST)/$(notdir $@) -c -I$(ROOTPATH) $(CPPFLAGS) $(CXXFLAGS) $< - $(CXX) -L$(ROOTPATH) $(RPATH)=$(ROOTPATH) -o \ - $(BINTEST)/$(basename $(notdir $@)) $(OBJTEST)/$(notdir $@) $(LDLIBS) \ +# Prevent make from removing our .o files +.SECONDARY: + +all: prepare $(TESTS) run-tests + +$(BINTEST)/%: $(OBJTEST)/%.o + $(CXX) -L$(ROOTPATH) $(RPATH)=$(ROOTPATH) -o $@ $^ $(LDLIBS) \ $(GTESTFLAG) $(EXTRAFLAGS) - + +$(OBJTEST)/%.o: $(basename $(subst $(OBJTEST),.,%)).cc + $(CXX) -o $@ -c -I$(ROOTPATH) $(CPPFLAGS) $(CXXFLAGS) $^ + .PHONY: prepare run-tests clean -run-tests: - $(CURDIR)/$(BINTEST)/* +run-tests: $(TESTS) + $(subst Test ,Test; ,$^) prepare: - mkdir -p $(OBJTEST) - mkdir -p $(BINTEST) + mkdir -p $(addprefix $(BINTEST)/,$(TESTDIRS)) + mkdir -p $(addprefix $(OBJTEST)/,$(TESTDIRS)) clean: rm -rf $(OBJTEST) From 5c96982522779fef5b63bcc16297e6633dafff03 Mon Sep 17 00:00:00 2001 From: Clifford Wolf Date: Sun, 11 Dec 2016 10:58:49 +0100 Subject: [PATCH 4/6] Build hotfix in tests/unit/Makefile --- tests/unit/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/Makefile b/tests/unit/Makefile index 447a5f61..9f1e5c99 100644 --- a/tests/unit/Makefile +++ b/tests/unit/Makefile @@ -1,6 +1,6 @@ GTESTFLAG := -lgtest -lgtest_main RPATH := -Wl,-rpath -EXTRAFLAGS := -lyosys +EXTRAFLAGS := -lyosys -pthreads OBJTEST := objtest BINTEST := bintest From 71c47f13ed15d4635a408832d69f0cfb6b35443e Mon Sep 17 00:00:00 2001 From: Clifford Wolf Date: Sun, 11 Dec 2016 11:02:56 +0100 Subject: [PATCH 5/6] Some minor CodingReadme changes in unit test section --- CodingReadme | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/CodingReadme b/CodingReadme index 041f3b1f..9e85add2 100644 --- a/CodingReadme +++ b/CodingReadme @@ -429,7 +429,7 @@ well with C/C++ code. Hence, it was chosen (google test) relatively easy learn. Install and configure google test (manually) -============================================ +-------------------------------------------- In this section, you will see a brief description of how to install google test. However, it is strongly recommended that you take a look to the official @@ -456,7 +456,7 @@ Ps.: Some distros already have googletest packed. If your distro supports it, you can use it instead of compile. Create new unit test -======================= +-------------------- If you want to add new unit tests for Yosys, just follow the steps below: @@ -467,19 +467,13 @@ If you want to add new unit tests for Yosys, just follow the steps below: unit test for kernel/celledges.cc, you will need to create a file like this: tests/unit/kernel/celledgesTest.cc; * Implement your unit test -* If you want to compile your tests, just go to yosys root directory and type: -``` -make unit-test -``` Run unit test -============= +------------- -To run all unit tests, you need to compile it first and then run it. Follow the -steps below (from the yosys root directory): +To compile and run all unit tests, just go to yosys root directory and type: ``` make unit-test -make run-all-unitest ``` If you want to remove all unit test files, type: From a61c88f12215eb8bfa1db62aec7c2c95bb3cc702 Mon Sep 17 00:00:00 2001 From: Clifford Wolf Date: Sun, 11 Dec 2016 13:48:18 +0100 Subject: [PATCH 6/6] Added $anyconst support to AIGER back-end --- backends/aiger/aiger.cc | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/backends/aiger/aiger.cc b/backends/aiger/aiger.cc index aefe5cf4..ab1fba6f 100644 --- a/backends/aiger/aiger.cc +++ b/backends/aiger/aiger.cc @@ -163,6 +163,13 @@ struct AigerWriter continue; } + if (cell->type == "$anyconst") + { + for (auto bit : sigmap(cell->getPort("\\Y"))) + ff_map[bit] = bit; + continue; + } + log_error("Unsupported cell type: %s (%s)\n", log_id(cell->type), log_id(cell)); }