— Albert Einstein
Many years ago, I wrote about the importance of having playgrounds, that is, easy-to-access try-out areas for carrying out programming-related experiments with the overall goal of exploring and learning.
Recently, I’ve reworked my C++ playground and uploaded it to GitHub. Compared to my previous C++ playground, the new one comes with the following major advantages:
- Shared access to playgrounds from multiple computers — since it is based on a Git repository.
- Every experiment has its own subdirectory — the top-level playground directory stays clean and clearly arranged.
- Unit test support through Google Test — running ‘make’ not just builds the experiment but also executes contained unit tests.
Once cloned and installed, you can start a new experiment is this:
1 2 3 4 |
cd ~/pg-cpp . pg-setup init_within_loop_body |
‘pg-setup’ will create a directory called ‘init_within_loop_body’ along with a ‘Makefile’ and a ‘init_within_loop_body.cpp’ source file. Plus, if you have defined your ‘EDITOR’ environment variable properly, it will open ‘init_within_loop_body.cpp’ in your favorite editor for you. All that’s left to do is add your experiment’s code to the testcase template:
1 2 3 4 5 6 7 8 9 10 11 12 |
// This experiment tests if a variable inside a loop body // is initialized with every iteration. TEST(init_within_loop_body, simple) { for (int i = 0; i < 10; ++i) { int k = 0; // Assume that k is initialized every time. EXPECT_EQ(0, k); ++k; } } |
Now, just type/execute ‘make’ (either from within your editor or from the command-line) and your code will be compiled and run:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
g++ -W -Wall -g -pthread -I /home/ralf/get-me-gtest/googletest-release-1.8.0/googletest/include -I /home/ralf/get-me-gtest/googletest-release-1.8.0/googlemock/include -L /home/ralf/get-me-gtest/googletest-release-1.8.0/googlemock init_within_loop_body.cpp -l gmock_main -o init_within_loop_body ./init_within_loop_body Running main() from gmock_main.cc [==========] Running 1 test from 1 test case. [----------] Global test environment set-up. [----------] 1 test from init_within_loop_body [ RUN ] init_within_loop_body.simple [ OK ] init_within_loop_body.simple (0 ms) [----------] 1 test from init_within_loop_body (0 ms total) [----------] Global test environment tear-down [==========] 1 test from 1 test case ran. (0 ms total) [ PASSED ] 1 test. |