Exercise: LEDStripeDisplay_nopoly
(Non-Polymorphic)¶
Requirement¶
Implement a temperature display (class LEDStripeDisplay_nopoly
) as
follows.
It uses a sensor (a
MockSensor_nopoly
for now) to measure the temperatureIt controls an “LED bar” consisting of multiple switches (
MockSwitch_nopoly
), one per LED.It is configured to show temperatures within a certain range
Temperatures below that range turn all LEDs off
Temperatures above that range turn all LEDs on
Temperatures within range are associated with one specific LED, which is turned on together with all LEDs left of it
The following tests formally specify the requirement if anything is left unclear. Use these to complete the exercise, one by one.
#include <gtest/gtest.h>
#include <switch-mock-nopoly.h>
#include <sensor-mock-nopoly.h>
#include <display-led-stripe-nopoly.h>
#include <vector>
TEST(display_led_stripe_nopoly_suite, somewhere_in_the_middle)
{
MockSensor_nopoly sensor(36.5); // <--- right in the middle of [0,80]
MockSwitch_nopoly sw0(MockSwitch_nopoly::OFF);
MockSwitch_nopoly sw1(MockSwitch_nopoly::OFF);
MockSwitch_nopoly sw2(MockSwitch_nopoly::OFF);
MockSwitch_nopoly sw3(MockSwitch_nopoly::OFF);
MockSwitch_nopoly sw4(MockSwitch_nopoly::OFF);
MockSwitch_nopoly sw5(MockSwitch_nopoly::OFF);
MockSwitch_nopoly sw6(MockSwitch_nopoly::OFF);
MockSwitch_nopoly sw7(MockSwitch_nopoly::OFF);
std::vector<MockSwitch_nopoly*> switches = { &sw0, &sw1, &sw2, &sw3, &sw4, &sw5, &sw6, &sw7 };
LEDStripeDisplay_nopoly display(
0, 80, // <--- [0,80]
&sensor, switches);
display.check();
ASSERT_EQ(sw0.state(), MockSwitch_nopoly::ON);
ASSERT_EQ(sw1.state(), MockSwitch_nopoly::ON);
ASSERT_EQ(sw2.state(), MockSwitch_nopoly::ON);
ASSERT_EQ(sw3.state(), MockSwitch_nopoly::ON);
ASSERT_EQ(sw4.state(), MockSwitch_nopoly::OFF);
ASSERT_EQ(sw5.state(), MockSwitch_nopoly::OFF);
ASSERT_EQ(sw6.state(), MockSwitch_nopoly::OFF);
ASSERT_EQ(sw7.state(), MockSwitch_nopoly::OFF);
}
#include <gtest/gtest.h>
#include <switch-mock-nopoly.h>
#include <sensor-mock-nopoly.h>
#include <display-led-stripe-nopoly.h>
#include <vector>
TEST(display_led_stripe_nopoly_suite, below_range)
{
MockSensor_nopoly sensor(-10); // <--- below [0,80]
MockSwitch_nopoly sw0(MockSwitch_nopoly::OFF);
MockSwitch_nopoly sw1(MockSwitch_nopoly::OFF);
MockSwitch_nopoly sw2(MockSwitch_nopoly::OFF);
MockSwitch_nopoly sw3(MockSwitch_nopoly::OFF);
MockSwitch_nopoly sw4(MockSwitch_nopoly::OFF);
MockSwitch_nopoly sw5(MockSwitch_nopoly::OFF);
MockSwitch_nopoly sw6(MockSwitch_nopoly::OFF);
MockSwitch_nopoly sw7(MockSwitch_nopoly::OFF);
std::vector<MockSwitch_nopoly*> switches = { &sw0, &sw1, &sw2, &sw3, &sw4, &sw5, &sw6, &sw7 };
LEDStripeDisplay_nopoly display(
0, 80, // <--- [0,80]
&sensor, switches);
display.check();
ASSERT_EQ(sw0.state(), MockSwitch_nopoly::OFF);
ASSERT_EQ(sw1.state(), MockSwitch_nopoly::OFF);
ASSERT_EQ(sw2.state(), MockSwitch_nopoly::OFF);
ASSERT_EQ(sw3.state(), MockSwitch_nopoly::OFF);
ASSERT_EQ(sw4.state(), MockSwitch_nopoly::OFF);
ASSERT_EQ(sw5.state(), MockSwitch_nopoly::OFF);
ASSERT_EQ(sw6.state(), MockSwitch_nopoly::OFF);
ASSERT_EQ(sw7.state(), MockSwitch_nopoly::OFF);
}
#include <gtest/gtest.h>
#include <switch-mock-nopoly.h>
#include <sensor-mock-nopoly.h>
#include <display-led-stripe-nopoly.h>
#include <vector>
TEST(display_led_stripe_nopoly_suite, above_range)
{
MockSensor_nopoly sensor(100); // <--- below [0,80]
MockSwitch_nopoly sw0(MockSwitch_nopoly::OFF);
MockSwitch_nopoly sw1(MockSwitch_nopoly::OFF);
MockSwitch_nopoly sw2(MockSwitch_nopoly::OFF);
MockSwitch_nopoly sw3(MockSwitch_nopoly::OFF);
MockSwitch_nopoly sw4(MockSwitch_nopoly::OFF);
MockSwitch_nopoly sw5(MockSwitch_nopoly::OFF);
MockSwitch_nopoly sw6(MockSwitch_nopoly::OFF);
MockSwitch_nopoly sw7(MockSwitch_nopoly::OFF);
std::vector<MockSwitch_nopoly*> switches = { &sw0, &sw1, &sw2, &sw3, &sw4, &sw5, &sw6, &sw7 };
LEDStripeDisplay_nopoly display(
0, 80, // <--- [0,80]
&sensor, switches);
display.check();
ASSERT_EQ(sw0.state(), MockSwitch_nopoly::ON);
ASSERT_EQ(sw1.state(), MockSwitch_nopoly::ON);
ASSERT_EQ(sw2.state(), MockSwitch_nopoly::ON);
ASSERT_EQ(sw3.state(), MockSwitch_nopoly::ON);
ASSERT_EQ(sw4.state(), MockSwitch_nopoly::ON);
ASSERT_EQ(sw5.state(), MockSwitch_nopoly::ON);
ASSERT_EQ(sw6.state(), MockSwitch_nopoly::ON);
ASSERT_EQ(sw7.state(), MockSwitch_nopoly::ON);
}
#include <gtest/gtest.h>
#include <switch-mock-nopoly.h>
#include <sensor-mock-nopoly.h>
#include <display-led-stripe-nopoly.h>
#include <vector>
TEST(display_led_stripe_nopoly_suite, movement)
{
MockSensor_nopoly sensor(36.5); // <--- initially in the middle
MockSwitch_nopoly sw0(MockSwitch_nopoly::OFF);
MockSwitch_nopoly sw1(MockSwitch_nopoly::OFF);
MockSwitch_nopoly sw2(MockSwitch_nopoly::OFF);
MockSwitch_nopoly sw3(MockSwitch_nopoly::OFF);
MockSwitch_nopoly sw4(MockSwitch_nopoly::OFF);
MockSwitch_nopoly sw5(MockSwitch_nopoly::OFF);
MockSwitch_nopoly sw6(MockSwitch_nopoly::OFF);
MockSwitch_nopoly sw7(MockSwitch_nopoly::OFF);
std::vector<MockSwitch_nopoly*> switches = { &sw0, &sw1, &sw2, &sw3, &sw4, &sw5, &sw6, &sw7 };
LEDStripeDisplay_nopoly display(
0, 80, // <--- [0,80]
&sensor, switches);
display.check();
ASSERT_EQ(sw0.state(), MockSwitch_nopoly::ON);
ASSERT_EQ(sw1.state(), MockSwitch_nopoly::ON);
ASSERT_EQ(sw2.state(), MockSwitch_nopoly::ON);
ASSERT_EQ(sw3.state(), MockSwitch_nopoly::ON);
ASSERT_EQ(sw4.state(), MockSwitch_nopoly::OFF);
ASSERT_EQ(sw5.state(), MockSwitch_nopoly::OFF);
ASSERT_EQ(sw6.state(), MockSwitch_nopoly::OFF);
ASSERT_EQ(sw7.state(), MockSwitch_nopoly::OFF);
sensor.set_temperature(67); // <--- temperature moves into segment [60,70]
display.check();
ASSERT_EQ(sw0.state(), MockSwitch_nopoly::ON);
ASSERT_EQ(sw1.state(), MockSwitch_nopoly::ON);
ASSERT_EQ(sw2.state(), MockSwitch_nopoly::ON);
ASSERT_EQ(sw3.state(), MockSwitch_nopoly::ON);
ASSERT_EQ(sw4.state(), MockSwitch_nopoly::ON);
ASSERT_EQ(sw5.state(), MockSwitch_nopoly::ON);
ASSERT_EQ(sw6.state(), MockSwitch_nopoly::ON);
ASSERT_EQ(sw7.state(), MockSwitch_nopoly::OFF);
sensor.set_temperature(9); // <--- back down into [0,10]
display.check();
ASSERT_EQ(sw0.state(), MockSwitch_nopoly::ON);
ASSERT_EQ(sw1.state(), MockSwitch_nopoly::OFF);
ASSERT_EQ(sw2.state(), MockSwitch_nopoly::OFF);
ASSERT_EQ(sw3.state(), MockSwitch_nopoly::OFF);
ASSERT_EQ(sw4.state(), MockSwitch_nopoly::OFF);
ASSERT_EQ(sw5.state(), MockSwitch_nopoly::OFF);
ASSERT_EQ(sw6.state(), MockSwitch_nopoly::OFF);
ASSERT_EQ(sw7.state(), MockSwitch_nopoly::OFF);
sensor.set_temperature(-10); // <--- below range -> all off
display.check();
ASSERT_EQ(sw0.state(), MockSwitch_nopoly::OFF);
ASSERT_EQ(sw1.state(), MockSwitch_nopoly::OFF);
ASSERT_EQ(sw2.state(), MockSwitch_nopoly::OFF);
ASSERT_EQ(sw3.state(), MockSwitch_nopoly::OFF);
ASSERT_EQ(sw4.state(), MockSwitch_nopoly::OFF);
ASSERT_EQ(sw5.state(), MockSwitch_nopoly::OFF);
ASSERT_EQ(sw6.state(), MockSwitch_nopoly::OFF);
ASSERT_EQ(sw7.state(), MockSwitch_nopoly::OFF);
}
#include <gtest/gtest.h>
#include <switch-mock-nopoly.h>
#include <sensor-mock-nopoly.h>
#include <display-led-stripe-nopoly.h>
#include <vector>
TEST(display_led_stripe_nopoly_suite, variation_2_switches)
{
MockSensor_nopoly sensor(36.5); // <--- right in the middle of [0,80]
MockSwitch_nopoly sw0(MockSwitch_nopoly::OFF);
MockSwitch_nopoly sw1(MockSwitch_nopoly::OFF);
std::vector<MockSwitch_nopoly*> switches = { &sw0, &sw1 };
LEDStripeDisplay_nopoly display(
0, 80, // <--- [0,80]
&sensor, switches);
display.check();
ASSERT_EQ(sw0.state(), MockSwitch_nopoly::ON);
ASSERT_EQ(sw1.state(), MockSwitch_nopoly::OFF);
}
TEST(display_led_stripe_nopoly_suite, variation_interval_not_from_0)
{
MockSensor_nopoly sensor(10.7); // <--- right above low
MockSwitch_nopoly sw0(MockSwitch_nopoly::OFF);
MockSwitch_nopoly sw1(MockSwitch_nopoly::OFF);
std::vector<MockSwitch_nopoly*> switches = { &sw0, &sw1 };
LEDStripeDisplay_nopoly display(
10.5, 80, // <--- [10.5,80]
&sensor, switches);
display.check();
ASSERT_EQ(sw0.state(), MockSwitch_nopoly::ON);
ASSERT_EQ(sw1.state(), MockSwitch_nopoly::OFF);
sensor.set_temperature(45.7); // <--- into second LED
display.check();
ASSERT_EQ(sw0.state(), MockSwitch_nopoly::ON);
ASSERT_EQ(sw1.state(), MockSwitch_nopoly::ON);
}