Skip to content

Commit

Permalink
tests: edge_impulse new api with shuffling
Browse files Browse the repository at this point in the history
tests are run multiple times with random order

Signed-off-by: Mateusz Michalek <mateusz.michalek@nordicsemi.no>
Signed-off-by: Marek Pieta <Marek.Pieta@nordicsemi.no>
  • Loading branch information
michalek-no authored and cvinayak committed Jun 21, 2023
1 parent 1619d7a commit 293fc45
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
1 change: 1 addition & 0 deletions tests/lib/edge_impulse/prj.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Tests
CONFIG_ZTEST_SHUFFLE=y
CONFIG_ZTEST_NEW_API=y
CONFIG_ZTEST=y

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
#include <zephyr/ztest.h>
#include <ei_run_classifier.h>

static size_t prediction_idx;

void ei_run_classifier_mock_init(void)
{
prediction_idx = 0;
}

/* Input data must be ascending sequence of floats. Difference between
* subsequent elements of input sequence equals 1. The first element
Expand Down Expand Up @@ -44,7 +50,6 @@ EI_IMPULSE_ERROR run_classifier(signal_t *signal,
ei_impulse_result_t *result,
bool debug)
{
static size_t prediction_idx = 0;
ARG_UNUSED(debug);

/* Test getting data. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright (c) 2023 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#ifndef _EI_RUN_CLASSIFIER_MOCK_H_
#define _EI_RUN_CLASSIFIER_MOCK_H_

void ei_run_classifier_mock_init(void);

#endif /* _EI_RUN_CLASSIFIER_MOCK_H_ */
27 changes: 22 additions & 5 deletions tests/lib/edge_impulse/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
#include <zephyr/kernel.h>
#include <ei_test_params.h>
#include <ei_wrapper.h>
#include <ei_run_classifier_mock.h>

#define EI_TEST_SEM_TIMEOUT K_MSEC(200 + (10 * EI_MOCK_BUSY_WAIT_TIME / 1000))
#define EI_TEST_ISR_WINDOW_SHIFT 2
#define EI_TEST_THREAD_WINDOW_SHIFT 2
#define EI_TEST_WINDOW_SHIFT_CB 1

#define TEST_THREAD_SLEEP_MS 10
static size_t timer_fn_calls;

static atomic_t rerun_in_cb;

static size_t prediction_idx;
Expand Down Expand Up @@ -141,6 +145,13 @@ static void result_ready_cb(int err)

static void *test_init(void)
{
static bool init_once;

if (init_once) {
return NULL;
}
init_once = true;

zassert_equal(ei_wrapper_get_frame_size(), EI_CLASSIFIER_RAW_SAMPLES_PER_FRAME,
"Wrong frame size");
zassert_equal(ei_wrapper_get_window_size(), EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE,
Expand Down Expand Up @@ -369,14 +380,13 @@ ZTEST(suite0, test_data_after_start)

static void test_thread_fn(void)
{
static const size_t sleep_ms = 10;
int err;

for (size_t i = 0; i <= EI_TEST_THREAD_WINDOW_SHIFT; i++) {
err = add_input_data(prediction_idx, 0);
zassert_ok(err, "Cannot add input data");

k_sleep(K_MSEC(sleep_ms));
k_sleep(K_MSEC(TEST_THREAD_SLEEP_MS));
}
}

Expand All @@ -399,26 +409,28 @@ ZTEST(suite0, test_data_thread)
zassert_ok(err, "Cannot start prediction");
err = k_sem_take(&test_sem, EI_TEST_SEM_TIMEOUT);
zassert_ok(err, "Cannot take semaphore");
err = k_thread_join(&thread, K_MSEC(TEST_THREAD_SLEEP_MS * 777));
zassert_ok(err, "Thread still running");
}

void timer_fn(struct k_timer *timer)
{
static size_t calls = 0;
int err;

err = add_input_data(prediction_idx, 0);
zassert_ok(err, "Cannot add input data");

calls++;
timer_fn_calls++;

if (calls > EI_TEST_ISR_WINDOW_SHIFT) {
if (timer_fn_calls > EI_TEST_ISR_WINDOW_SHIFT) {
k_timer_stop(timer);
}
}

ZTEST(suite0, test_data_isr)
{
static const size_t period_ms = 10;
timer_fn_calls = 0;
static K_TIMER_DEFINE(test_timer, timer_fn, NULL);
int err;

Expand All @@ -434,11 +446,16 @@ ZTEST(suite0, test_data_isr)
static void setup_fn(void *unused)
{
ARG_UNUSED(unused);

bool cancelled;
int err = ei_wrapper_clear_data(&cancelled);
prediction_idx = 0;
ei_run_classifier_mock_init();

zassert_false(cancelled, "Prediction was not cancelled");
zassert_ok(err, "Cannot clear data");
err = k_sem_take(&test_sem, K_MSEC(20));
zassert_true(err, "Unhandled prediction result");
}

ZTEST_SUITE(suite0, NULL, test_init, setup_fn, NULL, NULL);

0 comments on commit 293fc45

Please sign in to comment.