Visual Servoing Platform version 3.5.0
testIoTools.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2019 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 http://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>
49void checkReadBinaryValue(std::ifstream &file, const T checkValue)
50{
51 T value = (T)10;
53 if (value != checkValue) {
54 std::stringstream ss;
55 ss << "Read: " << value << " ; Expected: " << checkValue;
56 throw vpException(vpException::badValue, ss.str());
57 }
58}
59
60template <>
61void checkReadBinaryValue<float>(std::ifstream &file, const float checkValue)
62{
63 float value = 10.0f;
65 if (!vpMath::equal(value, checkValue, std::numeric_limits<float>::epsilon())) {
66 std::stringstream ss;
67 ss << "Read: " << value << " ; Expected: " << checkValue;
68 throw vpException(vpException::badValue, ss.str());
69 }
70}
71
72template <>
73void checkReadBinaryValue<double>(std::ifstream &file, const double checkValue)
74{
75 double value = 10.0;
77 if (!vpMath::equal(value, checkValue, std::numeric_limits<double>::epsilon())) {
78 std::stringstream ss;
79 ss << "Read: " << value << " ; Expected: " << checkValue;
80 throw vpException(vpException::badValue, ss.str());
81 }
82}
83}
84
85int main(int argc, const char **argv)
86{
87 const char c = vpIoTools::separator;
88 if (c == '\\') {
89 std::cout << "The directory separator character is '" << c << "' (Windows platform)." << std::endl;
90 } else {
91 std::cout << "The directory separator character is '" << c << "' (Unix like platform)." << std::endl;
92 }
93
94#if defined(_WIN32)
95 std::string pathname = "C:\\Program Files (x86)\\Java\\jre7";
96#else
97 std::string pathname = "/usr/bin/java";
98#endif
99
100 std::cout << "Parent of " << pathname << " is " << vpIoTools::getParent(pathname) << std::endl;
101 std::cout << "Name of " << pathname << " is " << vpIoTools::getName(pathname) << std::endl;
102
103 if (argc == 3 && std::string(argv[1]) == std::string("-i")) {
104 std::cout << "Parent of " << argv[2] << " is " << vpIoTools::getParent(argv[2]) << std::endl;
105 std::cout << "Name of " << argv[2] << " is " << vpIoTools::getName(argv[2]) << std::endl;
106 }
107
108 std::string windowsPathnameStyle = "\\usr\\bin\\java";
109 std::cout << "Parent of " << windowsPathnameStyle << " is " << vpIoTools::getParent(windowsPathnameStyle)
110 << std::endl;
111 std::cout << "Name of " << windowsPathnameStyle << " is " << vpIoTools::getName(windowsPathnameStyle) << std::endl;
112
113 std::string parent = "/usr/toto/", child = "\\blabla\\java";
114 std::cout << "parent=" << vpIoTools::path(parent) << " ; child=" << vpIoTools::path(child) << std::endl;
115 std::cout << "Create file path from parent=" << parent << " and child=" << child << " is "
116 << vpIoTools::createFilePath(parent, child) << std::endl;
117
118 std::string expandPath = "~/Documents/fictional directory/fictional file";
119 std::cout << "Path for " << expandPath << " is " << vpIoTools::path(expandPath) << std::endl;
120
121 std::cout << "Test get name with an empty pathname=" << vpIoTools::getName("") << std::endl;
122 std::cout << "Get parent with an empty pathname=" << vpIoTools::getParent("") << std::endl;
123 std::cout << "Get parent with a filename=" << vpIoTools::getParent("my_file.txt") << std::endl;
124 expandPath = "~/Documents/fictional dir/fictional file.txt";
125 std::cout << "Get name with a unix expand pathname " << expandPath << "=" << vpIoTools::getName(expandPath)
126 << std::endl;
127 std::cout << "Get parent with a unix expand pathname " << expandPath << "=" << vpIoTools::getParent(expandPath)
128 << std::endl;
129
130 pathname = "c:/dir";
131 std::cout << "pathname=" << vpIoTools::splitDrive(pathname).first << " ; " << vpIoTools::splitDrive(pathname).second
132 << std::endl;
133
134 std::cout << "isAbsolutePath of " << pathname << "=" << vpIoTools::isAbsolutePathname(pathname) << std::endl;
135
136 pathname = "c:/dir/fictional directory/fictional file.txt";
137 std::cout << "isAbsolutePath of " << pathname << "=" << vpIoTools::isAbsolutePathname(pathname) << std::endl;
138
139 pathname = "/home/user/Documents/fictional directory/fictional file.txt";
140 std::cout << "isAbsolutePath of " << pathname << "=" << vpIoTools::isAbsolutePathname(pathname) << std::endl;
141
142 pathname = "~/Documents/fictional directory/fictional file.txt";
143 std::cout << "isAbsolutePath of " << pathname << "=" << vpIoTools::isAbsolutePathname(pathname) << std::endl;
144
145 pathname = "fictional directory/fictional file.txt";
146 std::cout << "isAbsolutePath of " << pathname << "=" << vpIoTools::isAbsolutePathname(pathname) << std::endl;
147
148 // Test vpIoTools::splitDrive
149 unsigned int nbFail = 0, nbOk = 0;
150#if defined(_WIN32)
151 if (strcmp(vpIoTools::splitDrive("c:\\foo\\bar").first.c_str(), "c:") == 0) {
152 nbOk++;
153 } else {
154 nbFail++;
155 std::cout << "Fail=" << vpIoTools::splitDrive("c:\\foo\\bar").first << " should be=c:" << std::endl;
156 }
157 if (strcmp(vpIoTools::splitDrive("c:\\foo\\bar").second.c_str(), "\\foo\\bar") == 0) {
158 nbOk++;
159 } else {
160 nbFail++;
161 std::cout << "Fail=" << vpIoTools::splitDrive("c:\\foo\\bar").second << " should be=\\foo\\bar" << std::endl;
162 }
163
164 if (strcmp(vpIoTools::splitDrive("c:/foo/bar").first.c_str(), "c:") == 0) {
165 nbOk++;
166 } else {
167 nbFail++;
168 std::cout << "Fail=" << vpIoTools::splitDrive("c:/foo/bar").first << " should be=c:" << std::endl;
169 }
170 if (strcmp(vpIoTools::splitDrive("c:/foo/bar").second.c_str(), "/foo/bar") == 0) {
171 nbOk++;
172 } else {
173 nbFail++;
174 std::cout << "Fail=" << vpIoTools::splitDrive("c:/foo/bar").second << " should be=/foo/bar" << std::endl;
175 }
176
177 if (strcmp(vpIoTools::splitDrive("\\\\conky\\mountpoint\\foo\\bar").first.c_str(), "\\\\conky\\mountpoint") == 0) {
178 nbOk++;
179 } else {
180 nbFail++;
181 std::cout << "Fail=" << vpIoTools::splitDrive("\\\\conky\\mountpoint\\foo\\bar").first
182 << " should be=\\\\conky\\mountpoint" << std::endl;
183 }
184 if (strcmp(vpIoTools::splitDrive("\\\\conky\\mountpoint\\foo\\bar").second.c_str(), "\\foo\\bar") == 0) {
185 nbOk++;
186 } else {
187 nbFail++;
188 std::cout << "Fail=" << vpIoTools::splitDrive("\\\\conky\\mountpoint\\foo\\bar").second << " should be=\\foo\\bar"
189 << std::endl;
190 }
191
192 if (strcmp(vpIoTools::splitDrive("//conky/mountpoint/foo/bar").first.c_str(), "//conky/mountpoint") == 0) {
193 nbOk++;
194 } else {
195 nbFail++;
196 std::cout << "Fail=" << vpIoTools::splitDrive("//conky/mountpoint/foo/bar").first << " should be=//conky/mountpoint"
197 << std::endl;
198 }
199 if (strcmp(vpIoTools::splitDrive("//conky/mountpoint/foo/bar").second.c_str(), "/foo/bar") == 0) {
200 nbOk++;
201 } else {
202 nbFail++;
203 std::cout << "Fail=" << vpIoTools::splitDrive("//conky/mountpoint/foo/bar").second << " should be=/foo/bar"
204 << std::endl;
205 }
206
207 if (strcmp(vpIoTools::splitDrive("\\\\\\conky\\mountpoint\\foo\\bar").first.c_str(), "") == 0) {
208 nbOk++;
209 } else {
210 nbFail++;
211 std::cout << "Fail=" << vpIoTools::splitDrive("\\\\\\conky\\mountpoint\\foo\\bar").first
212 << " should be=" << std::endl;
213 }
214 if (strcmp(vpIoTools::splitDrive("\\\\\\conky\\mountpoint\\foo\\bar").second.c_str(),
215 "\\\\\\conky\\mountpoint\\foo\\bar") == 0) {
216 nbOk++;
217 } else {
218 nbFail++;
219 std::cout << "Fail=" << vpIoTools::splitDrive("\\\\\\conky\\mountpoint\\foo\\bar").second
220 << " should be=\\\\\\conky\\mountpoint\\foo\\bar" << std::endl;
221 }
222
223 if (strcmp(vpIoTools::splitDrive("///conky/mountpoint/foo/bar").first.c_str(), "") == 0) {
224 nbOk++;
225 } else {
226 nbFail++;
227 std::cout << "Fail=" << vpIoTools::splitDrive("///conky/mountpoint/foo/bar").first << " should be=" << std::endl;
228 }
229 if (strcmp(vpIoTools::splitDrive("///conky/mountpoint/foo/bar").second.c_str(), "///conky/mountpoint/foo/bar") == 0) {
230 nbOk++;
231 } else {
232 nbFail++;
233 std::cout << "Fail=" << vpIoTools::splitDrive("///conky/mountpoint/foo/bar").second
234 << " should be=///conky/mountpoint/foo/bar" << std::endl;
235 }
236
237 if (strcmp(vpIoTools::splitDrive("\\\\conky\\\\mountpoint\\foo\\bar").first.c_str(), "") == 0) {
238 nbOk++;
239 } else {
240 nbFail++;
241 std::cout << "Fail=" << vpIoTools::splitDrive("\\\\conky\\\\mountpoint\\foo\\bar").first
242 << " should be=" << std::endl;
243 }
244 if (strcmp(vpIoTools::splitDrive("\\\\conky\\\\mountpoint\\foo\\bar").second.c_str(),
245 "\\\\conky\\\\mountpoint\\foo\\bar") == 0) {
246 nbOk++;
247 } else {
248 nbFail++;
249 std::cout << "Fail=" << vpIoTools::splitDrive("\\\\conky\\\\mountpoint\\foo\\bar").second
250 << " should be=\\\\conky\\\\mountpoint\\foo\\bar" << std::endl;
251 }
252
253 if (strcmp(vpIoTools::splitDrive("//conky//mountpoint/foo/bar").first.c_str(), "") == 0) {
254 nbOk++;
255 } else {
256 nbFail++;
257 std::cout << "Fail=" << vpIoTools::splitDrive("//conky//mountpoint/foo/bar").first << " should be=" << std::endl;
258 }
259 if (strcmp(vpIoTools::splitDrive("//conky//mountpoint/foo/bar").second.c_str(), "//conky//mountpoint/foo/bar") == 0) {
260 nbOk++;
261 } else {
262 nbFail++;
263 std::cout << "Fail=" << vpIoTools::splitDrive("//conky//mountpoint/foo/bar").second
264 << " should be=//conky//mountpoint/foo/bar" << std::endl;
265 }
266
267 std::cout << "Test vpIoTools::splitDrive (Win32) - passed: " << nbOk << "/" << (nbOk + nbFail) << std::endl;
268
269 if (nbFail) {
270 std::cerr << "Failed test: vpIoTools::splitDrive (Win32)" << std::endl;
271 return EXIT_FAILURE;
272 }
273#endif
274
275// Test vpIoTools::getFileExtension
276#if defined(_WIN32)
277 nbFail = 0;
278 nbOk = 0;
279
280 if (strcmp(vpIoTools::getFileExtension("foo.ext").c_str(), ".ext") == 0) {
281 nbOk++;
282 } else {
283 nbFail++;
284 std::cout << "Fail=" << vpIoTools::getFileExtension("foo.ext") << " should be=.ext" << std::endl;
285 }
286
287 if (strcmp(vpIoTools::getFileExtension("/foo/foo.ext").c_str(), ".ext") == 0) {
288 nbOk++;
289 } else {
290 nbFail++;
291 std::cout << "Fail=" << vpIoTools::getFileExtension("/foo/foo.ext") << " should be=.ext" << std::endl;
292 }
293
294 if (strcmp(vpIoTools::getFileExtension(".ext").c_str(), "") == 0) {
295 nbOk++;
296 } else {
297 nbFail++;
298 std::cout << "Fail=" << vpIoTools::getFileExtension(".ext") << " should be=" << std::endl;
299 }
300
301 if (strcmp(vpIoTools::getFileExtension("\\foo.ext\\foo").c_str(), "") == 0) {
302 nbOk++;
303 } else {
304 nbFail++;
305 std::cout << "Fail=" << vpIoTools::getFileExtension("\\foo.ext\\foo") << " should be=" << std::endl;
306 }
307
308 if (strcmp(vpIoTools::getFileExtension("foo.ext\\").c_str(), "") == 0) {
309 nbOk++;
310 } else {
311 nbFail++;
312 std::cout << "Fail=" << vpIoTools::getFileExtension("foo.ext\\") << " should be=" << std::endl;
313 }
314
315 if (strcmp(vpIoTools::getFileExtension("").c_str(), "") == 0) {
316 nbOk++;
317 } else {
318 nbFail++;
319 std::cout << "Fail=" << vpIoTools::getFileExtension("") << " should be=" << std::endl;
320 }
321
322 if (strcmp(vpIoTools::getFileExtension("foo.bar.ext").c_str(), ".ext") == 0) {
323 nbOk++;
324 } else {
325 nbFail++;
326 std::cout << "Fail=" << vpIoTools::getFileExtension("foo.bar.ext") << " should be=.ext" << std::endl;
327 }
328
329 if (strcmp(vpIoTools::getFileExtension("xx/foo.bar.ext").c_str(), ".ext") == 0) {
330 nbOk++;
331 } else {
332 nbFail++;
333 std::cout << "Fail=" << vpIoTools::getFileExtension("xx/foo.bar.ext") << " should be=.ext" << std::endl;
334 }
335
336 if (strcmp(vpIoTools::getFileExtension("xx\\foo.bar.ext").c_str(), ".ext") == 0) {
337 nbOk++;
338 } else {
339 nbFail++;
340 std::cout << "Fail=" << vpIoTools::getFileExtension("xx\\foo.bar.ext") << " should be=.ext" << std::endl;
341 }
342
343 if (strcmp(vpIoTools::getFileExtension("c:a/b\\c.d").c_str(), ".d") == 0) {
344 nbOk++;
345 } else {
346 nbFail++;
347 std::cout << "Fail=" << vpIoTools::getFileExtension("c:a/b\\c.d") << " should be=.d" << std::endl;
348 }
349
350 std::cout << "Test vpIoTools::getFileExtension (WIN32 platform) - passed: " << nbOk << "/" << (nbOk + nbFail)
351 << std::endl;
352
353 if (nbFail) {
354 std::cerr << "Failed test: vpIoTools::getFileExtension (WIN32 platform)" << std::endl;
355 return EXIT_FAILURE;
356 }
357#else
358 nbFail = 0;
359 nbOk = 0;
360
361 if (strcmp(vpIoTools::getFileExtension("foo.bar").c_str(), ".bar") == 0) {
362 nbOk++;
363 } else {
364 nbFail++;
365 std::cout << "Fail=" << vpIoTools::getFileExtension("foo.bar") << " should be=.bar" << std::endl;
366 }
367
368 if (strcmp(vpIoTools::getFileExtension("foo.boo.bar").c_str(), ".bar") == 0) {
369 nbOk++;
370 } else {
371 nbFail++;
372 std::cout << "Fail=" << vpIoTools::getFileExtension("foo.boo.bar") << " should be=.bar" << std::endl;
373 }
374
375 if (strcmp(vpIoTools::getFileExtension("foo.boo.biff.bar").c_str(), ".bar") == 0) {
376 nbOk++;
377 } else {
378 nbFail++;
379 std::cout << "Fail=" << vpIoTools::getFileExtension("foo.boo.biff.bar") << " should be=.bar" << std::endl;
380 }
381
382 if (strcmp(vpIoTools::getFileExtension(".csh.rc").c_str(), ".rc") == 0) {
383 nbOk++;
384 } else {
385 nbFail++;
386 std::cout << "Fail=" << vpIoTools::getFileExtension(".csh.rc") << " should be=.rc" << std::endl;
387 }
388
389 if (strcmp(vpIoTools::getFileExtension("nodots").c_str(), "") == 0) {
390 nbOk++;
391 } else {
392 nbFail++;
393 std::cout << "Fail=" << vpIoTools::getFileExtension("nodots") << " should be=" << std::endl;
394 }
395
396 if (strcmp(vpIoTools::getFileExtension(".cshrc").c_str(), "") == 0) {
397 nbOk++;
398 } else {
399 nbFail++;
400 std::cout << "Fail=" << vpIoTools::getFileExtension(".cshrc") << " should be=" << std::endl;
401 }
402
403 if (strcmp(vpIoTools::getFileExtension("...manydots").c_str(), "") == 0) {
404 nbOk++;
405 } else {
406 nbFail++;
407 std::cout << "Fail=" << vpIoTools::getFileExtension("...manydots") << " should be=" << std::endl;
408 }
409
410 if (strcmp(vpIoTools::getFileExtension("...manydots.ext").c_str(), ".ext") == 0) {
411 nbOk++;
412 } else {
413 nbFail++;
414 std::cout << "Fail=" << vpIoTools::getFileExtension("...manydots.ext") << " should be=.ext" << std::endl;
415 }
416
417 if (strcmp(vpIoTools::getFileExtension(".").c_str(), "") == 0) {
418 nbOk++;
419 } else {
420 nbFail++;
421 std::cout << "Fail=" << vpIoTools::getFileExtension(".") << " should be=" << std::endl;
422 }
423
424 if (strcmp(vpIoTools::getFileExtension("..").c_str(), "") == 0) {
425 nbOk++;
426 } else {
427 nbFail++;
428 std::cout << "Fail=" << vpIoTools::getFileExtension("..") << " should be=" << std::endl;
429 }
430
431 if (strcmp(vpIoTools::getFileExtension("........").c_str(), "") == 0) {
432 nbOk++;
433 } else {
434 nbFail++;
435 std::cout << "Fail=" << vpIoTools::getFileExtension("........") << " should be=" << std::endl;
436 }
437
438 if (strcmp(vpIoTools::getFileExtension("").c_str(), "") == 0) {
439 nbOk++;
440 } else {
441 nbFail++;
442 std::cout << "Fail=" << vpIoTools::getFileExtension("") << " should be=" << std::endl;
443 }
444
445 std::cout << "Test vpIoTools::getFileExtension (Unix-like platform) - passed: " << nbOk << "/" << (nbOk + nbFail)
446 << std::endl;
447#endif
448
449 // Test makeDirectory()
450 try {
451 std::string username, directory_filename;
452 vpIoTools::getUserName(username);
453#if defined(_WIN32)
454 std::string tmp_dir = "C:/temp/" + username;
455#elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
456 std::string tmp_dir = "/tmp/" + username;
457#endif
458#if defined(_WIN32)
459 directory_filename = tmp_dir + "/test_directory1/test directory 2/";
460#elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
461 directory_filename = tmp_dir + "/test_directory1/test directory 2/";
462#endif
463 vpIoTools::makeDirectory(directory_filename);
464 vpIoTools::makeDirectory(directory_filename);
465 std::cout << "Create directories: " << directory_filename << std::endl;
466
467 if (! vpIoTools::checkDirectory(directory_filename)) {
468 std::cerr << "Error: " << directory_filename << " is not a directory" << std::endl;
469 return EXIT_FAILURE;
470 }
471
472#if defined(_WIN32)
473 directory_filename = tmp_dir + "/test_directory1/test directory 3";
474#elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
475 directory_filename = tmp_dir + "/test_directory1/test directory 3";
476#endif
477 vpIoTools::makeDirectory(directory_filename);
478 std::cout << "Create directories: " << directory_filename << std::endl;
479
480 if (! vpIoTools::checkDirectory(directory_filename)) {
481 std::cerr << "Error: " << directory_filename << " is not a directory" << std::endl;
482 return EXIT_FAILURE;
483 }
484
485#if defined(_WIN32)
486 directory_filename = "C:\\temp/" + username + "\\test_directory1\\test directory 4";
487#elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
488 directory_filename = "/tmp\\" + username + "\\test_directory1\\test directory 4";
489#endif
490 vpIoTools::makeDirectory(directory_filename);
491 vpIoTools::makeDirectory(directory_filename);
492 std::cout << "Create directories: " << directory_filename << std::endl;
493
494 if (! vpIoTools::checkDirectory(directory_filename)) {
495 std::cerr << "Error: " << directory_filename << " is not a directory" << std::endl;
496 return EXIT_FAILURE;
497 }
498
499#if defined(_WIN32)
500 directory_filename = "C:\\temp/" + username + "\\test_directory1\\test directory 5 . dir/test directory 6";
501#elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
502 directory_filename = "/tmp\\" + username + "\\test_directory1\\test directory 5 . dir/test directory 6";
503#endif
504 vpIoTools::makeDirectory(directory_filename);
505 std::cout << "Create directories: " << directory_filename << std::endl;
506
507 if (! vpIoTools::checkDirectory(directory_filename)) {
508 std::cerr << "Error: " << directory_filename << " is not a directory" << std::endl;
509 return EXIT_FAILURE;
510 }
511
512 //Delete test directory
513 if (!vpIoTools::remove(tmp_dir + "/test_directory1")) {
514 std::cerr << "Error: cannot remove directory: " << tmp_dir << "/test_directory1" << std::endl;
515 return EXIT_FAILURE;
516 }
517 } catch (const vpException &e) {
518 std::cerr << "Exception: " << e.what() << std::endl;
519 return EXIT_FAILURE;
520 }
521
522 // Test FIFO only implemented on unix like OS
523#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
524 try {
525 std::string username, fifo_file;
526 vpIoTools::getUserName(username);
527 std::string fifo_tmp_dir = "/tmp/" + username + "/fifo_test_directory";
528
529 if (!vpIoTools::checkDirectory(fifo_tmp_dir)) {
530 vpIoTools::makeDirectory(fifo_tmp_dir);
531 }
532
533 // Test 1
534 fifo_file = fifo_tmp_dir + "/" + "fifo_testfile";
535
536 vpIoTools::makeFifo(fifo_file);
537 std::cout << "Create fifo file: " << fifo_file << std::endl;
538
539 if (! vpIoTools::checkFifo(fifo_file)) {
540 std::cerr << "Error: file " << fifo_file << " is not a fifo file as expected" << std::endl;
541 return EXIT_FAILURE;
542 }
543
544 // Delete test file and directory
545 if (!vpIoTools::remove(fifo_file)) {
546 std::cerr << "Error: cannot remove fifo: " << fifo_file << std::endl;
547 return EXIT_FAILURE;
548 }
549 if (!vpIoTools::remove(fifo_tmp_dir)) {
550 std::cerr << "Error: cannot remove directory: " << fifo_tmp_dir << std::endl;
551 return EXIT_FAILURE;
552 }
553
554 } catch (const vpException &e) {
555 std::cerr << "Catch an exception: " << e.what() << std::endl;
556 return EXIT_FAILURE;
557 }
558#endif
559
560 // Test makeTempDirectory()
561#if defined(_WIN32) || (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX or Windows
562 try {
563 std::string directory_filename_tmp;
564 std::string tmp_dir = vpIoTools::getTempPath();
565
566 // Test 1
567 directory_filename_tmp = tmp_dir + "/" + "vpIoTools_test_XXXXXX";
568
569 std::string converted_dirname_tmp = vpIoTools::makeTempDirectory(directory_filename_tmp);
570
571 std::cout << "Create temp directory: " << converted_dirname_tmp << std::endl;
572
573 if (! vpIoTools::checkDirectory(converted_dirname_tmp)) {
574 std::cerr << "Error: " << converted_dirname_tmp << " is not a tmp directory" << std::endl;
575 return EXIT_FAILURE;
576 }
577
578 // Delete test directory
579 if (!vpIoTools::remove(converted_dirname_tmp)) {
580 std::cerr << "Error: cannot remove temp directory: " << converted_dirname_tmp << std::endl;
581 return EXIT_FAILURE;
582 }
583
584 // Test 2
586 converted_dirname_tmp = vpIoTools::makeTempDirectory(tmp_dir);
587
588 std::cout << "Create temp directory: " << converted_dirname_tmp << std::endl;
589
590 if (! vpIoTools::checkDirectory(converted_dirname_tmp)) {
591 std::cerr << "Error: " << converted_dirname_tmp << " is not a temp directory" << std::endl;
592 return EXIT_FAILURE;
593 }
594
595 // Delete test directory
596 if (!vpIoTools::remove(converted_dirname_tmp)) {
597 std::cerr << "Cannot remove directory: " << converted_dirname_tmp << std::endl;
598 return EXIT_FAILURE;
599 }
600
601 } catch (const vpException &e) {
602 std::cerr << "Catch an exception: " << e.what() << std::endl;
603 return EXIT_FAILURE;
604 }
605#endif
606
607 // Get the user login name
608 std::string username = vpIoTools::getUserName();
609 std::ofstream dummy_file;
610
611// Test isSamePathname()
612#if defined(_WIN32)
613 std::string path1 = "tmp/test/file.txt";
614 std::string path2 = "tmp/test/../test/file.txt";
615
616 nbOk = 0;
617 nbFail = 0;
618 bool res;
619
620 res = vpIoTools::isSamePathname(path1, path2); // True
621 std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
622 nbOk = res ? nbOk + 1 : nbOk;
623 nbFail = res ? nbFail : nbFail + 1;
624
625 path1 = ".\\tmp/test/file.txt";
626 res = vpIoTools::isSamePathname(path1, path2); // True
627 std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
628 nbOk = res ? nbOk + 1 : nbOk;
629 nbFail = res ? nbFail : nbFail + 1;
630
631 path1 = ".\\tmp/test\\../fake dir/..\\test\\file.txt";
632 res = vpIoTools::isSamePathname(path1, path2); // True
633 std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
634 nbOk = res ? nbOk + 1 : nbOk;
635 nbFail = res ? nbFail : nbFail + 1;
636
637 path2 = "/tmp/test/../test/file.txt";
638 res = vpIoTools::isSamePathname(path1, path2); // False
639 std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
640 nbOk = res ? nbOk : nbOk + 1;
641 nbFail = res ? nbFail + 1 : nbFail;
642
643 std::cout << "Test vpIoTools::isSamePathname (WIN32 platform) - passed: " << nbOk << "/" << (nbOk + nbFail)
644 << std::endl;
645 if (nbFail) {
646 std::cerr << "Failed test: vpIoTools::isSamePathname (WIN32 platform)" << std::endl;
647 return EXIT_FAILURE;
648 }
649#else
650 // realpath requires not fake path, so we create dummy file and directories
651
652 vpIoTools::makeDirectory("/tmp/" + username + "/test");
653 vpIoTools::makeDirectory("/tmp/" + username + "/dummy dir");
654
655 std::string path1 = "/tmp/" + username + "/test/file.txt";
656 std::string path2 = "/tmp/" + username + "/test/../test/file.txt";
657 dummy_file.open(path1.c_str());
658 if (!dummy_file.is_open()) {
659 return EXIT_SUCCESS;
660 }
661 dummy_file.close();
662
663 nbOk = 0;
664 nbFail = 0;
665 bool res;
666
667 res = vpIoTools::isSamePathname(path1, path2); // True
668 std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
669 nbOk = res ? nbOk + 1 : nbOk;
670 nbFail = res ? nbFail : nbFail + 1;
671
672 path1 = "\\tmp/" + username + "/./test/file.txt";
673 res = vpIoTools::isSamePathname(path1, path2); // True
674 std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
675 nbOk = res ? nbOk + 1 : nbOk;
676 nbFail = res ? nbFail : nbFail + 1;
677
678 path1 = "\\tmp/" + username + "/test\\../dummy dir/..\\test\\file.txt";
679 res = vpIoTools::isSamePathname(path1, path2); // True
680 std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
681 nbOk = res ? nbOk + 1 : nbOk;
682 nbFail = res ? nbFail : nbFail + 1;
683
684 path2 = "/tmp/" + username + "/test/../test";
685 res = vpIoTools::isSamePathname(path1, path2); // False
686 std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
687 nbOk = res ? nbOk : nbOk + 1;
688 nbFail = res ? nbFail + 1 : nbFail;
689
690 path1 = "/tmp/" + username + "/test/";
691 res = vpIoTools::isSamePathname(path1, path2); // True
692 std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
693 nbOk = res ? nbOk + 1 : nbOk;
694 nbFail = res ? nbFail : nbFail + 1;
695
696 std::cout << "Test vpIoTools::isSamePathname (Unix platform) - passed: " << nbOk << "/" << (nbOk + nbFail)
697 << std::endl;
698
699 //Delete test directory
700 if (!vpIoTools::remove("/tmp/" + username + "/test")) {
701 std::cerr << "Cannot remove directory: " << "/tmp/" << username << "/test" << std::endl;
702 }
703 if (!vpIoTools::remove("/tmp/" + username + "/dummy dir")) {
704 std::cerr << "Cannot remove directory: " << "/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: " << "/tmp/" << username << "/directory (1) with ' quote and spaces" << std::endl;
747 }
748
749 // Test endianness
750 {
751 std::string filename_endianness = vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(), "endianness/test_endianness_little_endian.bin");
752 std::ifstream file_endianness(filename_endianness.c_str(), std::ios::in | std::ios::binary);
753 if (file_endianness.is_open()) {
754 checkReadBinaryValue<short>(file_endianness, std::numeric_limits<short>::min());
755 checkReadBinaryValue<short>(file_endianness, std::numeric_limits<short>::max());
756
757 checkReadBinaryValue<unsigned short>(file_endianness, std::numeric_limits<unsigned short>::min());
758 checkReadBinaryValue<unsigned short>(file_endianness, std::numeric_limits<unsigned short>::max());
759
760 checkReadBinaryValue<int>(file_endianness, std::numeric_limits<int>::min());
761 checkReadBinaryValue<int>(file_endianness, std::numeric_limits<int>::max());
762
763 checkReadBinaryValue<unsigned int>(file_endianness, std::numeric_limits<unsigned int>::min());
764 checkReadBinaryValue<unsigned int>(file_endianness, std::numeric_limits<unsigned int>::max());
765
766 checkReadBinaryValue<float>(file_endianness, -std::numeric_limits<float>::max());
767 checkReadBinaryValue<float>(file_endianness, std::numeric_limits<float>::max());
768
769 checkReadBinaryValue<double>(file_endianness, -std::numeric_limits<double>::max());
770 checkReadBinaryValue<double>(file_endianness, std::numeric_limits<double>::max());
771
772 std::cout << "Test endianness is ok." << std::endl;
773 } else {
774 std::cout << "Cannot open file: " << filename_endianness << std::endl;
775 }
776 }
777
778 std::cout << std::endl << "Test succeed" << std::endl;
779 return EXIT_SUCCESS;
780}
error that can be emited by ViSP classes.
Definition: vpException.h:72
@ badValue
Used to indicate that a value is not in the allowed range.
Definition: vpException.h:97
const char * what() const
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1365
static std::string path(const std::string &pathname)
Definition: vpIoTools.cpp:1005
static bool checkFilename(const std::string &filename)
Definition: vpIoTools.cpp:802
static std::pair< std::string, std::string > splitDrive(const std::string &pathname)
Definition: vpIoTools.cpp:1755
static bool isSamePathname(const std::string &pathname1, const std::string &pathname2)
Definition: vpIoTools.cpp:1735
static std::string getTempPath()
Definition: vpIoTools.cpp:194
static bool isAbsolutePathname(const std::string &pathname)
Definition: vpIoTools.cpp:1711
static void readBinaryValueLE(std::ifstream &file, int16_t &short_value)
Definition: vpIoTools.cpp:1993
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:420
static long getIndex(const std::string &filename, const std::string &format)
Definition: vpIoTools.cpp:1573
static std::string getUserName()
Definition: vpIoTools.cpp:316
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1670
static std::string getFileExtension(const std::string &pathname, bool checkFile=false)
Definition: vpIoTools.cpp:1430
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:570
static bool remove(const std::string &filename)
Definition: vpIoTools.cpp:929
static std::string makeTempDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:728
static bool checkFifo(const std::string &filename)
Definition: vpIoTools.cpp:480
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1606
static std::string getName(const std::string &pathname)
Definition: vpIoTools.cpp:1510
static const char separator
Definition: vpIoTools.h:186
static void makeFifo(const std::string &dirname)
Definition: vpIoTools.cpp:621
static bool equal(double x, double y, double s=0.001)
Definition: vpMath.h:295