xref: /4.0.0/platform/tests/getopt_test.cc (revision c0012dd1)
1/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2#include <iostream>
3#include <cstdlib>
4#include <string.h>
5#include <getopt.h>
6#include <vector>
7#include <string>
8#include <platform/cbassert.h>
9
10
11#ifdef _MSC_VER
12#define strdup _strdup
13#endif
14
15char **vec2array(const std::vector<std::string> &vec) {
16    char **arr = new char*[vec.size()];
17    for (unsigned int ii = 0; ii < (unsigned int)vec.size(); ++ii) {
18        arr[ii] = strdup(vec[ii].c_str());
19    }
20    return arr;
21}
22
23static void release(char **arr, size_t size) {
24    for (size_t ii = 0; ii < size; ++ii) {
25        free(arr[ii]);
26    }
27    delete []arr;
28}
29
30typedef std::vector<std::string> getoptvec;
31
32static void getopt_test_0(void) {
33    getoptvec vec;
34    vec.push_back("program");
35    vec.push_back("-a");
36    vec.push_back("-b");
37    int argc = (int)vec.size();
38    char **argv = vec2array(vec);
39
40    cb_assert('a' == getopt(argc, argv, "a"));
41    cb_assert('?' == getopt(argc, argv, "a"));
42    cb_assert(optind == 3);
43
44    release(argv, vec.size());
45}
46
47static void getopt_test_1(void) {
48    getoptvec vec;
49    vec.push_back("program");
50    vec.push_back("-a");
51    vec.push_back("--");
52    vec.push_back("-b");
53    int argc = (int)vec.size();
54    char **argv = vec2array(vec);
55    cb_assert('a' == getopt(argc, argv, "a"));
56    cb_assert(-1 == getopt(argc, argv, "a"));
57    cb_assert(optind == 3);
58
59    release(argv, vec.size());
60}
61
62static void getopt_test_2(void) {
63    getoptvec vec;
64
65    vec.push_back("..\\memcached\\engine_testapp");
66    vec.push_back("-E");
67    vec.push_back("ep.dll");
68    vec.push_back("-T");
69    vec.push_back("ep_testsuite.dll");
70    vec.push_back("-e");
71    vec.push_back("flushall_enabled=true;ht_size=13;ht_locks=7");
72    vec.push_back("-v");
73    vec.push_back("-C");
74    vec.push_back("7");
75    vec.push_back("-s");
76    vec.push_back("foo");
77
78    int argc = (int)vec.size();
79    char **argv = vec2array(vec);
80
81    cb_assert('E' == getopt(argc, argv, "E:T:e:vC:s"));
82    cb_assert(strcmp(argv[2], optarg) == 0);
83    cb_assert('T' == getopt(argc, argv, "E:T:e:vC:s"));
84    cb_assert(strcmp(argv[4], optarg) == 0);
85    cb_assert('e' == getopt(argc, argv, "E:T:e:vC:s"));
86    cb_assert(strcmp(argv[6], optarg) == 0);
87    cb_assert('v' == getopt(argc, argv, "E:T:e:vC:s"));
88    cb_assert('C' == getopt(argc, argv, "E:T:e:vC:s"));
89    cb_assert(strcmp(argv[9], optarg) == 0);
90    cb_assert('s' == getopt(argc, argv, "E:T:e:vC:s"));
91    cb_assert(-1 == getopt(argc, argv, "E:T:e:vC:s"));
92    cb_assert(optind == 11);
93
94    release(argv, vec.size());
95}
96
97int main(int argc, char **argv)
98{
99    if (argc != 2) {
100        std::cerr << "Usage: " << argv[0] << " [testcase]" << std::endl;
101        return 1;
102    }
103
104    switch (atoi(argv[1])) {
105    case 0:
106        getopt_test_0();
107        break;
108    case 1:
109        getopt_test_1();
110        break;
111    case 2:
112        getopt_test_2();
113        break;
114    default:
115        std::cerr << "Unknown test case" << std::endl;
116        return 1;
117    }
118
119    return 0;
120}
121