int main (void) { /* This test validates that stderr is used correctly, so move the original into fd 10. */ if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO || (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL) return 2; ASSERT (freopen ("test-getopt.tmp", "w", stderr) == stderr); /* These default values are required by POSIX. */ ASSERT (optind == 1); ASSERT (opterr != 0); setenv ("POSIXLY_CORRECT", "1", 1); test_getopt (); #if GNULIB_TEST_GETOPT_GNU test_getopt_long_posix (); #endif unsetenv ("POSIXLY_CORRECT"); test_getopt (); #if GNULIB_TEST_GETOPT_GNU test_getopt_long (); test_getopt_long_only (); #endif ASSERT (fclose (stderr) == 0); ASSERT (remove ("test-getopt.tmp") == 0); return 0; }
ret_t mmp_getopt_unittest(t_mmp_tap_cycle_s *cycle) { ret_t ret; if ((ret = mmp_tap_test(cycle, "xgetopt", "unimplemented", test_getopt()))!=MMP_ERR_OK) return ret; return MMP_ERR_OK; }
int main () { unsetenv ("POSIXLY_CORRECT"); test_getopt (); #if GNULIB_GETOPT_GNU test_getopt_long (); #endif return 0; }
int main(void) { const char *test1[] = { "foo", "-ab", "-c", "foo", "bar", "-dbar", "-", "-efbaz", "--", "-g" }; const char *test2[] = { "foo", "-a" }; const char *test3[] = { "foo" }; const char *test4[] = { "foo", "-a", "foo", "-b" }; plan(37); /* We currently have no tests for opterr and error reporting. */ is_int(1, test_opterr, "default opterr value"); test_opterr = 0; /* Basic test with permuting. */ is_int('a', test_getopt(10, (char **) test1, "abc:d:f:g"), "-a"); is_int('b', test_getopt(10, (char **) test1, "abc:d:f:g"), "-b"); is_int('c', test_getopt(10, (char **) test1, "abc:d:f:g"), "-c"); is_string("foo", test_optarg, "-c foo"); is_int('d', test_getopt(10, (char **) test1, "abc:d:f:g"), "-d"); is_string("bar", test_optarg, "-dbar"); is_int('?', test_getopt(10, (char **) test1, "abc:d:f:g"), "- option"); is_int('e', test_optopt, "-e"); is_int('f', test_getopt(10, (char **) test1, "abc:d:f:g"), "-f"); is_string("baz", test_optarg, "-fbaz"); is_int(-1, test_getopt(10, (char **) test1, "abc:d:f:g"), "end of options"); is_int(7, test_optind, "optind value"); is_string("bar", test1[7], "bar is first non-argument"); is_string("-", test1[8], "then -"); is_string("-g", test1[9], "then -g"); /* Test for missing argument. */ test_optind = 1; is_int('?', test_getopt(2, (char **) test2, "a:"), "-a without arg"); is_int('a', test_optopt, "optopt set properly"); test_optind = 1; test_optopt = 0; is_int('a', test_getopt(2, (char **) test2, "a::"), "-a w/optional arg"); ok(test_optarg == NULL, "no optarg"); is_int(2, test_optind, "correct optind"); test_optind = 1; test_optopt = 0; is_int(':', test_getopt(2, (char **) test2, ":a:"), ": starting option string"); is_int('a', test_optopt, "...with correct optopt"); /* Test for no arguments. */ test_optind = 1; is_int(-1, test_getopt(1, (char **) test3, "abc"), "no arguments"); is_int(1, test_optind, "...and optind set properly"); /* Test for non-option handling. */ test_optind = 1; is_int('a', test_getopt(4, (char **) test4, "+abc"), "-a with +"); is_int(-1, test_getopt(4, (char **) test4, "+abc"), "end of arguments"); is_int(2, test_optind, "and optind is correct"); is_string("foo", test4[2], "foo is first non-option"); is_string("-b", test4[3], "-b is second non-option"); test_optind = 1; is_int('a', test_getopt(4, (char **) test4, "-abc"), "-a with -"); is_int('\1', test_getopt(4, (char **) test4, "-abc"), "foo with -"); is_string("foo", test_optarg, "...and optarg is correct"); is_int('b', test_getopt(4, (char **) test4, "-abc"), "-b with -"); ok(test_optarg == NULL, "...and optarg is not set"); is_int(-1, test_getopt(4, (char **) test4, "-abc"), "and now end of arguments"); is_int(4, test_optind, "...and optind is set correctly"); exit(0); }