feisty meow concerns codebase  2.140
test_file_info.cpp
Go to the documentation of this file.
1 /*
2 * Name : test_file_info
3 * Author : Chris Koeritz
4 * Copyright (c) 1991-$now By Author. This program is free software; you can *
5 * http://www.fsf.org/copyleft/gpl.html *
6 * Please send any updates to: fred@gruntose.com *
7 \*****************************************************************************/
8 
10 #include <basis/enhance_cpp.h>
11 #include <basis/functions.h>
12 #include <basis/astring.h>
13 #include <filesystem/file_info.h>
16 #include <mathematics/chaos.h>
17 #include <timely/time_stamp.h>
18 #include <unit_test/unit_base.h>
19 
20 using namespace application;
21 using namespace basis;
22 using namespace filesystem;
23 using namespace loggers;
24 using namespace mathematics;
25 using namespace timely;
26 using namespace unit_test;
27 
28 //#define DEBUG_TEST_FILE_INFO
29  // uncomment for noisy version.
30 
31 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
32 
33 static chaos a_randomizer;
34 
36 
37 class test_file_info : public application_shell, public unit_base
38 {
39 public:
40  test_file_info() : application_shell(), unit_base() {}
41 
42  DEFINE_CLASS_NAME("test_file_info");
43 
44  virtual int execute();
45 };
46 
48 
49 //hmmm: stolen from ssl_init.
51 {
52  byte_array seed;
53  for (int i = 0; i < length; i++)
54  seed += abyte(chaos().inclusive(0, 255));
55  return seed;
56 }
57 
58 int test_file_info::execute()
59 {
60  FUNCDEF("execute");
61 #ifdef __UNIX__
62  file_time absurdity_time("/");
63 #endif
64 #ifdef __WIN32__
65  file_time absurdity_time("c:/");
66 #endif
67 
68  // test storing info via the constructor.
69  file_info testing(filename("/usr/schrodingers/dog/got/away"), 7298238);
70  testing._time = absurdity_time;
71  testing._checksum = 1283412;
72  ASSERT_EQUAL((int)testing._file_size, (int)7298238, "constructor file size");
73  ASSERT_EQUAL(testing._time, absurdity_time, "constructor file time");
74  ASSERT_EQUAL(testing._checksum, 1283412, "constructor checksum");
75  ASSERT_EQUAL((filename &)testing, filename("/usr/schrodingers/dog/got/away"),
76  "constructor filename");
77 
78  // test packing the object and packed_size.
79  byte_array packed;
80  int size = testing.packed_size();
81  testing.pack(packed);
82  ASSERT_EQUAL(size, packed.length(), "basic packed size accuracy");
83  file_info unstuffy;
84  ASSERT_TRUE(unstuffy.unpack(packed), "basic unpacking");
85 
86  // test validity after unpacking.
87  ASSERT_EQUAL((int)unstuffy._file_size, (int)7298238, "constructor file size");
88  ASSERT_EQUAL(unstuffy._time, absurdity_time, "constructor file time");
89  ASSERT_EQUAL(unstuffy._checksum, 1283412, "constructor checksum");
90  ASSERT_EQUAL((filename &)unstuffy, filename("/usr/schrodingers/dog/got/away"),
91  "constructor filename");
92 
93  // test the extra bits, the attachment and secondary name.
94  astring seconame = "glorabahotep";
95  testing.secondary(seconame );
96  const byte_array randobytes = random_bytes(chaos().inclusive(37, 4128));
97  testing.attachment(randobytes);
98  packed.reset();
99  size = testing.packed_size();
100  testing.pack(packed);
101  ASSERT_EQUAL(size, packed.length(), "secondary packed size accuracy");
102  ASSERT_TRUE(unstuffy.unpack(packed), "secondary unpacking");
103  // test that the secondary name and attachment came back.
104  ASSERT_EQUAL(seconame, unstuffy.secondary(), "secondary name incorrect");
105  ASSERT_EQUAL(randobytes, unstuffy.attachment(), "secondary attachment inaccurate");
106 
107  return final_report();
108 }
109 
110 HOOPLE_MAIN(test_file_info, )
111 
The application_shell is a base object for console programs.
void reset(int number=0, const contents *initial_contents=NULL_POINTER)
Resizes this array and sets the contents from an array of contents.
Definition: array.h:349
int length() const
Returns the current reported length of the allocated C array.
Definition: array.h:115
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
Encapsulates some measures and calculations based on a file's contents.
Definition: file_info.h:29
double _file_size
the size of the file.
Definition: file_info.h:40
int _checksum
the checksum for the file.
Definition: file_info.h:42
file_time _time
the file's access time.
Definition: file_info.h:41
virtual bool unpack(basis::byte_array &packed_form)
Restores the packable from the "packed_form".
Definition: file_info.cpp:203
Provides operations commonly needed on file names.
Definition: filename.h:64
virtual bool unpack(basis::byte_array &packed_form)
Restores the packable from the "packed_form".
Definition: filename.cpp:471
virtual void pack(basis::byte_array &packed_form) const
Creates a packed form of the packable object in "packed_form".
Definition: filename.cpp:465
virtual int packed_size() const
Estimates the space needed for the packed structure.
Definition: filename.cpp:460
a platform-independent way to acquire random numbers in a specific range.
Definition: chaos.h:51
#define DEFINE_CLASS_NAME(objname)
Defines the name of a class by providing a couple standard methods.
Definition: enhance_cpp.h:45
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
Definition: enhance_cpp.h:57
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.
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
unsigned char abyte
A fairly important unit which is seldom defined...
Definition: definitions.h:51
A platform independent way to obtain the timestamp of a file.
Definition: byte_filer.cpp:37
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
#include <time.h>
Definition: earth_time.cpp:37
Useful support functions for unit testing, especially within hoople.
Definition: unit_base.cpp:35
byte_array random_bytes(int length)
#define ASSERT_EQUAL(a, b, test_name)
Definition: unit_base.h:38
#define ASSERT_TRUE(a, test_name)
Definition: unit_base.h:46