Skip to content

Commit 76a60d7

Browse files
authored
Week 10 (#11)
1 parent 5f31752 commit 76a60d7

File tree

7 files changed

+82
-2
lines changed

7 files changed

+82
-2
lines changed

.clang-tidy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ Checks: '
33
readability*,
44
google*,
55
misc-const-correctness,
6-
-readability-identifier-length
6+
-readability-identifier-length,
7+
-readability-else-after-return
78
'
89
CheckOptions:
910
- key: readability-identifier-naming.NamespaceCase

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function(cpp_test SOURCE_NAME)
4949
endfunction(cpp_test)
5050

5151
##### subdirectories #####
52-
set(DIRS week-W week-1 week-2 week-3 week-4 week-5 week-6 week-7 week-8 week-9)
52+
set(DIRS week-W week-2 week-3 week-4 week-5 week-6 week-7 week-8 week-9 week-10)
5353
foreach(DIR ${DIRS})
5454
add_subdirectory(${DIR})
5555
endforeach()

week-10/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
set(DIRS task-07-01)
2+
foreach(DIR ${DIRS})
3+
add_subdirectory(${DIR})
4+
endforeach()

week-10/task-07-01/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include_directories(include)
2+
3+
add_subdirectory(test)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
#include <cmath>
3+
#include <stdexcept>
4+
#include <utility>
5+
#include <variant>
6+
7+
namespace math {
8+
9+
std::variant<std::monostate, double, std::pair<double, double>> quadratic_equation_solution(
10+
double a, double b, double c) {
11+
if (a == 0.0) {
12+
throw std::invalid_argument("Coefficient 'a' should not be 0");
13+
}
14+
const double d_squared = b * b - 4 * a * c;
15+
if (d_squared < 0) {
16+
return std::variant<std::monostate, double, std::pair<double, double>>{};
17+
} else if (d_squared == 0) {
18+
return std::variant<std::monostate, double, std::pair<double, double>>{
19+
std::in_place_type<double>, -b / (2 * a)};
20+
} else {
21+
const double d = std::sqrt(d_squared);
22+
return std::variant<std::monostate, double, std::pair<double, double>>{
23+
std::in_place_type<std::pair<double, double>>, (-b - d) / 2 * a, (-b + d) / 2 * a};
24+
}
25+
}
26+
27+
} // namespace math
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cpp_test(test-07-01.cpp)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "gtest/gtest.h"
2+
#include "quadratic-equation.hpp"
3+
4+
using math::quadratic_equation_solution;
5+
6+
TEST(QuadraticEquationSolutionTest, NoSolutions) {
7+
auto result = quadratic_equation_solution(1.0, 0.0, 1.0);
8+
ASSERT_TRUE(std::holds_alternative<std::monostate>(result));
9+
}
10+
11+
TEST(QuadraticEquationSolutionTest, OneSolution) {
12+
auto result = quadratic_equation_solution(1.0, -2.0, 1.0); // NOLINT
13+
ASSERT_TRUE(std::holds_alternative<double>(result));
14+
const double solution = std::get<double>(result);
15+
EXPECT_DOUBLE_EQ(solution, 1.0);
16+
}
17+
18+
TEST(QuadraticEquationSolutionTest, OneSolution2) {
19+
auto result = quadratic_equation_solution(1.0, -4.0, 4.0); // NOLINT
20+
ASSERT_TRUE(std::holds_alternative<double>(result));
21+
const double solution = std::get<double>(result);
22+
EXPECT_DOUBLE_EQ(solution, 2.0);
23+
}
24+
25+
TEST(QuadraticEquationSolutionTest, TwoSolutionsDistinct) {
26+
auto result = quadratic_equation_solution(1.0, -3.0, 2.0); // NOLINT
27+
ASSERT_TRUE((std::holds_alternative<std::pair<double, double>>(result)));
28+
auto solutions = std::get<std::pair<double, double>>(result);
29+
30+
EXPECT_DOUBLE_EQ(solutions.first, 1.0);
31+
EXPECT_DOUBLE_EQ(solutions.second, 2.0);
32+
}
33+
34+
TEST(QuadraticEquationSolutionTest, LinearEquation) {
35+
EXPECT_THROW(quadratic_equation_solution(0.0, 2.0, -4.0);, std::invalid_argument); // NOLINT
36+
}
37+
38+
TEST(QuadraticEquationSolutionTest, ZeroCoefficientCase) {
39+
EXPECT_THROW(quadratic_equation_solution(0.0, 0.0, 1.0);, std::invalid_argument);
40+
}
41+
42+
TEST(QuadraticEquationSolutionTest, AllZeroCoefficients) {
43+
EXPECT_THROW(quadratic_equation_solution(0.0, 0.0, 0.0);, std::invalid_argument);
44+
}

0 commit comments

Comments
 (0)