Test functions in IoTools.
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <visp3/core/vpIoTools.h>
int
main(int argc, const char ** argv)
{
if(c == '\\')
{
std::cout << "The directory separator character is '" << c << "' (Windows platform)." << std::endl;
}
else {
std::cout << "The directory separator character is '" << c << "' (Unix like platform)." << std::endl;
}
#if defined(_WIN32)
std::string pathname = "C:\\Program Files (x86)\\Java\\jre7";
#else
std::string pathname = "/usr/bin/java";
#endif
std::cout <<
"Name of " << pathname <<
" is " <<
vpIoTools::getName(pathname) << std::endl;
if(argc == 3 && std::string(argv[1]) == std::string("-i"))
{
}
std::string windowsPathnameStyle = "\\usr\\bin\\java";
std::cout <<
"Parent of " << windowsPathnameStyle <<
" is " <<
vpIoTools::getParent(windowsPathnameStyle) << std::endl;
std::cout <<
"Name of " << windowsPathnameStyle <<
" is " <<
vpIoTools::getName(windowsPathnameStyle) << std::endl;
std::string parent = "/usr/toto/", child = "\\blabla\\java";
std::cout << "Create file path from parent=" << parent << " and child=" << child << " is "
std::string expandPath = "~/Documents/fictional directory/fictional file";
std::cout <<
"Path for " << expandPath <<
" is " <<
vpIoTools::path(expandPath) << std::endl;
std::cout <<
"Test get name with an empty pathname=" <<
vpIoTools::getName(
"") << std::endl;
expandPath = "~/Documents/fictional dir/fictional file.txt";
std::cout <<
"Get name with a unix expand pathname " << expandPath <<
"=" <<
vpIoTools::getName(expandPath) << std::endl;
std::cout <<
"Get parent with a unix expand pathname " << expandPath <<
"=" <<
vpIoTools::getParent(expandPath) << std::endl;
pathname = "c:/dir";
pathname = "c:/dir/fictional directory/fictional file.txt";
pathname = "/home/user/Documents/fictional directory/fictional file.txt";
pathname = "~/Documents/fictional directory/fictional file.txt";
pathname = "fictional directory/fictional file.txt";
unsigned int nbFail = 0, nbOk = 0;
#if defined(_WIN32)
nbOk++;
}
else {
nbFail++;
}
nbOk++;
}
else {
nbFail++;
std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"c:\\foo\\bar").second <<
" should be=\\foo\\bar" << std::endl;
}
nbOk++;
}
else {
nbFail++;
}
nbOk++;
}
else {
nbFail++;
std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"c:/foo/bar").second <<
" should be=/foo/bar" << std::endl;
}
if(strcmp(
vpIoTools::splitDrive(
"\\\\conky\\mountpoint\\foo\\bar").first.c_str(),
"\\\\conky\\mountpoint") == 0) {
nbOk++;
}
else {
nbFail++;
std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"\\\\conky\\mountpoint\\foo\\bar").first <<
" should be=\\\\conky\\mountpoint" << std::endl;
}
nbOk++;
}
else {
nbFail++;
std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"\\\\conky\\mountpoint\\foo\\bar").second <<
" should be=\\foo\\bar" << std::endl;
}
if(strcmp(
vpIoTools::splitDrive(
"//conky/mountpoint/foo/bar").first.c_str(),
"//conky/mountpoint") == 0) {
nbOk++;
}
else {
nbFail++;
std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"//conky/mountpoint/foo/bar").first <<
" should be=//conky/mountpoint" << std::endl;
}
nbOk++;
}
else {
nbFail++;
std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"//conky/mountpoint/foo/bar").second <<
" should be=/foo/bar" << std::endl;
}
nbOk++;
}
else {
nbFail++;
std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"\\\\\\conky\\mountpoint\\foo\\bar").first <<
" should be=" << std::endl;
}
"\\\\\\conky\\mountpoint\\foo\\bar") == 0) {
nbOk++;
}
else {
nbFail++;
std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"\\\\\\conky\\mountpoint\\foo\\bar").second <<
" should be=\\\\\\conky\\mountpoint\\foo\\bar" << std::endl;
}
nbOk++;
}
else {
nbFail++;
std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"///conky/mountpoint/foo/bar").first <<
" should be=" << std::endl;
}
if(strcmp(
vpIoTools::splitDrive(
"///conky/mountpoint/foo/bar").second.c_str(),
"///conky/mountpoint/foo/bar") == 0) {
nbOk++;
}
else {
nbFail++;
std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"///conky/mountpoint/foo/bar").second <<
" should be=///conky/mountpoint/foo/bar" << std::endl;
}
nbOk++;
}
else {
nbFail++;
std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"\\\\conky\\\\mountpoint\\foo\\bar").first <<
" should be=" << std::endl;
}
"\\\\conky\\\\mountpoint\\foo\\bar") == 0) {
nbOk++;
}
else {
nbFail++;
std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"\\\\conky\\\\mountpoint\\foo\\bar").second <<
" should be=\\\\conky\\\\mountpoint\\foo\\bar" << std::endl;
}
nbOk++;
}
else {
nbFail++;
std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"//conky//mountpoint/foo/bar").first <<
" should be=" << std::endl;
}
if(strcmp(
vpIoTools::splitDrive(
"//conky//mountpoint/foo/bar").second.c_str(),
"//conky//mountpoint/foo/bar") == 0) {
nbOk++;
}
else {
nbFail++;
std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"//conky//mountpoint/foo/bar").second <<
" should be=//conky//mountpoint/foo/bar" << std::endl;
}
std::cout << "Test vpIoTools::splitDrive (Win32) - passed: " << nbOk << "/" << (nbOk+nbFail) << std::endl;
if (nbFail > 0) {
std::cerr << "Failed test: vpIoTools::splitDrive (Win32)" << std::endl;
return EXIT_FAILURE;
}
#endif
#if defined(_WIN32)
nbFail = 0;
nbOk = 0;
nbOk++;
}
else {
nbFail++;
}
nbOk++;
}
else {
nbFail++;
}
nbOk++;
}
else {
nbFail++;
}
nbOk++;
}
else {
nbFail++;
}
nbOk++;
}
else {
nbFail++;
}
nbOk++;
}
else {
nbFail++;
}
nbOk++;
}
else {
nbFail++;
}
nbOk++;
}
else {
nbFail++;
}
nbOk++;
}
else {
nbFail++;
}
nbOk++;
}
else {
nbFail++;
}
std::cout << "Test vpIoTools::getFileExtension (WIN32 platform) - passed: " << nbOk << "/" << (nbOk+nbFail) << std::endl;
if (nbFail > 0) {
std::cerr << "Failed test: vpIoTools::getFileExtension (WIN32 platform)" << std::endl;
return EXIT_FAILURE;
}
#else
nbFail = 0;
nbOk = 0;
nbOk++;
}
else {
nbFail++;
}
nbOk++;
}
else {
nbFail++;
}
nbOk++;
}
else {
nbFail++;
}
nbOk++;
}
else {
nbFail++;
}
nbOk++;
}
else {
nbFail++;
}
nbOk++;
}
else {
nbFail++;
}
nbOk++;
}
else {
nbFail++;
}
nbOk++;
}
else {
nbFail++;
}
nbOk++;
}
else {
nbFail++;
}
nbOk++;
}
else {
nbFail++;
}
nbOk++;
}
else {
nbFail++;
}
nbOk++;
}
else {
nbFail++;
}
std::cout << "Test vpIoTools::getFileExtension (Unix-like platform) - passed: " << nbOk << "/" << (nbOk+nbFail) << std::endl;
#endif
#if defined(_WIN32)
std::string path1 = "tmp/test/file.txt";
std::string path2 = "tmp/test/../test/file.txt";
nbOk = 0;
nbFail = 0;
bool res;
std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
nbOk = res ? nbOk+1 : nbOk;
nbFail = res ? nbFail : nbFail+1;
path1 = ".\\tmp/test/file.txt";
std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
nbOk = res ? nbOk+1 : nbOk;
nbFail = res ? nbFail : nbFail+1;
path1 = ".\\tmp/test\\../fake dir/..\\test\\file.txt";
std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
nbOk = res ? nbOk+1 : nbOk;
nbFail = res ? nbFail : nbFail+1;
path2 = "/tmp/test/../test/file.txt";
std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
nbOk = res ? nbOk : nbOk+1;
nbFail = res ? nbFail+1 : nbFail;
std::cout << "Test vpIoTools::isSamePathname (WIN32 platform) - passed: " << nbOk << "/" << (nbOk+nbFail) << std::endl;
if (nbFail > 0) {
std::cerr << "Failed test: vpIoTools::isSamePathname (WIN32 platform)" << std::endl;
return EXIT_FAILURE;
}
#else
std::string username = "";
std::string path1 = "/tmp/" + username + "/test/file.txt";
std::string path2 = "/tmp/" + username + "/test/../test/file.txt";
std::ofstream dummy_file(path1.c_str());
if (!dummy_file.is_open()) {
return EXIT_SUCCESS;
}
dummy_file.close();
nbOk = 0;
nbFail = 0;
bool res;
std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
nbOk = res ? nbOk+1 : nbOk;
nbFail = res ? nbFail : nbFail+1;
path1 = "\\tmp/" + username + "/./test/file.txt";
std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
nbOk = res ? nbOk+1 : nbOk;
nbFail = res ? nbFail : nbFail+1;
path1 = "\\tmp/" + username + "/test\\../dummy dir/..\\test\\file.txt";
std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
nbOk = res ? nbOk+1 : nbOk;
nbFail = res ? nbFail : nbFail+1;
path2 = "/tmp/" + username + "/test/../test";
std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
nbOk = res ? nbOk : nbOk+1;
nbFail = res ? nbFail+1 : nbFail;
path1 = "/tmp/" + username + "/test/";
std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
nbOk = res ? nbOk+1 : nbOk;
nbFail = res ? nbFail : nbFail+1;
std::cout << "Test vpIoTools::isSamePathname (Unix platform) - passed: " << nbOk << "/" << (nbOk+nbFail) << std::endl;
if (nbFail > 0) {
std::cerr << "Failed test: vpIoTools::isSamePathname (Unix platform)" << std::endl;
return EXIT_FAILURE;
}
#endif
std::cout << std::endl << "End" << std::endl;
return EXIT_SUCCESS;
}