mirror of
https://github.com/espressif/esp-idf.git
synced 2025-12-16 04:22:22 +00:00
examples: add unit testing example
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
set(COMPONENT_SRCS "mean.c")
|
||||
set(COMPONENT_ADD_INCLUDEDIRS "include")
|
||||
|
||||
register_component()
|
||||
@@ -0,0 +1,18 @@
|
||||
/* testable.h: Implementation of a testable component.
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* @brief Calculate arithmetic mean of integer values
|
||||
* @param values array of values
|
||||
* @param count number of elements in the array
|
||||
* @return arithmetic mean of values, or zero count is zero
|
||||
*/
|
||||
int testable_mean(const int* values, int count);
|
||||
24
examples/system/unit_test/components/testable/mean.c
Normal file
24
examples/system/unit_test/components/testable/mean.c
Normal file
@@ -0,0 +1,24 @@
|
||||
/* mean.c: Implementation of a mean function of testable component.
|
||||
See test/test_mean.c for the associated unit test.
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
|
||||
#include "testable.h"
|
||||
|
||||
|
||||
int testable_mean(const int* values, int count)
|
||||
{
|
||||
if (count == 0) {
|
||||
return 0;
|
||||
}
|
||||
int sum = 0;
|
||||
for (int i = 0; i < count; ++i) {
|
||||
sum += values[i];
|
||||
}
|
||||
return sum / count;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
set(COMPONENT_SRCDIRS ".")
|
||||
set(COMPONENT_ADD_INCLUDEDIRS ".")
|
||||
|
||||
set(COMPONENT_REQUIRES unity testable)
|
||||
|
||||
register_component()
|
||||
@@ -0,0 +1,7 @@
|
||||
# This is the minimal test component makefile.
|
||||
#
|
||||
# The following line is needed to force the linker to include all the object
|
||||
# files into the application, even if the functions in these object files
|
||||
# are not referenced from outside (which is usually the case for unit tests).
|
||||
#
|
||||
COMPONENT_ADD_LDFLAGS = -Wl,--whole-archive -l$(COMPONENT_NAME) -Wl,--no-whole-archive
|
||||
@@ -0,0 +1,36 @@
|
||||
/* test_mean.c: Implementation of a testable component.
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
|
||||
#include <limits.h>
|
||||
#include "unity.h"
|
||||
#include "testable.h"
|
||||
|
||||
#define countof(x) (sizeof(x)/sizeof(x[0]))
|
||||
|
||||
TEST_CASE("Mean of an empty array is zero", "[mean]")
|
||||
{
|
||||
const int values[] = { 0 };
|
||||
TEST_ASSERT_EQUAL(0, testable_mean(values, 0));
|
||||
}
|
||||
|
||||
TEST_CASE("Mean of a test vector", "[mean]")
|
||||
{
|
||||
const int v[] = {1, 3, 5, 7, 9};
|
||||
TEST_ASSERT_EQUAL(5, testable_mean(v, countof(v)));
|
||||
}
|
||||
|
||||
/* This test case currently fails, and developer has added a tag to indicate this.
|
||||
* For the test runner, "[fails]" string does not carry any special meaning.
|
||||
* However it can be used to filter out tests when running.
|
||||
*/
|
||||
TEST_CASE("Another test case which fails", "[mean][fails]")
|
||||
{
|
||||
const int v1[] = {INT_MAX, INT_MAX, INT_MAX, INT_MAX};
|
||||
TEST_ASSERT_EQUAL(INT_MAX, testable_mean(v1, countof(v1)));
|
||||
}
|
||||
Reference in New Issue
Block a user