Skip to content

Commit

Permalink
Exercise solutions aligned with new exercises based on ac++11-502
Browse files Browse the repository at this point in the history
  • Loading branch information
martinbond7 committed Oct 23, 2024
1 parent 72bbc93 commit 8c08567
Show file tree
Hide file tree
Showing 202 changed files with 3,256 additions and 3,835 deletions.
40 changes: 40 additions & 0 deletions solutions/01-user-defined-types/src/Alarm.cpp
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;
}
29 changes: 29 additions & 0 deletions solutions/01-user-defined-types/src/Alarm.h
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
18 changes: 18 additions & 0 deletions solutions/01-user-defined-types/src/main.cpp
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";
}
12 changes: 12 additions & 0 deletions solutions/01-user-defined-types/tests/CMakeLists.txt
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)
78 changes: 78 additions & 0 deletions solutions/01-user-defined-types/tests/alarm_tests.cpp
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());
}
11 changes: 0 additions & 11 deletions solutions/01_object_model/src/main.cpp

This file was deleted.

43 changes: 43 additions & 0 deletions solutions/02-functions-constants/src/Alarm.cpp
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';
}
30 changes: 30 additions & 0 deletions solutions/02-functions-constants/src/Alarm.h
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
27 changes: 27 additions & 0 deletions solutions/02-functions-constants/src/main.cpp
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";
}
22 changes: 0 additions & 22 deletions solutions/02_user_defined_types/src/Event.cpp

This file was deleted.

23 changes: 0 additions & 23 deletions solutions/02_user_defined_types/src/Event.h

This file was deleted.

15 changes: 0 additions & 15 deletions solutions/02_user_defined_types/src/main.cpp

This file was deleted.

Loading

0 comments on commit 8c08567

Please sign in to comment.