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 temperature

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

Topics Covered

cluster_linux Linux cluster_linux_toolchain Toolchain, And Cross Development cluster_linux_basics Linux Basics cluster_linux_basics_shell The Shell (Bash - “Bourne Again Shell”) cluster_linux_basics_intro Introduction: Concepts and Terminology cluster_c The C Programming Language cluster_c_introduction Introduction cluster_cxx C++: Miscellaneous Live-Hacking cluster_cxx_exercises C++ Exercises cluster_cxx03 C++ cluster_cxx03_data_encapsulation Data Encapsulation 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 CMake: Local Build linux_toolchain_cmake_local->linux_toolchain_static_library linux_toolchain_separate_compilation->linux_toolchain_basics linux_basics_shell_cwd Current Working Directory linux_basics_shell_commandline Commandline linux_basics_shell_cwd->linux_basics_shell_commandline linux_basics_shell_paths Absolute and Relative Paths linux_basics_shell_cwd->linux_basics_shell_paths linux_basics_intro_process Processes, Scheduling, Address Spaces linux_basics_shell_cwd->linux_basics_intro_process linux_basics_shell_file_dir_create_rm->linux_basics_shell_cwd linux_basics_shell_file_dir_create_rm->linux_basics_shell_paths linux_basics_intro_overview Overview linux_basics_shell_commandline->linux_basics_intro_overview linux_basics_shell_paths->linux_basics_shell_commandline linux_basics_intro_process->linux_basics_intro_overview c_introduction_installation Installation cxx_exercises_sensor_mock_nopoly Exercise: MockSensor_nopoly (Non-Polymorphic) cxx_exercises_sensor_mock_nopoly->linux_toolchain_cmake_local cxx03_data_encapsulation_classes_objects Classes and Objects cxx_exercises_sensor_mock_nopoly->cxx03_data_encapsulation_classes_objects cxx_exercises_switch_mock_nopoly Exercise: Mocking Switch (Non-Polymorphic) cxx_exercises_switch_mock_nopoly->linux_toolchain_cmake_local cxx_exercises_switch_mock_nopoly->cxx03_data_encapsulation_classes_objects cxx_exercises_display_led_stripe_nopoly Exercise: LEDStripeDisplay_nopoly (Non-Polymorphic) cxx_exercises_display_led_stripe_nopoly->linux_toolchain_cmake_local cxx_exercises_display_led_stripe_nopoly->cxx_exercises_sensor_mock_nopoly cxx_exercises_display_led_stripe_nopoly->cxx_exercises_switch_mock_nopoly cxx_exercises_display_led_stripe_nopoly->cxx03_data_encapsulation_classes_objects 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