Exercise: Hysteresis_nopoly (Non-Polymorphic)

Requirement

Implement a class Hysteresis_nopoly (a Thermostat) that works as follows.

  • It uses a sensor (a MockSensor_nopoly for now) to measure the temperature

  • It controls a switch (a MockSwitch_nopoly for now)

  • If the temperature falls below a certain configurable low threshold, the switch is set to on (heating is turned on)

  • If the temperature rises above a certain configurable high threshold, the switch is set to off (heating is turned on)

Use the following tests to drive you through the exercise:

#include <gtest/gtest.h>
#include <sensor-mock-nopoly.h>
#include <switch-mock-nopoly.h>
#include <hysteresis-nopoly.h>

TEST(hysteresis_suite, nop_when_within_range)
{
    MockSensor_nopoly sensor(30.2);
    MockSwitch_nopoly switcH(MockSwitch_nopoly::OFF);

    Hysteresis_nopoly hyst(&sensor, &switcH, 20.1, 30.4);

    hyst.check();                                      // <--- sees sensor well within range
    ASSERT_EQ(switcH.state(), MockSwitch_nopoly::OFF);
}
#include <gtest/gtest.h>
#include <sensor-mock-nopoly.h>
#include <switch-mock-nopoly.h>
#include <hysteresis-nopoly.h>

TEST(hysteresis_suite, falls_below_range)
{
    MockSensor_nopoly sensor(30.2);
    MockSwitch_nopoly switcH(MockSwitch_nopoly::OFF);

    Hysteresis_nopoly hyst(&sensor, &switcH, 
                           20.1, 30.4);                // <--- initially within range

    hyst.check();
    ASSERT_EQ(switcH.state(), MockSwitch_nopoly::OFF);

    sensor.set_temperature(20.0);                      // <--- falls below range

    hyst.check();
    ASSERT_EQ(switcH.state(), MockSwitch_nopoly::ON);
}
#include <gtest/gtest.h>
#include <sensor-mock-nopoly.h>
#include <switch-mock-nopoly.h>
#include <hysteresis-nopoly.h>

TEST(hysteresis_suite, rises_above_range)
{
    MockSensor_nopoly sensor(30.2);
    MockSwitch_nopoly switcH(MockSwitch_nopoly::OFF);

    Hysteresis_nopoly hyst(&sensor, &switcH, 20.1, 30.4);

    hyst.check();
    ASSERT_EQ(switcH.state(), MockSwitch_nopoly::OFF);

    sensor.set_temperature(35);                        // <--- rises above range

    hyst.check();
    ASSERT_EQ(switcH.state(), MockSwitch_nopoly::OFF);
}
#include <gtest/gtest.h>
#include <sensor-mock-nopoly.h>
#include <switch-mock-nopoly.h>
#include <hysteresis-nopoly.h>

TEST(hysteresis_suite, rises_above_range_when_on)
{
    MockSensor_nopoly sensor(30.2);
    MockSwitch_nopoly switcH(MockSwitch_nopoly::OFF);

    Hysteresis_nopoly hyst(&sensor, &switcH, 20.1, 30.4);

    sensor.set_temperature(-273.15);                   // <--- ensure that switch will be 
                                                       //      on after next check()

    hyst.check();
    ASSERT_EQ(switcH.state(), MockSwitch_nopoly::ON);

    sensor.set_temperature(35);                        // <--- rises above range

    hyst.check();
    ASSERT_EQ(switcH.state(), MockSwitch_nopoly::OFF);
}

Topics Covered

cluster_c The C Programming Language cluster_c_introduction Introduction cluster_cxx C++: Miscellaneous Live-Hacking cluster_cxx_exercises C++ Exercises cluster_linux Linux cluster_linux_toolchain Toolchain, And Cross Development cluster_linux_basics Linux Basics cluster_linux_basics_intro Introduction: Concepts and Terminology cluster_linux_basics_shell The Shell (Bash - “Bourne Again Shell”) cluster_cxx03 C++ cluster_cxx03_data_encapsulation Data Encapsulation c_introduction_installation Installation cxx_exercises_hysteresis_nopoly Exercise: Hysteresis_nopoly (Non-Polymorphic) cxx_exercises_sensor_mock_nopoly Exercise: MockSensor_nopoly (Non-Polymorphic) cxx_exercises_hysteresis_nopoly->cxx_exercises_sensor_mock_nopoly cxx_exercises_switch_mock_nopoly Exercise: Mocking Switch (Non-Polymorphic) cxx_exercises_hysteresis_nopoly->cxx_exercises_switch_mock_nopoly linux_toolchain_cmake_local CMake: Local Build cxx_exercises_hysteresis_nopoly->linux_toolchain_cmake_local cxx03_data_encapsulation_classes_objects Classes and Objects cxx_exercises_hysteresis_nopoly->cxx03_data_encapsulation_classes_objects cxx_exercises_sensor_mock_nopoly->linux_toolchain_cmake_local cxx_exercises_sensor_mock_nopoly->cxx03_data_encapsulation_classes_objects cxx_exercises_switch_mock_nopoly->linux_toolchain_cmake_local cxx_exercises_switch_mock_nopoly->cxx03_data_encapsulation_classes_objects linux_toolchain_static_library Object Code Archives/Static Libraries linux_toolchain_separate_compilation Zooming In: Separate Compilation, and Linking Statically linux_toolchain_static_library->linux_toolchain_separate_compilation linux_toolchain_basics Toolchain: Basics linux_basics_shell_file_dir_create_rm Creating And Removing Files and Directories linux_toolchain_basics->linux_basics_shell_file_dir_create_rm linux_toolchain_cmake_local->linux_toolchain_static_library linux_toolchain_separate_compilation->linux_toolchain_basics linux_basics_intro_overview Overview linux_basics_intro_process Processes, Scheduling, Address Spaces linux_basics_intro_process->linux_basics_intro_overview linux_basics_shell_paths Absolute and Relative Paths linux_basics_shell_file_dir_create_rm->linux_basics_shell_paths linux_basics_shell_cwd Current Working Directory linux_basics_shell_file_dir_create_rm->linux_basics_shell_cwd linux_basics_shell_commandline Commandline linux_basics_shell_paths->linux_basics_shell_commandline linux_basics_shell_commandline->linux_basics_intro_overview linux_basics_shell_cwd->linux_basics_intro_process linux_basics_shell_cwd->linux_basics_shell_paths linux_basics_shell_cwd->linux_basics_shell_commandline cxx03_introduction Introduction cxx03_introduction->c_introduction_installation cxx03_data_encapsulation_classes_objects->cxx03_introduction cxx03_data_encapsulation_c Object Oriented Programming In Good Ol’ C cxx03_data_encapsulation_classes_objects->cxx03_data_encapsulation_c cxx03_data_encapsulation_c->cxx03_introduction