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