Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
testIoTools.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5 *
6 * This software is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 * See the file LICENSE.txt at the root directory of this source
11 * distribution for additional information about the GNU GPL.
12 *
13 * For using ViSP with software that can not be combined with the GNU
14 * GPL, please contact Inria about acquiring a ViSP Professional
15 * Edition License.
16 *
17 * See https://visp.inria.fr for more information.
18 *
19 * This software was developed at:
20 * Inria Rennes - Bretagne Atlantique
21 * Campus Universitaire de Beaulieu
22 * 35042 Rennes Cedex
23 * France
24 *
25 * If you have questions regarding the use of this file, please contact
26 * Inria at visp@inria.fr
27 *
28 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30 *
31 * Description:
32 * Test functions in vpIoTools.
33 *
34*****************************************************************************/
41#include <iostream>
42#include <stdio.h>
43#include <string.h>
44#include <visp3/core/vpIoTools.h>
45
46namespace
47{
48template <typename T> void checkReadBinaryValue(std::ifstream &file, const T checkValue)
49{
50 T value = (T)10;
52 if (value != checkValue) {
53 std::stringstream ss;
54 ss << "Read: " << value << " ; Expected: " << checkValue;
55 throw vpException(vpException::badValue, ss.str());
56 }
57}
58
59template <> void checkReadBinaryValue<float>(std::ifstream &file, const float checkValue)
60{
61 float value = 10.0f;
63 if (!vpMath::equal(value, checkValue, std::numeric_limits<float>::epsilon())) {
64 std::stringstream ss;
65 ss << "Read: " << value << " ; Expected: " << checkValue;
66 throw vpException(vpException::badValue, ss.str());
67 }
68}
69
70template <> void checkReadBinaryValue<double>(std::ifstream &file, const double checkValue)
71{
72 double value = 10.0;
74 if (!vpMath::equal(value, checkValue, std::numeric_limits<double>::epsilon())) {
75 std::stringstream ss;
76 ss << "Read: " << value << " ; Expected: " << checkValue;
77 throw vpException(vpException::badValue, ss.str());
78 }
79}
80} // namespace
81
82int main(int argc, const char **argv)
83{
84 const char c = vpIoTools::separator;
85 if (c == '\\') {
86 std::cout << "The directory separator character is '" << c << "' (Windows platform)." << std::endl;
87 } else {
88 std::cout << "The directory separator character is '" << c << "' (Unix like platform)." << std::endl;
89 }
90
91#if defined(_WIN32)
92 std::string pathname = "C:\\Program Files (x86)\\Java\\jre7";
93#else
94 std::string pathname = "/usr/bin/java";
95#endif
96
97 std::cout << "Parent of " << pathname << " is " << vpIoTools::getParent(pathname) << std::endl;
98 std::cout << "Name of " << pathname << " is " << vpIoTools::getName(pathname) << std::endl;
99
100 if (argc == 3 && std::string(argv[1]) == std::string("-i")) {
101 std::cout << "Parent of " << argv[2] << " is " << vpIoTools::getParent(argv[2]) << std::endl;
102 std::cout << "Name of " << argv[2] << " is " << vpIoTools::getName(argv[2]) << std::endl;
103 }
104
105 std::string windowsPathnameStyle = "\\usr\\bin\\java";
106 std::cout << "Parent of " << windowsPathnameStyle << " is " << vpIoTools::getParent(windowsPathnameStyle)
107 << std::endl;
108 std::cout << "Name of " << windowsPathnameStyle << " is " << vpIoTools::getName(windowsPathnameStyle) << std::endl;
109
110 std::string parent = "/usr/toto/", child = "\\blabla\\java";
111 std::cout << "parent=" << vpIoTools::path(parent) << " ; child=" << vpIoTools::path(child) << std::endl;
112 std::cout << "Create file path from parent=" << parent << " and child=" << child << " is "
113 << vpIoTools::createFilePath(parent, child) << std::endl;
114
115 std::string expandPath = "~/Documents/fictional directory/fictional file";
116 std::cout << "Path for " << expandPath << " is " << vpIoTools::path(expandPath) << std::endl;
117
118 std::cout << "Test get name with an empty pathname=" << vpIoTools::getName("") << std::endl;
119 std::cout << "Get parent with an empty pathname=" << vpIoTools::getParent("") << std::endl;
120 std::cout << "Get parent with a filename=" << vpIoTools::getParent("my_file.txt") << std::endl;
121 expandPath = "~/Documents/fictional dir/fictional file.txt";
122 std::cout << "Get name with a unix expand pathname " << expandPath << "=" << vpIoTools::getName(expandPath)
123 << std::endl;
124 std::cout << "Get parent with a unix expand pathname " << expandPath << "=" << vpIoTools::getParent(expandPath)
125 << std::endl;
126
127 pathname = "c:/dir";
128 std::cout << "pathname=" << vpIoTools::splitDrive(pathname).first << " ; " << vpIoTools::splitDrive(pathname).second
129 << std::endl;
130
131 std::cout << "isAbsolutePath of " << pathname << "=" << vpIoTools::isAbsolutePathname(pathname) << std::endl;
132
133 pathname = "c:/dir/fictional directory/fictional file.txt";
134 std::cout << "isAbsolutePath of " << pathname << "=" << vpIoTools::isAbsolutePathname(pathname) << std::endl;
135
136 pathname = "/home/user/Documents/fictional directory/fictional file.txt";
137 std::cout << "isAbsolutePath of " << pathname << "=" << vpIoTools::isAbsolutePathname(pathname) << std::endl;
138
139 pathname = "~/Documents/fictional directory/fictional file.txt";
140 std::cout << "isAbsolutePath of " << pathname << "=" << vpIoTools::isAbsolutePathname(pathname) << std::endl;
141
142 pathname = "fictional directory/fictional file.txt";
143 std::cout << "isAbsolutePath of " << pathname << "=" << vpIoTools::isAbsolutePathname(pathname) << std::endl;
144
145 // Test vpIoTools::splitDrive
146 unsigned int nbFail = 0, nbOk = 0;
147#if defined(_WIN32)
148 if (strcmp(vpIoTools::splitDrive("c:\\foo\\bar").first.c_str(), "c:") == 0) {
149 nbOk++;
150 } else {
151 nbFail++;
152 std::cout << "Fail=" << vpIoTools::splitDrive("c:\\foo\\bar").first << " should be=c:" << std::endl;
153 }
154 if (strcmp(vpIoTools::splitDrive("c:\\foo\\bar").second.c_str(), "\\foo\\bar") == 0) {
155 nbOk++;
156 } else {
157 nbFail++;
158 std::cout << "Fail=" << vpIoTools::splitDrive("c:\\foo\\bar").second << " should be=\\foo\\bar" << std::endl;
159 }
160
161 if (strcmp(vpIoTools::splitDrive("c:/foo/bar").first.c_str(), "c:") == 0) {
162 nbOk++;
163 } else {
164 nbFail++;
165 std::cout << "Fail=" << vpIoTools::splitDrive("c:/foo/bar").first << " should be=c:" << std::endl;
166 }
167 if (strcmp(vpIoTools::splitDrive("c:/foo/bar").second.c_str(), "/foo/bar") == 0) {
168 nbOk++;
169 } else {
170 nbFail++;
171 std::cout << "Fail=" << vpIoTools::splitDrive("c:/foo/bar").second << " should be=/foo/bar" << std::endl;
172 }
173
174 if (strcmp(vpIoTools::splitDrive("\\\\conky\\mountpoint\\foo\\bar").first.c_str(), "\\\\conky\\mountpoint") == 0) {
175 nbOk++;
176 } else {
177 nbFail++;
178 std::cout << "Fail=" << vpIoTools::splitDrive("\\\\conky\\mountpoint\\foo\\bar").first
179 << " should be=\\\\conky\\mountpoint" << std::endl;
180 }
181 if (strcmp(vpIoTools::splitDrive("\\\\conky\\mountpoint\\foo\\bar").second.c_str(), "\\foo\\bar") == 0) {
182 nbOk++;
183 } else {
184 nbFail++;
185 std::cout << "Fail=" << vpIoTools::splitDrive("\\\\conky\\mountpoint\\foo\\bar").second << " should be=\\foo\\bar"
186 << std::endl;
187 }
188
189 if (strcmp(vpIoTools::splitDrive("//conky/mountpoint/foo/bar").first.c_str(), "//conky/mountpoint") == 0) {
190 nbOk++;
191 } else {
192 nbFail++;
193 std::cout << "Fail=" << vpIoTools::splitDrive("//conky/mountpoint/foo/bar").first << " should be=//conky/mountpoint"
194 << std::endl;
195 }
196 if (strcmp(vpIoTools::splitDrive("//conky/mountpoint/foo/bar").second.c_str(), "/foo/bar") == 0) {
197 nbOk++;
198 } else {
199 nbFail++;
200 std::cout << "Fail=" << vpIoTools::splitDrive("//conky/mountpoint/foo/bar").second << " should be=/foo/bar"
201 << std::endl;
202 }
203
204 if (strcmp(vpIoTools::splitDrive("\\\\\\conky\\mountpoint\\foo\\bar").first.c_str(), "") == 0) {
205 nbOk++;
206 } else {
207 nbFail++;
208 std::cout << "Fail=" << vpIoTools::splitDrive("\\\\\\conky\\mountpoint\\foo\\bar").first
209 << " should be=" << std::endl;
210 }
211 if (strcmp(vpIoTools::splitDrive("\\\\\\conky\\mountpoint\\foo\\bar").second.c_str(),
212 "\\\\\\conky\\mountpoint\\foo\\bar") == 0) {
213 nbOk++;
214 } else {
215 nbFail++;
216 std::cout << "Fail=" << vpIoTools::splitDrive("\\\\\\conky\\mountpoint\\foo\\bar").second
217 << " should be=\\\\\\conky\\mountpoint\\foo\\bar" << std::endl;
218 }
219
220 if (strcmp(vpIoTools::splitDrive("///conky/mountpoint/foo/bar").first.c_str(), "") == 0) {
221 nbOk++;
222 } else {
223 nbFail++;
224 std::cout << "Fail=" << vpIoTools::splitDrive("///conky/mountpoint/foo/bar").first << " should be=" << std::endl;
225 }
226 if (strcmp(vpIoTools::splitDrive("///conky/mountpoint/foo/bar").second.c_str(), "///conky/mountpoint/foo/bar") == 0) {
227 nbOk++;
228 } else {
229 nbFail++;
230 std::cout << "Fail=" << vpIoTools::splitDrive("///conky/mountpoint/foo/bar").second
231 << " should be=///conky/mountpoint/foo/bar" << std::endl;
232 }
233
234 if (strcmp(vpIoTools::splitDrive("\\\\conky\\\\mountpoint\\foo\\bar").first.c_str(), "") == 0) {
235 nbOk++;
236 } else {
237 nbFail++;
238 std::cout << "Fail=" << vpIoTools::splitDrive("\\\\conky\\\\mountpoint\\foo\\bar").first
239 << " should be=" << std::endl;
240 }
241 if (strcmp(vpIoTools::splitDrive("\\\\conky\\\\mountpoint\\foo\\bar").second.c_str(),
242 "\\\\conky\\\\mountpoint\\foo\\bar") == 0) {
243 nbOk++;
244 } else {
245 nbFail++;
246 std::cout << "Fail=" << vpIoTools::splitDrive("\\\\conky\\\\mountpoint\\foo\\bar").second
247 << " should be=\\\\conky\\\\mountpoint\\foo\\bar" << std::endl;
248 }
249
250 if (strcmp(vpIoTools::splitDrive("//conky//mountpoint/foo/bar").first.c_str(), "") == 0) {
251 nbOk++;
252 } else {
253 nbFail++;
254 std::cout << "Fail=" << vpIoTools::splitDrive("//conky//mountpoint/foo/bar").first << " should be=" << std::endl;
255 }
256 if (strcmp(vpIoTools::splitDrive("//conky//mountpoint/foo/bar").second.c_str(), "//conky//mountpoint/foo/bar") == 0) {
257 nbOk++;
258 } else {
259 nbFail++;
260 std::cout << "Fail=" << vpIoTools::splitDrive("//conky//mountpoint/foo/bar").second
261 << " should be=//conky//mountpoint/foo/bar" << std::endl;
262 }
263
264 std::cout << "Test vpIoTools::splitDrive (Win32) - passed: " << nbOk << "/" << (nbOk + nbFail) << std::endl;
265
266 if (nbFail) {
267 std::cerr << "Failed test: vpIoTools::splitDrive (Win32)" << std::endl;
268 return EXIT_FAILURE;
269 }
270#endif
271
272// Test vpIoTools::getFileExtension
273#if defined(_WIN32)
274 nbFail = 0;
275 nbOk = 0;
276
277 if (strcmp(vpIoTools::getFileExtension("foo.ext").c_str(), ".ext") == 0) {
278 nbOk++;
279 } else {
280 nbFail++;
281 std::cout << "Fail=" << vpIoTools::getFileExtension("foo.ext") << " should be=.ext" << std::endl;
282 }
283
284 if (strcmp(vpIoTools::getFileExtension("/foo/foo.ext").c_str(), ".ext") == 0) {
285 nbOk++;
286 } else {
287 nbFail++;
288 std::cout << "Fail=" << vpIoTools::getFileExtension("/foo/foo.ext") << " should be=.ext" << std::endl;
289 }
290
291 if (strcmp(vpIoTools::getFileExtension(".ext").c_str(), "") == 0) {
292 nbOk++;
293 } else {
294 nbFail++;
295 std::cout << "Fail=" << vpIoTools::getFileExtension(".ext") << " should be=" << std::endl;
296 }
297
298 if (strcmp(vpIoTools::getFileExtension("\\foo.ext\\foo").c_str(), "") == 0) {
299 nbOk++;
300 } else {
301 nbFail++;
302 std::cout << "Fail=" << vpIoTools::getFileExtension("\\foo.ext\\foo") << " should be=" << std::endl;
303 }
304
305 if (strcmp(vpIoTools::getFileExtension("foo.ext\\").c_str(), "") == 0) {
306 nbOk++;
307 } else {
308 nbFail++;
309 std::cout << "Fail=" << vpIoTools::getFileExtension("foo.ext\\") << " should be=" << std::endl;
310 }
311
312 if (strcmp(vpIoTools::getFileExtension("").c_str(), "") == 0) {
313 nbOk++;
314 } else {
315 nbFail++;
316 std::cout << "Fail=" << vpIoTools::getFileExtension("") << " should be=" << std::endl;
317 }
318
319 if (strcmp(vpIoTools::getFileExtension("foo.bar.ext").c_str(), ".ext") == 0) {
320 nbOk++;
321 } else {
322 nbFail++;
323 std::cout << "Fail=" << vpIoTools::getFileExtension("foo.bar.ext") << " should be=.ext" << std::endl;
324 }
325
326 if (strcmp(vpIoTools::getFileExtension("xx/foo.bar.ext").c_str(), ".ext") == 0) {
327 nbOk++;
328 } else {
329 nbFail++;
330 std::cout << "Fail=" << vpIoTools::getFileExtension("xx/foo.bar.ext") << " should be=.ext" << std::endl;
331 }
332
333 if (strcmp(vpIoTools::getFileExtension("xx\\foo.bar.ext").c_str(), ".ext") == 0) {
334 nbOk++;
335 } else {
336 nbFail++;
337 std::cout << "Fail=" << vpIoTools::getFileExtension("xx\\foo.bar.ext") << " should be=.ext" << std::endl;
338 }
339
340 if (strcmp(vpIoTools::getFileExtension("c:a/b\\c.d").c_str(), ".d") == 0) {
341 nbOk++;
342 } else {
343 nbFail++;
344 std::cout << "Fail=" << vpIoTools::getFileExtension("c:a/b\\c.d") << " should be=.d" << std::endl;
345 }
346
347 std::cout << "Test vpIoTools::getFileExtension (WIN32 platform) - passed: " << nbOk << "/" << (nbOk + nbFail)
348 << std::endl;
349
350 if (nbFail) {
351 std::cerr << "Failed test: vpIoTools::getFileExtension (WIN32 platform)" << std::endl;
352 return EXIT_FAILURE;
353 }
354#else
355 nbFail = 0;
356 nbOk = 0;
357
358 if (strcmp(vpIoTools::getFileExtension("foo.bar").c_str(), ".bar") == 0) {
359 nbOk++;
360 } else {
361 nbFail++;
362 std::cout << "Fail=" << vpIoTools::getFileExtension("foo.bar") << " should be=.bar" << std::endl;
363 }
364
365 if (strcmp(vpIoTools::getFileExtension("foo.boo.bar").c_str(), ".bar") == 0) {
366 nbOk++;
367 } else {
368 nbFail++;
369 std::cout << "Fail=" << vpIoTools::getFileExtension("foo.boo.bar") << " should be=.bar" << std::endl;
370 }
371
372 if (strcmp(vpIoTools::getFileExtension("foo.boo.biff.bar").c_str(), ".bar") == 0) {
373 nbOk++;
374 } else {
375 nbFail++;
376 std::cout << "Fail=" << vpIoTools::getFileExtension("foo.boo.biff.bar") << " should be=.bar" << std::endl;
377 }
378
379 if (strcmp(vpIoTools::getFileExtension(".csh.rc").c_str(), ".rc") == 0) {
380 nbOk++;
381 } else {
382 nbFail++;
383 std::cout << "Fail=" << vpIoTools::getFileExtension(".csh.rc") << " should be=.rc" << std::endl;
384 }
385
386 if (strcmp(vpIoTools::getFileExtension("nodots").c_str(), "") == 0) {
387 nbOk++;
388 } else {
389 nbFail++;
390 std::cout << "Fail=" << vpIoTools::getFileExtension("nodots") << " should be=" << std::endl;
391 }
392
393 if (strcmp(vpIoTools::getFileExtension(".cshrc").c_str(), "") == 0) {
394 nbOk++;
395 } else {
396 nbFail++;
397 std::cout << "Fail=" << vpIoTools::getFileExtension(".cshrc") << " should be=" << std::endl;
398 }
399
400 if (strcmp(vpIoTools::getFileExtension("...manydots").c_str(), "") == 0) {
401 nbOk++;
402 } else {
403 nbFail++;
404 std::cout << "Fail=" << vpIoTools::getFileExtension("...manydots") << " should be=" << std::endl;
405 }
406
407 if (strcmp(vpIoTools::getFileExtension("...manydots.ext").c_str(), ".ext") == 0) {
408 nbOk++;
409 } else {
410 nbFail++;
411 std::cout << "Fail=" << vpIoTools::getFileExtension("...manydots.ext") << " should be=.ext" << std::endl;
412 }
413
414 if (strcmp(vpIoTools::getFileExtension(".").c_str(), "") == 0) {
415 nbOk++;
416 } else {
417 nbFail++;
418 std::cout << "Fail=" << vpIoTools::getFileExtension(".") << " should be=" << std::endl;
419 }
420
421 if (strcmp(vpIoTools::getFileExtension("..").c_str(), "") == 0) {
422 nbOk++;
423 } else {
424 nbFail++;
425 std::cout << "Fail=" << vpIoTools::getFileExtension("..") << " should be=" << std::endl;
426 }
427
428 if (strcmp(vpIoTools::getFileExtension("........").c_str(), "") == 0) {
429 nbOk++;
430 } else {
431 nbFail++;
432 std::cout << "Fail=" << vpIoTools::getFileExtension("........") << " should be=" << std::endl;
433 }
434
435 if (strcmp(vpIoTools::getFileExtension("").c_str(), "") == 0) {
436 nbOk++;
437 } else {
438 nbFail++;
439 std::cout << "Fail=" << vpIoTools::getFileExtension("") << " should be=" << std::endl;
440 }
441
442 std::cout << "Test vpIoTools::getFileExtension (Unix-like platform) - passed: " << nbOk << "/" << (nbOk + nbFail)
443 << std::endl;
444#endif
445
446 // Test makeDirectory()
447 try {
448 std::string username, directory_filename;
449 vpIoTools::getUserName(username);
450#if defined(_WIN32)
451 std::string tmp_dir = "C:/temp/" + username;
452#elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
453 std::string tmp_dir = "/tmp/" + username;
454#endif
455#if defined(_WIN32)
456 directory_filename = tmp_dir + "/test_directory1/test directory 2/";
457#elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
458 directory_filename = tmp_dir + "/test_directory1/test directory 2/";
459#endif
460 vpIoTools::makeDirectory(directory_filename);
461 vpIoTools::makeDirectory(directory_filename);
462 std::cout << "Create directories: " << directory_filename << std::endl;
463
464 if (!vpIoTools::checkDirectory(directory_filename)) {
465 std::cerr << "Error: " << directory_filename << " is not a directory" << std::endl;
466 return EXIT_FAILURE;
467 }
468
469#if defined(_WIN32)
470 directory_filename = tmp_dir + "/test_directory1/test directory 3";
471#elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
472 directory_filename = tmp_dir + "/test_directory1/test directory 3";
473#endif
474 vpIoTools::makeDirectory(directory_filename);
475 std::cout << "Create directories: " << directory_filename << std::endl;
476
477 if (!vpIoTools::checkDirectory(directory_filename)) {
478 std::cerr << "Error: " << directory_filename << " is not a directory" << std::endl;
479 return EXIT_FAILURE;
480 }
481
482#if defined(_WIN32)
483 directory_filename = "C:\\temp/" + username + "\\test_directory1\\test directory 4";
484#elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
485 directory_filename = "/tmp\\" + username + "\\test_directory1\\test directory 4";
486#endif
487 vpIoTools::makeDirectory(directory_filename);
488 vpIoTools::makeDirectory(directory_filename);
489 std::cout << "Create directories: " << directory_filename << std::endl;
490
491 if (!vpIoTools::checkDirectory(directory_filename)) {
492 std::cerr << "Error: " << directory_filename << " is not a directory" << std::endl;
493 return EXIT_FAILURE;
494 }
495
496#if defined(_WIN32)
497 directory_filename = "C:\\temp/" + username + "\\test_directory1\\test directory 5 . dir/test directory 6";
498#elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
499 directory_filename = "/tmp\\" + username + "\\test_directory1\\test directory 5 . dir/test directory 6";
500#endif
501 vpIoTools::makeDirectory(directory_filename);
502 std::cout << "Create directories: " << directory_filename << std::endl;
503
504 if (!vpIoTools::checkDirectory(directory_filename)) {
505 std::cerr << "Error: " << directory_filename << " is not a directory" << std::endl;
506 return EXIT_FAILURE;
507 }
508
509 // Delete test directory
510 if (!vpIoTools::remove(tmp_dir + "/test_directory1")) {
511 std::cerr << "Error: cannot remove directory: " << tmp_dir << "/test_directory1" << std::endl;
512 return EXIT_FAILURE;
513 }
514 } catch (const vpException &e) {
515 std::cerr << "Exception: " << e.what() << std::endl;
516 return EXIT_FAILURE;
517 }
518
519 // Test FIFO only implemented on unix like OS
520#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
521 try {
522 std::string username, fifo_file;
523 vpIoTools::getUserName(username);
524 std::string fifo_tmp_dir = "/tmp/" + username + "/fifo_test_directory";
525
526 if (!vpIoTools::checkDirectory(fifo_tmp_dir)) {
527 vpIoTools::makeDirectory(fifo_tmp_dir);
528 }
529
530 // Test 1
531 fifo_file = fifo_tmp_dir + "/" + "fifo_testfile";
532
533 vpIoTools::makeFifo(fifo_file);
534 std::cout << "Create fifo file: " << fifo_file << std::endl;
535
536 if (!vpIoTools::checkFifo(fifo_file)) {
537 std::cerr << "Error: file " << fifo_file << " is not a fifo file as expected" << std::endl;
538 return EXIT_FAILURE;
539 }
540
541 // Delete test file and directory
542 if (!vpIoTools::remove(fifo_file)) {
543 std::cerr << "Error: cannot remove fifo: " << fifo_file << std::endl;
544 return EXIT_FAILURE;
545 }
546 if (!vpIoTools::remove(fifo_tmp_dir)) {
547 std::cerr << "Error: cannot remove directory: " << fifo_tmp_dir << std::endl;
548 return EXIT_FAILURE;
549 }
550
551 } catch (const vpException &e) {
552 std::cerr << "Catch an exception: " << e.what() << std::endl;
553 return EXIT_FAILURE;
554 }
555#endif
556
557 // Test makeTempDirectory()
558#if defined(_WIN32) || \
559 (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX or Windows
560 try {
561 std::string directory_filename_tmp;
562 std::string tmp_dir = vpIoTools::getTempPath();
563
564 // Test 1
565 directory_filename_tmp = tmp_dir + "/" + "vpIoTools_test_XXXXXX";
566
567 std::string converted_dirname_tmp = vpIoTools::makeTempDirectory(directory_filename_tmp);
568
569 std::cout << "Create temp directory: " << converted_dirname_tmp << std::endl;
570
571 if (!vpIoTools::checkDirectory(converted_dirname_tmp)) {
572 std::cerr << "Error: " << converted_dirname_tmp << " is not a tmp directory" << std::endl;
573 return EXIT_FAILURE;
574 }
575
576 // Delete test directory
577 if (!vpIoTools::remove(converted_dirname_tmp)) {
578 std::cerr << "Error: cannot remove temp directory: " << converted_dirname_tmp << std::endl;
579 return EXIT_FAILURE;
580 }
581
582 // Test 2
584 converted_dirname_tmp = vpIoTools::makeTempDirectory(tmp_dir);
585
586 std::cout << "Create temp directory: " << converted_dirname_tmp << std::endl;
587
588 if (!vpIoTools::checkDirectory(converted_dirname_tmp)) {
589 std::cerr << "Error: " << converted_dirname_tmp << " is not a temp directory" << std::endl;
590 return EXIT_FAILURE;
591 }
592
593 // Delete test directory
594 if (!vpIoTools::remove(converted_dirname_tmp)) {
595 std::cerr << "Cannot remove directory: " << converted_dirname_tmp << std::endl;
596 return EXIT_FAILURE;
597 }
598
599 } catch (const vpException &e) {
600 std::cerr << "Catch an exception: " << e.what() << std::endl;
601 return EXIT_FAILURE;
602 }
603#endif
604
605 // Get the user login name
606 std::string username = vpIoTools::getUserName();
607 std::ofstream dummy_file;
608
609// Test isSamePathname()
610#if defined(_WIN32)
611 std::string path1 = "tmp/test/file.txt";
612 std::string path2 = "tmp/test/../test/file.txt";
613
614 nbOk = 0;
615 nbFail = 0;
616 bool res;
617
618 res = vpIoTools::isSamePathname(path1, path2); // True
619 std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
620 nbOk = res ? nbOk + 1 : nbOk;
621 nbFail = res ? nbFail : nbFail + 1;
622
623 path1 = ".\\tmp/test/file.txt";
624 res = vpIoTools::isSamePathname(path1, path2); // True
625 std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
626 nbOk = res ? nbOk + 1 : nbOk;
627 nbFail = res ? nbFail : nbFail + 1;
628
629 path1 = ".\\tmp/test\\../fake dir/..\\test\\file.txt";
630 res = vpIoTools::isSamePathname(path1, path2); // True
631 std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
632 nbOk = res ? nbOk + 1 : nbOk;
633 nbFail = res ? nbFail : nbFail + 1;
634
635 path2 = "/tmp/test/../test/file.txt";
636 res = vpIoTools::isSamePathname(path1, path2); // False
637 std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
638 nbOk = res ? nbOk : nbOk + 1;
639 nbFail = res ? nbFail + 1 : nbFail;
640
641 std::cout << "Test vpIoTools::isSamePathname (WIN32 platform) - passed: " << nbOk << "/" << (nbOk + nbFail)
642 << std::endl;
643 if (nbFail) {
644 std::cerr << "Failed test: vpIoTools::isSamePathname (WIN32 platform)" << std::endl;
645 return EXIT_FAILURE;
646 }
647#else
648 // realpath requires not fake path, so we create dummy file and directories
649
650 vpIoTools::makeDirectory("/tmp/" + username + "/test");
651 vpIoTools::makeDirectory("/tmp/" + username + "/dummy dir");
652
653 std::string path1 = "/tmp/" + username + "/test/file.txt";
654 std::string path2 = "/tmp/" + username + "/test/../test/file.txt";
655 dummy_file.open(path1.c_str());
656 if (!dummy_file.is_open()) {
657 return EXIT_SUCCESS;
658 }
659 dummy_file.close();
660
661 nbOk = 0;
662 nbFail = 0;
663 bool res;
664
665 res = vpIoTools::isSamePathname(path1, path2); // True
666 std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
667 nbOk = res ? nbOk + 1 : nbOk;
668 nbFail = res ? nbFail : nbFail + 1;
669
670 path1 = "\\tmp/" + username + "/./test/file.txt";
671 res = vpIoTools::isSamePathname(path1, path2); // True
672 std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
673 nbOk = res ? nbOk + 1 : nbOk;
674 nbFail = res ? nbFail : nbFail + 1;
675
676 path1 = "\\tmp/" + username + "/test\\../dummy dir/..\\test\\file.txt";
677 res = vpIoTools::isSamePathname(path1, path2); // True
678 std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
679 nbOk = res ? nbOk + 1 : nbOk;
680 nbFail = res ? nbFail : nbFail + 1;
681
682 path2 = "/tmp/" + username + "/test/../test";
683 res = vpIoTools::isSamePathname(path1, path2); // False
684 std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
685 nbOk = res ? nbOk : nbOk + 1;
686 nbFail = res ? nbFail + 1 : nbFail;
687
688 path1 = "/tmp/" + username + "/test/";
689 res = vpIoTools::isSamePathname(path1, path2); // True
690 std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
691 nbOk = res ? nbOk + 1 : nbOk;
692 nbFail = res ? nbFail : nbFail + 1;
693
694 std::cout << "Test vpIoTools::isSamePathname (Unix platform) - passed: " << nbOk << "/" << (nbOk + nbFail)
695 << std::endl;
696
697 // Delete test directory
698 if (!vpIoTools::remove("/tmp/" + username + "/test")) {
699 std::cerr << "Cannot remove directory: "
700 << "/tmp/" << username << "/test" << std::endl;
701 }
702 if (!vpIoTools::remove("/tmp/" + username + "/dummy dir")) {
703 std::cerr << "Cannot remove directory: "
704 << "/tmp/" << username << "/dummy dir" << std::endl;
705 }
706
707 if (nbFail) {
708 std::cerr << "Failed test: vpIoTools::isSamePathname (Unix platform)" << std::endl;
709 return EXIT_FAILURE;
710 }
711#endif
712
713 // Test getIndex()
714 if (vpIoTools::getIndex("file-1.txt", "file-%d.txt") != 1) {
715 std::cerr << "Failed test: vpIoTools::getIndex(\"file-1.txt\", \"file-%d.txt\")" << std::endl;
716 return EXIT_FAILURE;
717 }
718 if (vpIoTools::getIndex("/tmp/file0040.txt", "/tmp/file%04d.txt") != 40) {
719 std::cerr << "Failed test: vpIoTools::getIndex(\"/tmp/file0040.txt\", \"/tmp/file%04d.txt\")" << std::endl;
720 return EXIT_FAILURE;
721 }
722 if (vpIoTools::getIndex("file.txt", "file%d.txt") != -1) {
723 std::cerr << "Failed test: vpIoTools::getIndex(\"file.txt\", \"file%d.txt\")" << std::endl;
724 return EXIT_FAILURE;
725 }
726
727 // Test checkFilename()
728 vpIoTools::makeDirectory("/tmp/" + username + "/directory (1) with ' quote and spaces");
729 path1 = "/tmp/" + username +
730 "/directory (1) with ' quote and spaces/file with ' quote (1) and "
731 "spaces.txt";
732 dummy_file.open(path1.c_str());
733 if (!dummy_file.is_open()) {
734 return EXIT_SUCCESS;
735 }
736 dummy_file.close();
737
738 if (!vpIoTools::checkFilename(path1)) {
739 std::cerr << "Problem with checkFilename(" << path1 << ")!" << std::endl;
740 return EXIT_FAILURE;
741 }
742 std::cout << "Test vpIoTools::checkFilename() is ok." << std::endl;
743
744 // Delete test directory
745 if (!vpIoTools::remove("/tmp/" + username + "/directory (1) with ' quote and spaces")) {
746 std::cerr << "Cannot remove directory: "
747 << "/tmp/" << username << "/directory (1) with ' quote and spaces" << std::endl;
748 }
749
750 // Test endianness
751 {
752 std::string filename_endianness =
753 vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(), "endianness/test_endianness_little_endian.bin");
754 std::ifstream file_endianness(filename_endianness.c_str(), std::ios::in | std::ios::binary);
755 if (file_endianness.is_open()) {
756 checkReadBinaryValue<short>(file_endianness, std::numeric_limits<short>::min());
757 checkReadBinaryValue<short>(file_endianness, std::numeric_limits<short>::max());
758
759 checkReadBinaryValue<unsigned short>(file_endianness, std::numeric_limits<unsigned short>::min());
760 checkReadBinaryValue<unsigned short>(file_endianness, std::numeric_limits<unsigned short>::max());
761
762 checkReadBinaryValue<int>(file_endianness, std::numeric_limits<int>::min());
763 checkReadBinaryValue<int>(file_endianness, std::numeric_limits<int>::max());
764
765 checkReadBinaryValue<unsigned int>(file_endianness, std::numeric_limits<unsigned int>::min());
766 checkReadBinaryValue<unsigned int>(file_endianness, std::numeric_limits<unsigned int>::max());
767
768 checkReadBinaryValue<float>(file_endianness, -std::numeric_limits<float>::max());
769 checkReadBinaryValue<float>(file_endianness, std::numeric_limits<float>::max());
770
771 checkReadBinaryValue<double>(file_endianness, -std::numeric_limits<double>::max());
772 checkReadBinaryValue<double>(file_endianness, std::numeric_limits<double>::max());
773
774 std::cout << "Test endianness is ok." << std::endl;
775 } else {
776 std::cout << "Cannot open file: " << filename_endianness << std::endl;
777 }
778 }
779
780 // Test vpIoTools::toLowerCase
781 {
782 int nbFail = 0;
783 int nbOk = 0;
784 std::string testString = std::string("Yolo-V3");
785 std::string expectedLower = std::string("yolo-v3");
786 std::string expectedUpper = std::string("YOLO-V3");
787 #if defined(_WIN32)
788
789 if (strcmp(vpIoTools::toLowerCase(testString).c_str(), expectedLower.c_str()) == 0) {
790 nbOk++;
791 } else {
792 nbFail++;
793 std::cout << "Fail=" <<vpIoTools::toLowerCase(testString).c_str() << " should be=" << expectedLower << std::endl;
794 }
795
796 if (strcmp(vpIoTools::toUpperCase(testString).c_str(), expectedUpper.c_str()) == 0) {
797 nbOk++;
798 } else {
799 nbFail++;
800 std::cout << "Fail=" <<vpIoTools::toUpperCase(testString).c_str() << " should be=" << expectedUpper << std::endl;
801 }
802
803 std::cout << "Test vpIoTools::toLowerCase (WIN32 platform) - passed: " << nbOk << "/" << (nbOk + nbFail)
804 << std::endl;
805
806 if (nbFail) {
807 std::cerr << "Failed test: vpIoTools::toLowerCase (WIN32 platform)" << std::endl;
808 return EXIT_FAILURE;
809 }
810 #else
811 if (strcmp(vpIoTools::toLowerCase(testString).c_str(), expectedLower.c_str()) == 0) {
812 nbOk++;
813 } else {
814 nbFail++;
815 std::cout << "Fail=" <<vpIoTools::toLowerCase(testString).c_str() << " should be=" << expectedLower << std::endl;
816 }
817
818 if (strcmp(vpIoTools::toUpperCase(testString).c_str(), expectedUpper.c_str()) == 0) {
819 nbOk++;
820 } else {
821 nbFail++;
822 std::cout << "Fail=" <<vpIoTools::toUpperCase(testString).c_str() << " should be=" << expectedUpper << std::endl;
823 }
824
825 std::cout << "Test vpIoTools::toLowerCase (Unix-like platform) - passed: " << nbOk << "/" << (nbOk + nbFail)
826 << std::endl;
827
828 if (nbFail) {
829 std::cerr << "Failed test: vpIoTools::toLowerCase (Unix-like platform)" << std::endl;
830 return EXIT_FAILURE;
831 }
832 #endif
833 }
834
835
836 std::cout << std::endl << "Test succeed" << std::endl;
837 return EXIT_SUCCESS;
838}
error that can be emitted by ViSP classes.
Definition vpException.h:59
@ badValue
Used to indicate that a value is not in the allowed range.
Definition vpException.h:85
const char * what() const
static std::string getViSPImagesDataPath()
static std::string path(const std::string &pathname)
static std::string toLowerCase(const std::string &input)
Return a lower-case version of the string input . Numbers and special characters stay the same.
static bool checkFilename(const std::string &filename)
static std::pair< std::string, std::string > splitDrive(const std::string &pathname)
static bool isSamePathname(const std::string &pathname1, const std::string &pathname2)
static std::string getTempPath()
static bool isAbsolutePathname(const std::string &pathname)
static void readBinaryValueLE(std::ifstream &file, int16_t &short_value)
static bool checkDirectory(const std::string &dirname)
static long getIndex(const std::string &filename, const std::string &format)
static std::string getUserName()
static std::string createFilePath(const std::string &parent, const std::string &child)
static std::string getFileExtension(const std::string &pathname, bool checkFile=false)
static void makeDirectory(const std::string &dirname)
static bool remove(const std::string &filename)
static std::string makeTempDirectory(const std::string &dirname)
static bool checkFifo(const std::string &filename)
static std::string getParent(const std::string &pathname)
static std::string getName(const std::string &pathname)
static std::string toUpperCase(const std::string &input)
Return a upper-case version of the string input . Numbers and special characters stay the same.
static const char separator
Definition vpIoTools.h:181
static void makeFifo(const std::string &dirname)
static bool equal(double x, double y, double threshold=0.001)
Definition vpMath.h:369