File: pathtest.cc

package info (click to toggle)
dune-common 2.10.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,824 kB
  • sloc: cpp: 52,256; python: 3,979; sh: 1,658; makefile: 17
file content (204 lines) | stat: -rw-r--r-- 5,685 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception

#include <iostream>
#include <ostream>
#include <string>

#include <dune/common/exceptions.hh>
#include <dune/common/ios_state.hh>
#include <dune/common/path.hh>

void setCode(int& code, bool status) {
  if(!status)
    code = 1;
  else
  if(code == 77)
    code = 0;
}

void concatPathsTests(int& code) {
  typedef const char* const triple[3];
  static const triple data[] = {
    {"a" , "b" , "a/b" },
    {"/a", "b" , "/a/b"},
    {"a/", "b" , "a/b" },
    {"a" , "b/", "a/b/"},
    {"..", "b" , "../b"},
    {"a" , "..", "a/.."},
    {"." , "b" , "./b" },
    {"a" , "." , "a/." },
    {""  , "b" , "b"   },
    {"a" , ""  , "a"   },
    {""  , ""  , ""    },
    {NULL, NULL, NULL  }
  };
  for(const triple* p = data; (*p)[0] != NULL; ++p) {
    const std::string& result = Dune::concatPaths((*p)[0], (*p)[1]);
    bool success = result == (*p)[2];
    setCode(code, success);
    if(!success)
      std::cerr << "concatPaths(\"" << (*p)[0] << "\", "
                << "\"" << (*p)[1] << "\"): got \"" << result << "\", "
                << "expected \"" << (*p)[2] << "\"" << std::endl;
  }
}

void processPathTests(int& code) {
  typedef const char* const pair[3];
  static const pair data[] = {
    {""      , ""     },
    {"."     , ""     },
    {"./"    , ""     },
    {"a/.."  , ""     },
    {".."    , "../"  },
    {"../a"  , "../a/"},
    {"a"     , "a/"   },
    {"a//"   , "a/"   },
    {"a///b" , "a/b/" },
    {"/"     , "/"    },
    {"/."    , "/"    },
    {"/.."   , "/"    },
    {"/a/.." , "/"    },
    {"/a"    , "/a/"  },
    {"/a/"   , "/a/"  },
    {"/../a/", "/a/"  },
    {NULL    , NULL   }
  };
  for(const pair* p = data; (*p)[0] != NULL; ++p) {
    const std::string& result = Dune::processPath((*p)[0]);
    bool success = result == (*p)[1];
    setCode(code, success);
    if(!success)
      std::cerr << "processPath(\"" << (*p)[0] << "\"): got "
                << "\"" << result << "\", expected "
                << "\"" << (*p)[1] << "\"" << std::endl;
  }
}

void prettyPathTests(int& code) {
  struct triple {
    const char* p;
    bool isDir;
    const char* result;
  };
  static const triple data[] = {
    {""      , true , "."    },
    {""      , false, "."    },
    {"."     , true , "."    },
    {"."     , false, "."    },
    {"./"    , true , "."    },
    {"./"    , false, "."    },
    {"a/.."  , true , "."    },
    {"a/.."  , false, "."    },
    {".."    , true , ".."   },
    {".."    , false, ".."   },
    {"../a"  , true , "../a/"},
    {"../a"  , false, "../a" },
    {"a"     , true , "a/"   },
    {"a"     , false, "a"    },
    {"a//"   , true , "a/"   },
    {"a//"   , false, "a"    },
    {"a///b" , true , "a/b/" },
    {"a///b" , false, "a/b"  },
    {"/"     , true , "/"    },
    {"/"     , false, "/"    },
    {"/."    , true , "/"    },
    {"/."    , false, "/"    },
    {"/.."   , true , "/"    },
    {"/.."   , false, "/"    },
    {"/a/.." , true , "/"    },
    {"/a/.." , false, "/"    },
    {"/a"    , true , "/a/"  },
    {"/a"    , false, "/a"   },
    {"/a/"   , true , "/a/"  },
    {"/a/"   , false, "/a"   },
    {"/../a/", true , "/a/"  },
    {"/../a/", false, "/a"   },
    {NULL,     false, NULL   }
  };

  Dune::ios_base_all_saver state(std::cerr);
  std::cerr << std::boolalpha;

  for(const triple* p = data; p->p != NULL; ++p) {
    const std::string& result = Dune::prettyPath(p->p, p->isDir);
    bool success = result == p->result;
    setCode(code, success);
    if(!success)
      std::cerr << "prettyPath(\"" << p->p << "\", " << p->isDir << "): got "
                << "\"" << result << "\", expected \"" << p->result << "\""
                << std::endl;
  }
}

void relativePathTests(int& code) {
  typedef const char* const triple[3];
  static const triple data[] = {
    {""  , ""  , ""     },
    {""  , "b" , "b/"   },
    {""  , "..", "../"  },
    {"a" , ""  , "../"  },
    {"a" , "b" , "../b/"},
    {"/" , "/" , ""     },
    {"/a", "/" , "../"  },
    {"/" , "/b", "b/"   },
    {"/a", "/b", "../b/"},
    {NULL, NULL, NULL   }
  };

  for(const triple* p = data; (*p)[0] != NULL; ++p) {
    const std::string& result = Dune::relativePath((*p)[0], (*p)[1]);
    bool success = result == (*p)[2];
    setCode(code, success);
    if(!success)
      std::cerr << "relativePath(\"" << (*p)[0] << "\", "
                << "\"" << (*p)[1] << "\"): got \"" << result << "\", "
                << "expected \"" << (*p)[2] << "\"" << std::endl;
  }

  typedef const char* const pair[2];
  static const pair except_data[] = {
    {""  , "/" },
    {"a" , "/" },
    {"/" , ""  },
    {"/" , "b" },
    {"..", ""  },
    {NULL, NULL}
  };

  for(const pair* p = except_data; (*p)[0] != NULL; ++p) {
    std::string result;
    try {
      result = Dune::relativePath((*p)[0], (*p)[1]);
    }
    catch(const Dune::NotImplemented&) {
      setCode(code, true);
      continue;
    }
    setCode(code, false);
    std::cerr << "relativePath(\"" << (*p)[0] << "\", "
              << "\"" << (*p)[1] << "\"): got \"" << result << "\", "
              << "expected exception thrown" << std::endl;
  }
}

int main ()
{
  try {
    int code = 77;

    concatPathsTests(code);
    processPathTests(code);
    prettyPathTests(code);
    relativePathTests(code);

    return code;
  }
  catch(const Dune::Exception& e) {
    std::cerr << "Exception thrown: " << e << std::endl;
    throw;
  }
}