mirror of
				https://github.com/YosysHQ/yosys
				synced 2025-11-04 05:19:11 +00:00 
			
		
		
		
	Merge branch 'LSS-USP-unit-test-structure'
This commit is contained in:
		
						commit
						8a717ae1dc
					
				
					 6 changed files with 146 additions and 0 deletions
				
			
		
							
								
								
									
										3
									
								
								.gitignore
									
										
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								.gitignore
									
										
									
									
										vendored
									
									
								
							| 
						 | 
				
			
			@ -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/
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										69
									
								
								CodingReadme
									
										
									
									
									
								
							
							
						
						
									
										69
									
								
								CodingReadme
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -411,3 +411,72 @@ 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 and pthread
 | 
			
		||||
* Clone google test project from: https://github.com/google/googletest 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
 | 
			
		||||
 | 
			
		||||
Run unit test
 | 
			
		||||
-------------
 | 
			
		||||
 | 
			
		||||
To compile and run all unit tests, just go to yosys root directory and type:
 | 
			
		||||
```
 | 
			
		||||
make unit-test
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
If you want to remove all unit test files, type:
 | 
			
		||||
```
 | 
			
		||||
make clean-unit-test
 | 
			
		||||
```
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										11
									
								
								Makefile
									
										
									
									
									
								
							
							
						
						
									
										11
									
								
								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,14 @@ 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)"
 | 
			
		||||
 | 
			
		||||
clean-unit-test:
 | 
			
		||||
	@$(MAKE) -C $(UNITESTPATH) clean
 | 
			
		||||
 | 
			
		||||
install: $(TARGETS) $(EXTRA_TARGETS)
 | 
			
		||||
	$(INSTALL_SUDO) mkdir -p $(DESTDIR)$(BINDIR)
 | 
			
		||||
	$(INSTALL_SUDO) install $(TARGETS) $(DESTDIR)$(BINDIR)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										35
									
								
								tests/unit/Makefile
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								tests/unit/Makefile
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,35 @@
 | 
			
		|||
GTESTFLAG := -lgtest -lgtest_main
 | 
			
		||||
RPATH := -Wl,-rpath
 | 
			
		||||
EXTRAFLAGS := -lyosys -pthreads
 | 
			
		||||
 | 
			
		||||
OBJTEST := objtest
 | 
			
		||||
BINTEST := bintest
 | 
			
		||||
 | 
			
		||||
ALLTESTFILE := $(shell find -name '*Test.cc' -printf '%P ')
 | 
			
		||||
TESTDIRS := $(sort $(dir $(ALLTESTFILE)))
 | 
			
		||||
TESTS := $(addprefix $(BINTEST)/, $(basename $(ALLTESTFILE:%Test.cc=%Test.o)))
 | 
			
		||||
 | 
			
		||||
# 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: $(TESTS)
 | 
			
		||||
	$(subst Test ,Test; ,$^)
 | 
			
		||||
 | 
			
		||||
prepare:
 | 
			
		||||
	mkdir -p $(addprefix $(BINTEST)/,$(TESTDIRS))
 | 
			
		||||
	mkdir -p $(addprefix $(OBJTEST)/,$(TESTDIRS))
 | 
			
		||||
 | 
			
		||||
clean:
 | 
			
		||||
	rm -rf $(OBJTEST)
 | 
			
		||||
	rm -rf $(BINTEST)
 | 
			
		||||
							
								
								
									
										14
									
								
								tests/unit/kernel/logTest.cc
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								tests/unit/kernel/logTest.cc
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,14 @@
 | 
			
		|||
#include <gtest/gtest.h>
 | 
			
		||||
 | 
			
		||||
#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
 | 
			
		||||
							
								
								
									
										14
									
								
								tests/unit/kernel/rtlilTest.cc
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								tests/unit/kernel/rtlilTest.cc
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,14 @@
 | 
			
		|||
#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);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
YOSYS_NAMESPACE_END
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue