Test functions in IoTools.
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <visp3/core/vpIoTools.h>
namespace
{
template <typename T>
void checkReadBinaryValue(std::ifstream &file, const T checkValue)
{
T value = (T)10;
if (value != checkValue) {
std::stringstream ss;
ss << "Read: " << value << " ; Expected: " << checkValue;
}
}
template <>
void checkReadBinaryValue<float>(std::ifstream &file, const float checkValue)
{
float value = 10.0f;
if (!
vpMath::equal(value, checkValue, std::numeric_limits<float>::epsilon())) {
std::stringstream ss;
ss << "Read: " << value << " ; Expected: " << checkValue;
}
}
template <>
void checkReadBinaryValue<double>(std::ifstream &file, const double checkValue)
{
double value = 10.0;
if (!
vpMath::equal(value, checkValue, std::numeric_limits<double>::epsilon())) {
std::stringstream ss;
ss << "Read: " << value << " ; Expected: " << checkValue;
}
}
}
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";
<< std::endl;
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++;
<< " should be=\\\\conky\\mountpoint" << std::endl;
}
if (strcmp(
vpIoTools::splitDrive(
"\\\\conky\\mountpoint\\foo\\bar").second.c_str(),
"\\foo\\bar") == 0) {
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++;
<< " should be=" << std::endl;
}
"\\\\\\conky\\mountpoint\\foo\\bar") == 0) {
nbOk++;
} else {
nbFail++;
<< " 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++;
<< " should be=///conky/mountpoint/foo/bar" << std::endl;
}
nbOk++;
} else {
nbFail++;
<< " should be=" << std::endl;
}
"\\\\conky\\\\mountpoint\\foo\\bar") == 0) {
nbOk++;
} else {
nbFail++;
<< " 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++;
<< " should be=//conky//mountpoint/foo/bar" << std::endl;
}
std::cout << "Test vpIoTools::splitDrive (Win32) - passed: " << nbOk << "/" << (nbOk + nbFail) << std::endl;
if (nbFail) {
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) {
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
try {
std::string username, directory_filename;
#if defined(_WIN32)
std::string tmp_dir = "C:/temp/" + username;
#elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
std::string tmp_dir = "/tmp/" + username;
#endif
#if defined(_WIN32)
directory_filename = tmp_dir + "/test_directory1/test directory 2/";
#elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
directory_filename = tmp_dir + "/test_directory1/test directory 2/";
#endif
std::cout << "Create directories: " << directory_filename << std::endl;
std::cerr << "Error: " << directory_filename << " is not a directory" << std::endl;
return EXIT_FAILURE;
}
#if defined(_WIN32)
directory_filename = tmp_dir + "/test_directory1/test directory 3";
#elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
directory_filename = tmp_dir + "/test_directory1/test directory 3";
#endif
std::cout << "Create directories: " << directory_filename << std::endl;
std::cerr << "Error: " << directory_filename << " is not a directory" << std::endl;
return EXIT_FAILURE;
}
#if defined(_WIN32)
directory_filename = "C:\\temp/" + username + "\\test_directory1\\test directory 4";
#elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
directory_filename = "/tmp\\" + username + "\\test_directory1\\test directory 4";
#endif
std::cout << "Create directories: " << directory_filename << std::endl;
std::cerr << "Error: " << directory_filename << " is not a directory" << std::endl;
return EXIT_FAILURE;
}
#if defined(_WIN32)
directory_filename = "C:\\temp/" + username + "\\test_directory1\\test directory 5 . dir/test directory 6";
#elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
directory_filename = "/tmp\\" + username + "\\test_directory1\\test directory 5 . dir/test directory 6";
#endif
std::cout << "Create directories: " << directory_filename << std::endl;
std::cerr << "Error: " << directory_filename << " is not a directory" << std::endl;
return EXIT_FAILURE;
}
std::cerr << "Error: cannot remove directory: " << tmp_dir << "/test_directory1" << std::endl;
return EXIT_FAILURE;
}
std::cerr <<
"Exception: " << e.
what() << std::endl;
return EXIT_FAILURE;
}
#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
try {
std::string username, fifo_file;
std::string fifo_tmp_dir = "/tmp/" + username + "/fifo_test_directory";
}
fifo_file = fifo_tmp_dir + "/" + "fifo_testfile";
std::cout << "Create fifo file: " << fifo_file << std::endl;
std::cerr << "Error: file " << fifo_file << " is not a fifo file as expected" << std::endl;
return EXIT_FAILURE;
}
std::cerr << "Error: cannot remove fifo: " << fifo_file << std::endl;
return EXIT_FAILURE;
}
std::cerr << "Error: cannot remove directory: " << fifo_tmp_dir << std::endl;
return EXIT_FAILURE;
}
std::cerr <<
"Catch an exception: " << e.
what() << std::endl;
return EXIT_FAILURE;
}
#endif
#if defined(_WIN32) || (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX or Windows
try {
std::string directory_filename_tmp;
directory_filename_tmp = tmp_dir + "/" + "vpIoTools_test_XXXXXX";
std::cout << "Create temp directory: " << converted_dirname_tmp << std::endl;
std::cerr << "Error: " << converted_dirname_tmp << " is not a tmp directory" << std::endl;
return EXIT_FAILURE;
}
std::cerr << "Error: cannot remove temp directory: " << converted_dirname_tmp << std::endl;
return EXIT_FAILURE;
}
std::cout << "Create temp directory: " << converted_dirname_tmp << std::endl;
std::cerr << "Error: " << converted_dirname_tmp << " is not a temp directory" << std::endl;
return EXIT_FAILURE;
}
std::cerr << "Cannot remove directory: " << converted_dirname_tmp << std::endl;
return EXIT_FAILURE;
}
std::cerr <<
"Catch an exception: " << e.
what() << std::endl;
return EXIT_FAILURE;
}
#endif
std::ofstream dummy_file;
#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) {
std::cerr << "Failed test: vpIoTools::isSamePathname (WIN32 platform)" << std::endl;
return EXIT_FAILURE;
}
#else
std::string path1 = "/tmp/" + username + "/test/file.txt";
std::string path2 = "/tmp/" + username + "/test/../test/file.txt";
dummy_file.open(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;
std::cerr << "Cannot remove directory: " << "/tmp/" << username << "/test" << std::endl;
}
std::cerr << "Cannot remove directory: " << "/tmp/" << username << "/dummy dir" << std::endl;
}
if (nbFail) {
std::cerr << "Failed test: vpIoTools::isSamePathname (Unix platform)" << std::endl;
return EXIT_FAILURE;
}
#endif
std::cerr << "Failed test: vpIoTools::getIndex(\"file-1.txt\", \"file-%d.txt\")" << std::endl;
return EXIT_FAILURE;
}
std::cerr << "Failed test: vpIoTools::getIndex(\"/tmp/file0040.txt\", \"/tmp/file%04d.txt\")" << std::endl;
return EXIT_FAILURE;
}
std::cerr << "Failed test: vpIoTools::getIndex(\"file.txt\", \"file%d.txt\")" << std::endl;
return EXIT_FAILURE;
}
path1 = "/tmp/" + username +
"/directory (1) with ' quote and spaces/file with ' quote (1) and "
"spaces.txt";
dummy_file.open(path1.c_str());
if (!dummy_file.is_open()) {
return EXIT_SUCCESS;
}
dummy_file.close();
std::cerr << "Problem with checkFilename(" << path1 << ")!" << std::endl;
return EXIT_FAILURE;
}
std::cout << "Test vpIoTools::checkFilename() is ok." << std::endl;
if (!
vpIoTools::remove(
"/tmp/" + username +
"/directory (1) with ' quote and spaces")) {
std::cerr << "Cannot remove directory: " << "/tmp/" << username << "/directory (1) with ' quote and spaces" << std::endl;
}
{
std::ifstream file_endianness(filename_endianness.c_str(), std::ios::in | std::ios::binary);
if (file_endianness.is_open()) {
checkReadBinaryValue<short>(file_endianness, std::numeric_limits<short>::min());
checkReadBinaryValue<short>(file_endianness, std::numeric_limits<short>::max());
checkReadBinaryValue<unsigned short>(file_endianness, std::numeric_limits<unsigned short>::min());
checkReadBinaryValue<unsigned short>(file_endianness, std::numeric_limits<unsigned short>::max());
checkReadBinaryValue<int>(file_endianness, std::numeric_limits<int>::min());
checkReadBinaryValue<int>(file_endianness, std::numeric_limits<int>::max());
checkReadBinaryValue<unsigned int>(file_endianness, std::numeric_limits<unsigned int>::min());
checkReadBinaryValue<unsigned int>(file_endianness, std::numeric_limits<unsigned int>::max());
checkReadBinaryValue<float>(file_endianness, -std::numeric_limits<float>::max());
checkReadBinaryValue<float>(file_endianness, std::numeric_limits<float>::max());
checkReadBinaryValue<double>(file_endianness, -std::numeric_limits<double>::max());
checkReadBinaryValue<double>(file_endianness, std::numeric_limits<double>::max());
std::cout << "Test endianness is ok." << std::endl;
} else {
std::cout << "Cannot open file: " << filename_endianness << std::endl;
}
}
std::cout << std::endl << "Test succeed" << std::endl;
return EXIT_SUCCESS;
}