-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Exercise solutions aligned with new exercises based on ac++11-502
- Loading branch information
1 parent
72bbc93
commit 8c08567
Showing
202 changed files
with
3,256 additions
and
3,835 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Alarm.cpp | ||
// See project README.md for disclaimer and additional information. | ||
// Feabhas Ltd | ||
|
||
#include "Alarm.h" | ||
#include <iostream> | ||
|
||
Alarm::Alarm() { | ||
std::clog << "Alarm default ctor " << this << '\n'; | ||
} | ||
|
||
Alarm::Alarm(Type alarm_init) : value{alarm_init} { | ||
std::clog << "Alarm non-default ctor " << this << '\n'; | ||
} | ||
|
||
Alarm::~Alarm() { | ||
std::clog << "Alarm dtor" << this << '\n'; | ||
} | ||
|
||
const char* Alarm::to_string() const { | ||
switch (value) { | ||
case advisory: | ||
return "advisory"; | ||
case caution: | ||
return "caution"; | ||
case warning: | ||
return "warning"; | ||
default: | ||
return "invalid"; | ||
} | ||
} | ||
|
||
Alarm::Type Alarm::type() const { | ||
return value; | ||
} | ||
|
||
std::ostream& operator<<(std::ostream& os, Alarm const& alarm) { | ||
os << alarm.to_string(); | ||
return os; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Alarm.h | ||
// See project README.md for disclaimer and additional information. | ||
// Feabhas Ltd | ||
|
||
#pragma once | ||
#ifndef ALARM_H | ||
#define ALARM_H | ||
|
||
#include <iosfwd> | ||
|
||
class Alarm { | ||
public: | ||
enum Type { invalid, advisory, caution, warning }; | ||
|
||
Alarm(); | ||
~Alarm(); | ||
explicit Alarm(Type alarm_init); | ||
|
||
Type type() const; | ||
|
||
const char* to_string() const; | ||
|
||
private: | ||
Type value{Type::invalid}; | ||
}; | ||
|
||
std::ostream& operator<<(std::ostream& os, Alarm const& alarm); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// main.cpp | ||
// See project README.md for disclaimer and additional information. | ||
// Feabhas Ltd | ||
|
||
#include "Alarm.h" | ||
#include <iostream> | ||
|
||
int main() { | ||
Alarm a1; // | ||
Alarm a2{}; // Value-initialised | ||
Alarm a3{Alarm::warning}; // Explicitly initialised | ||
|
||
std::cout << static_cast<int>(a1.type()) << '\n'; | ||
std::cout << a2.to_string() << '\n'; | ||
std::cout << a3 << '\n'; | ||
|
||
std::cout << "\nCompleted OK\n"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
find_package(GTest REQUIRED) | ||
include(GoogleTest) | ||
|
||
add_executable(AlarmTest alarm_tests.cpp ${CMAKE_SOURCE_DIR}/src/Alarm.cpp) | ||
|
||
target_include_directories(AlarmTest PRIVATE | ||
${CMAKE_SOURCE_DIR}/src | ||
) | ||
|
||
target_link_libraries(AlarmTest GTest::gtest GTest::gtest_main) | ||
|
||
gtest_discover_tests(AlarmTest) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// alarm_tests.cpp | ||
// See project README.md for disclaimer and additional information. | ||
// Feabhas Ltd | ||
|
||
#include "Alarm.h" | ||
#include "gtest/gtest.h" | ||
|
||
namespace { | ||
class AlarmTest : public ::testing::Test { | ||
protected: | ||
AlarmTest() = default; | ||
std::stringstream ss{}; | ||
}; | ||
} // namespace | ||
|
||
TEST_F(AlarmTest, type_invalid) { | ||
Alarm a{}; | ||
ASSERT_EQ(Alarm::invalid, a.type()); | ||
} | ||
|
||
TEST_F(AlarmTest, type_advisory) { | ||
Alarm a{Alarm::advisory}; | ||
ASSERT_EQ(Alarm::advisory, a.type()); | ||
} | ||
|
||
TEST_F(AlarmTest, type_caution) { | ||
Alarm a{Alarm::caution}; | ||
ASSERT_EQ(Alarm::caution, a.type()); | ||
} | ||
|
||
TEST_F(AlarmTest, type_warning) { | ||
Alarm a{Alarm::warning}; | ||
ASSERT_EQ(Alarm::warning, a.type()); | ||
} | ||
|
||
TEST_F(AlarmTest, string_invalid) { | ||
Alarm a{}; | ||
ASSERT_EQ("invalid", a.to_string()); | ||
} | ||
|
||
TEST_F(AlarmTest, string_advisory) { | ||
Alarm a{Alarm::advisory}; | ||
ASSERT_EQ("advisory", a.to_string()); | ||
} | ||
|
||
TEST_F(AlarmTest, string_caution) { | ||
Alarm a{Alarm::caution}; | ||
ASSERT_EQ("caution", a.to_string()); | ||
} | ||
|
||
TEST_F(AlarmTest, string_warning) { | ||
Alarm a{Alarm::warning}; | ||
ASSERT_EQ("warning", a.to_string()); | ||
} | ||
|
||
TEST_F(AlarmTest, ostream_invalid) { | ||
Alarm a{}; | ||
ss << a; | ||
ASSERT_EQ("invalid", ss.str()); | ||
} | ||
|
||
TEST_F(AlarmTest, ostream_advisory) { | ||
Alarm a{Alarm::advisory}; | ||
ss << a; | ||
ASSERT_EQ("advisory", ss.str()); | ||
} | ||
|
||
TEST_F(AlarmTest, ostream_caution) { | ||
Alarm a{Alarm::caution}; | ||
ss << a; | ||
ASSERT_EQ("caution", ss.str()); | ||
} | ||
|
||
TEST_F(AlarmTest, ostream_warning) { | ||
Alarm a{Alarm::warning}; | ||
ss << a; | ||
ASSERT_EQ("warning", ss.str()); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Alarm.cpp | ||
// See project README.md for disclaimer and additional information. | ||
// Feabhas Ltd | ||
|
||
#include "Alarm.h" | ||
#include <iostream> | ||
|
||
Alarm::Alarm(Type alarm_init) : value{ alarm_init } | ||
{} | ||
|
||
const char* Alarm::to_string() const { | ||
switch (value) { | ||
case Type::advisory: | ||
return "advisory"; | ||
case Type::caution: | ||
return "caution"; | ||
case Type::warning: | ||
return "warning"; | ||
default: | ||
return "invalid"; | ||
} | ||
} | ||
|
||
Alarm::Type Alarm::type() const { | ||
return value; | ||
} | ||
|
||
std::ostream& operator<<(std::ostream& os, Alarm const& alarm) { | ||
os << alarm.to_string(); | ||
return os; | ||
} | ||
|
||
Alarm make_alarm(Alarm::Type type) | ||
{ | ||
return Alarm{ type }; | ||
} | ||
|
||
void print_alarm(Alarm const& alarm) | ||
{ | ||
std::cout << static_cast<int>(alarm.type()) << ':' | ||
<< alarm.to_string() | ||
<< '\n'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Alarm.h | ||
// See project README.md for disclaimer and additional information. | ||
// Feabhas Ltd | ||
|
||
#pragma once | ||
#ifndef ALARM_H | ||
#define ALARM_H | ||
|
||
#include <iosfwd> | ||
|
||
class Alarm { | ||
public: | ||
enum class Type { invalid, advisory, caution, warning }; | ||
|
||
Alarm() = default; | ||
explicit Alarm(Type alarm_init); | ||
|
||
const char* to_string() const; | ||
Type type() const; | ||
|
||
private: | ||
Type value{ Type::invalid }; | ||
}; | ||
|
||
std::ostream& operator<<(std::ostream& os, const Alarm& alarm); | ||
|
||
void print_alarm(Alarm const& alarm); | ||
Alarm make_alarm(Alarm::Type type); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// main.cpp | ||
// See project README.md for disclaimer and additional information. | ||
// Feabhas Ltd | ||
|
||
#include "Alarm.h" | ||
#include <iostream> | ||
|
||
int main() | ||
{ | ||
Alarm a1; // | ||
Alarm a2{}; // Value-initialised | ||
Alarm a3{ Alarm::Type::warning }; // Explicitly initialised | ||
auto a4 = make_alarm(Alarm::Type::caution); // factory-function | ||
print_alarm(a2); | ||
print_alarm(a3); | ||
print_alarm(a4); | ||
|
||
// std::cout << static_cast<int>(a1.type()) << '\n'; | ||
// std::cout << a2.to_string() << '\n'; | ||
// std::cout << a1 << '\n'; | ||
// std::cout << a2 << '\n'; | ||
// std::cout << a3 << '\n'; | ||
// std::cout << a4 << '\n'; | ||
a1 = make_alarm(Alarm::Type::advisory); // RVO | ||
print_alarm(a1); | ||
std::cout << "\nCompleted OK\n"; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.