file_manip_tests.cpp

Go to the documentation of this file.
00001 
00011 #include <unistd.h>
00012 #include <stdlib.h>
00013 
00014 #include <string>
00015 #include <iostream>
00016 #include <list>
00017 
00018 #include "file_manip.h"
00019 
00020 using namespace std;
00021 
00022 template <typename Input, typename Output>
00023 struct input_output {
00024     Input input;
00025     Output output;
00026 };
00027 
00028 
00029 template <typename Input, typename Output, typename Result>
00030 static void check_result(char const * fct_name, Input const & input,
00031           Output const & output, Result const & result)
00032 {
00033     if (result != output) {
00034         cerr << fct_name << " "
00035              << "for:\n\"" << input << "\"\n"
00036              << "expect:\n\"" << output << "\"\n"
00037              << "found:\n\"" << result << "\"\n";
00038         exit(EXIT_FAILURE);
00039     }
00040 }
00041 
00042 template <typename Input, typename Output, typename Result>
00043 static void check_result(char const * fct_name, Input const & input1,
00044     Input input2, Output const & output, Result const & result)
00045 {
00046     if (result != output) {
00047         cerr << fct_name << ": \n"
00048              << "for:\n\"" << input1 << "\"\n"
00049              << "\"" << input2 << "\"\n"
00050              << "expect:\n\"" << output << "\"\n"
00051              << "found:\n\"" << result << "\"\n";
00052         exit(EXIT_FAILURE);
00053     }
00054 }
00055 
00056 
00057 static input_output<char const *, char const *> expect_dirname[] =
00058 {
00059     { "/", "/" },
00060     { "//////", "/" },
00061     { "/usr", "/" },
00062     { "///usr", "/" },
00063     // suprising but conform to dirname(1)
00064     { "///usr/dir", "///usr" },
00065     { "usr/dir", "usr" },
00066     { "usr", "." },
00067     { "n", "." },
00068     { "../..", ".." },
00069     { "/../..", "/.." },
00070     { "./..", "." },
00071     { "./.", "." },
00072     { "..", "." },
00073     { ".", "." },
00074     { "", "." },
00075     { 0, 0 }
00076 };
00077 
00078 static void dirname_tests()
00079 {
00080     input_output<char const *, char const *> const * cur;
00081     for (cur = expect_dirname; cur->input; ++cur) {
00082         string result = op_dirname(cur->input);
00083         check_result("dirname", cur->input, cur->output, result);
00084     }
00085 }
00086 
00087 
00088 static input_output<char const *, char const*> expect_basename[] =
00089 {
00090     { "/", "/" },
00091     { "//////", "/" },
00092     { "/usr", "usr" },
00093     { "///usr", "usr" },
00094     { "///usr/dir", "dir" },
00095     { "///usr//dir", "dir" },
00096     { "usr/dir", "dir" },
00097     { "usr", "usr" },
00098     { "../..", ".." },
00099     { "/../..", ".." },
00100     { "./..", ".." },
00101     { "./.", "." },
00102     { ".", "." },
00103     { 0, 0 }
00104 };
00105 
00106 static void basename_tests()
00107 {
00108     input_output<char const *, char const *> const * cur;
00109     for (cur = expect_basename; cur->input; ++cur) {
00110         string result = op_basename(cur->input);
00111         check_result("basename", cur->input, cur->output, result);
00112     }
00113 }
00114 
00115 
00116 static input_output<char const *, bool> expect_is_directory[] =
00117 {
00118     { ".", true },
00119     { "/.", true },
00120     { "./", true },
00121     { "/", true },
00122     { "../", true },
00123     { "../.", true },
00124     { "non_existing_dir", false },
00125     { 0, 0 }
00126 };
00127 
00128 static void is_directory_tests()
00129 {
00130     input_output<char const *, bool> const * cur;
00131     for (cur = expect_is_directory; cur->input; ++cur) {
00132         bool result = is_directory(cur->input);
00133         check_result("is_directory", cur->input, cur->output, result);
00134     }
00135 }
00136 
00137 
00138 static input_output<pair<string, string>, bool>
00139 expect_is_files_identical[] = {
00140 #define MAKE_PAIR(a, b) make_pair(string(a), string(b))
00141     { MAKE_PAIR(__FILE__, __FILE__), true },
00142     { MAKE_PAIR(__FILE__, "not_existing"), false },
00143     { MAKE_PAIR("not_exisiting", __FILE__), false },
00144     { MAKE_PAIR("not_exisiting", "not_existing"), false },
00145     { MAKE_PAIR("", ""), false }
00146 #undef MAKE_PAIR
00147 };
00148 
00149 void is_files_identical_tests(char const * prog_name)
00150 {
00151     check_result("is_files_identical", prog_name, prog_name,
00152                  is_files_identical(prog_name, prog_name), true);
00153 
00154     input_output<pair<string, string>, bool> const * cur;
00155     for (cur = expect_is_files_identical; !cur->input.first.empty(); ++cur) {
00156         bool result = is_files_identical(cur->input.first,
00157                                          cur->input.second);
00158         check_result("is_files_identical", cur->input.first,
00159                      cur->input.second, cur->output, result);
00160     }
00161 }
00162 
00163 
00164 static input_output<char const *, bool> expect_op_file_readable[] =
00165 {
00166     { __FILE__, true },
00167     { "./" __FILE__, true },
00168     { ".", false },
00169     { "/.", false },
00170     { "./", false },
00171     { "/", false },
00172     { "../", false },
00173     { "../.", false },
00174     { "non_existing_file", false },
00175     { 0, 0 }
00176 };
00177 
00178 static void op_file_readable_tests()
00179 {
00180     input_output<char const *, bool> const * cur;
00181     for (cur = expect_op_file_readable; cur->input; ++cur) {
00182         bool result = op_file_readable(cur->input);
00183         check_result("op_file_readable", cur->input, cur->output, result);
00184     }
00185 }
00186 
00187 
00188 static input_output<string, string> expect_realpath[] =
00189 {
00190     // realpath() file argument must exists.
00191     { "file_manip_tests.o", "file_manip_tests.o" },
00192     { "../tests/" "file_manip_tests.o", "file_manip_tests.o" },
00193     { ".//.//" "file_manip_tests.o", "file_manip_tests.o" },
00194     // POSIX namespaces ignored by realpath(3)
00195     { "//", "/" },
00196     { "//usr", "/usr" },
00197     { "///", "/" },
00198     { "", "" }
00199 };
00200 
00201 
00202 // FIXME: useful to test symlinks too
00203 static void realpath_tests()
00204 {
00205     input_output<string, string> const * cur;
00206     for (cur = expect_realpath; !cur->input.empty(); ++cur) {
00207         string result = op_realpath(cur->input);
00208         string expect = cur->output;
00209         if (cur->input[0] != '/')
00210             expect = SRCDIR + expect;
00211         check_result("op_realpath", cur->input,
00212                     expect, result);
00213     }
00214 }
00215 
00216 
00217 void create_file_list_tests()
00218 {
00219     list<string> result;
00220     if (!create_file_list(result, ".")) {
00221         cerr << "create_file_list() fail\n";
00222         exit(EXIT_FAILURE);
00223     }
00224     if (result.empty()) {
00225         cerr << "create_file_list(); empty result\n";
00226         exit(EXIT_FAILURE);
00227     }
00228 }
00229 
00230 
00231 int main(int, char * argv[])
00232 {
00233     dirname_tests();
00234     basename_tests();
00235     is_directory_tests();
00236     is_files_identical_tests(argv[0]);
00237     op_file_readable_tests();
00238     realpath_tests();
00239     create_file_list_tests();
00240     return EXIT_SUCCESS;
00241 }

Generated on 8 Nov 2012 for Oprofile by  doxygen 1.6.1