From c66df1664497546f32662409778731143e45a552 Mon Sep 17 00:00:00 2001 From: James Young <18669334+noroadsleft@users.noreply.github.com> Date: Sat, 28 Nov 2020 12:02:18 -0800 Subject: 2020 November 28 Breaking Changes Update (#11053) * Branch point for 2020 November 28 Breaking Change * Remove matrix_col_t to allow MATRIX_ROWS > 32 (#10183) * Add support for soft serial to ATmega32U2 (#10204) * Change MIDI velocity implementation to allow direct control of velocity value (#9940) * Add ability to build a subset of all keyboards based on platform. * Actually use eeprom_driver_init(). * Make bootloader_jump weak for ChibiOS. (#10417) * Joystick 16-bit support (#10439) * Per-encoder resolutions (#10259) * Share button state from mousekey to pointing_device (#10179) * Add hotfix for chibios keyboards not wake (#10088) * Add advanced/efficient RGB Matrix Indicators (#8564) * Naming change. * Support for STM32 GPIOF,G,H,I,J,K (#10206) * Add milc as a dependency and remove the installed milc (#10563) * ChibiOS upgrade: early init conversions (#10214) * ChibiOS upgrade: configuration file migrator (#9952) * Haptic and solenoid cleanup (#9700) * XD75 cleanup (#10524) * OLED display update interval support (#10388) * Add definition based on currently-selected serial driver. (#10716) * New feature: Retro Tapping per key (#10622) * Allow for modification of output RGB values when using rgblight/rgb_matrix. (#10638) * Add housekeeping task callbacks so that keyboards/keymaps are capable of executing code for each main loop iteration. (#10530) * Rescale both ChibiOS and AVR backlighting. * Reduce Helix keyboard build variation (#8669) * Minor change to behavior allowing display updates to continue between task ticks (#10750) * Some GPIO manipulations in matrix.c change to atomic. (#10491) * qmk cformat (#10767) * [Keyboard] Update the Speedo firmware for v3.0 (#10657) * Maartenwut/Maarten namechange to evyd13/Evy (#10274) * [quantum] combine repeated lines of code (#10837) * Add step sequencer feature (#9703) * aeboards/ext65 refactor (#10820) * Refactor xelus/dawn60 for Rev2 later (#10584) * add DEBUG_MATRIX_SCAN_RATE_ENABLE to common_features.mk (#10824) * [Core] Added `add_oneshot_mods` & `del_oneshot_mods` (#10549) * update chibios os usb for the otg driver (#8893) * Remove HD44780 References, Part 4 (#10735) * [Keyboard] Add Valor FRL TKL (+refactor) (#10512) * Fix cursor position bug in oled_write_raw functions (#10800) * Fixup version.h writing when using SKIP_VERSION=yes (#10972) * Allow for certain code in the codebase assuming length of string. (#10974) * Add AT90USB support for serial.c (#10706) * Auto shift: support repeats and early registration (#9826) * Rename ledmatrix.h to match .c file (#7949) * Split RGB_MATRIX_ENABLE into _ENABLE and _DRIVER (#10231) * Split LED_MATRIX_ENABLE into _ENABLE and _DRIVER (#10840) * Merge point for 2020 Nov 28 Breaking Change --- quantum/sequencer/tests/midi_mock.c | 26 ++ quantum/sequencer/tests/midi_mock.h | 26 ++ quantum/sequencer/tests/rules.mk | 11 + quantum/sequencer/tests/sequencer_tests.cpp | 590 ++++++++++++++++++++++++++++ quantum/sequencer/tests/testlist.mk | 1 + 5 files changed, 654 insertions(+) create mode 100644 quantum/sequencer/tests/midi_mock.c create mode 100644 quantum/sequencer/tests/midi_mock.h create mode 100644 quantum/sequencer/tests/rules.mk create mode 100644 quantum/sequencer/tests/sequencer_tests.cpp create mode 100644 quantum/sequencer/tests/testlist.mk (limited to 'quantum/sequencer/tests') diff --git a/quantum/sequencer/tests/midi_mock.c b/quantum/sequencer/tests/midi_mock.c new file mode 100644 index 0000000000..236e16f9d7 --- /dev/null +++ b/quantum/sequencer/tests/midi_mock.c @@ -0,0 +1,26 @@ +/* Copyright 2020 Rodolphe Belouin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "midi_mock.h" + +uint16_t last_noteon = 0; +uint16_t last_noteoff = 0; + +uint16_t midi_compute_note(uint16_t keycode) { return keycode; } + +void process_midi_basic_noteon(uint16_t note) { last_noteon = note; } + +void process_midi_basic_noteoff(uint16_t note) { last_noteoff = note; } diff --git a/quantum/sequencer/tests/midi_mock.h b/quantum/sequencer/tests/midi_mock.h new file mode 100644 index 0000000000..4d8c2eb307 --- /dev/null +++ b/quantum/sequencer/tests/midi_mock.h @@ -0,0 +1,26 @@ +/* Copyright 2020 Rodolphe Belouin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include + +extern uint16_t last_noteon; +extern uint16_t last_noteoff; + +uint16_t midi_compute_note(uint16_t keycode); +void process_midi_basic_noteon(uint16_t note); +void process_midi_basic_noteoff(uint16_t note); diff --git a/quantum/sequencer/tests/rules.mk b/quantum/sequencer/tests/rules.mk new file mode 100644 index 0000000000..76c221cf92 --- /dev/null +++ b/quantum/sequencer/tests/rules.mk @@ -0,0 +1,11 @@ +# The letter case of these variables might seem odd. However: +# - it is consistent with the serial_link example that is used as a reference in the Unit Testing article (https://docs.qmk.fm/#/unit_testing?id=adding-tests-for-new-or-existing-features) +# - Neither `make test:sequencer` or `make test:SEQUENCER` work when using SCREAMING_SNAKE_CASE + +sequencer_DEFS := -DNO_DEBUG -DMIDI_MOCKED + +sequencer_SRC := \ + $(QUANTUM_PATH)/sequencer/tests/midi_mock.c \ + $(QUANTUM_PATH)/sequencer/tests/sequencer_tests.cpp \ + $(QUANTUM_PATH)/sequencer/sequencer.c \ + $(TMK_PATH)/common/test/timer.c diff --git a/quantum/sequencer/tests/sequencer_tests.cpp b/quantum/sequencer/tests/sequencer_tests.cpp new file mode 100644 index 0000000000..e81984e5b5 --- /dev/null +++ b/quantum/sequencer/tests/sequencer_tests.cpp @@ -0,0 +1,590 @@ +/* Copyright 2020 Rodolphe Belouin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "gtest/gtest.h" + +extern "C" { +#include "sequencer.h" +#include "midi_mock.h" +#include "quantum/quantum_keycodes.h" +} + +extern "C" { +void set_time(uint32_t t); +void advance_time(uint32_t ms); +} + +class SequencerTest : public ::testing::Test { + protected: + void SetUp() override { + config_copy.enabled = sequencer_config.enabled; + + for (int i = 0; i < SEQUENCER_STEPS; i++) { + config_copy.steps[i] = sequencer_config.steps[i]; + } + + for (int i = 0; i < SEQUENCER_TRACKS; i++) { + config_copy.track_notes[i] = sequencer_config.track_notes[i]; + } + + config_copy.tempo = sequencer_config.tempo; + config_copy.resolution = sequencer_config.resolution; + + state_copy.active_tracks = sequencer_internal_state.active_tracks; + state_copy.current_track = sequencer_internal_state.current_track; + state_copy.current_step = sequencer_internal_state.current_step; + state_copy.timer = sequencer_internal_state.timer; + + last_noteon = 0; + last_noteoff = 0; + + set_time(0); + } + + void TearDown() override { + sequencer_config.enabled = config_copy.enabled; + + for (int i = 0; i < SEQUENCER_STEPS; i++) { + sequencer_config.steps[i] = config_copy.steps[i]; + } + + for (int i = 0; i < SEQUENCER_TRACKS; i++) { + sequencer_config.track_notes[i] = config_copy.track_notes[i]; + } + + sequencer_config.tempo = config_copy.tempo; + sequencer_config.resolution = config_copy.resolution; + + sequencer_internal_state.active_tracks = state_copy.active_tracks; + sequencer_internal_state.current_track = state_copy.current_track; + sequencer_internal_state.current_step = state_copy.current_step; + sequencer_internal_state.timer = state_copy.timer; + } + + sequencer_config_t config_copy; + sequencer_state_t state_copy; +}; + +TEST_F(SequencerTest, TestOffByDefault) { EXPECT_EQ(is_sequencer_on(), false); } + +TEST_F(SequencerTest, TestOn) { + sequencer_config.enabled = false; + + sequencer_on(); + EXPECT_EQ(is_sequencer_on(), true); + + // sequencer_on is idempotent + sequencer_on(); + EXPECT_EQ(is_sequencer_on(), true); +} + +TEST_F(SequencerTest, TestOff) { + sequencer_config.enabled = true; + + sequencer_off(); + EXPECT_EQ(is_sequencer_on(), false); + + // sequencer_off is idempotent + sequencer_off(); + EXPECT_EQ(is_sequencer_on(), false); +} + +TEST_F(SequencerTest, TestToggle) { + sequencer_config.enabled = false; + + sequencer_toggle(); + EXPECT_EQ(is_sequencer_on(), true); + + sequencer_toggle(); + EXPECT_EQ(is_sequencer_on(), false); +} + +TEST_F(SequencerTest, TestNoActiveTrackByDefault) { + for (int i = 0; i < SEQUENCER_TRACKS; i++) { + EXPECT_EQ(is_sequencer_track_active(i), false); + } +} + +TEST_F(SequencerTest, TestGetActiveTracks) { + sequencer_internal_state.active_tracks = (1 << 7) + (1 << 6) + (1 << 3) + (1 << 1) + (1 << 0); + + EXPECT_EQ(is_sequencer_track_active(0), true); + EXPECT_EQ(is_sequencer_track_active(1), true); + EXPECT_EQ(is_sequencer_track_active(2), false); + EXPECT_EQ(is_sequencer_track_active(3), true); + EXPECT_EQ(is_sequencer_track_active(4), false); + EXPECT_EQ(is_sequencer_track_active(5), false); + EXPECT_EQ(is_sequencer_track_active(6), true); + EXPECT_EQ(is_sequencer_track_active(7), true); +} + +TEST_F(SequencerTest, TestGetActiveTracksOutOfBound) { + sequencer_set_track_activation(-1, true); + sequencer_set_track_activation(8, true); + + EXPECT_EQ(is_sequencer_track_active(-1), false); + EXPECT_EQ(is_sequencer_track_active(8), false); +} + +TEST_F(SequencerTest, TestToggleTrackActivation) { + sequencer_internal_state.active_tracks = (1 << 7) + (1 << 6) + (1 << 3) + (1 << 1) + (1 << 0); + + sequencer_toggle_track_activation(6); + + EXPECT_EQ(is_sequencer_track_active(0), true); + EXPECT_EQ(is_sequencer_track_active(1), true); + EXPECT_EQ(is_sequencer_track_active(2), false); + EXPECT_EQ(is_sequencer_track_active(3), true); + EXPECT_EQ(is_sequencer_track_active(4), false); + EXPECT_EQ(is_sequencer_track_active(5), false); + EXPECT_EQ(is_sequencer_track_active(6), false); + EXPECT_EQ(is_sequencer_track_active(7), true); +} + +TEST_F(SequencerTest, TestToggleSingleTrackActivation) { + sequencer_internal_state.active_tracks = (1 << 7) + (1 << 6) + (1 << 3) + (1 << 1) + (1 << 0); + + sequencer_toggle_single_active_track(2); + + EXPECT_EQ(is_sequencer_track_active(0), false); + EXPECT_EQ(is_sequencer_track_active(1), false); + EXPECT_EQ(is_sequencer_track_active(2), true); + EXPECT_EQ(is_sequencer_track_active(3), false); + EXPECT_EQ(is_sequencer_track_active(4), false); + EXPECT_EQ(is_sequencer_track_active(5), false); + EXPECT_EQ(is_sequencer_track_active(6), false); + EXPECT_EQ(is_sequencer_track_active(7), false); +} + +TEST_F(SequencerTest, TestStepOffByDefault) { + for (int i = 0; i < SEQUENCER_STEPS; i++) { + EXPECT_EQ(is_sequencer_step_on(i), false); + } +} + +TEST_F(SequencerTest, TestIsStepOffWithNoActiveTracks) { + sequencer_config.steps[3] = 0xFF; + EXPECT_EQ(is_sequencer_step_on(3), false); +} + +TEST_F(SequencerTest, TestIsStepOffWithGivenActiveTracks) { + sequencer_set_track_activation(2, true); + sequencer_set_track_activation(3, true); + + sequencer_config.steps[3] = (1 << 0) + (1 << 1); + + // No active tracks have the step enabled, so it is off + EXPECT_EQ(is_sequencer_step_on(3), false); +} + +TEST_F(SequencerTest, TestIsStepOnWithGivenActiveTracks) { + sequencer_set_track_activation(2, true); + sequencer_set_track_activation(3, true); + + sequencer_config.steps[3] = (1 << 2); + + // Track 2 has the step enabled, so it is on + EXPECT_EQ(is_sequencer_step_on(3), true); +} + +TEST_F(SequencerTest, TestIsStepOffForGivenTrack) { + sequencer_config.steps[3] = 0x00; + EXPECT_EQ(is_sequencer_step_on_for_track(3, 5), false); +} + +TEST_F(SequencerTest, TestIsStepOnForGivenTrack) { + sequencer_config.steps[3] = (1 << 5); + EXPECT_EQ(is_sequencer_step_on_for_track(3, 5), true); +} + +TEST_F(SequencerTest, TestSetStepOn) { + sequencer_internal_state.active_tracks = (1 << 6) + (1 << 3) + (1 << 2); + sequencer_config.steps[2] = (1 << 5) + (1 << 2); + + sequencer_set_step(2, true); + + EXPECT_EQ(sequencer_config.steps[2], (1 << 6) + (1 << 5) + (1 << 3) + (1 << 2)); +} + +TEST_F(SequencerTest, TestSetStepOff) { + sequencer_internal_state.active_tracks = (1 << 6) + (1 << 3) + (1 << 2); + sequencer_config.steps[2] = (1 << 5) + (1 << 2); + + sequencer_set_step(2, false); + + EXPECT_EQ(sequencer_config.steps[2], (1 << 5)); +} + +TEST_F(SequencerTest, TestToggleStepOff) { + sequencer_internal_state.active_tracks = (1 << 6) + (1 << 3) + (1 << 2); + sequencer_config.steps[2] = (1 << 5) + (1 << 2); + + sequencer_toggle_step(2); + + EXPECT_EQ(sequencer_config.steps[2], (1 << 5)); +} + +TEST_F(SequencerTest, TestToggleStepOn) { + sequencer_internal_state.active_tracks = (1 << 6) + (1 << 3) + (1 << 2); + sequencer_config.steps[2] = 0; + + sequencer_toggle_step(2); + + EXPECT_EQ(sequencer_config.steps[2], (1 << 6) + (1 << 3) + (1 << 2)); +} + +TEST_F(SequencerTest, TestSetAllStepsOn) { + sequencer_internal_state.active_tracks = (1 << 6) + (1 << 3) + (1 << 2); + sequencer_config.steps[2] = (1 << 7) + (1 << 6); + sequencer_config.steps[4] = (1 << 3) + (1 << 1); + + sequencer_set_all_steps(true); + + EXPECT_EQ(sequencer_config.steps[2], (1 << 7) + (1 << 6) + (1 << 3) + (1 << 2)); + EXPECT_EQ(sequencer_config.steps[4], (1 << 6) + (1 << 3) + (1 << 2) + (1 << 1)); +} + +TEST_F(SequencerTest, TestSetAllStepsOff) { + sequencer_internal_state.active_tracks = (1 << 6) + (1 << 3) + (1 << 2); + sequencer_config.steps[2] = (1 << 7) + (1 << 6); + sequencer_config.steps[4] = (1 << 3) + (1 << 1); + + sequencer_set_all_steps(false); + + EXPECT_EQ(sequencer_config.steps[2], (1 << 7)); + EXPECT_EQ(sequencer_config.steps[4], (1 << 1)); +} + +TEST_F(SequencerTest, TestSetTempoZero) { + sequencer_config.tempo = 123; + + sequencer_set_tempo(0); + + EXPECT_EQ(sequencer_config.tempo, 123); +} + +TEST_F(SequencerTest, TestIncreaseTempoMax) { + sequencer_config.tempo = UINT8_MAX; + + sequencer_increase_tempo(); + + EXPECT_EQ(sequencer_config.tempo, UINT8_MAX); +} + +TEST_F(SequencerTest, TestSetResolutionLowerBound) { + sequencer_config.resolution = SQ_RES_4; + + sequencer_set_resolution((sequencer_resolution_t)-1); + + EXPECT_EQ(sequencer_config.resolution, SQ_RES_4); +} + +TEST_F(SequencerTest, TestSetResolutionUpperBound) { + sequencer_config.resolution = SQ_RES_4; + + sequencer_set_resolution(SEQUENCER_RESOLUTIONS); + + EXPECT_EQ(sequencer_config.resolution, SQ_RES_4); +} + +TEST_F(SequencerTest, TestGetBeatDuration) { + EXPECT_EQ(get_beat_duration(60), 1000); + EXPECT_EQ(get_beat_duration(120), 500); + EXPECT_EQ(get_beat_duration(240), 250); + EXPECT_EQ(get_beat_duration(0), 1000); +} + +TEST_F(SequencerTest, TestGetStepDuration60) { + /** + * Resolution cheatsheet: + * 1/2 => 2 steps per 4 beats + * 1/2T => 3 steps per 4 beats + * 1/4 => 4 steps per 4 beats + * 1/4T => 6 steps per 4 beats + * 1/8 => 8 steps per 4 beats + * 1/8T => 12 steps per 4 beats + * 1/16 => 16 steps per 4 beats + * 1/16T => 24 steps per 4 beats + * 1/32 => 32 steps per 4 beats + * + * The number of steps for binary resolutions follows the powers of 2. + * The ternary variants are simply 1.5x faster. + */ + EXPECT_EQ(get_step_duration(60, SQ_RES_2), 2000); + EXPECT_EQ(get_step_duration(60, SQ_RES_4), 1000); + EXPECT_EQ(get_step_duration(60, SQ_RES_8), 500); + EXPECT_EQ(get_step_duration(60, SQ_RES_16), 250); + EXPECT_EQ(get_step_duration(60, SQ_RES_32), 125); + + EXPECT_EQ(get_step_duration(60, SQ_RES_2T), 1333); + EXPECT_EQ(get_step_duration(60, SQ_RES_4T), 666); + EXPECT_EQ(get_step_duration(60, SQ_RES_8T), 333); + EXPECT_EQ(get_step_duration(60, SQ_RES_16T), 166); +} + +TEST_F(SequencerTest, TestGetStepDuration120) { + /** + * Resolution cheatsheet: + * 1/2 => 2 steps per 4 beats + * 1/2T => 3 steps per 4 beats + * 1/4 => 4 steps per 4 beats + * 1/4T => 6 steps per 4 beats + * 1/8 => 8 steps per 4 beats + * 1/8T => 12 steps per 4 beats + * 1/16 => 16 steps per 4 beats + * 1/16T => 24 steps per 4 beats + * 1/32 => 32 steps per 4 beats + * + * The number of steps for binary resolutions follows the powers of 2. + * The ternary variants are simply 1.5x faster. + */ + EXPECT_EQ(get_step_duration(30, SQ_RES_2), 4000); + EXPECT_EQ(get_step_duration(30, SQ_RES_4), 2000); + EXPECT_EQ(get_step_duration(30, SQ_RES_8), 1000); + EXPECT_EQ(get_step_duration(30, SQ_RES_16), 500); + EXPECT_EQ(get_step_duration(30, SQ_RES_32), 250); + + EXPECT_EQ(get_step_duration(30, SQ_RES_2T), 2666); + EXPECT_EQ(get_step_duration(30, SQ_RES_4T), 1333); + EXPECT_EQ(get_step_duration(30, SQ_RES_8T), 666); + EXPECT_EQ(get_step_duration(30, SQ_RES_16T), 333); +} + +void setUpMatrixScanSequencerTest(void) { + sequencer_config.enabled = true; + sequencer_config.tempo = 120; + sequencer_config.resolution = SQ_RES_16; + + // Configure the notes for each track + sequencer_config.track_notes[0] = MI_C; + sequencer_config.track_notes[1] = MI_D; + sequencer_config.track_notes[2] = MI_E; + sequencer_config.track_notes[3] = MI_F; + sequencer_config.track_notes[4] = MI_G; + sequencer_config.track_notes[5] = MI_A; + sequencer_config.track_notes[6] = MI_B; + sequencer_config.track_notes[7] = MI_C; + + // Turn on some steps + sequencer_config.steps[0] = (1 << 0); + sequencer_config.steps[2] = (1 << 1) + (1 << 0); +} + +TEST_F(SequencerTest, TestMatrixScanSequencerShouldAttackFirstTrackOfFirstStep) { + setUpMatrixScanSequencerTest(); + + matrix_scan_sequencer(); + EXPECT_EQ(last_noteon, MI_C); + EXPECT_EQ(last_noteoff, 0); +} + +TEST_F(SequencerTest, TestMatrixScanSequencerShouldAttackSecondTrackAfterFirstTrackOfFirstStep) { + setUpMatrixScanSequencerTest(); + + matrix_scan_sequencer(); + EXPECT_EQ(sequencer_internal_state.current_step, 0); + EXPECT_EQ(sequencer_internal_state.current_track, 1); + EXPECT_EQ(sequencer_internal_state.phase, SEQUENCER_PHASE_ATTACK); +} + +TEST_F(SequencerTest, TestMatrixScanSequencerShouldNotAttackInactiveTrackFirstStep) { + setUpMatrixScanSequencerTest(); + + sequencer_internal_state.current_step = 0; + sequencer_internal_state.current_track = 1; + + // Wait some time after the first track has been attacked + advance_time(SEQUENCER_TRACK_THROTTLE); + + matrix_scan_sequencer(); + EXPECT_EQ(last_noteon, 0); + EXPECT_EQ(last_noteoff, 0); +} + +TEST_F(SequencerTest, TestMatrixScanSequencerShouldAttackThirdTrackAfterSecondTrackOfFirstStep) { + setUpMatrixScanSequencerTest(); + + sequencer_internal_state.current_step = 0; + sequencer_internal_state.current_track = 1; + + // Wait some time after the second track has been attacked + advance_time(2 * SEQUENCER_TRACK_THROTTLE); + + matrix_scan_sequencer(); + EXPECT_EQ(sequencer_internal_state.current_step, 0); + EXPECT_EQ(sequencer_internal_state.current_track, 2); + EXPECT_EQ(sequencer_internal_state.phase, SEQUENCER_PHASE_ATTACK); +} + +TEST_F(SequencerTest, TestMatrixScanSequencerShouldEnterReleasePhaseAfterLastTrackHasBeenProcessedFirstStep) { + setUpMatrixScanSequencerTest(); + + sequencer_internal_state.current_step = 0; + sequencer_internal_state.current_track = SEQUENCER_TRACKS - 1; + + // Wait until all notes have been attacked + advance_time((SEQUENCER_TRACKS - 1) * SEQUENCER_TRACK_THROTTLE); + + matrix_scan_sequencer(); + EXPECT_EQ(last_noteon, 0); + EXPECT_EQ(last_noteoff, 0); + EXPECT_EQ(sequencer_internal_state.current_step, 0); + EXPECT_EQ(sequencer_internal_state.current_track, SEQUENCER_TRACKS - 1); + EXPECT_EQ(sequencer_internal_state.phase, SEQUENCER_PHASE_RELEASE); +} + +TEST_F(SequencerTest, TestMatrixScanSequencerShouldReleaseBackwards) { + setUpMatrixScanSequencerTest(); + + sequencer_internal_state.current_step = 0; + sequencer_internal_state.current_track = SEQUENCER_TRACKS - 1; + sequencer_internal_state.phase = SEQUENCER_PHASE_RELEASE; + + // Wait until all notes have been attacked + advance_time((SEQUENCER_TRACKS - 1) * SEQUENCER_TRACK_THROTTLE); + // + the release timeout + advance_time(SEQUENCER_PHASE_RELEASE_TIMEOUT); + + matrix_scan_sequencer(); + EXPECT_EQ(sequencer_internal_state.current_step, 0); + EXPECT_EQ(sequencer_internal_state.current_track, SEQUENCER_TRACKS - 2); + EXPECT_EQ(sequencer_internal_state.phase, SEQUENCER_PHASE_RELEASE); +} + +TEST_F(SequencerTest, TestMatrixScanSequencerShouldNotReleaseInactiveTrackFirstStep) { + setUpMatrixScanSequencerTest(); + + sequencer_internal_state.current_step = 0; + sequencer_internal_state.current_track = SEQUENCER_TRACKS - 1; + sequencer_internal_state.phase = SEQUENCER_PHASE_RELEASE; + + // Wait until all notes have been attacked + advance_time((SEQUENCER_TRACKS - 1) * SEQUENCER_TRACK_THROTTLE); + // + the release timeout + advance_time(SEQUENCER_PHASE_RELEASE_TIMEOUT); + + matrix_scan_sequencer(); + EXPECT_EQ(last_noteon, 0); + EXPECT_EQ(last_noteoff, 0); +} + +TEST_F(SequencerTest, TestMatrixScanSequencerShouldReleaseFirstTrackFirstStep) { + setUpMatrixScanSequencerTest(); + + sequencer_internal_state.current_step = 0; + sequencer_internal_state.current_track = 0; + sequencer_internal_state.phase = SEQUENCER_PHASE_RELEASE; + + // Wait until all notes have been attacked + advance_time((SEQUENCER_TRACKS - 1) * SEQUENCER_TRACK_THROTTLE); + // + the release timeout + advance_time(SEQUENCER_PHASE_RELEASE_TIMEOUT); + // + all the other notes have been released + advance_time((SEQUENCER_TRACKS - 1) * SEQUENCER_TRACK_THROTTLE); + + matrix_scan_sequencer(); + EXPECT_EQ(last_noteon, 0); + EXPECT_EQ(last_noteoff, MI_C); +} + +TEST_F(SequencerTest, TestMatrixScanSequencerShouldEnterPausePhaseAfterRelease) { + setUpMatrixScanSequencerTest(); + + sequencer_internal_state.current_step = 0; + sequencer_internal_state.current_track = 0; + sequencer_internal_state.phase = SEQUENCER_PHASE_RELEASE; + + // Wait until all notes have been attacked + advance_time((SEQUENCER_TRACKS - 1) * SEQUENCER_TRACK_THROTTLE); + // + the release timeout + advance_time(SEQUENCER_PHASE_RELEASE_TIMEOUT); + // + all the other notes have been released + advance_time((SEQUENCER_TRACKS - 1) * SEQUENCER_TRACK_THROTTLE); + + matrix_scan_sequencer(); + EXPECT_EQ(sequencer_internal_state.current_step, 0); + EXPECT_EQ(sequencer_internal_state.current_track, 0); + EXPECT_EQ(sequencer_internal_state.phase, SEQUENCER_PHASE_PAUSE); +} + +TEST_F(SequencerTest, TestMatrixScanSequencerShouldProcessFirstTrackOfSecondStepAfterPause) { + setUpMatrixScanSequencerTest(); + + sequencer_internal_state.current_step = 0; + sequencer_internal_state.current_track = 0; + sequencer_internal_state.phase = SEQUENCER_PHASE_PAUSE; + + // Wait until all notes have been attacked + advance_time((SEQUENCER_TRACKS - 1) * SEQUENCER_TRACK_THROTTLE); + // + the release timeout + advance_time(SEQUENCER_PHASE_RELEASE_TIMEOUT); + // + all the other notes have been released + advance_time((SEQUENCER_TRACKS - 1) * SEQUENCER_TRACK_THROTTLE); + // + the step duration (one 16th at tempo=120 lasts 125ms) + advance_time(125); + + matrix_scan_sequencer(); + EXPECT_EQ(sequencer_internal_state.current_step, 1); + EXPECT_EQ(sequencer_internal_state.current_track, 1); + EXPECT_EQ(sequencer_internal_state.phase, SEQUENCER_PHASE_ATTACK); +} + +TEST_F(SequencerTest, TestMatrixScanSequencerShouldProcessSecondTrackTooEarly) { + setUpMatrixScanSequencerTest(); + + sequencer_internal_state.current_step = 2; + sequencer_internal_state.current_track = 1; + + matrix_scan_sequencer(); + EXPECT_EQ(last_noteon, 0); + EXPECT_EQ(last_noteoff, 0); +} + +TEST_F(SequencerTest, TestMatrixScanSequencerShouldProcessSecondTrackOnTime) { + setUpMatrixScanSequencerTest(); + + sequencer_internal_state.current_step = 2; + sequencer_internal_state.current_track = 1; + + // Wait until first track has been attacked + advance_time(SEQUENCER_TRACK_THROTTLE); + + matrix_scan_sequencer(); + EXPECT_EQ(last_noteon, MI_D); + EXPECT_EQ(last_noteoff, 0); +} + +TEST_F(SequencerTest, TestMatrixScanSequencerShouldLoopOnceSequenceIsOver) { + setUpMatrixScanSequencerTest(); + + sequencer_internal_state.current_step = SEQUENCER_STEPS - 1; + sequencer_internal_state.current_track = 0; + sequencer_internal_state.phase = SEQUENCER_PHASE_PAUSE; + + // Wait until all notes have been attacked + advance_time((SEQUENCER_TRACKS - 1) * SEQUENCER_TRACK_THROTTLE); + // + the release timeout + advance_time(SEQUENCER_PHASE_RELEASE_TIMEOUT); + // + all the other notes have been released + advance_time((SEQUENCER_TRACKS - 1) * SEQUENCER_TRACK_THROTTLE); + // + the step duration (one 16th at tempo=120 lasts 125ms) + advance_time(125); + + matrix_scan_sequencer(); + EXPECT_EQ(sequencer_internal_state.current_step, 0); + EXPECT_EQ(sequencer_internal_state.current_track, 1); + EXPECT_EQ(sequencer_internal_state.phase, SEQUENCER_PHASE_ATTACK); +} diff --git a/quantum/sequencer/tests/testlist.mk b/quantum/sequencer/tests/testlist.mk new file mode 100644 index 0000000000..bb38991109 --- /dev/null +++ b/quantum/sequencer/tests/testlist.mk @@ -0,0 +1 @@ +TEST_LIST += sequencer -- cgit v1.2.3