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);
}