C++ program to get projects started and going on Linux Enviornment
- Colored logging
- Interactive standard input
- Easy object construction
- Enhanced String Operations
- Unit Testing
- Debugging
sudo apt update
sudo apt install g++
sudo apt install makeRun the main file by using make run. You can also set c++ version
make run: defaults to c++17make run std=$CPP_VERSION: runs at user-specified version
Run this command: make setup-tests
# fetch the library
sudo apt-get install libgtest-dev
# mark current workspace directory
PROJ_DIR=$PWD
# install and unpack gtest files
sudo apt-get install cmake # install cmake
cd /usr/src/gtest
sudo cmake CMakeLists.txt
sudo make
# copy or symlink libgtest.a and libgtest_main.a to your /usr/lib folder
sudo cp *.a /usr/lib
# navigate back to workspace
cd $PROJ_DIR
# Lets see if it works
make testRunning main() from ./googletest/src/gtest_main.cc
[==========] Running 0 tests from 0 test suites.
[==========] 0 tests from 0 test suites ran. (0 ms total)
[ PASSED ] 0 tests.- Run
make testfile - You will be prompted:
Enter name of new test case: - Enter "Sample" or any other word w/ NO SPACES
- Run
make test
Running main() from ./googletest/src/gtest_main.cc
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from SampleTest
[ RUN ] SampleTest.TestDescription
[ OK ] SampleTest.TestDescription (0 ms)
[----------] 1 test from SampleTest (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[ PASSED ] 1 test.- Open
tests/SampleTests.cpp - Modify your code so it looks like this (or just copy and paste)
#include <gtest/gtest.h>
bool pow2(const int &x)
{
if ((x <= 0) || ((x % 2 != 0) && (x > 1)))
return false;
if (x == 1)
return true;
return pow2(x / 2);
}
TEST(SampleTest, PowerOf2True)
{
EXPECT_TRUE(pow2(256));
}make predebug=> Generates vscode debug files and opens VSCode- You will be prompted to choose main or test files
Which program are you debugging? (m)ain (t)est Enter a choice:
- VsCode Menu ->
Run->Start Debugging
make debug=> Build and runs main file in gdbmake debug-tests=> Build and runs test files in gdb
#include <input.hpp>
#include <console.hpp>
int main(int argc, char **argv)
{
auto name = input<string>("Enter your name: ");
auto age = input<int>("Enter your age: ");
LOG(name, age);
return 0;
}#include <objects.hpp>
using namespace objects;
int main(int argc, char **argv)
{
// str is a pointer type => string *
auto str = construct<string>("string ptr");
/*
...
// some code here
...
*/
// DON'T FORGET TO CLEANUP
destroy(str);
return 0;
}#include <objects.hpp>
using namespace objects;
int main(int argc, char **argv)
{
// objects::SmartPtr will auto destroy
SmartPtr<string> ptr("string ptr");
// do some stuff...
return 0;
}#include <strings.hpp>
#include <console.hpp>
#include <vector>
#include <string>
using namespace std;
using namespace strings;
int main(int argc, char **argv)
{
vector<string> v = {"king", "queen", "rook", "bishop", "knight", "pawn"};
string chess = join(v, "<>");
LOG(chess);
// "king<>queen<>rook<>bishop<>knight<>pawn"
return 0;
}#include <strings.hpp>
#include <console.hpp>
#include <vector>
#include <string>
using namespace std;
using namespace strings;
int main(int argc, char **argv)
{
string s = "king<>queen<>rook<>bishop<>knight<>pawn";
auto result = split(s, "<>");
for (const string & i: result)
LOG(i);
/**
* king
* queen
* rook
* bishop
* knight
* pawn
*/
return 0;
}