TDD is quite a popular practice among the software developers but in the firmware development case it's not quite so. One of the reasons behind this is the hurdle in setting up the TDD environment. In this blog post I am not going to explain the importance of doing TDD, rather in this post I will be showing you how we can set up a TDD build environment using google test and develop a circular buffer following the TDD approach. So Let’s engage in TDD-ing
gTest Setup
Firstly we have to install google Test , this is available from the apt-repository
The above command will pull down the sources and it is upto us to build the gTest. This process Requires CMake
Now go to gtest src which got downloaded by apt & build gTest
Now Lets copy the libraries to system default /usr/lib so that it is accessible from any PATH
Requirements Gathering
Before starting to code we do have a mental model of the module that we want to develop . The idea of TDD is to write test cases first and then do coding . We will do the same but first let’s write down the requirements of the module in our imagination.
Circular Buffer spec
The buffer will be 4 in size
Data can be put into the buffer
Data can be read from the buffer
The buffer should be FIFO in nature
The buffer should be fixed sized
User should know if buffer is empty
User should have the option to clear the buffer
TDD project Folder Structure
The Project folder has the following structure
Now let’s look what each of the elements are
Let’s create all the folders and files
Linking gTest & CMake
Now we will prepare the CMake makefile so that our sources gets linked with the compiled gTest libraries that we built at top . This CMake file is very easy to understand . the contents are
CMakeLists.txt
Test Driver Structure
Now that our CMakeFile is ready we will prepare the main.cpp the Test Driver File .
main.cpp
Now let’s put some minimum code in our sources and check if build passes
CBuffer.h
CBuffer.cpp
Build System Check
Now let’s check if the Test Build system prepared so far works or not
The job of CMake is to create a makefile
Let’s use make to build our executable binary
Now run the executable created
Writing The First Test Case
In TDD we have to follow 4 major steps
Setup
Exercise
Verify
Cleanup
Each Test Case should go through the above steps
We will write test cases for spec 6 & 7
We will call clear function to empty out the buffer
And then check if buffer is empty
Now let’s compile again
You will get compilation error
Getting compilation/build errors first is also part of The TDD process. The mandatory rule is to write test cases first no matter what . The hidden gem of TDD is that by writing the test cases first we get to think about the module API calls and the code grows with this approach
Now Let’s put code
CBuffer.cpp
CBuffer.h
Next step is to add our 3rd step Verify
main.cpp
Now Compile again and you will see successful build output
Let’s run the executable binary
This time we see Test Case output of the verification stage. In this step we see Test cases getting failed This is the way TDD development progresses. We observe code getting failed and then we write code to pass the test cases. This is what we are going to do now
CBuffer.cpp
Add the orange marked code and Now if you compile and run the test executable again then you will see that the test have passed
Similarly you will be adding all the other test cases and code will get developed along the away
I am not going to explain all the other test cases here anymore as this has already been a very long post. You can find the rest of the test cases along with all the other source files available in this link
https://github.com/hassin23ayz/TDD-practice/tree/master/cpp/circular-buffer
you are spreading knowledge and it helps a people to understand the problem on embeded systems i apreciate your work
ReplyDeleteJTAG