Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add tests for recent getopt changes.
- Loading branch information
Ulrich Drepper
committed
Apr 8, 2010
1 parent
66b93be
commit e326768
Showing
7 changed files
with
404 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* BZ 11039 */ | ||
#include <unistd.h> | ||
#include <stdio.h> | ||
|
||
static int | ||
one_test (const char *fmt, int argc, char *argv[], int expected[argc - 1]) | ||
{ | ||
optind = 1; | ||
|
||
int res = 0; | ||
for (int i = 0; i < argc - 1; ++i) | ||
{ | ||
rewind (stderr); | ||
if (ftruncate (fileno (stderr), 0) != 0) | ||
{ | ||
puts ("cannot truncate file"); | ||
return 1; | ||
} | ||
|
||
int c = getopt (argc, argv, fmt); | ||
if (c != expected[i]) | ||
{ | ||
printf ("format '%s' test %d failed: expected '%c', got '%c'\n", | ||
fmt, i, expected[i], c); | ||
res = 1; | ||
} | ||
if (ftell (stderr) != 0) | ||
{ | ||
printf ("format '%s' test %d failed: printed to stderr\n", | ||
fmt, i); | ||
res = 1; | ||
} | ||
} | ||
|
||
return res; | ||
} | ||
|
||
|
||
static int | ||
do_test (void) | ||
{ | ||
char *fname = tmpnam (NULL); | ||
if (fname == NULL) | ||
{ | ||
puts ("cannot generate name for temporary file"); | ||
return 1; | ||
} | ||
|
||
if (freopen (fname, "w+", stderr) == NULL) | ||
{ | ||
puts ("cannot redirect stderr"); | ||
return 1; | ||
} | ||
|
||
remove (fname); | ||
|
||
int ret = one_test ("+:a:b", 2, | ||
(char *[2]) { (char *) "bug-getopt1", (char *) "-a" }, | ||
(int [1]) { ':' }); | ||
|
||
ret |= one_test ("+:a:b", 3, | ||
(char *[3]) { (char *) "bug-getopt1", (char *) "-b", | ||
(char *) "-a" }, | ||
(int [2]) { 'b', ':' }); | ||
|
||
if (ret == 0) | ||
puts ("all OK"); | ||
|
||
return ret; | ||
} | ||
|
||
#define TEST_FUNCTION do_test () | ||
#include "../test-skeleton.c" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* BZ 11039 */ | ||
#include <unistd.h> | ||
#include <stdio.h> | ||
|
||
static int | ||
one_test (const char *fmt, int argc, char *argv[], int expected[argc - 1]) | ||
{ | ||
int res = 0; | ||
for (int i = 0; i < argc - 1; ++i) | ||
{ | ||
rewind (stderr); | ||
if (ftruncate (fileno (stderr), 0) != 0) | ||
{ | ||
puts ("cannot truncate file"); | ||
return 1; | ||
} | ||
|
||
int c = getopt (argc, argv, fmt); | ||
if (c != expected[i]) | ||
{ | ||
printf ("format '%s' test %d failed: expected '%c', got '%c'\n", | ||
fmt, i, expected[i], c); | ||
res = 1; | ||
} | ||
if (ftell (stderr) == 0) | ||
{ | ||
printf ("format '%s' test %d failed: not printed to stderr\n", | ||
fmt, i); | ||
res = 1; | ||
} | ||
} | ||
|
||
return res; | ||
} | ||
|
||
|
||
static int | ||
do_test (void) | ||
{ | ||
char *fname = tmpnam (NULL); | ||
if (fname == NULL) | ||
{ | ||
puts ("cannot generate name for temporary file"); | ||
return 1; | ||
} | ||
|
||
if (freopen (fname, "w+", stderr) == NULL) | ||
{ | ||
puts ("cannot redirect stderr"); | ||
return 1; | ||
} | ||
|
||
remove (fname); | ||
|
||
optind = 0; | ||
int ret = one_test ("+a", 2, | ||
(char *[2]) { (char *) "bug-getopt2", (char *) "-+" }, | ||
(int [1]) { '?' }); | ||
|
||
optind = 1; | ||
ret |= one_test ("+a", 2, | ||
(char *[2]) { (char *) "bug-getopt2", (char *) "-+" }, | ||
(int [1]) { '?' }); | ||
|
||
if (ret == 0) | ||
puts ("all OK"); | ||
|
||
return ret; | ||
} | ||
|
||
#define TEST_FUNCTION do_test () | ||
#include "../test-skeleton.c" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* BZ 11040 */ | ||
#include <getopt.h> | ||
#include <unistd.h> | ||
#include <stdio.h> | ||
|
||
static const struct option opts[] = | ||
{ | ||
{ "alpha", no_argument, NULL, 'a' }, | ||
{ "beta", required_argument, NULL, 'b' }, | ||
{ NULL, 0, NULL, 0 } | ||
}; | ||
|
||
static int | ||
one_test (const char *fmt, int argc, char *argv[], int n, int expected[n], | ||
int out[n]) | ||
{ | ||
optind = 1; | ||
|
||
int res = 0; | ||
for (int i = 0; i < n; ++i) | ||
{ | ||
rewind (stderr); | ||
if (ftruncate (fileno (stderr), 0) != 0) | ||
{ | ||
puts ("cannot truncate file"); | ||
return 1; | ||
} | ||
|
||
int c = getopt_long (argc, argv, fmt, opts, NULL); | ||
if (c != expected[i]) | ||
{ | ||
printf ("format '%s' test %d failed: expected '%c', got '%c'\n", | ||
fmt, i, expected[i], c); | ||
res = 1; | ||
} | ||
if ((ftell (stderr) != 0) != out[i]) | ||
{ | ||
printf ("format '%s' test %d failed: %sprinted to stderr\n", | ||
fmt, i, out[i] ? "not " : ""); | ||
res = 1; | ||
} | ||
} | ||
|
||
return res; | ||
} | ||
|
||
|
||
static int | ||
do_test (void) | ||
{ | ||
char *fname = tmpnam (NULL); | ||
if (fname == NULL) | ||
{ | ||
puts ("cannot generate name for temporary file"); | ||
return 1; | ||
} | ||
|
||
if (freopen (fname, "w+", stderr) == NULL) | ||
{ | ||
puts ("cannot redirect stderr"); | ||
return 1; | ||
} | ||
|
||
remove (fname); | ||
|
||
int ret = one_test ("ab:W;", 2, | ||
(char *[2]) { (char *) "bug-getopt3", (char *) "-a;" }, | ||
2, (int [2]) { 'a', '?' }, (int [2]) { 0, 1 }); | ||
|
||
ret |= one_test ("ab:W;", 2, | ||
(char *[2]) { (char *) "bug-getopt3", (char *) "-a:" }, 2, | ||
(int [2]) { 'a', '?' }, (int [2]) { 0, 1 }); | ||
|
||
if (ret == 0) | ||
puts ("all OK"); | ||
|
||
return ret; | ||
} | ||
|
||
#define TEST_FUNCTION do_test () | ||
#include "../test-skeleton.c" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* BZ 11041 */ | ||
#include <getopt.h> | ||
#include <unistd.h> | ||
#include <stdio.h> | ||
|
||
static const struct option opts[] = | ||
{ | ||
{ "alpha", optional_argument, NULL, 'a' }, | ||
{ NULL, 0, NULL, 0 } | ||
}; | ||
|
||
static int | ||
one_test (const char *fmt, int argc, char *argv[], int n, int expected[n]) | ||
{ | ||
optind = 1; | ||
|
||
int res = 0; | ||
for (int i = 0; i < n; ++i) | ||
{ | ||
rewind (stderr); | ||
if (ftruncate (fileno (stderr), 0) != 0) | ||
{ | ||
puts ("cannot truncate file"); | ||
return 1; | ||
} | ||
|
||
int c = getopt_long (argc, argv, fmt, opts, NULL); | ||
if (c != expected[i]) | ||
{ | ||
printf ("format '%s' test %d failed: expected '%c', got '%c'\n", | ||
fmt, i, expected[i], c); | ||
res = 1; | ||
} | ||
else if (optarg != NULL) | ||
{ | ||
printf ("format '%s' test %d failed: optarg is \"%s\", not NULL\n", | ||
fmt, i, optarg); | ||
res = 1; | ||
} | ||
if (ftell (stderr) != 0) | ||
{ | ||
printf ("format '%s' test %d failed: printed to stderr\n", | ||
fmt, i); | ||
res = 1; | ||
} | ||
} | ||
|
||
return res; | ||
} | ||
|
||
|
||
static int | ||
do_test (void) | ||
{ | ||
char *fname = tmpnam (NULL); | ||
if (fname == NULL) | ||
{ | ||
puts ("cannot generate name for temporary file"); | ||
return 1; | ||
} | ||
|
||
if (freopen (fname, "w+", stderr) == NULL) | ||
{ | ||
puts ("cannot redirect stderr"); | ||
return 1; | ||
} | ||
|
||
remove (fname); | ||
|
||
int ret = one_test ("W;", 2, | ||
(char *[2]) { (char *) "bug-getopt4", (char *) "--a" }, | ||
1, (int [1]) { 'a' }); | ||
|
||
ret |= one_test ("W;", 3, | ||
(char *[3]) { (char *) "bug-getopt4", (char *) "-W", | ||
(char *) "a" }, | ||
1, (int [1]) { 'a' }); | ||
|
||
if (ret == 0) | ||
puts ("all OK"); | ||
|
||
return ret; | ||
} | ||
|
||
#define TEST_FUNCTION do_test () | ||
#include "../test-skeleton.c" |
Oops, something went wrong.