Skip to content

Commit 6738734

Browse files
authored
Merge pull request #2 from urboob21/dev_branch
Dev_2025_11_08
2 parents 95ec4a1 + 0c226d4 commit 6738734

7 files changed

Lines changed: 259 additions & 30 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
*build
2-
*private*
2+
*private*
3+
*.vscode

CMakeLists.txt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,20 @@ cmake_minimum_required(VERSION 3.14)
55
# ----------------------------------------------------------------------------------------
66
project(cpp_lab_project # ${PROJECT_NAME}
77
VERSION 1.0.0
8-
DESCRIPTION "The project with GoogleTest and CMake"
8+
DESCRIPTION "A C/C++ project uses CMake, GoogleTest, gcc, g++, cppcheck, and lcov, integrated with Docker and GitHub Actions for CI/CD."
99
LANGUAGES CXX
1010
)
1111

12+
# Build timestamp
13+
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/include)
14+
string(TIMESTAMP BUILD_TIME "%Y-%m-%d %H:%M:%S")
15+
16+
configure_file(
17+
${CMAKE_SOURCE_DIR}/include/version.h.in
18+
${CMAKE_BINARY_DIR}/generated/version.h
19+
)
20+
21+
1222
set(PROJECT_NAME_TEST ${PROJECT_NAME}_unit_test) # name for the unit-test executable
1323

1424
# ----------------------------------------------------------------------------------------
@@ -60,11 +70,14 @@ enable_testing()
6070
# Header files directory
6171
set(APP_HEADERS
6272
"include"
73+
"${CMAKE_BINARY_DIR}/generated" # cmake generated headers
6374
)
6475

6576
# Core source files
6677
set(APP_SOURCES
6778
"src/DeleteMe.cpp"
79+
"src/core/basics/InitializeVariable.cpp"
80+
"src/core/basics/Operations.cpp"
6881
)
6982

7083
# Test files

README.md

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,67 @@
1-
## OVERVIEW
2-
- Structure:
3-
includes/ → header files (.h, .hpp)
4-
src/ → source files (.cpp)
5-
tests/ → GoogleTest test cases
1+
## 1. OVERVIEW
2+
**Project Structure**
3+
```
4+
includes/ → Header files (.h, .hpp)
5+
src/ → Source files (.cpp)
6+
tests/ → GoogleTest test cases
7+
```
8+
## 2. DEPENDENCIES
9+
Make sure the following tools are installed before building the project:
10+
- **g++ / gcc**
11+
- **CMake**
12+
- **Git**
13+
- **lcov** (for code coverage)
14+
- **cppcheck** (for static analysis)
615

7-
## DEPENDENCIES
8-
9-
## SETUP
16+
## 3. SETUP
1017
* Setup the Local Test Environment
11-
* Using your own Ubuntu system
18+
* 1.Using your own Ubuntu system
1219
* Install `gcc`, `cmake`, `git`, and `pthread` (Skip this step if you already install)
1320
```
1421
$ sudo apt-get update
15-
$ sudo apt-get install g++=4:5.3.1-1ubuntu1
16-
$ sudo apt-get install lcov=1.12-2
17-
$ sudo apt-get install cmake=3.5.1-1ubuntu3
18-
$ sudo apt-get install git=1:2.7.4-0ubuntu1.6
19-
$ sudo apt-get install libpthread-stubs0-dev=0.3-4
20-
22+
$ sudo apt-get install g++
23+
$ sudo apt-get install lcov
24+
$ sudo apt-get install cmake
25+
$ sudo apt-get install git
26+
$ sudo apt-get install cppcheck
2127
```
2228
* Build the application and the tests
2329
```
2430
$ cd build
2531
$ cmake ..
2632
$ cmake --build .
27-
2833
```
2934
* Run the application and the test
3035
```
3136
$ ./cpp_lab_project
3237
$ ./cpp_lab_project_test
3338
```
34-
* (Optional) Run the cpp check
39+
* (Optional) Run static analysis
3540
```
3641
$ sudo apt-get install cppcheck
3742
$ cppcheck "folder" / "file"
3843
```
39-
* Using **Docker**
40-
* Use the -t or --tag flag to set the name of the image to be created. (the full name is actually sample-ci-cpp:latest, since latest is the default tag)
41-
* Opening an interactive shell inside your Docker container to explore, test, or debug the environment built from your image.
42-
* docker run → start a new container.
43-
* -it → run it interactively:
44-
* -i = keep STDIN open (so you can type commands)
45-
* -t = allocate a terminal (TTY)
46-
* sample-ci-cpp:latest → the image you built earlier.
47-
* /bin/bash → the command to execute inside the container (opens a Bash shell).
44+
* 2.Using **Docker**
45+
* Build the Docker image
4846
```
4947
docker build --tag sample-ci-cpp .
48+
```
49+
* Run an interactive container
50+
```
5051
docker run -it sample-ci-cpp:latest /bin/bash
52+
```
53+
* Inspect the environment
54+
```
5155
printenv
5256
```
57+
* *Notes:*
58+
* Use the -t or --tag flag to set the name of the image to be created. (the full name is actually sample-ci-cpp:latest, since latest is the default tag)
59+
* Opening an interactive shell inside your Docker container to explore, test, or debug the environment built from your image.
60+
* docker run to start a new container.
61+
* -it → run it interactively:
62+
* -i = keep STDIN open (so you can type commands)
63+
* -t = allocate a terminal (TTY)
64+
* sample-ci-cpp:latest → the image you built earlier.
65+
* /bin/bash → the command to execute inside the container (opens a Bash shell).
5366
54-
## DOCUMENTATIONs
67+
## 4. DOCUMENTATIONS

include/version.h.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#pragma once
2+
3+
#define APP_NAME "@PROJECT_NAME@"
4+
#define APP_VERSION "@PROJECT_VERSION@"
5+
#define APP_DESCRIPTION "@PROJECT_DESCRIPTION@"
6+
#define APP_BUILD_TIME "@BUILD_TIME@"
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
void initialize_variable();
5+
6+
// A struct that runs code when its object is created
7+
struct InitializeVariable
8+
{
9+
InitializeVariable()
10+
{
11+
cout << "\n"
12+
<< "\n"
13+
<< "InitializeVariable\n";
14+
initialize_variable();
15+
}
16+
};
17+
18+
// All global and static objects are constructed before main() begins.
19+
static InitializeVariable autoRunInstance;
20+
21+
struct Foo
22+
{
23+
Foo()
24+
{
25+
cout << "Default constructor/ default init\n";
26+
}
27+
28+
// explicit Foo(int)
29+
explicit Foo(int)
30+
{
31+
cout << "Constructor called with int / copy init\n";
32+
}
33+
34+
Foo(const Foo &other)
35+
{
36+
std::cout << "Copy constructor called\n";
37+
}
38+
};
39+
40+
void initialize_variable()
41+
{
42+
cout << "\n--- Variable Initialization Examples ---\n";
43+
// There are there common ways to intialize a variable
44+
// * Default
45+
[[maybe_unused]] int initDefaultVar;
46+
[[maybe_unused]] Foo initDefaultObj;
47+
48+
// * Traditional initialization
49+
// * Copy-init: Type var = value;
50+
// 1. Compiler tries to convert the value to a temporary Foo.
51+
// 2. If the constructor is explicit, implicit conversion is blocked -> compilation error.
52+
// 3. Otherwise, a temporary Foo is created and then copied/moved into the variable.
53+
[[maybe_unused]] Foo copyInitObj = (Foo)2.3; // explicit cast: double 2.3 -> int 2 (implicit narrowing) -> Foo(int) -> temporary Foo -> copied/moved into copyInitObj
54+
// Foo copyInitObjError = 2.3; // ERROR: implicit conversion blocked by explicit constructor
55+
// We can explicitly prevent certain conversions using = delete or using {}
56+
57+
// * direct-init: Type var(value);
58+
[[maybe_unused]] Foo directInitObj(4); // call Foo(int)
59+
[[maybe_unused]] Foo directInitObj2(4.3); // look for constructor -> implicit 4.3(float) -> 4(int) -> call Foo(int) ->
60+
61+
// * Brace init
62+
// calls the constructor directly without allowing implicit conversions.
63+
[[maybe_unused]] Foo braceInit{3};
64+
// Foo braceInit2{3.3}; // ERORR => Prefer this way
65+
}

src/core/basics/Operations.cpp

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
void arithmeticOperator();
5+
void logicalOperator();
6+
void bitWiseOperator();
7+
8+
struct Operations
9+
{
10+
Operations()
11+
{
12+
cout << "\n"
13+
<< "\n"
14+
<< "Operation\n";
15+
arithmeticOperator();
16+
logicalOperator();
17+
bitWiseOperator();
18+
}
19+
};
20+
21+
static Operations autoRunInstance;
22+
23+
void arithmeticOperator()
24+
{
25+
cout << "\n--- ArithmeticOperator Examples ---\n";
26+
int a{100}, b{200};
27+
28+
// Addition
29+
cout << "a = " << a << ", b = " << b << "\n";
30+
int sum = a + b;
31+
cout << "sum = " << sum << "\n";
32+
33+
// Subtraction
34+
cout << "a = " << a << ", b = " << b << "\n";
35+
int different = a - b;
36+
cout << "different = " << different << "\n";
37+
38+
// Multiplication
39+
cout << "a = " << a << ", b = " << b << "\n";
40+
int product = a * b;
41+
cout << "product = " << product << "\n";
42+
43+
// Division
44+
cout << "a = " << a << ", b = " << b << "\n";
45+
int quotient = a / b;
46+
cout << "quotient = " << quotient << "\n";
47+
48+
// Modulus
49+
cout << "a = " << a << ", b = " << b << "\n";
50+
int remainder = a % b;
51+
cout << "remainder = " << remainder << "\n";
52+
53+
// Increment
54+
cout << "a = " << a << "\n";
55+
int preIn = ++a; // increase a, return copy
56+
cout << "preIn = " << preIn << "\n";
57+
58+
cout << "a = " << a << "\n";
59+
int postIn = a++; // copy a to a copy, then increase a, return copy
60+
cout << "postIn = " << postIn << "\n";
61+
62+
// Decrement
63+
cout << "b = " << b << "\n";
64+
int preDe = --b;
65+
cout << "preDe = " << preDe << "\n";
66+
67+
cout << "b = " << b << "\n";
68+
int postDe = b--;
69+
cout << "postDe = " << postDe << "\n";
70+
71+
// Comma:
72+
int value = (a++, b); // a is incremented, then b is returned
73+
cout << "a = " << a << ", b = " << b << "\n";
74+
cout << "comma(a++, b) = " << value << "\n";
75+
}
76+
77+
void logicalOperator()
78+
{
79+
cout << "\n--- LogicalOperator Examples ---\n";
80+
bool a = true;
81+
bool b = false;
82+
bool c = true;
83+
84+
cout << boolalpha; // show true/false instead of 1/0
85+
cout << "a = " << a << ", b = " << b << ", c = " << c << "\n\n";
86+
87+
// AND (&&)
88+
cout << "[AND] a && b = " << (a && b) << "\n";
89+
90+
// OR (||)
91+
cout << "[OR ] a || b = " << (a || b) << "\n";
92+
93+
// NOT (!)
94+
cout << "[NOT] !c = " << (!c) << "\n";
95+
}
96+
97+
#include <bitset>
98+
void bitWiseOperator()
99+
{
100+
cout << "\n--- BitWiseOperator Examples ---\n";
101+
bitset<8> bitsA{0b1111'1111};
102+
bitset<8> bitsB{0b1111'0000};
103+
104+
cout << "bitA = " << bitsA << ", bitB = " << bitsB << "\n";
105+
106+
// AND
107+
bitset<8> result = bitsA & bitsB;
108+
cout << "bitA && bitB= " << result << "\n";
109+
110+
// OR
111+
result = bitsA | bitsB;
112+
cout << "bitA | bitB= " << result << "\n";
113+
114+
// XOR
115+
result = bitsA ^ bitsB;
116+
cout << "bitA ^ bitB= " << result << "\n";
117+
118+
// NOT
119+
result = ~bitsA;
120+
cout << "~bitA = " << result << "\n";
121+
122+
// LEFT SHIFT
123+
result = bitsA << 1;
124+
cout << "bitA << 1 = " << result << "\n";
125+
126+
// RIGHT SHIFT
127+
result = bitsA >> 1;
128+
cout << "bitA >> 1 = " << result << "\n";
129+
}

src/main.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#include <iostream>
2+
#include "version.h"
23

34
int main(int argc, char *argv[])
45
{
5-
std::cout << "Hello World" << std::endl;
6+
std::cout << APP_NAME << " v" << APP_VERSION << std::endl;
7+
std::cout << APP_DESCRIPTION << std::endl;
68
return 0;
79
}

0 commit comments

Comments
 (0)