Added unit tests for virtual outputs.

This commit is contained in:
David Fenyes 2020-03-09 05:16:52 -05:00
parent 5a6a64dd55
commit 23237fae88
5 changed files with 332 additions and 67 deletions

View File

@ -33,7 +33,7 @@
// 23-25 PORTC0-2 ROW outputs (row number)
// 27 PORTC4
#include <stdio.h>
#include <stdint.h>
#include "asdf_keymap_defs.h"
#include "asdf_config.h"
@ -41,78 +41,70 @@
#include "asdf_arch.h"
typedef enum {
INITIAL_STATE,
STABLE_LOW,
STABLE_HIGH,
TRANSITION_LOW,
TRANSITION_HIGH,
PULSE_DELAY_LOW,
PULSE_DELAY_HIGH,
PULSE_HIGH_DETECTED,
PULSE_LOW_DETECTED,
NUM_VALID_PULSE_STATES, // error states below this
ERROR_DOUBLE_DELAY,
ERROR_DOUBLE_SET,
ERROR_NO_TRANSITION_BEFORE_DELAY,
ERROR_NO_TRANSITION_AFTER_DELAY,
ERROR_DOUBLE_TRANSITION, // fast pulse without delay
ERROR_DOUBLE_PULSE,
} pulse_state_t;
PULSE_EVENT_SET_HIGH,
PULSE_EVENT_SET_LOW,
PULSE_EVENT_DELAY,
NUM_PULSE_EVENTS
} pulse_event_t;
typedef enum { SET_HIGH, SET_LOW, PULSE_DELAY, NUM_PULSE_STATE_INPUTS } pulse_state_event_t;
static pulse_state_t pulse_transition_table[NUM_VALID_PULSE_STATES][NUM_PULSE_STATE_INPUTS] =
static pulse_state_t pulse_transition_table[PD_ST_NUM_VALID_PULSE_STATES][NUM_PULSE_EVENTS] =
{
[STABLE_LOW] =
[PD_ST_INITIAL_STATE] =
{
[SET_HIGH] = TRANSITION_HIGH,
[SET_LOW] = ERROR_DOUBLE_SET,
[PULSE_DELAY] = ERROR_NO_TRANSITION_BEFORE_DELAY,
[PULSE_EVENT_SET_HIGH] = PD_ST_STABLE_HIGH,
[PULSE_EVENT_SET_LOW] = PD_ST_STABLE_LOW,
[PULSE_EVENT_DELAY] = PD_ST_ERROR_PULSE_FROM_INITIAL_STATE,
},
[STABLE_HIGH] =
[PD_ST_STABLE_LOW] =
{
[SET_HIGH] = ERROR_DOUBLE_SET,
[SET_LOW] = TRANSITION_LOW,
[PULSE_DELAY] = ERROR_NO_TRANSITION_BEFORE_DELAY,
[PULSE_EVENT_SET_HIGH] = PD_ST_TRANSITION_HIGH,
[PULSE_EVENT_SET_LOW] = PD_ST_STABLE_LOW,
[PULSE_EVENT_DELAY] = PD_ST_ERROR_NO_TRANSITION_BEFORE_DELAY,
},
[TRANSITION_LOW] =
[PD_ST_STABLE_HIGH] =
{
[SET_HIGH] = ERROR_DOUBLE_TRANSITION,
[SET_LOW] = ERROR_DOUBLE_SET,
[PULSE_DELAY] = PULSE_DELAY_LOW,
[PULSE_EVENT_SET_HIGH] = PD_ST_STABLE_HIGH,
[PULSE_EVENT_SET_LOW] = PD_ST_TRANSITION_LOW,
[PULSE_EVENT_DELAY] = PD_ST_ERROR_NO_TRANSITION_BEFORE_DELAY,
},
[TRANSITION_HIGH] =
[PD_ST_TRANSITION_LOW] =
{
[SET_HIGH] = ERROR_DOUBLE_SET,
[SET_LOW] = ERROR_DOUBLE_TRANSITION,
[PULSE_DELAY] = PULSE_DELAY_HIGH,
[PULSE_EVENT_SET_HIGH] = PD_ST_TRANSITION_HIGH,
[PULSE_EVENT_SET_LOW] = PD_ST_STABLE_LOW,
[PULSE_EVENT_DELAY] = PD_ST_PULSE_DELAY_LOW,
},
[PULSE_DELAY_LOW] =
[PD_ST_TRANSITION_HIGH] =
{
[SET_HIGH] = PULSE_LOW_DETECTED,
[SET_LOW] = ERROR_NO_TRANSITION_AFTER_DELAY,
[PULSE_DELAY] = ERROR_DOUBLE_PULSE,
[PULSE_EVENT_SET_HIGH] = PD_ST_STABLE_HIGH,
[PULSE_EVENT_SET_LOW] = PD_ST_TRANSITION_LOW,
[PULSE_EVENT_DELAY] = PD_ST_PULSE_DELAY_HIGH,
},
[PULSE_DELAY_HIGH] =
[PD_ST_PULSE_DELAY_LOW] =
{
[SET_HIGH] = ERROR_NO_TRANSITION_AFTER_DELAY,
[SET_LOW] = PULSE_HIGH_DETECTED,
[PULSE_DELAY] = ERROR_DOUBLE_PULSE,
[PULSE_EVENT_SET_HIGH] = PD_ST_PULSE_LOW_DETECTED,
[PULSE_EVENT_SET_LOW] = PD_ST_ERROR_NO_TRANSITION_AFTER_DELAY,
[PULSE_EVENT_DELAY] = PD_ST_ERROR_DOUBLE_DELAY,
},
[PULSE_HIGH_DETECTED] =
[PD_ST_PULSE_DELAY_HIGH] =
{
[SET_HIGH] = STABLE_HIGH,
[SET_LOW] = STABLE_LOW,
[PULSE_DELAY] = ERROR_NO_TRANSITION_BEFORE_DELAY
[PULSE_EVENT_SET_HIGH] = PD_ST_ERROR_NO_TRANSITION_AFTER_DELAY,
[PULSE_EVENT_SET_LOW] = PD_ST_PULSE_HIGH_DETECTED,
[PULSE_EVENT_DELAY] = PD_ST_ERROR_DOUBLE_DELAY,
},
[PULSE_LOW_DETECTED] =
[PD_ST_PULSE_HIGH_DETECTED] =
{
[SET_HIGH] = STABLE_HIGH,
[SET_LOW] = STABLE_LOW,
[PULSE_DELAY] = ERROR_NO_TRANSITION_BEFORE_DELAY
[PULSE_EVENT_SET_HIGH] = PD_ST_TRANSITION_HIGH,
[PULSE_EVENT_SET_LOW] = PD_ST_STABLE_LOW,
[PULSE_EVENT_DELAY] = PD_ST_ERROR_NO_TRANSITION_BEFORE_DELAY
},
[PD_ST_PULSE_LOW_DETECTED] =
{
[PULSE_EVENT_SET_HIGH] = PD_ST_STABLE_HIGH,
[PULSE_EVENT_SET_LOW] = PD_ST_TRANSITION_LOW,
[PULSE_EVENT_DELAY] = PD_ST_ERROR_NO_TRANSITION_BEFORE_DELAY
},
};
static uint8_t outputs[NUM_REAL_OUTPUTS];
static pulse_state_t pulses[NUM_REAL_OUTPUTS];
@ -151,12 +143,12 @@ void asdf_arch_null_output(uint8_t value)
//
// COMPLEXITY: 2
//
static pulse_state_t pulse_detect(pulse_state_t current_state, pulse_state_event_t event)
static pulse_state_t pulse_detect(pulse_state_t current_state, pulse_event_t event)
{
pulse_state_t next_state = current_state;
// advance state if current state is valid (not an error state)
if (current_state < NUM_VALID_PULSE_STATES) {
if (current_state < PD_ST_NUM_VALID_PULSE_STATES) {
next_state = pulse_transition_table[current_state][event];
}
return next_state;
@ -181,7 +173,7 @@ static pulse_state_t pulse_detect(pulse_state_t current_state, pulse_state_event
//
static void set_output(asdf_virtual_real_dev_t output_dev, uint8_t value)
{
pulse_state_event_t pulse_event = value ? SET_HIGH : SET_LOW;
pulse_event_t pulse_event = value ? PULSE_EVENT_SET_HIGH : PULSE_EVENT_SET_LOW;
outputs[output_dev] = value;
pulses[output_dev] = pulse_detect(pulses[output_dev], pulse_event);
@ -372,6 +364,25 @@ uint8_t asdf_arch_check_output(asdf_virtual_real_dev_t device)
return outputs[device];
}
// PROCEDURE: asdf_arch_check_pulse
// INPUTS:(asdf_virtual_real_dev_t) device - which device to check
// OUTPUTS: the value of the device pulse detector
//
// DESCRIPTION: For a given real device, return the state of the pulse detector
//
// SIDE EFFECTS: none
//
// NOTES:
//
// SCOPE: public
//
// COMPLEXITY: 1
//
uint8_t asdf_arch_check_pulse(asdf_virtual_real_dev_t device)
{
return pulses[device];
}
// PROCEDURE: asdf_arch_pulse_delay
// INPUTS: none
// OUTPUTS: none
@ -390,7 +401,7 @@ uint8_t asdf_arch_check_output(asdf_virtual_real_dev_t device)
void asdf_arch_pulse_delay(void)
{
for (uint8_t i = 0; i < NUM_REAL_OUTPUTS; i++) {
pulses[i] = pulse_detect(pulses[i], PULSE_DELAY);
pulses[i] = pulse_detect(pulses[i], PULSE_EVENT_DELAY);
}
}
@ -410,7 +421,7 @@ void asdf_arch_init(void)
{
for (uint8_t i = 0; i < NUM_REAL_OUTPUTS; i++) {
outputs[i] = 0;
pulses[i] = INITIAL_STATE;
pulses[i] = PD_ST_INITIAL_STATE;
}
}

View File

@ -74,6 +74,27 @@
#include "asdf_virtual.h"
#include "asdf_arch.h"
typedef enum {
PD_ST_INITIAL_STATE = 0,
PD_ST_STABLE_LOW = 1,
PD_ST_STABLE_HIGH = 2,
PD_ST_TRANSITION_LOW = 3,
PD_ST_TRANSITION_HIGH = 4,
PD_ST_PULSE_DELAY_LOW = 5,
PD_ST_PULSE_DELAY_HIGH = 6,
PD_ST_PULSE_HIGH_DETECTED = 7,
PD_ST_PULSE_LOW_DETECTED = 8,
PD_ST_NUM_VALID_PULSE_STATES = 9, // error states below this
PD_ST_ERROR_DOUBLE_DELAY = 10,
PD_ST_ERROR_DOUBLE_SET = 11,
PD_ST_ERROR_NO_TRANSITION_BEFORE_DELAY = 12,
PD_ST_ERROR_NO_TRANSITION_AFTER_DELAY = 13,
PD_ST_ERROR_DOUBLE_TRANSITION = 14, // fast pulse without delay
PD_ST_ERROR_PULSE_FROM_INITIAL_STATE = 15,
} pulse_state_t;
static uint8_t outputs[NUM_REAL_OUTPUTS];
// PROCEDURE: asdf_arch_null_output
@ -142,6 +163,12 @@ void asdf_arch_out3_hi_z_set(uint8_t value);
// DESCRIPTION: For a given real device, return the current setting (true or false)
uint8_t asdf_arch_check_output(asdf_virtual_real_dev_t device);
// PROCEDURE: asdf_arch_check_pulse
// INPUTS:(asdf_virtual_real_dev_t) device - which device to check
// OUTPUTS: the value of the device pulse detector
// DESCRIPTION: For a given real device, return the state of the pulse detector
uint8_t asdf_arch_check_pulse(asdf_virtual_real_dev_t device);
// PROCEDURE: asdf_arch_pulse_delay
// INPUTS: none
// OUTPUTS: none

View File

@ -148,25 +148,51 @@
#define ASDF_TEST_CAPS_MAP_INDEX ASDF_TEST_BASE + 1
#define ASDF_TEST_KEYMAP_INITIALIZER_LENGTH 3
#define ASDF_TEST_KEYMAP_INITIALIZER \
#define ASDF_TEST_KEYMAP_INITIALIZER_1 \
{ \
{ \
.virtual_device = VCAPS_LED, \
.real_device = VMAP_LED1, \
/* Single assignment */ \
.virtual_device = VOUT1, \
.real_device = VMAP_OUT1, \
.function = V_NOFUNC, \
.initial_value = 1, \
.initial_value = 0, \
}, \
{ \
/* single toggle */ \
.virtual_device = VOUT2, \
.real_device = VMAP_OUT2, \
.function = V_TOGGLE, \
.initial_value = 0, \
}, \
{ \
/* single pulse */ \
.virtual_device = VOUT3, \
.real_device = VMAP_OUT3, \
.function = V_PULSE, \
.initial_value = 0, \
}, \
}
#define ASDF_TEST_KEYMAP_INITIALIZER_2 \
{ \
{ \
/* Triple assignment */ \
.virtual_device = VOUT1, \
.real_device = VMAP_OUT1, \
.function = V_TOGGLE, \
.initial_value = 0, \
}, \
{ \
.virtual_device = VOUT1, \
.real_device = VMAP_OUT1, \
.real_device = VMAP_OUT2, \
.function = V_TOGGLE, \
.initial_value = 1, \
}, \
{ \
.virtual_device = VOUT2, .real_device = VMAP_OUT3, .function = V_PULSE, .initial_value = 1, \
.virtual_device = VOUT1, .real_device = VMAP_OUT3, .function = V_TOGGLE, .initial_value = 0, \
} \
}
#define ASDF_TEST_KEYMAP_INITIALIZER ASDF_TEST_KEYMAP_INITIALIZER_1, ASDF_TEST_KEYMAP_INITIALIZER_2
// The following preprocessor "code" permits various keymaps to be created and
// included without generating a lot of complicating code dependencies. The use

View File

@ -140,8 +140,7 @@ static void asdf_virtual_real_toggle(asdf_virtual_real_dev_t real_out)
{
uint8_t value = real_device_table[real_out].shadow;
real_device_table[real_out].shadow = !value;
vout_set[real_out](value);
asdf_virtual_real_set(real_out, !value);
}
// PROCEDURE: asdf_virtual_action

View File

@ -0,0 +1,202 @@
#include <stdint.h>
#include <stdarg.h>
#include "asdf_arch.h"
#include "unity.h"
#include "asdf.h"
#include "asdf_ascii.h"
#include "asdf_modifiers.h"
#include "asdf_keymaps.h"
#include "asdf_keymap_defs.h"
#include "asdf_config.h"
uint32_t max(uint8_t first, uint8_t second)
{
uint32_t max = first;
if (second > max) {
max = second;
}
return max;
}
void setUp(void)
{
asdf_arch_init();
asdf_keymaps_init();
}
void tearDown(void) {}
// check to see that the longest keymap initializer sequence initia is detected.
void test_inizializer_length_is_max_length(void)
{
TEST_ASSERT_EQUAL_INT32(max(ASDF_TEST_KEYMAP_INITIALIZER_LENGTH,
ASDF_TEST2_KEYMAP_INITIALIZER_LENGTH), ASDF_KEYMAP_INITIALIZER_LENGTH);
}
void test_single_virtual_output_is_initialized(void)
{
// initially on keymap 0. Test to see that OUT1 has been initialized to 0.
TEST_ASSERT_EQUAL_INT32(asdf_arch_check_output(VMAP_OUT1), 0);
// and verify that this is not just the default value
TEST_ASSERT_NOT_EQUAL(ASDF_VIRTUAL_OUT_DEFAULT_VALUE, asdf_arch_check_output(VMAP_OUT1));
}
void test_uninitialized_virtual_out_is_default(void)
{
TEST_ASSERT_EQUAL_INT32(ASDF_VIRTUAL_OUT_DEFAULT_VALUE, asdf_arch_check_output(VMAP_LED1));
}
void test_set_virtual_output(void)
{
asdf_virtual_action(VOUT1, V_SET_LO);
TEST_ASSERT_EQUAL_INT32(0, asdf_arch_check_output(VMAP_OUT1));
asdf_virtual_action(VOUT1, V_SET_HI);
TEST_ASSERT_EQUAL_INT32(1, asdf_arch_check_output(VMAP_OUT1));
}
void test_toggle_virtual_output(void)
{
// start by setting vout1 to 0
asdf_virtual_action(VOUT1, V_SET_LO);
TEST_ASSERT_EQUAL_INT32(0, asdf_arch_check_output(VMAP_OUT1));
// toggle high
asdf_virtual_action(VOUT1, V_TOGGLE);
TEST_ASSERT_EQUAL_INT32(1, asdf_arch_check_output(VMAP_OUT1));
// toggle back low.
asdf_virtual_action(VOUT1, V_TOGGLE);
TEST_ASSERT_EQUAL_INT32(0, asdf_arch_check_output(VMAP_OUT1));
}
void test_pulse_high_virtual_output(void)
{
asdf_virtual_action(VOUT1, V_SET_LO);
TEST_ASSERT_EQUAL_INT32(PD_ST_STABLE_LOW, asdf_arch_check_pulse(VMAP_OUT1));
TEST_ASSERT_EQUAL_INT32(0, asdf_arch_check_output(VMAP_OUT1));
asdf_virtual_action(VOUT1, V_PULSE);
// output should be low
TEST_ASSERT_EQUAL_INT32(0, asdf_arch_check_output(VMAP_OUT1));
// high pulse should be detected.
TEST_ASSERT_EQUAL_INT32(PD_ST_PULSE_HIGH_DETECTED, asdf_arch_check_pulse(VMAP_OUT1));
}
void test_pulse_low_virtual_output(void)
{
asdf_virtual_action(VOUT1, V_SET_HI);
asdf_virtual_action(VOUT1, V_SET_HI);
TEST_ASSERT_EQUAL_INT32(PD_ST_STABLE_HIGH, asdf_arch_check_pulse(VMAP_OUT1));
TEST_ASSERT_EQUAL_INT32(1, asdf_arch_check_output(VMAP_OUT1));
asdf_virtual_action(VOUT1, V_PULSE);
// output should be high
TEST_ASSERT_EQUAL_INT32(1, asdf_arch_check_output(VMAP_OUT1));
// low pulse should be detected.
TEST_ASSERT_EQUAL_INT32(PD_ST_PULSE_LOW_DETECTED, asdf_arch_check_pulse(VMAP_OUT1));
}
// This test ties three real outputs to a virtual output and toggles the virtual
// output.
void test_toggle_triple_output(void)
{
asdf_keymaps_select_keymap(1);
// check that initial values have been set:
TEST_ASSERT_EQUAL_INT32(0, asdf_arch_check_output(VMAP_OUT1));
TEST_ASSERT_EQUAL_INT32(1, asdf_arch_check_output(VMAP_OUT2));
TEST_ASSERT_EQUAL_INT32(0, asdf_arch_check_output(VMAP_OUT3));
asdf_virtual_activate(VOUT1); // funtion is set to toggle
TEST_ASSERT_EQUAL_INT32(1, asdf_arch_check_output(VMAP_OUT1));
TEST_ASSERT_EQUAL_INT32(0, asdf_arch_check_output(VMAP_OUT2));
TEST_ASSERT_EQUAL_INT32(1, asdf_arch_check_output(VMAP_OUT3));
asdf_virtual_action(VOUT1, V_TOGGLE);
TEST_ASSERT_EQUAL_INT32(0, asdf_arch_check_output(VMAP_OUT1));
TEST_ASSERT_EQUAL_INT32(1, asdf_arch_check_output(VMAP_OUT2));
TEST_ASSERT_EQUAL_INT32(0, asdf_arch_check_output(VMAP_OUT3));
}
// This test ties three real outputs to a virtual output and sets the virtual
// output high and low
void test_set_triple_output(void)
{
asdf_keymaps_select_keymap(1);
// check that initial values have been set:
TEST_ASSERT_EQUAL_INT32(0, asdf_arch_check_output(VMAP_OUT1));
TEST_ASSERT_EQUAL_INT32(1, asdf_arch_check_output(VMAP_OUT2));
TEST_ASSERT_EQUAL_INT32(0, asdf_arch_check_output(VMAP_OUT3));
asdf_virtual_action(VOUT1, V_SET_HI);
TEST_ASSERT_EQUAL_INT32(1, asdf_arch_check_output(VMAP_OUT1));
TEST_ASSERT_EQUAL_INT32(1, asdf_arch_check_output(VMAP_OUT2));
TEST_ASSERT_EQUAL_INT32(1, asdf_arch_check_output(VMAP_OUT3));
asdf_virtual_action(VOUT1, V_SET_LO);
TEST_ASSERT_EQUAL_INT32(0, asdf_arch_check_output(VMAP_OUT1));
TEST_ASSERT_EQUAL_INT32(0, asdf_arch_check_output(VMAP_OUT2));
TEST_ASSERT_EQUAL_INT32(0, asdf_arch_check_output(VMAP_OUT3));
}
// This test ties three real outputs to a virtual output and pulses the virtual
// output high and low
void test_pulse_triple_output(void)
{
asdf_keymaps_select_keymap(1);
// check that initial values have been set:
TEST_ASSERT_EQUAL_INT32(0, asdf_arch_check_output(VMAP_OUT1));
TEST_ASSERT_EQUAL_INT32(1, asdf_arch_check_output(VMAP_OUT2));
TEST_ASSERT_EQUAL_INT32(0, asdf_arch_check_output(VMAP_OUT3));
asdf_virtual_action(VOUT1, V_SET_HI);
asdf_virtual_action(VOUT1, V_SET_HI);
TEST_ASSERT_EQUAL_INT32(1, asdf_arch_check_output(VMAP_OUT1));
TEST_ASSERT_EQUAL_INT32(1, asdf_arch_check_output(VMAP_OUT2));
TEST_ASSERT_EQUAL_INT32(1, asdf_arch_check_output(VMAP_OUT3));
TEST_ASSERT_EQUAL_INT32(PD_ST_STABLE_HIGH, asdf_arch_check_pulse(VMAP_OUT1));
TEST_ASSERT_EQUAL_INT32(PD_ST_STABLE_HIGH, asdf_arch_check_pulse(VMAP_OUT2));
TEST_ASSERT_EQUAL_INT32(PD_ST_STABLE_HIGH, asdf_arch_check_pulse(VMAP_OUT3));
asdf_virtual_action(VOUT1, V_SET_LO);
TEST_ASSERT_EQUAL_INT32(0, asdf_arch_check_output(VMAP_OUT1));
TEST_ASSERT_EQUAL_INT32(0, asdf_arch_check_output(VMAP_OUT2));
TEST_ASSERT_EQUAL_INT32(0, asdf_arch_check_output(VMAP_OUT3));
TEST_ASSERT_EQUAL_INT32(PD_ST_TRANSITION_LOW, asdf_arch_check_pulse(VMAP_OUT1));
TEST_ASSERT_EQUAL_INT32(PD_ST_TRANSITION_LOW, asdf_arch_check_pulse(VMAP_OUT2));
TEST_ASSERT_EQUAL_INT32(PD_ST_TRANSITION_LOW, asdf_arch_check_pulse(VMAP_OUT3));
// asdf_virtual_activate(VOUT1); // funtion is set to toggle
//TEST_ASSERT_EQUAL_INT32(1, asdf_arch_check_output(VMAP_OUT1));
//TEST_ASSERT_EQUAL_INT32(0, asdf_arch_check_output(VMAP_OUT2));
//TEST_ASSERT_EQUAL_INT32(1, asdf_arch_check_output(VMAP_OUT3));
//asdf_virtual_action(VOUT1, V_TOGGLE);
//TEST_ASSERT_EQUAL_INT32(0, asdf_arch_check_output(VMAP_OUT1));
//TEST_ASSERT_EQUAL_INT32(1, asdf_arch_check_output(VMAP_OUT2));
//TEST_ASSERT_EQUAL_INT32(0, asdf_arch_check_output(VMAP_OUT3));
}
int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_inizializer_length_is_max_length);
RUN_TEST(test_single_virtual_output_is_initialized);
RUN_TEST(test_uninitialized_virtual_out_is_default);
RUN_TEST(test_set_virtual_output);
RUN_TEST(test_toggle_virtual_output);
RUN_TEST(test_pulse_high_virtual_output);
RUN_TEST(test_pulse_low_virtual_output);
RUN_TEST(test_toggle_triple_output);
RUN_TEST(test_set_triple_output);
RUN_TEST(test_pulse_triple_output);
return UNITY_END();
}