source: web/old/remctl-2.14/tests/portable/getopt-t.c @ f6f3e91

web
Last change on this file since f6f3e91 was f6f3e91, checked in by Jessica B. Hamrick <jhamrick@…>, 15 years ago

Preserve directory hierarchy (not sure what happened to it)

  • Property mode set to 100644
File size: 3.4 KB
Line 
1/*
2 * getopt test suite.
3 *
4 * Written by Russ Allbery <rra@stanford.edu>
5 * Copyright 2008, 2009 Board of Trustees, Leland Stanford Jr. University
6 *
7 * See LICENSE for licensing terms.
8 */
9
10#include <config.h>
11#include <portable/system.h>
12#include <portable/getopt.h>
13
14#include <tests/tap/basic.h>
15
16int test_getopt(int, char **, const char *);
17extern int test_optind;
18extern int test_opterr;
19extern int test_optopt;
20extern char *test_optarg;
21
22int
23main(void)
24{
25    const char *test1[] = {
26        "foo", "-ab", "-c", "foo", "bar", "-dbar", "-", "-efbaz", "--", "-g"
27    };
28    const char *test2[] = { "foo", "-a" };
29    const char *test3[] = { "foo" };
30    const char *test4[] = { "foo", "-a", "foo", "-b" };
31
32    plan(37);
33
34    /* We currently have no tests for opterr and error reporting. */
35    is_int(1, test_opterr, "default opterr value");
36    test_opterr = 0;
37
38    /* Basic test with permuting. */
39    is_int('a', test_getopt(10, (char **) test1, "abc:d:f:g"), "-a");
40    is_int('b', test_getopt(10, (char **) test1, "abc:d:f:g"), "-b");
41    is_int('c', test_getopt(10, (char **) test1, "abc:d:f:g"), "-c");
42    is_string("foo", test_optarg, "-c foo");
43    is_int('d', test_getopt(10, (char **) test1, "abc:d:f:g"), "-d");
44    is_string("bar", test_optarg, "-dbar");
45    is_int('?', test_getopt(10, (char **) test1, "abc:d:f:g"), "- option");
46    is_int('e', test_optopt, "-e");
47    is_int('f', test_getopt(10, (char **) test1, "abc:d:f:g"), "-f");
48    is_string("baz", test_optarg, "-fbaz");
49    is_int(-1, test_getopt(10, (char **) test1, "abc:d:f:g"),
50           "end of options");
51    is_int(7, test_optind, "optind value");
52    is_string("bar", test1[7], "bar is first non-argument");
53    is_string("-", test1[8], "then -");
54    is_string("-g", test1[9], "then -g");
55
56    /* Test for missing argument. */
57    test_optind = 1;
58    is_int('?', test_getopt(2, (char **) test2, "a:"), "-a without arg");
59    is_int('a', test_optopt, "optopt set properly");
60    test_optind = 1;
61    test_optopt = 0;
62    is_int('a', test_getopt(2, (char **) test2, "a::"), "-a w/optional arg");
63    ok(test_optarg == NULL, "no optarg");
64    is_int(2, test_optind, "correct optind");
65    test_optind = 1;
66    test_optopt = 0;
67    is_int(':', test_getopt(2, (char **) test2, ":a:"),
68           ": starting option string");
69    is_int('a', test_optopt, "...with correct optopt");
70
71    /* Test for no arguments. */
72    test_optind = 1;
73    is_int(-1, test_getopt(1, (char **) test3, "abc"), "no arguments");
74    is_int(1, test_optind, "...and optind set properly");
75
76    /* Test for non-option handling. */
77    test_optind = 1;
78    is_int('a', test_getopt(4, (char **) test4, "+abc"), "-a with +");
79    is_int(-1, test_getopt(4, (char **) test4, "+abc"), "end of arguments");
80    is_int(2, test_optind, "and optind is correct");
81    is_string("foo", test4[2], "foo is first non-option");
82    is_string("-b", test4[3], "-b is second non-option");
83    test_optind = 1;
84    is_int('a', test_getopt(4, (char **) test4, "-abc"), "-a with -");
85    is_int('\1', test_getopt(4, (char **) test4, "-abc"), "foo with -");
86    is_string("foo", test_optarg, "...and optarg is correct");
87    is_int('b', test_getopt(4, (char **) test4, "-abc"), "-b with -");
88    ok(test_optarg == NULL, "...and optarg is not set");
89    is_int(-1, test_getopt(4, (char **) test4, "-abc"),
90           "and now end of arguments");
91    is_int(4, test_optind, "...and optind is set correctly");
92
93    exit(0);
94}
Note: See TracBrowser for help on using the repository browser.