feisty meow concerns codebase  2.140
test_file_time.cpp
Go to the documentation of this file.
1 /*
2 * Name : test_file_time
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_time.h>
16 #include <mathematics/chaos.h>
17 #include <timely/time_stamp.h>
18 #include <unit_test/unit_base.h>
19 
20 #include <stdio.h>
21 #include <sys/stat.h>
22 #ifdef __UNIX__
23  #include <unistd.h>
24 #endif
25 
26 using namespace application;
27 using namespace basis;
28 using namespace filesystem;
29 using namespace loggers;
30 using namespace mathematics;
31 using namespace timely;
32 using namespace unit_test;
33 
34 //#define DEBUG_TEST_FILE_INFO
35  // uncomment for noisy version.
36 
37 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
38 
39 static chaos a_randomizer;
40 
42 
43 class test_file_time : public application_shell, public unit_base
44 {
45 public:
46  test_file_time() : application_shell(), unit_base() {}
47 
48  DEFINE_CLASS_NAME("test_file_time");
49 
50  virtual int execute();
51 };
52 
54 
55 int test_file_time::execute()
56 {
57  FUNCDEF("execute");
58 
59 #ifdef __UNIX__
60  // just open the root directory on unix; always works.
61  astring toppy("/");
62 #endif
63 #ifdef __WIN32__
64  // windows cannot fopen a directory. this blows. so we pick a file
65  // that should work for most windowses.
66  astring toppy("c:/Windows/notepad.exe");
67 #endif
68 
69  // test storing info via the constructor.
70  file_time absurdity_time(toppy);
71  FILE *topdir = fopen(toppy.s(), "r");
72  ASSERT_INEQUAL(topdir, NULL_POINTER, "opening topdir for testing");
73  if (topdir == NULL_POINTER) {
74  return 1;
75  }
76  file_time nutty_time(topdir);
77 
78  int filenum = fileno(topdir);
79  struct stat sbuffer;
80  int stat_okay = fstat(filenum, &sbuffer);
81  ASSERT_FALSE(stat_okay, "failure to read filetime");
82  file_time goofy_time(sbuffer.st_mtime);
83  fclose(topdir);
84  file_time testing(goofy_time); // copy ctor.
85  // test that they all got the same idea from the file.
86  ASSERT_EQUAL(absurdity_time, nutty_time, "filename vs. FILE ctor");
87  ASSERT_EQUAL(absurdity_time, goofy_time, "filename vs. time_t ctor");
88  ASSERT_EQUAL(absurdity_time, testing, "filename vs. copy ctor");
89  ASSERT_EQUAL(nutty_time, goofy_time, "FILE vs. time_t ctor");
90  ASSERT_EQUAL(nutty_time, testing, "FILE vs. copy ctor");
91  // one reversed direction check.
92  ASSERT_EQUAL(goofy_time, absurdity_time, "time_t vs. filename ctor");
93 
94  // test packing the object and packed_size.
95  byte_array packed;
96  int size = testing.packed_size();
97  testing.pack(packed);
98  ASSERT_EQUAL(size, packed.length(), "packed size accuracy");
99  file_time unstuffy;
100  ASSERT_TRUE(unstuffy.unpack(packed), "unpacking");
101  ASSERT_EQUAL((double)testing.raw(), (double)unstuffy.raw(), "unpacked contents should be equal to prior");
102 
103  // test the text_form method.
104  astring text;
105  testing.text_form(text);
106  ASSERT_INEQUAL(text.length(), 0, "text_form produces text");
107 
108  // test validity after unpacking.
109  ASSERT_EQUAL(unstuffy, goofy_time, "constructor file size");
110 
111  return final_report();
112 }
113 
114 HOOPLE_MAIN(test_file_time, )
115 
#define stat
Definition: Xos2defs.h:41
#define fileno
Definition: Xos2defs.h:27
#define fstat
Definition: Xos2defs.h:28
The application_shell is a base object for console programs.
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
virtual void text_form(base_string &state_fill) const
Provides a text view of all the important info owned by this object.
Definition: astring.cpp:130
int length() const
Returns the current length of the string.
Definition: astring.cpp:132
A very common template for a dynamic array of bytes.
Definition: byte_array.h:36
time_t raw() const
provides the OS version of the file's timestamp.
Definition: file_time.h:71
virtual bool unpack(basis::byte_array &packed_form)
Definition: file_time.cpp:137
a platform-independent way to acquire random numbers in a specific range.
Definition: chaos.h:51
#define NULL_POINTER
The value representing a pointer to nothing.
Definition: definitions.h:32
#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
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
#define ASSERT_EQUAL(a, b, test_name)
Definition: unit_base.h:38
#define ASSERT_TRUE(a, test_name)
Definition: unit_base.h:46
#define ASSERT_FALSE(a, test_name)
Definition: unit_base.h:50
#define ASSERT_INEQUAL(a, b, test_name)
Definition: unit_base.h:42