File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # EditorConfig helps developers define and maintain consistent
2+ # coding styles between different editors and IDEs
3+ # editorconfig.org
4+ root = true
5+
6+ [* ]
7+ end_of_line = lf
8+ charset = utf-8
9+ trim_trailing_whitespace = true
10+ insert_final_newline = true
11+ indent_style = space
12+
13+ [* .py ]
14+ indent_style = space
15+ indent_size = 4
16+
17+ [* .yml ]
18+ indent_style = space
19+ indent_size = 4
20+
21+ [* .{cpp,h} ]
22+ indent_style = space
23+ indent_size = 3
24+
25+ [* .{diff,md,ui} ]
26+ trim_trailing_whitespace = false
27+
28+ [CMakeLists.txt ]
29+ indent_style = space
30+ indent_size = 4
Original file line number Diff line number Diff line change 1+ name : Code Coverage
2+
3+ on :
4+ push :
5+ branches : [ main ]
6+ pull_request :
7+ branches : [ main ]
8+
9+ jobs :
10+ coverage :
11+ runs-on : ubuntu-latest
12+
13+ steps :
14+ - name : Checkout code
15+ uses : actions/checkout@v4
16+
17+ - name : Install dependencies
18+ run : |
19+ sudo apt update
20+ sudo apt install -y lcov cmake ninja-build g++ libgtest-dev
21+
22+ - name : Configure project with coverage flags
23+ run : cmake -B build -DENABLE_COVERAGE=ON -DCMAKE_BUILD_TYPE=Debug -G Ninja
24+
25+ - name : Build the project
26+ run : cmake --build build
27+
28+ - name : Run tests
29+ run : cd build && ctest --output-on-failure
30+
31+ - name : Generate coverage report
32+ run : |
33+ cd build
34+ lcov --directory . --capture --output-file coverage.info \
35+ --rc geninfo_auto_base=1 --ignore-errors mismatch
36+ lcov --remove coverage.info '/usr/*' '*/tests/*' --output-file coverage.filtered.info
37+ genhtml coverage.filtered.info --output-directory coverage-report
38+
39+ - name : Show coverage summary
40+ run : |
41+ cd build
42+ lcov --summary coverage.filtered.info
43+
44+ - name : Upload coverage report as artifact
45+ uses : actions/upload-artifact@v4
46+ with :
47+ name : coverage-report
48+ path : build/coverage-report
49+
50+ - name : Enforce coverage threshold
51+ run : |
52+ cd build
53+ THRESHOLD=100
54+ COVERAGE=$(lcov --summary coverage.filtered.info | grep "lines" | awk '{ print $2 }' | sed 's/%//')
55+ echo "Total coverage: $COVERAGE%"
56+ if (( $(echo "$COVERAGE < $THRESHOLD" | bc -l) )); then
57+ echo "❌ Coverage below threshold of ${THRESHOLD}%"
58+ exit 1
59+ else
60+ echo "✅ Coverage is above threshold"
61+ fi
Original file line number Diff line number Diff line change 3030* .exe
3131* .out
3232* .app
33+
34+ # cpp-coverage
35+ build /
Original file line number Diff line number Diff line change 1+ cmake_minimum_required (VERSION 3.16 )
2+ project (MyQueueCoverage)
3+
4+ find_package (GTest REQUIRED )
5+ include (GoogleTest )
6+
7+ option (ENABLE_COVERAGE "Enable coverage reporting" OFF )
8+
9+ add_library (MyQueue src/MyQueue.cpp )
10+ target_include_directories (MyQueue PUBLIC src )
11+
12+ add_executable (MyQueueTests tests/TestMyQueue.cpp )
13+ target_link_libraries (MyQueueTests MyQueue GTest::gtest_main )
14+
15+ enable_testing ()
16+ gtest_discover_tests (MyQueueTests )
17+
18+ if (ENABLE_COVERAGE AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang" )
19+ target_compile_options (MyQueue PRIVATE --coverage -O0 -g )
20+ target_link_options (MyQueue PRIVATE --coverage )
21+ target_compile_options (MyQueueTests PRIVATE --coverage -O0 -g )
22+ target_link_options (MyQueueTests PRIVATE --coverage )
23+ endif ()
Original file line number Diff line number Diff line change 1+ #include " MyQueue.h"
2+
3+ void MyQueue::Push (int val)
4+ {
5+ _q.push (val);
6+ }
7+
8+ void MyQueue::Pop ()
9+ {
10+ if (_q.empty ())
11+ return ;
12+
13+ _q.pop ();
14+ }
15+
16+ bool MyQueue::IsEmpty () const
17+ {
18+ return _q.empty ();
19+ }
Original file line number Diff line number Diff line change 1+ #pragma once
2+ #include < queue>
3+
4+ class MyQueue
5+ {
6+ public:
7+ void Push (int val);
8+ void Pop ();
9+ bool IsEmpty () const ;
10+
11+ private:
12+ std::queue<int > _q;
13+ };
Original file line number Diff line number Diff line change 1+ #include < gtest/gtest.h>
2+ #include " MyQueue.h"
3+
4+ TEST (MyQueueTest, PopOnEmptyQueueDoesNothing) {
5+ MyQueue q;
6+ q.Pop ();
7+ EXPECT_TRUE (q.IsEmpty ());
8+ }
9+
10+ TEST (MyQueueTest, PushAndPopWorks) {
11+ MyQueue q;
12+ q.Push (42 );
13+ EXPECT_FALSE (q.IsEmpty ());
14+ q.Pop ();
15+ EXPECT_TRUE (q.IsEmpty ());
16+ }
You can’t perform that action at this time.
0 commit comments