Motr  M0
getopts.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2012-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 #include "ut/ut.h" /* M0_UT_ASSERT */
24 #include "lib/thread.h" /* LAMBDA */
25 #include "lib/getopts.h" /* m0_bcount_get */
26 #include "lib/time.h" /* m0_time_t */
27 #include "lib/errno.h" /* ENOENT */
28 
29 void test_getopts(void)
30 {
31  int result;
32  int argc;
33  int argc_scaled;
34  int num;
35  bool e;
36  m0_bcount_t bcount;
37  m0_time_t time;
38  char *argv[] = {
39  "getopts-ut",
40  "-e",
41  "-n", "010",
42  NULL
43  };
44  char *argv_scaled[] = {
45  "-a", "2b",
46  "-b", "30k",
47  "-c", "400m",
48  "-d", "5000g",
49  "-x", "70K",
50  "-y", "800M",
51  "-z", "9000G",
52  "-j", "123456789012345",
53  NULL
54  };
55  struct m0_ut_redirect redir;
56 
57  argc = ARRAY_SIZE(argv) - 1;
58  argc_scaled = ARRAY_SIZE(argv_scaled) - 1;
59 
60  m0_stream_redirect(stderr, "/dev/null", &redir);
61  result = M0_GETOPTS("getopts-ut", argc, argv);
62  M0_UT_ASSERT(result == -EINVAL);
63  m0_stream_restore(&redir);
64 
65  e = false;
66  result = M0_GETOPTS("getopts-ut", argc, argv,
67  M0_FORMATARG('n', "Num", "%i", &num),
68  M0_FLAGARG('e', "E", &e));
69  M0_UT_ASSERT(result == 0);
70  M0_UT_ASSERT(e == true);
71  M0_UT_ASSERT(num == 8);
72 
73  result = M0_GETOPTS("getopts-ut", argc, argv,
74  M0_FORMATARG('n', "Num", "%d", &num),
75  M0_FLAGARG('e', "E", &e));
76  M0_UT_ASSERT(num == 10);
77 
78  result = M0_GETOPTS("getopts-ut", argc, argv,
79  M0_STRINGARG('n', "Num",
80  LAMBDA(void, (const char *s){
81  M0_UT_ASSERT(!strcmp(s, "010"));
82  })),
83  M0_FLAGARG('e', "E", &e));
84  M0_UT_ASSERT(result == 0);
85 
86  /* test for valid "scaled"-type options */
87  result = M0_GETOPTS("getopts-ut", argc_scaled, argv_scaled,
88  M0_SCALEDARG('a', "scaled",
89  LAMBDA(void, (m0_bcount_t bcount){
90  M0_UT_ASSERT(bcount == 2 * 512);})),
91  M0_SCALEDARG('b', "scaled",
92  LAMBDA(void, (m0_bcount_t bcount){
93  M0_UT_ASSERT(bcount == 30 * 1024);})),
94  M0_SCALEDARG('c', "scaled",
95  LAMBDA(void, (m0_bcount_t bcount){
96  M0_UT_ASSERT(bcount == 400 * 1024 * 1024);})),
97  M0_SCALEDARG('d', "scaled",
98  LAMBDA(void, (m0_bcount_t bcount){
99  M0_UT_ASSERT(bcount ==
100  5000ULL * 1024 * 1024 * 1024);})),
101  M0_SCALEDARG('x', "scaled",
102  LAMBDA(void, (m0_bcount_t bcount){
103  M0_UT_ASSERT(bcount == 70 * 1000);})),
104  M0_SCALEDARG('y', "scaled",
105  LAMBDA(void, (m0_bcount_t bcount){
106  M0_UT_ASSERT(bcount == 800 * 1000000);})),
107  M0_SCALEDARG('z', "scaled",
108  LAMBDA(void, (m0_bcount_t bcount){
109  M0_UT_ASSERT(bcount ==
110  9000 * 1000000000ULL);})),
111  M0_SCALEDARG('j', "scaled",
112  LAMBDA(void, (m0_bcount_t bcount){
113  M0_UT_ASSERT(bcount ==
114  123456789012345ULL);})));
115  M0_UT_ASSERT(result == 0);
116 
117  argv[--argc] = NULL;
118  argv[--argc] = NULL;
119 
120  e = false;
121  result = M0_GETOPTS("getopts-ut", argc, argv,
122  M0_FLAGARG('e', "E", &e));
123  M0_UT_ASSERT(result == 0);
124  M0_UT_ASSERT(e == true);
125  argv[--argc] = NULL;
126 
127  result = M0_GETOPTS("getopts-ut", argc, argv);
128  M0_UT_ASSERT(result == 0);
129 
130  /* m0_bcount_get() */
131  result = m0_bcount_get("123456789012345G", &bcount);
132  M0_UT_ASSERT(result == -EOVERFLOW);
133  result = m0_bcount_get("1asdf", &bcount);
134  M0_UT_ASSERT(result == -EINVAL);
135 
136  /* m0_time_get() */
137  result = m0_time_get("1", &time);
138  M0_UT_ASSERT(result == 0);
139  M0_UT_ASSERT(m0_time_seconds(time) == 1);
140  M0_UT_ASSERT(m0_time_nanoseconds(time) == 0);
141 
142  result = m0_time_get("1.20s", &time);
143  M0_UT_ASSERT(result == 0);
144  M0_UT_ASSERT(m0_time_seconds(time) == 1);
145  M0_UT_ASSERT(m0_time_nanoseconds(time) == 200000000);
146 
147  result = m0_time_get("2.300ms", &time);
148  M0_UT_ASSERT(result == 0);
149  M0_UT_ASSERT(m0_time_seconds(time) == 0);
150  M0_UT_ASSERT(m0_time_nanoseconds(time) == 2300000);
151 
152  result = m0_time_get("3.4000us", &time);
153  M0_UT_ASSERT(result == 0);
154  M0_UT_ASSERT(m0_time_seconds(time) == 0);
155  M0_UT_ASSERT(m0_time_nanoseconds(time) == 3400);
156 
157  result = m0_time_get("5.60000ns", &time);
158  M0_UT_ASSERT(result == 0);
159  M0_UT_ASSERT(m0_time_seconds(time) == 0);
160  M0_UT_ASSERT(m0_time_nanoseconds(time) == 5);
161 
162  result = m0_time_get("12345.67890s", &time);
163  M0_UT_ASSERT(result == 0);
164  M0_UT_ASSERT(m0_time_seconds(time) == 12345);
165  M0_UT_ASSERT(m0_time_nanoseconds(time) == 678900000);
166 
167  result = m0_time_get(".1s", &time);
168  M0_UT_ASSERT(result == 0);
169  M0_UT_ASSERT(m0_time_seconds(time) == 0);
170  M0_UT_ASSERT(m0_time_nanoseconds(time) == 100000000);
171 
172  result = m0_time_get(".01s", &time);
173  M0_UT_ASSERT(result == 0);
174  M0_UT_ASSERT(m0_time_seconds(time) == 0);
175  M0_UT_ASSERT(m0_time_nanoseconds(time) == 10000000);
176 
177  result = m0_time_get("12345.67890sec", &time);
178  M0_UT_ASSERT(result == -EINVAL);
179 
180  result = m0_time_get("18446744073709551616", &time);
181  M0_UT_ASSERT(result == -E2BIG);
182 }
183 
184 /*
185  * Local variables:
186  * c-indentation-style: "K&R"
187  * c-basic-offset: 8
188  * tab-width: 8
189  * fill-column: 80
190  * scroll-step: 1
191  * End:
192  */
#define M0_GETOPTS(progname, argc, argv,...)
Definition: getopts.h:169
#define M0_FLAGARG(ch, desc, ptr)
Definition: getopts.h:232
#define NULL
Definition: misc.h:38
M0_INTERNAL void m0_stream_restore(const struct m0_ut_redirect *redir)
Definition: ut.c:107
uint64_t m0_time_t
Definition: time.h:37
uint64_t m0_time_nanoseconds(const m0_time_t time)
Definition: time.c:89
uint64_t m0_bcount_t
Definition: types.h:77
#define M0_STRINGARG(ch, desc, func)
Definition: getopts.h:207
#define LAMBDA(T,...)
Definition: thread.h:153
#define M0_FORMATARG(ch, desc, fmt, ptr)
Definition: getopts.h:218
M0_INTERNAL int m0_time_get(const char *arg, m0_time_t *out)
Definition: getopts.c:69
uint64_t m0_time_seconds(const m0_time_t time)
Definition: time.c:83
#define M0_SCALEDARG(ch, desc, func)
Definition: getopts.h:197
void test_getopts(void)
Definition: getopts.c:29
M0_INTERNAL int m0_bcount_get(const char *arg, m0_bcount_t *out)
Definition: getopts.c:35
M0_INTERNAL void m0_stream_redirect(FILE *stream, const char *path, struct m0_ut_redirect *redir)
Definition: ut.c:87
int num
Definition: bulk_if.c:54
static struct m0_addb2_source * s
Definition: consumer.c:39
#define ARRAY_SIZE(a)
Definition: misc.h:45
#define M0_UT_ASSERT(a)
Definition: ut.h:46