51
51
52
52
struct test_state_s
53
53
{
54
- FAR const char * dev_path ;
54
+ char dev_path [ PATH_MAX ] ;
55
55
int fd ;
56
56
};
57
57
58
58
/****************************************************************************
59
59
* Private Functions
60
60
****************************************************************************/
61
61
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
+
62
75
/****************************************************************************
63
76
* Name: setup
64
77
****************************************************************************/
65
78
66
79
static int setup (FAR void * * state )
67
80
{
68
81
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 ;
73
83
test_state -> fd = open (test_state -> dev_path , O_RDONLY );
74
84
assert_true (test_state -> fd > 0 );
75
85
76
- * state = test_state ;
77
86
return 0 ;
78
87
}
79
88
@@ -86,7 +95,6 @@ static int teardown(FAR void **state)
86
95
FAR struct test_state_s * test_state ;
87
96
test_state = (FAR struct test_state_s * )* state ;
88
97
assert_int_equal (close (test_state -> fd ), 0 );
89
- free (test_state );
90
98
return 0 ;
91
99
}
92
100
@@ -131,9 +139,31 @@ static void read_from_device(FAR void **state)
131
139
132
140
int main (int argc , FAR char * argv [])
133
141
{
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
+
134
163
const struct CMUnitTest tests [] =
135
164
{
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 ),
137
167
};
138
168
139
169
return cmocka_run_group_tests (tests , NULL , NULL );
0 commit comments