first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / core / 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   astring toppy("/");
61 #endif
62 #ifdef __WIN32__
63   astring toppy("c:/ntldr");  // will work for any modern windows OS.
64 #endif
65
66   // test storing info via the constructor.
67   file_time absurdity_time(toppy);
68   FILE *topdir = fopen(toppy.s(), "r");
69   file_time nutty_time(topdir);
70   struct stat sbuffer;
71   int filenum = fileno(topdir);
72   int stat_okay = fstat(filenum, &sbuffer);
73   ASSERT_FALSE(stat_okay, "failure to read filetime");
74   file_time goofy_time(sbuffer.st_mtime); 
75   fclose(topdir);
76   file_time testing(goofy_time);  // copy ctor.
77   // test that they all got the same idea from the file.
78   ASSERT_EQUAL(absurdity_time, nutty_time, "filename vs. FILE ctor");
79   ASSERT_EQUAL(absurdity_time, goofy_time, "filename vs. time_t ctor");
80   ASSERT_EQUAL(absurdity_time, testing, "filename vs. copy ctor");
81   ASSERT_EQUAL(nutty_time, goofy_time, "FILE vs. time_t ctor");
82   ASSERT_EQUAL(nutty_time, testing, "FILE vs. copy ctor");
83   // one reversed direction check.
84   ASSERT_EQUAL(goofy_time, absurdity_time, "time_t vs. filename ctor");
85
86   // test packing the object and packed_size.
87   byte_array packed;
88   int size = testing.packed_size();
89   testing.pack(packed);
90   ASSERT_EQUAL(size, packed.length(), "packed size accuracy");
91   file_time unstuffy;
92   ASSERT_TRUE(unstuffy.unpack(packed), "unpacking");
93   ASSERT_EQUAL((double)testing.raw(), (double)unstuffy.raw(), "unpacked contents should be equal to prior");
94
95   // test the text_form method.
96   astring text;
97   testing.text_form(text);
98   ASSERT_INEQUAL(text.length(), 0, "text_form produces text");
99
100   // test validity after unpacking.
101   ASSERT_EQUAL(unstuffy, goofy_time, "constructor file size");
102
103   return final_report();
104 }
105
106 HOOPLE_MAIN(test_file_time, )
107