summaryrefslogtreecommitdiffstats
path: root/lib/tests/test_opts1.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/tests/test_opts1.c')
-rw-r--r--lib/tests/test_opts1.c159
1 files changed, 75 insertions, 84 deletions
diff --git a/lib/tests/test_opts1.c b/lib/tests/test_opts1.c
index 0b91622a..077c5b63 100644
--- a/lib/tests/test_opts1.c
+++ b/lib/tests/test_opts1.c
@@ -21,14 +21,44 @@
21 21
22#include "tap.h" 22#include "tap.h"
23 23
24void my_free(int *argc, char **argv) { 24/* This my_free would be the most efficient way of freeing an extra-opts array,
25 * provided as an example */
26#if 0
27void my_free(int *argc, char **newargv, char **argv) {
25 int i; 28 int i;
26 printf (" Arg(%i): ", *argc); 29 /* Return if both arrays are the same */
27 for (i=1; i<*argc; i++) printf ("'%s' ", argv[i]); 30 if (newargv == argv) return;
31
32 for (i=1; i<*argc; i++) {
33 /* Free array items until we reach the start of the original argv */
34 if (newargv[i] == argv[1]) break;
35 free(newargv[i]);
36 }
37 /* Free the array itself */
38 free(newargv);
39}
40#else
41void my_free(int *argc, char **newargv, char **argv) {
42 /* Free stuff (and print while we're at it) */
43 int i, freeflag=1;
44 printf (" Arg(%i): ", *argc+1);
45 printf ("'%s' ", newargv[0]);
46 for (i=1; i<*argc; i++) {
47 printf ("'%s' ", newargv[i]);
48 /* Stop freeing when we get to the start of the original array */
49 if (freeflag) {
50 if (newargv[i] == argv[1])
51 freeflag=0;
52 else
53 free(newargv[i]);
54 }
55 }
28 printf ("\n"); 56 printf ("\n");
29 free(argv); 57 /* Free only if it's a different array */
58 if (newargv != argv) free(newargv);
30 *argc=0; 59 *argc=0;
31} 60}
61#endif
32 62
33int array_diff(int i1, char **a1, int i2, char **a2) { 63int array_diff(int i1, char **a1, int i2, char **a2) {
34 int i; 64 int i;
@@ -54,94 +84,55 @@ int array_diff(int i1, char **a1, int i2, char **a2) {
54int 84int
55main (int argc, char **argv) 85main (int argc, char **argv)
56{ 86{
57 char **argv_test=NULL, **argv_known=NULL; 87 char **argv_new=NULL;
58 int i, argc_test; 88 int i, argc_test;
59 89
60 plan_tests(5); 90 plan_tests(5);
61 91
62 argv_test=(char **)malloc(2*sizeof(char **)); 92 {
63 argv_test[0] = "prog_name"; 93 char *argv_test[] = {"prog_name", (char *) NULL};
64 argv_test[1] = NULL; 94 argc_test=1;
65 argc_test=1; 95 char *argv_known[] = {"prog_name", (char *) NULL};
66 argv_known=(char **)realloc(argv_known, 2*sizeof(char **)); 96 argv_new=np_extra_opts(&argc_test, argv_test, "check_disk");
67 argv_known[0] = "prog_name"; 97 ok(array_diff(argc_test, argv_new, 1, argv_known), "No opts, returns correct argv/argc");
68 argv_known[1] = NULL; 98 my_free(&argc_test, argv_new, argv_test);
69 argv_test=np_extra_opts(&argc_test, argv_test, "check_disk"); 99 }
70 ok(array_diff(argc_test, argv_test, 1, argv_known), "No opts, returns correct argv/argc");
71 my_free(&argc_test, argv_test);
72 100
73 argv_test=(char **)malloc(6*sizeof(char **)); 101 {
74 argv_test[0] = "prog_name"; 102 char *argv_test[] = {"prog_name", "arg1", "--arg2=val1", "--arg3", "val2", (char *) NULL};
75 argv_test[1] = "arg1"; 103 argc_test=5;
76 argv_test[2] = "--arg2=val1"; 104 char *argv_known[] = {"prog_name", "arg1", "--arg2=val1", "--arg3", "val2", (char *) NULL};
77 argv_test[3] = "--arg3"; 105 argv_new=np_extra_opts(&argc_test, argv_test, "check_disk");
78 argv_test[4] = "val2"; 106 ok(array_diff(argc_test, argv_new, 5, argv_known), "No extra opts, verbatim copy of argv");
79 argv_test[5] = NULL; 107 my_free(&argc_test, argv_new, argv_test);
80 argc_test=5; 108 }
81 argv_known=(char **)realloc(argv_known, 6*sizeof(char **));
82 argv_known[0] = "prog_name";
83 argv_known[1] = "arg1";
84 argv_known[2] = "--arg2=val1";
85 argv_known[3] = "--arg3";
86 argv_known[4] = "val2";
87 argv_known[5] = NULL;
88 argv_test=np_extra_opts(&argc_test, argv_test, "check_disk");
89 ok(array_diff(argc_test, argv_test, 5, argv_known), "No extra opts, verbatim copy of argv");
90 my_free(&argc_test,argv_test);
91 109
92 argv_test=(char **)malloc(3*sizeof(char **)); 110 {
93 argv_test[0] = "prog_name"; 111 char *argv_test[] = {"prog_name", "--extra-opts=@./config-opts.ini", (char *) NULL};
94 argv_test[1] = "--extra-opts=@./config-opts.ini"; 112 argc_test=2;
95 argv_test[2] = NULL; 113 char *argv_known[] = {"prog_name", "--foo=Bar", "--this=Your Mother!", "--blank", (char *) NULL};
96 argc_test=2; 114 argv_new=np_extra_opts(&argc_test, argv_test, "check_disk");
97 argv_known=(char **)realloc(argv_known, 5*sizeof(char **)); 115 ok(array_diff(argc_test, argv_new, 4, argv_known), "Only extra opts using default section");
98 argv_known[0] = "prog_name"; 116 my_free(&argc_test, argv_new, argv_test);
99 argv_known[1] = "--foo=Bar"; 117 }
100 argv_known[2] = "--this=Your Mother!";
101 argv_known[3] = "--blank";
102 argv_known[4] = NULL;
103 argv_test=np_extra_opts(&argc_test, argv_test, "check_disk");
104 ok(array_diff(argc_test, argv_test, 4, argv_known), "Only extra opts using default section");
105 my_free(&argc_test,argv_test);
106 118
107 argv_test=(char **)malloc(5*sizeof(char **)); 119 {
108 argv_test[0] = "prog_name"; 120 char *argv_test[] = {"prog_name", "--extra-opts=sect1@./config-opts.ini", "--extra-opts", "sect2@./config-opts.ini", (char *) NULL};
109 argv_test[1] = "--extra-opts=sect1@./config-opts.ini"; 121 argc_test=4;
110 argv_test[2] = "--extra-opts"; 122 char *argv_known[] = {"prog_name", "--one=two", "--something else=oops", "--this=that", (char *) NULL};
111 argv_test[3] = "sect2@./config-opts.ini"; 123 argv_new=np_extra_opts(&argc_test, argv_test, "check_disk");
112 argv_test[4] = NULL; 124 ok(array_diff(argc_test, argv_new, 4, argv_known), "Only extra opts specified twice");
113 argc_test=4; 125 my_free(&argc_test, argv_new, argv_test);
114 argv_known=(char **)realloc(argv_known, 5*sizeof(char **)); 126 }
115 argv_known[0] = "prog_name";
116 argv_known[1] = "--one=two";
117 argv_known[2] = "--something else=oops";
118 argv_known[3] = "--this=that";
119 argv_known[4] = NULL;
120 argv_test=np_extra_opts(&argc_test, argv_test, "check_disk");
121 ok(array_diff(argc_test, argv_test, 4, argv_known), "Only extra opts specified twice");
122 my_free(&argc_test,argv_test);
123 127
124 argv_test=(char **)malloc(7*sizeof(char **)); 128 {
125 argv_test[0] = "prog_name"; 129 char *argv_test[] = {"prog_name", "--arg1=val1", "--extra-opts=@./config-opts.ini", "--extra-opts", "sect1@./config-opts.ini", "--arg2", (char *) NULL};
126 argv_test[1] = "--arg1=val1"; 130 argc_test=6;
127 argv_test[2] = "--extra-opts=@./config-opts.ini"; 131 char *argv_known[] = {"prog_name", "--foo=Bar", "--this=Your Mother!", "--blank", "--one=two", "--arg1=val1", "--arg2", (char *) NULL};
128 argv_test[3] = "--extra-opts"; 132 argv_new=np_extra_opts(&argc_test, argv_test, "check_disk");
129 argv_test[4] = "sect1@./config-opts.ini"; 133 ok(array_diff(argc_test, argv_new, 7, argv_known), "twice extra opts using two sections");
130 argv_test[5] = "--arg2"; 134 my_free(&argc_test, argv_new, argv_test);
131 argv_test[6] = NULL; 135 }
132 argc_test=6;
133 argv_known=(char **)realloc(argv_known, 8*sizeof(char **));
134 argv_known[0] = "prog_name";
135 argv_known[1] = "--foo=Bar";
136 argv_known[2] = "--this=Your Mother!";
137 argv_known[3] = "--blank";
138 argv_known[4] = "--one=two";
139 argv_known[5] = "--arg1=val1";
140 argv_known[6] = "--arg2";
141 argv_known[7] = NULL;
142 argv_test=np_extra_opts(&argc_test, argv_test, "check_disk");
143 ok(array_diff(argc_test, argv_test, 7, argv_known), "twice extra opts using two sections");
144 my_free(&argc_test,argv_test);
145 136
146 return exit_status(); 137 return exit_status();
147} 138}