14e25490ffdd5deabc54ae91d3a779ba4fe26894
[feisty_meow.git] / nucleus / library / tests_filesystem / test_file_time.cpp
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
9 #include <application/hoople_main.h>
10 #include <basis/enhance_cpp.h>
11 #include <basis/functions.h>
12 #include <basis/astring.h>
13 #include <filesystem/file_time.h>
14 #include <loggers/program_wide_logger.h>
15 #include <loggers/critical_events.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
41 //////////////
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
53 //////////////
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, NIL, "opening topdir for testing");
73   if (topdir == NIL) {
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