first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / core / library / tests_filesystem / test_huge_file.cpp
1 /*
2 *  Name   : test_huge_file
3 *  Author : Chris Koeritz
4 **
5 * Copyright (c) 1991-$now By Author.  This program is free software; you can  *
6 * redistribute it and/or modify it under the terms of the GNU General Public  *
7 * License as published by the Free Software Foundation; either version 2 of   *
8 * the License or (at your option) any later version.  This is online at:      *
9 *     http://www.fsf.org/copyleft/gpl.html                                    *
10 * Please send any updates to: fred@gruntose.com                               *
11 */
12
13 #include <application/hoople_main.h>
14 #include <basis/byte_array.h>
15 #include <basis/functions.h>
16 #include <basis/guards.h>
17 #include <basis/astring.h>
18 #include <configuration/application_configuration.h>
19 #include <filesystem/directory.h>
20 #include <filesystem/filename.h>
21 #include <filesystem/huge_file.h>
22 #include <loggers/critical_events.h>
23 #include <loggers/program_wide_logger.h>
24 #include <mathematics/chaos.h>
25 #include <structures/static_memory_gremlin.h>
26 #include <structures/string_array.h>
27 #include <unit_test/unit_base.h>
28
29 using namespace application;
30 using namespace basis;
31 using namespace configuration;
32 using namespace mathematics;
33 using namespace filesystem;
34 using namespace loggers;
35 using namespace structures;
36 using namespace textual;
37 using namespace timely;
38 using namespace unit_test;
39
40 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
41
42 class test_huge_file : public virtual unit_base, virtual public application_shell
43 {
44 public:
45   test_huge_file() : application_shell() {}
46   DEFINE_CLASS_NAME("test_huge_file");
47   void run_file_scan();
48   virtual int execute();
49 };
50
51 void test_huge_file::run_file_scan()
52 {
53   FUNCDEF("run_file_scan");
54   chaos randomizer;
55
56   string_array files(application::_global_argc, (const char **)application::_global_argv);
57   files.zap(0, 0);  // toss the first element since that's our app filename.
58
59   if (!files.length()) {
60     // pretend they gave us the list of files in the TMP directory.  some of
61     // these might fail if they're locked up.
62 //    astring tmpdir = environment::get("TMP");
63     astring tmpdir = application_configuration::current_directory();
64     directory dir(tmpdir);
65     for (int i = 0; i < dir.files().length(); i++) {
66       // skip text files since we use those right here.
67       if (dir.files()[i].ends(".txt"))
68         continue;
69       astring chewed_string = tmpdir + "/" + dir.files()[i];
70       files += chewed_string;
71     }
72 //LOG(astring("added files since no cmd args: ") + files.text_form());
73   }
74
75   byte_array data_found;
76   for (int i = 0; i < files.length(); i++) {
77     astring curr = files[i];
78 //    LOG(a_sprintf("file %d: ", i) + curr);
79     huge_file test(curr, "rb");
80     ASSERT_TRUE(test.good(), "good check should say yes, it's good.");
81     double len = test.length();
82 //log(a_sprintf("file len is %.0f", len));
83     double posn = 0;
84     while ( (posn < len) && !test.eof() ) {
85       int readlen = randomizer.inclusive(1, 256 * KILOBYTE);
86 //log(a_sprintf("read %.0f bytes, posn now %.0f bytes", double(readlen), posn));
87       int bytes_read = 0;
88       outcome ret = test.read(data_found, readlen, bytes_read);
89       ASSERT_EQUAL(ret.value(), huge_file::OKAY, "should be able to read file");
90       if (ret == huge_file::OKAY) {
91         posn += bytes_read;
92       }
93     }
94     ASSERT_TRUE(test.eof(), "eof check should be at eof.");
95     if (posn != len) 
96       log(a_sprintf("failed check, want %.0f, got %.0f", double(len), double(posn)));
97     ASSERT_EQUAL(posn, len, "eof check should be at right position: ");
98 //    log(astring("successfully read ") + curr);
99   }
100 }
101
102 int test_huge_file::execute()
103 {
104   FUNCDEF("execute");
105   run_file_scan();
106   return final_report();
107 }
108
109 HOOPLE_MAIN(test_huge_file, )
110