00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include <basis/byte_array.h>
00016 #include <basis/chaos.h>
00017 #include <basis/function.h>
00018 #include <basis/guards.h>
00019 #include <basis/istring.h>
00020 #include <basis/portable.h>
00021 #include <basis/string_array.h>
00022 #include <opsystem/application_shell.h>
00023 #include <loggers/console_logger.h>
00024 #include <opsystem/directory.h>
00025 #include <loggers/file_logger.h>
00026 #include <opsystem/filename.h>
00027 #include <opsystem/huge_file.h>
00028 #include <data_struct/static_memory_gremlin.h>
00029
00030 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger(), s)
00031
00032 class test_huge_file : public application_shell
00033 {
00034 public:
00035 test_huge_file() : application_shell(class_name()) {}
00036 IMPLEMENT_CLASS_NAME("test_huge_file");
00037 int run_file_scan();
00038 virtual int execute();
00039 };
00040
00041 int test_huge_file::run_file_scan()
00042 {
00043 FUNCDEF("run_file_scan");
00044 chaos randomizer;
00045
00046 string_array files(__argc, (const char **)__argv);
00047 files.zap(0, 0);
00048
00049 if (!files.length()) {
00050
00051
00052
00053 istring tmpdir = portable::current_directory();
00054 directory dir(tmpdir);
00055 for (int i = 0; i < dir.files().length(); i++) {
00056
00057 if (dir.files()[i].ends(".txt"))
00058 continue;
00059 istring chewed_string = tmpdir + "/" + dir.files()[i];
00060 files += chewed_string;
00061 }
00062 LOG(istring("added files since no cmd args: ") + files.text_form());
00063 }
00064
00065 byte_array data_found;
00066 for (int i = 0; i < files.length(); i++) {
00067 istring curr = files[i];
00068 LOG(isprintf("file %d: ", i) + curr);
00069 huge_file test(curr, "rb");
00070 if (!test.good())
00071 LOG(istring("good check: ") + curr + " cannot be opened.");
00072 double len = test.length();
00073 log(isprintf("file len is %.0f", len));
00074 double posn = 0;
00075 while ( (posn < len) && !test.eof() ) {
00076 int readlen = randomizer.inclusive(1, 256 * KILOBYTE);
00077 log(isprintf("read %.0f bytes, posn now %.0f bytes", double(readlen), posn));
00078 int bytes_read = 0;
00079 outcome ret = test.read(data_found, readlen, bytes_read);
00080 if (ret != huge_file::OKAY) {
00081 deadly_error(class_name(), "reading", curr + " failed to be read.");
00082 } else {
00083 posn += bytes_read;
00084 }
00085 }
00086 if (!test.eof())
00087 deadly_error(class_name(), "eof check", curr + " not at eof.");
00088 if (posn != len)
00089 deadly_error(class_name(), "eof check", curr + " is at wrong position: "
00090 + isprintf("want %.0f, got %.0f", double(len), double(posn)));
00091 log(istring("successfully read ") + curr);
00092 }
00093
00094 return 0;
00095 }
00096
00097 int test_huge_file::execute()
00098 {
00099 FUNCDEF("execute");
00100 SET_DEFAULT_COMBO_LOGGER;
00101 int ret = run_file_scan();
00102 if (ret) return ret;
00103
00104 guards::alert_message("huge_file:: works for those functions tested.\n");
00105 return 0;
00106 }
00107
00108 HOOPLE_MAIN(test_huge_file, )
00109