Skip to content

Commit 8b17cfe

Browse files
Zhangshoukuixiaoxiang781216
authored andcommitted
drivertest_i2c_spi: Supports incoming device nodes so we can test both iic and spi
Signed-off-by: zhangshoukui <zhangshoukui@xiaomi.com>
1 parent 8d82bd7 commit 8b17cfe

File tree

1 file changed

+38
-8
lines changed

1 file changed

+38
-8
lines changed

testing/drivertest/drivertest_i2c_spi.c

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,38 @@
5151

5252
struct test_state_s
5353
{
54-
FAR const char *dev_path;
54+
char dev_path[PATH_MAX];
5555
int fd;
5656
};
5757

5858
/****************************************************************************
5959
* Private Functions
6060
****************************************************************************/
6161

62+
/****************************************************************************
63+
* Name: show_usage
64+
****************************************************************************/
65+
66+
static void show_usage(FAR const char *progname,
67+
FAR const char *path, int exitcode)
68+
{
69+
printf("Usage: %s -d <devpath>\n", progname);
70+
printf(" [-d devpath] Sensor device node.\n"
71+
" Default: %s Current: %s\n", ACC_DEVPATH, path);
72+
exit(exitcode);
73+
}
74+
6275
/****************************************************************************
6376
* Name: setup
6477
****************************************************************************/
6578

6679
static int setup(FAR void **state)
6780
{
6881
FAR struct test_state_s *test_state;
69-
test_state = malloc(sizeof(struct test_state_s));
70-
assert_true(test_state != NULL);
71-
72-
test_state->dev_path = ACC_DEVPATH;
82+
test_state = (FAR struct test_state_s *)*state;
7383
test_state->fd = open(test_state->dev_path, O_RDONLY);
7484
assert_true(test_state->fd > 0);
7585

76-
*state = test_state;
7786
return 0;
7887
}
7988

@@ -86,7 +95,6 @@ static int teardown(FAR void **state)
8695
FAR struct test_state_s *test_state;
8796
test_state = (FAR struct test_state_s *)*state;
8897
assert_int_equal(close(test_state->fd), 0);
89-
free(test_state);
9098
return 0;
9199
}
92100

@@ -131,9 +139,31 @@ static void read_from_device(FAR void **state)
131139

132140
int main(int argc, FAR char *argv[])
133141
{
142+
struct test_state_s test_state;
143+
int ch;
144+
145+
memset(&test_state, 0, sizeof(test_state));
146+
snprintf(test_state.dev_path, sizeof(test_state.dev_path), "%s",
147+
ACC_DEVPATH);
148+
while ((ch = getopt(argc, argv, "d:h")) != ERROR)
149+
{
150+
switch (ch)
151+
{
152+
case 'd':
153+
snprintf(test_state.dev_path, sizeof(test_state.dev_path), "%s",
154+
optarg);
155+
break;
156+
case 'h':
157+
case '?':
158+
show_usage(argv[0], test_state.dev_path, EXIT_FAILURE);
159+
break;
160+
}
161+
}
162+
134163
const struct CMUnitTest tests[] =
135164
{
136-
cmocka_unit_test_setup_teardown(read_from_device, setup, teardown),
165+
cmocka_unit_test_prestate_setup_teardown(read_from_device, setup,
166+
teardown, &test_state),
137167
};
138168

139169
return cmocka_run_group_tests(tests, NULL, NULL);

0 commit comments

Comments
 (0)