Skip to content

Commit de9460d

Browse files
committed
Refactor
1 parent 3a6573a commit de9460d

5 files changed

Lines changed: 112 additions & 128 deletions

File tree

‎scripts/gen_coverage_lcov.sh‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,30 @@
11
#!/usr/bin/env bash
22
set -e
33

4-
# -----------------------------------------------------------------------------
54
# Check lcov installation
6-
# -----------------------------------------------------------------------------
75
if ! command -v lcov >/dev/null 2>&1; then
86
echo "Error: lcov is not installed."
97
echo "Install with: sudo apt install lcov"
108
exit 1
119
fi
1210

13-
# -----------------------------------------------------------------------------
1411
# Configure project (only if build folder does not exist)
15-
# -----------------------------------------------------------------------------
1612
if [ ! -d build ]; then
1713
cmake -S . -B build \
1814
-DCMAKE_BUILD_TYPE=Debug \
1915
-DCMAKE_CXX_FLAGS="--coverage -O0 -g"
2016
fi
2117

22-
# -----------------------------------------------------------------------------
2318
# Build project
24-
# -----------------------------------------------------------------------------
2519
cmake --build build
2620

27-
# -----------------------------------------------------------------------------
2821
# Reset previous coverage
29-
# -----------------------------------------------------------------------------
3022
lcov --directory build --zerocounters
3123

32-
# -----------------------------------------------------------------------------
3324
# Run unit tests
34-
# -----------------------------------------------------------------------------
3525
ctest --test-dir build --output-on-failure
3626

37-
# -----------------------------------------------------------------------------
3827
# Capture coverage
39-
# -----------------------------------------------------------------------------
4028
mkdir -p coverage_lcov
4129

4230
lcov --capture \
@@ -49,13 +37,9 @@ lcov --capture \
4937
lcov --remove coverage_lcov/coverage.info '/usr/*' \
5038
--output-file coverage_lcov/coverage.info
5139

52-
# -----------------------------------------------------------------------------
5340
# Generate HTML report
54-
# -----------------------------------------------------------------------------
5541
genhtml coverage_lcov/coverage.info \
5642
--output-directory coverage_lcov
5743

58-
# -----------------------------------------------------------------------------
5944
# Open coverage report
60-
# -----------------------------------------------------------------------------
6145
xdg-open coverage_lcov/index.html

‎src/core/basics/ControlFlow.cpp‎

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
11
#include <iostream>
2-
#include <random>
3-
using namespace std;
2+
#include "ExampleRegistry.h"
43

4+
namespace {
55
void conditionals() {
6-
cout << "\n--- Conditional Examples ---\n";
6+
std::cout << "\n--- Conditional Examples ---\n";
77
// if-else
88
int x = rand();
9-
cout << "x = " << x << "\n";
9+
std::cout << "x = " << x << "\n";
1010
if (x > 0) {
11-
cout << "x is positive\n";
11+
std::cout << "x is positive\n";
1212
} else if (x < 0) {
13-
cout << "x is negative\n";
13+
std::cout << "x is negative\n";
1414
} else {
15-
cout << "x is zero\n";
15+
std::cout << "x is zero\n";
1616
}
1717

1818
// switch
1919
int choice = rand() % 2 + 1;
20-
cout << "choice = " << x << "\n";
20+
std::cout << "choice = " << x << "\n";
2121
switch (choice) {
2222
case 1:
23-
cout << "Choice is 1\n";
23+
std::cout << "Choice is 1\n";
2424
break;
2525
case 2:
26-
cout << "Choice is 2\n";
26+
std::cout << "Choice is 2\n";
2727
break;
2828
default:
29-
cout << "Choice is something else\n";
29+
std::cout << "Choice is something else\n";
3030
break;
3131
}
3232
}
3333

3434
void jumps() {
35-
cout << "\n--- Jump Statement Examples ---\n";
35+
std::cout << "\n--- Jump Statement Examples ---\n";
3636

3737
// goto
3838
int num = rand();
3939
if (num == 3)
4040
goto jumpLabel;
41-
cout << "This line will be skipped.\n";
41+
std::cout << "This line will be skipped.\n";
4242

4343
jumpLabel:
44-
cout << "Jumped here using goto!\n";
44+
std::cout << "Jumped here using goto!\n";
4545

4646
// break / continue
4747
for (int i = 0; i < 5; ++i) {
4848
if (i == 2)
4949
continue; // skip 2
5050
if (i == 4)
5151
break; // stop loop at 4
52-
cout << "i = " << i << "\n";
52+
std::cout << "i = " << i << "\n";
5353
}
5454
}
5555

@@ -59,43 +59,43 @@ int square(int n) {
5959
}
6060

6161
void functionCalls() {
62-
cout << "\n--- Function Call Examples ---\n";
62+
std::cout << "\n--- Function Call Examples ---\n";
6363
// function call
6464
int result = square(5);
65-
cout << "square(5) = " << result << "\n";
65+
std::cout << "square(5) = " << result << "\n";
6666
}
6767

6868
void loops() {
69-
cout << "\n--- Loop Examples ---\n";
69+
std::cout << "\n--- Loop Examples ---\n";
7070

7171
// while
7272
int i = 0;
7373
while (i < 3) {
74-
cout << "while loop i = " << i << "\n";
74+
std::cout << "while loop i = " << i << "\n";
7575
++i;
7676
}
7777

7878
// do-while
7979
int j = 0;
8080
do {
81-
cout << "do-while loop j = " << j << "\n";
81+
std::cout << "do-while loop j = " << j << "\n";
8282
++j;
8383
} while (j < 2);
8484

8585
// for(initialization; condition; update)
8686
for (int k = 0; k < 3; ++k) {
87-
cout << "for loop k = " << k << "\n";
87+
std::cout << "for loop k = " << k << "\n";
8888
}
8989

9090
// ranged-for
9191
const int arr[] = {10, 20, 30};
9292
for (int value : arr) {
93-
cout << "ranged-for value = " << value << "\n";
93+
std::cout << "ranged-for value = " << value << "\n";
9494
}
9595
}
9696

9797
void halts() {
98-
cout << "\n--- Halt Examples ---\n";
98+
std::cout << "\n--- Halt Examples ---\n";
9999

100100
// std::exit() — terminates the program normally
101101
// std::abort() — terminates abnormally (no cleanup)
@@ -106,17 +106,16 @@ void halts() {
106106
}
107107

108108
void exceptions() {
109-
cout << "\n--- Exception Handling Examples ---\n";
109+
std::cout << "\n--- Exception Handling Examples ---\n";
110110

111111
// try - catch - throw
112112
try {
113-
throw runtime_error("Something went wrong!");
114-
} catch (const exception& e) {
115-
cout << "Caught exception: " << e.what() << "\n";
113+
throw std::runtime_error("Something went wrong!");
114+
} catch (const std::exception& e) {
115+
std::cout << "Caught exception: " << e.what() << "\n";
116116
}
117117
}
118-
119-
#include "ExampleRegistry.h"
118+
} // namespace
120119

121120
class ControlFlow : public IExample {
122121
public:
Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
// cppcheck-suppress-file [unreadVariable, unusedVariable, functionStatic]
12
#include <iostream>
2-
using namespace std;
33

44
#include "ExampleRegistry.h"
55

@@ -16,48 +16,54 @@ class InitializeVariable : public IExample {
1616
REGISTER_EXAMPLE(InitializeVariable, "core", "InitializeVariable");
1717

1818
struct Foo {
19-
Foo() { cout << "Default constructor/ default init\n"; }
19+
Foo() { std::cout << "Default constructor/ default init\n"; }
2020

2121
// Foo(int)
22-
explicit Foo(int) { cout << "Constructor called with int / copy init\n"; }
22+
explicit Foo(int) {
23+
std::cout << "Constructor called with int / copy init\n";
24+
}
2325

24-
Foo(const Foo& other) { std::cout << "Copy constructor called\n"; }
26+
Foo(const Foo& other) {
27+
other.dump();
28+
std::cout << "Copy constructor called\n";
29+
}
30+
31+
void dump() const {}
2532
};
2633

2734
void initialize_variable() {
28-
cout << "\n--- Variable Initialization Examples ---\n";
35+
std::cout << "\n--- Variable Initialization Examples ---\n";
2936
// There are there common ways to intialize a variable
3037
// * 1) Default-initialization
31-
[[maybe_unused]] int initDefaultVar;
32-
[[maybe_unused]] Foo initDefaultObj;
38+
int init_default_var;
39+
Foo init_default_obj;
3340

34-
[[maybe_unused]] unsigned char* c = new unsigned char;
41+
auto* c = new unsigned char;
3542
*c = 0xF; // actual init at this point for c
3643
delete c;
3744

3845
// * 2) Value-initialization
39-
[[maybe_unused]] int initValueVar1();
40-
[[maybe_unused]] int initValueVar2{};
46+
int init_value_var1();
47+
int init_value_var2{};
4148

4249
// * 3) direct-init: Type var(value);
43-
[[maybe_unused]] Foo directInitObj(4); // call Foo(int)
44-
[[maybe_unused]] Foo directInitObj2(
45-
4.3); // look for constructor -> implicit 4.3(float) -> 4(int) -> call Foo(int) ->
50+
Foo direct_init_obj(4); // call Foo(int)
51+
Foo direct_init_obj2(4.3); // look for constructor -> implicit 4.3(float) -> 4(int) -> call Foo(int)
4652

4753
// * 4) Copy-init: Type var = other;
4854
// 1. Compiler tries to convert the value to a temporary Foo.
4955
// 2. If the constructor is explicit, implicit conversion is blocked -> compilation error.
5056
// 3. Otherwise, a temporary Foo is created and then copied/moved into the variable.
51-
[[maybe_unused]] Foo copyInitObj =
52-
(Foo)2.3; // explicit cast: double 2.3 -> int 2 (implicit narrowing) -> Foo(int)
53-
// -> temporary Foo -> copied/moved into copyInitObj
57+
Foo copy_init_obj = (Foo)2.3;
58+
// explicit cast: double 2.3 -> int 2 (implicit narrowing) -> Foo(int)
59+
// -> temporary Foo -> copied/moved into copyInitObj
5460
// Foo copyInitObjError = 2.3; // ERROR: implicit conversion blocked by explicit constructor
5561
// We can explicitly prevent certain conversions using = delete or using {}
5662

5763
// * 5) List-initialization (Brace init)
5864
// calls the constructor directly without allowing implicit conversions.
59-
[[maybe_unused]] Foo braceInit{3};
65+
Foo brace_init{3};
6066
// Foo braceInit2{3.3}; // ERORR => Prefer this way
61-
[[maybe_unused]] int ar[] = {1, 2, 3}; // aggregate init
62-
[[maybe_unused]] int ar2[]{1, 2, 3};
67+
int ar[] = {1, 2, 3}; // aggregate init
68+
int ar2[]{1, 2, 3};
6369
}

0 commit comments

Comments
 (0)