1 /*
2 * Copyright 2022 Google LLC
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include "emul_tester.h"
8
9 #include <zephyr/device.h>
10 #include <zephyr/devicetree.h>
11 #include <zephyr/drivers/emul.h>
12 #include <zephyr/ztest.h>
13 #include <zephyr/ztest_assert.h>
14 #include <zephyr/ztest_test.h>
15
16 #define TEST_ACCEL DT_NODELABEL(test_bmi)
17
18 #define TEST_EMUL_A DT_NODELABEL(emul_tester_a)
19 #define TEST_EMUL_B DT_NODELABEL(emul_tester_b)
20
ZTEST(emul,test_emul_dt_get)21 ZTEST(emul, test_emul_dt_get)
22 {
23 /* This variable is static to verify that the result of EMUL_DT_GET is a
24 * compile-time constant.
25 */
26 static const struct emul *emul_static = EMUL_DT_GET(TEST_ACCEL);
27
28 /* Verify that EMUL_DT_GET returned the expected struct emul. */
29 zassert_not_null(emul_static, "EMUL_DT_GET returned NULL");
30 zassert_ok(strcmp(emul_static->dev->name, DT_NODE_FULL_NAME(TEST_ACCEL)),
31 "Unexpected device name %s", emul_static->dev->name);
32 }
33
ZTEST(emul,test_emul_backend_api)34 ZTEST(emul, test_emul_backend_api)
35 {
36 static const struct emul *emul_a = EMUL_DT_GET(TEST_EMUL_A);
37 static const struct emul *emul_b = EMUL_DT_GET(TEST_EMUL_B);
38 int set_action_value;
39 int scale;
40 int get_action_value;
41
42 set_action_value = 5;
43
44 scale = DT_PROP(TEST_EMUL_A, scale);
45 zassert_not_null(emul_a, "emul_tester_a not found");
46 zassert_ok(emul_tester_backend_set_action(emul_a, set_action_value));
47 zassert_ok(emul_tester_backend_get_action(emul_a, &get_action_value));
48 zassert_equal(get_action_value, set_action_value * scale);
49
50 scale = DT_PROP(TEST_EMUL_B, scale);
51 zassert_not_null(emul_b, "emul_tester_b not found");
52 zassert_ok(emul_tester_backend_set_action(emul_b, set_action_value));
53 zassert_ok(emul_tester_backend_get_action(emul_b, &get_action_value));
54 zassert_equal(get_action_value, set_action_value * scale);
55 }
56
57 ZTEST_SUITE(emul, NULL, NULL, NULL, NULL, NULL);
58