feisty meow concerns codebase 2.140
test_huge_file.cpp
Go to the documentation of this file.
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
14#include <basis/byte_array.h>
15#include <basis/functions.h>
16#include <basis/guards.h>
17#include <basis/astring.h>
20#include <filesystem/filename.h>
24#include <mathematics/chaos.h>
27#include <unit_test/unit_base.h>
28
29using namespace application;
30using namespace basis;
31using namespace configuration;
32using namespace mathematics;
33using namespace filesystem;
34using namespace loggers;
35using namespace structures;
36using namespace textual;
37using namespace timely;
38using namespace unit_test;
39
40#define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
41
42class test_huge_file : public virtual unit_base, virtual public application_shell
43{
44public:
45 test_huge_file() : application_shell() {}
46 DEFINE_CLASS_NAME("test_huge_file");
47 void run_file_scan();
48 virtual int execute();
49};
50
51void test_huge_file::run_file_scan()
52{
53 FUNCDEF("run_file_scan");
55
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.
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
101int test_huge_file::execute()
102{
103 FUNCDEF("execute");
104 run_file_scan();
105 return final_report();
106}
107
108HOOPLE_MAIN(test_huge_file, )
109
The application_shell is a base object for console programs.
virtual int execute()=0
< retrieves the command line from the /proc hierarchy on linux.
application_shell()
constructs an application_shell to serve as the root of the program.
a_sprintf is a specialization of astring that provides printf style support.
Definition astring.h:440
Provides a dynamically resizable ASCII character string.
Definition astring.h:35
A very common template for a dynamic array of bytes.
Definition byte_array.h:36
Outcomes describe the state of completion for an operation.
Definition outcome.h:31
int value() const
Definition outcome.h:51
static basis::astring current_directory()
returns the current directory as reported by the operating system.
Implements a scanner that finds all filenames in the directory specified.
Definition directory.h:27
Supports reading and writing to very large files, > 4 gigabytes.
Definition huge_file.h:36
a platform-independent way to acquire random numbers in a specific range.
Definition chaos.h:51
An array of strings with some additional helpful methods.
#define DEFINE_CLASS_NAME(objname)
Defines the name of a class by providing a couple standard methods.
Definition enhance_cpp.h:42
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
Definition enhance_cpp.h:54
Provides macros that implement the 'main' program of an application.
#define HOOPLE_MAIN(obj_name, obj_args)
options that should work for most unix and linux apps.
Definition hoople_main.h:61
Implements an application lock to ensure only one is running at once.
char ** _global_argv
The guards collection helps in testing preconditions and reporting errors.
Definition array.h:30
const int KILOBYTE
Number of bytes in a kilobyte.
A platform independent way to obtain the timestamp of a file.
A logger that sends to the console screen using the standard output device.
An extension to floating point primitives providing approximate equality.
Definition averager.h:21
A dynamic container class that holds any kind of object via pointers.
Definition amorph.h:55
#include <time.h>
Useful support functions for unit testing, especially within hoople.
Definition unit_base.cpp:35
#define randomizer()
#define test(expr)
#define ASSERT_EQUAL(a, b, test_name)
Definition unit_base.h:38
#define ASSERT_TRUE(a, test_name)
Definition unit_base.h:46