test: sensor: add testcase

Add module test of API sensor_sample_fetch_chan().
Add module test of unit coversion function.

Signed-off-by: Ying ming <mingx.ying@intel.com>
This commit is contained in:
Ying ming 2020-12-17 21:54:44 +08:00 committed by Carles Cufí
parent 49031cf6e2
commit 602b71ca5c
3 changed files with 73 additions and 7 deletions

View file

@ -0,0 +1,2 @@
CONFIG_ZTEST=y
CONFIG_FPU=y

View file

@ -111,14 +111,24 @@ void test_sensor_get_channels(void)
dev = device_get_binding(DUMMY_SENSOR_NAME);
zassert_not_null(dev, "failed: dev is null.");
/* test fetch single channel */
zassert_equal(sensor_sample_fetch_chan(dev, chan_elements[0].chan),
RETURN_SUCCESS, "fail to fetch sample.");
/* Get and check channel 0 value. */
zassert_equal(sensor_channel_get(dev, chan_elements[0].chan,
&data), RETURN_SUCCESS, "fail to get channel.");
zassert_equal(data.val1, chan_elements[0].data.val1,
"the data is not match.");
zassert_equal(data.val2, chan_elements[0].data.val2,
"the data is not match.");
/* test fetch all channel */
zassert_equal(sensor_sample_fetch(dev), RETURN_SUCCESS,
"fail to fetch sample.");
/* Get and check channels value. */
for (int i = 0; i < TOTAL_CHAN_ELEMENTS; i++) {
/* Get and check channels value except for chanel 0. */
for (int i = 1; i < TOTAL_CHAN_ELEMENTS; i++) {
zassert_equal(sensor_channel_get(dev, chan_elements[i].chan,
&data), RETURN_SUCCESS, "fail to get channel.");
zassert_equal(data.val1, chan_elements[i].data.val1,
"the data is not match.");
zassert_equal(data.val2, chan_elements[i].data.val2,
@ -212,7 +222,7 @@ void test_sensor_handle_triggers(void)
zassert_equal(trigger_elements[i].data.val2,
data.val2, "read-back returned wrong val2");
/* setting a sensors trigger and handler */
/* setting a sensor's trigger and handler */
zassert_equal(sensor_trigger_set(dev,
&trigger_elements[i].trig,
trigger_handler),
@ -244,7 +254,7 @@ void test_sensor_handle_triggers(void)
&data),
-ENOTSUP, "fail to get attributes");
/* setting a sensors trigger and handler for no trig dev */
/* setting a sensor's trigger and handler for no trig dev */
zassert_equal(sensor_trigger_set(dev_no_trig,
&trigger_elements[i].trig,
trigger_handler),
@ -252,12 +262,62 @@ void test_sensor_handle_triggers(void)
}
}
/**
* @brief Test unit coversion of sensor module
* @details Verify helper function to convert acceleration from
* Gs to m/s^2 and from m/s^2 to Gs. Verify helper function
* to convert radians to degrees and degrees to radians. Verify
* helper function for converting struct sensor_value to double.
*/
void test_sensor_unit_conversion(void)
{
struct sensor_value data;
/* Test acceleration unit conversion */
sensor_g_to_ms2(1, &data);
zassert_equal(data.val1, SENSOR_G/1000000LL,
"the data is not match.");
zassert_equal(data.val2, SENSOR_G%(data.val1 * 1000000LL),
"the data is not match.");
zassert_equal(sensor_ms2_to_g(&data), 1,
"the data is not match.");
/* set test data to negative value */
data.val1 = -data.val1;
data.val2 = -data.val2;
zassert_equal(sensor_ms2_to_g(&data), -1,
"the data is not match.");
/* Test the conversion between angle and radian */
sensor_degrees_to_rad(180, &data);
zassert_equal(data.val1, SENSOR_PI/1000000LL,
"the data is not match.");
zassert_equal(data.val2, SENSOR_PI%(data.val1 * 1000000LL),
"the data is not match.");
zassert_equal(sensor_rad_to_degrees(&data), 180,
"the data is not match.");
/* set test data to negative value */
data.val1 = -data.val1;
data.val2 = -data.val2;
zassert_equal(sensor_rad_to_degrees(&data), -180,
"the data is not match.");
/* reset test data to positive value */
data.val1 = -data.val1;
data.val2 = -data.val2;
/* Test struct sensor_value to double */
#if defined(CONFIG_FPU)
zassert_equal((long long)(sensor_value_to_double(&data) * 1000000LL),
SENSOR_PI, "the data is not match.");
#endif
}
/*test case main entry*/
void test_main(void)
{
ztest_test_suite(test_sensor_api,
ztest_1cpu_unit_test(test_sensor_get_channels),
ztest_1cpu_unit_test(test_sensor_handle_triggers));
ztest_1cpu_unit_test(test_sensor_handle_triggers),
ztest_1cpu_unit_test(test_sensor_unit_conversion));
ztest_run_test_suite(test_sensor_api);
}

View file

@ -1,3 +1,7 @@
tests:
driver.sensor:
extra_args: CONF_FILE=prj.conf
tags: driver sensor subsys
driver.sensor.fpu:
extra_args: CONF_FILE=prj_fpu.conf
tags: driver sensor subsys