cleaning up usage of TMP variable to always get it from environment class.
[feisty_meow.git] / nucleus / 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 current directory.  some of
61     // these might fail if they're locked up.
62     astring tmpdir = application_configuration::current_directory();
63     directory dir(tmpdir);
64     for (int i = 0; i < dir.files().length(); i++) {
65       // skip text files since we use those right here.
66       if (dir.files()[i].ends(".txt"))
67         continue;
68       astring chewed_string = tmpdir + "/" + dir.files()[i];
69       files += chewed_string;
70     }
71 //LOG(astring("added files since no cmd args: ") + files.text_form());
72   }
73
74   byte_array data_found;
75   for (int i = 0; i < files.length(); i++) {
76     astring curr = files[i];
77 //    LOG(a_sprintf("file %d: ", i) + curr);
78     huge_file test(curr, "rb");
79     ASSERT_TRUE(test.good(), "good check should say yes, it's good.");
80     double len = test.length();
81 //log(a_sprintf("file len is %.0f", len));
82     double posn = 0;
83     while ( (posn < len) && !test.eof() ) {
84       int readlen = randomizer.inclusive(1, 256 * KILOBYTE);
85 //log(a_sprintf("read %.0f bytes, posn now %.0f bytes", double(readlen), posn));
86       int bytes_read = 0;
87       outcome ret = test.read(data_found, readlen, bytes_read);
88       ASSERT_EQUAL(ret.value(), huge_file::OKAY, "should be able to read file");
89       if (ret == huge_file::OKAY) {
90         posn += bytes_read;
91       }
92     }
93     ASSERT_TRUE(test.eof(), "eof check should be at eof.");
94     if (posn != len) 
95       log(a_sprintf("failed check, want %.0f, got %.0f", double(len), double(posn)));
96     ASSERT_EQUAL(posn, len, "eof check should be at right position: ");
97 //    log(astring("successfully read ") + curr);
98   }
99 }
100
101 int test_huge_file::execute()
102 {
103   FUNCDEF("execute");
104   run_file_scan();
105   return final_report();
106 }
107
108 HOOPLE_MAIN(test_huge_file, )
109