99 *
1010 */
1111
12- #include <zephyr/kernel.h>
13- #include <zephyr/ztest.h>
14- #include <string.h>
15- #include <zephyr/posix/unistd.h>
1612#include <getopt.h>
13+ #include <string.h>
1714
18- ZTEST_SUITE (getopt_test_suite , NULL , NULL , NULL , NULL , NULL );
15+ #include <zephyr/kernel.h>
16+ #include <zephyr/posix/unistd.h>
17+ #include <zephyr/ztest.h>
1918
20- ZTEST (getopt_test_suite , test_getopt_basic )
19+ ZTEST (posix_c_lib_ext , test_getopt_basic )
2120{
2221 static const char * const nargv [] = {
23- "cmd_name" ,
24- "-b" ,
25- "-a" ,
26- "-h" ,
27- "-c" ,
28- "-l" ,
29- "-h" ,
30- "-a" ,
31- "-i" ,
32- "-w" ,
22+ "cmd_name" , "-b" , "-a" , "-h" , "-c" , "-l" , "-h" , "-a" , "-i" , "-w" ,
3323 };
3424 static const char * accepted_opt = "abchw" ;
3525 static const char * expected = "bahc?ha?w" ;
@@ -63,7 +53,7 @@ enum getopt_idx {
6353 GETOPT_IDX_OPTARG
6454};
6555
66- ZTEST (getopt_test_suite , test_getopt )
56+ ZTEST (posix_c_lib_ext , test_getopt )
6757{
6858 struct getopt_state * state ;
6959 static const char * test_opts = "ac:" ;
@@ -96,8 +86,7 @@ ZTEST(getopt_test_suite, test_getopt)
9686 zassert_equal (0 , strcmp (argv [GETOPT_IDX_OPTARG ], state -> optarg ),
9787 "unexpected optarg result" );
9888 /* Non thread safe usage: */
99- zassert_equal (0 , strcmp (argv [GETOPT_IDX_OPTARG ], optarg ),
100- "unexpected optarg result" );
89+ zassert_equal (0 , strcmp (argv [GETOPT_IDX_OPTARG ], optarg ), "unexpected optarg result" );
10190}
10291
10392enum getopt_long_idx {
@@ -107,7 +96,7 @@ enum getopt_long_idx {
10796 GETOPT_LONG_IDX_OPTARG
10897};
10998
110- ZTEST (getopt_test_suite , test_getopt_long )
99+ ZTEST (posix_c_lib_ext , test_getopt_long )
111100{
112101 /* Below test is based on example
113102 * https://www.gnu.org/software/libc/manual/html_node/Getopt-Long-Option-Example.html
@@ -120,16 +109,16 @@ ZTEST(getopt_test_suite, test_getopt_long)
120109 int c ;
121110 struct option long_options [] = {
122111 /* These options set a flag. */
123- {"verbose" , no_argument , & verbose_flag , 1 },
124- {"brief" , no_argument , & verbose_flag , 0 },
112+ {"verbose" , no_argument , & verbose_flag , 1 },
113+ {"brief" , no_argument , & verbose_flag , 0 },
125114 /* These options don’t set a flag.
126115 * We distinguish them by their indices.
127116 */
128- {"add" , no_argument , 0 , 'a' },
129- {"create" , required_argument , 0 , 'c' },
130- {"delete" , required_argument , 0 , 'd' },
131- {"long" , required_argument , 0 , 'e' },
132- {0 , 0 , 0 , 0 }
117+ {"add" , no_argument , 0 , 'a' },
118+ {"create" , required_argument , 0 , 'c' },
119+ {"delete" , required_argument , 0 , 'd' },
120+ {"long" , required_argument , 0 , 'e' },
121+ {0 , 0 , 0 , 0 },
133122 };
134123 static const char * accepted_opt = "ac:d:e:" ;
135124
@@ -170,64 +159,51 @@ ZTEST(getopt_test_suite, test_getopt_long)
170159 /* Get state of the current thread */
171160 getopt_init ();
172161 argv = (char * * )argv1 ;
173- c = getopt_long (argc1 , argv , accepted_opt ,
174- long_options , & option_index );
162+ c = getopt_long (argc1 , argv , accepted_opt , long_options , & option_index );
175163 zassert_equal (verbose_flag , 1 , "verbose flag expected" );
176164 c = getopt_long (argc1 , argv , accepted_opt , long_options , & option_index );
177165 state = getopt_state_get ();
178166 zassert_equal ('c' , c , "unexpected option" );
179- zassert_equal (0 , strcmp (state -> optarg , argv [GETOPT_LONG_IDX_OPTARG ]),
180- "unexpected optarg" );
181- c = getopt_long (argc1 , argv , accepted_opt ,
182- long_options , & option_index );
167+ zassert_equal (0 , strcmp (state -> optarg , argv [GETOPT_LONG_IDX_OPTARG ]), "unexpected optarg" );
168+ c = getopt_long (argc1 , argv , accepted_opt , long_options , & option_index );
183169 zassert_equal (-1 , c , "getopt_long shall return -1" );
184170
185171 /* Test scenario 2 */
186172 argv = (char * * )argv2 ;
187173 getopt_init ();
188- c = getopt_long (argc2 , argv , accepted_opt ,
189- long_options , & option_index );
174+ c = getopt_long (argc2 , argv , accepted_opt , long_options , & option_index );
190175 zassert_equal (verbose_flag , 0 , "verbose flag expected" );
191- c = getopt_long (argc2 , argv , accepted_opt ,
192- long_options , & option_index );
176+ c = getopt_long (argc2 , argv , accepted_opt , long_options , & option_index );
193177 zassert_equal ('d' , c , "unexpected option" );
194178 state = getopt_state_get ();
195- zassert_equal (0 , strcmp (state -> optarg , argv [GETOPT_LONG_IDX_OPTARG ]),
196- "unexpected optarg" );
197- c = getopt_long (argc2 , argv , accepted_opt ,
198- long_options , & option_index );
179+ zassert_equal (0 , strcmp (state -> optarg , argv [GETOPT_LONG_IDX_OPTARG ]), "unexpected optarg" );
180+ c = getopt_long (argc2 , argv , accepted_opt , long_options , & option_index );
199181 zassert_equal (-1 , c , "getopt_long shall return -1" );
200182
201183 /* Test scenario 3 */
202184 argv = (char * * )argv3 ;
203185 getopt_init ();
204- c = getopt_long (argc3 , argv , accepted_opt ,
205- long_options , & option_index );
186+ c = getopt_long (argc3 , argv , accepted_opt , long_options , & option_index );
206187 zassert_equal (verbose_flag , 0 , "verbose flag expected" );
207- c = getopt_long (argc3 , argv , accepted_opt ,
208- long_options , & option_index );
188+ c = getopt_long (argc3 , argv , accepted_opt , long_options , & option_index );
209189 zassert_equal ('a' , c , "unexpected option" );
210- c = getopt_long (argc3 , argv , accepted_opt ,
211- long_options , & option_index );
190+ c = getopt_long (argc3 , argv , accepted_opt , long_options , & option_index );
212191 zassert_equal (-1 , c , "getopt_long shall return -1" );
213192
214193 /* Test scenario 4 */
215194 argv = (char * * )argv4 ;
216195 getopt_init ();
217- c = getopt_long (argc4 , argv , accepted_opt ,
218- long_options , & option_index );
196+ c = getopt_long (argc4 , argv , accepted_opt , long_options , & option_index );
219197 zassert_equal (verbose_flag , 0 , "verbose flag expected" );
220- c = getopt_long (argc4 , argv , accepted_opt ,
221- long_options , & option_index );
198+ c = getopt_long (argc4 , argv , accepted_opt , long_options , & option_index );
222199 /* Function was called with option '-l'. It is expected it will be
223200 * NOT evaluated to '--long' which has flag 'e'.
224201 */
225202 zassert_not_equal ('e' , c , "unexpected option match" );
226- c = getopt_long (argc4 , argv , accepted_opt ,
227- long_options , & option_index );
203+ c = getopt_long (argc4 , argv , accepted_opt , long_options , & option_index );
228204}
229205
230- ZTEST (getopt_test_suite , test_getopt_long_only )
206+ ZTEST (posix_c_lib_ext , test_getopt_long_only )
231207{
232208 /* Below test is based on example
233209 * https://www.gnu.org/software/libc/manual/html_node/Getopt-Long-Option-Example.html
@@ -240,16 +216,16 @@ ZTEST(getopt_test_suite, test_getopt_long_only)
240216 int c ;
241217 struct option long_options [] = {
242218 /* These options set a flag. */
243- {"verbose" , no_argument , & verbose_flag , 1 },
244- {"brief" , no_argument , & verbose_flag , 0 },
219+ {"verbose" , no_argument , & verbose_flag , 1 },
220+ {"brief" , no_argument , & verbose_flag , 0 },
245221 /* These options don’t set a flag.
246222 * We distinguish them by their indices.
247223 */
248- {"add" , no_argument , 0 , 'a' },
249- {"create" , required_argument , 0 , 'c' },
250- {"delete" , required_argument , 0 , 'd' },
251- {"long" , required_argument , 0 , 'e' },
252- {0 , 0 , 0 , 0 }
224+ {"add" , no_argument , 0 , 'a' },
225+ {"create" , required_argument , 0 , 'c' },
226+ {"delete" , required_argument , 0 , 'd' },
227+ {"long" , required_argument , 0 , 'e' },
228+ {0 , 0 , 0 , 0 },
253229 };
254230 static const char * accepted_opt = "ac:d:e:" ;
255231
@@ -289,62 +265,48 @@ ZTEST(getopt_test_suite, test_getopt_long_only)
289265 /* Test scenario 1 */
290266 argv = (char * * )argv1 ;
291267 getopt_init ();
292- c = getopt_long_only (argc1 , argv , accepted_opt ,
293- long_options , & option_index );
268+ c = getopt_long_only (argc1 , argv , accepted_opt , long_options , & option_index );
294269 zassert_equal (verbose_flag , 1 , "verbose flag expected" );
295- c = getopt_long_only (argc1 , argv , accepted_opt ,
296- long_options , & option_index );
270+ c = getopt_long_only (argc1 , argv , accepted_opt , long_options , & option_index );
297271 state = getopt_state_get ();
298272 zassert_equal ('c' , c , "unexpected option" );
299- zassert_equal (0 , strcmp (state -> optarg , argv [GETOPT_LONG_IDX_OPTARG ]),
300- "unexpected optarg" );
301- c = getopt_long_only (argc1 , argv , accepted_opt ,
302- long_options , & option_index );
273+ zassert_equal (0 , strcmp (state -> optarg , argv [GETOPT_LONG_IDX_OPTARG ]), "unexpected optarg" );
274+ c = getopt_long_only (argc1 , argv , accepted_opt , long_options , & option_index );
303275 zassert_equal (-1 , c , "getopt_long_only shall return -1" );
304276
305277 /* Test scenario 2 */
306278 argv = (char * * )argv2 ;
307279 getopt_init ();
308280 state = getopt_state_get ();
309- c = getopt_long_only (argc2 , argv , accepted_opt ,
310- long_options , & option_index );
281+ c = getopt_long_only (argc2 , argv , accepted_opt , long_options , & option_index );
311282 zassert_equal (verbose_flag , 0 , "verbose flag expected" );
312- c = getopt_long_only (argc2 , argv , accepted_opt ,
313- long_options , & option_index );
283+ c = getopt_long_only (argc2 , argv , accepted_opt , long_options , & option_index );
314284 state = getopt_state_get ();
315285 zassert_equal ('d' , c , "unexpected option" );
316- zassert_equal (0 , strcmp (state -> optarg , argv [GETOPT_LONG_IDX_OPTARG ]),
317- "unexpected optarg" );
318- c = getopt_long_only (argc2 , argv , accepted_opt ,
319- long_options , & option_index );
286+ zassert_equal (0 , strcmp (state -> optarg , argv [GETOPT_LONG_IDX_OPTARG ]), "unexpected optarg" );
287+ c = getopt_long_only (argc2 , argv , accepted_opt , long_options , & option_index );
320288 zassert_equal (-1 , c , "getopt_long_only shall return -1" );
321289
322290 /* Test scenario 3 */
323291 argv = (char * * )argv3 ;
324292 getopt_init ();
325- c = getopt_long_only (argc3 , argv , accepted_opt ,
326- long_options , & option_index );
293+ c = getopt_long_only (argc3 , argv , accepted_opt , long_options , & option_index );
327294 zassert_equal (verbose_flag , 0 , "verbose flag expected" );
328- c = getopt_long_only (argc3 , argv , accepted_opt ,
329- long_options , & option_index );
295+ c = getopt_long_only (argc3 , argv , accepted_opt , long_options , & option_index );
330296 zassert_equal ('a' , c , "unexpected option" );
331- c = getopt_long_only (argc3 , argv , accepted_opt ,
332- long_options , & option_index );
297+ c = getopt_long_only (argc3 , argv , accepted_opt , long_options , & option_index );
333298 zassert_equal (-1 , c , "getopt_long_only shall return -1" );
334299
335300 /* Test scenario 4 */
336301 argv = (char * * )argv4 ;
337302 getopt_init ();
338- c = getopt_long_only (argc4 , argv , accepted_opt ,
339- long_options , & option_index );
303+ c = getopt_long_only (argc4 , argv , accepted_opt , long_options , & option_index );
340304 zassert_equal (verbose_flag , 0 , "verbose flag expected" );
341- c = getopt_long_only (argc4 , argv , accepted_opt ,
342- long_options , & option_index );
305+ c = getopt_long_only (argc4 , argv , accepted_opt , long_options , & option_index );
343306
344307 /* Function was called with option '-l'. It is expected it will be
345308 * evaluated to '--long' which has flag 'e'.
346309 */
347310 zassert_equal ('e' , c , "unexpected option" );
348- c = getopt_long_only (argc4 , argv , accepted_opt ,
349- long_options , & option_index );
311+ c = getopt_long_only (argc4 , argv , accepted_opt , long_options , & option_index );
350312}
0 commit comments