1 // -*- c++ -*-
2 // Generated by assa-genesis
3 //------------------------------------------------------------------------------
4 // $Id: timeval_test.cpp,v 1.6 2005/10/08 02:42:01 vlg Exp $
5 //------------------------------------------------------------------------------
6 //                            timeval_test.cpp
7 //------------------------------------------------------------------------------
8 //  Copyright (c) 2002,2005 by Vladislav Grinchenko
9 //
10 //  Permission to use, copy, modify, and distribute this software
11 //  and its documentation for any purpose and without fee is hereby
12 //  granted, provided that the above copyright notice appear in all
13 //  copies.  The author makes no representations about the suitability
14 //  of this software for any purpose.  It is provided "as is" without
15 //  express or implied warranty.
16 //------------------------------------------------------------------------------
17 //
18 // Date   : Wed Oct 16 15:09:19 2002
19 //
20 //------------------------------------------------------------------------------
21 static const char help_msg[]=
22 "                                                                            \n"
23 " NAME:                                                                      \n"
24 "                                                                            \n"
25 "   timeval_test                                                             \n"
26 "                                                                            \n"
27 " DESCRIPTION:                                                               \n"
28 "                                                                            \n"
29 "   Basic test for TimeVal class.                                            \n"
30 "                                                                            \n"
31 " USAGE:                                                                     \n"
32 "                                                                            \n"
33 "   shell>  timeval_test [OPTIONS]                                           \n"
34 "                                                                            \n"
35 " OPTIONS:                                                                   \n"
36 "                                                                            \n"
37 " -D, --log-file NAME     - Write debug to NAME file                         \n"
38 " -d, --log-stdout        - Write debug to standard output                   \n"
39 " -z, --log-size NUM      - Maximum size debug file can reach (dfl: is 10Mb) \n"
40 " -m, --mask MASK         - Mask (default: ALL = 0x7fffffff)                 \n"
41 " -h, --help              - Print this messag                                \n"
42 " -v, --version           - Print version number                            \n";
43 //------------------------------------------------------------------------------
44 
45 #include <string>
46 using std::string;
47 #include <iostream>
48 using namespace std;
49 
50 #include <assa/GenServer.h>
51 #include <assa/Singleton.h>
52 #include <assa/TimeVal.h>
53 #include <assa/Assure.h>
54 using namespace ASSA;
55 
56 class TV_Test :
57     public GenServer,
58     public Singleton<TV_Test>
59 {
60 public:
61     TV_Test ();
62 
63     virtual void init_service ();
64     virtual void process_events ();
65 
66 private:
67 	void log_header (const string& msg_);
68 	void dump_all_formats (TimeVal& tv_, const string& msg_);
69 };
70 
71 /* Useful definitions */
72 
73 #define TV_TEST TV_Test::get_instance()
74 #define REACTOR TV_TEST->get_reactor()
75 
76 // Static declarations mandated by Singleton class
77 ASSA_DECL_SINGLETON(TV_Test);
78 
TV_Test()79 TV_Test::TV_Test ()
80 {
81     m_log_file = "timeval_test.log";
82 }
83 
84 void
init_service()85 TV_Test::init_service ()
86 {
87     trace("TV_Test::init_service");
88 
89     Log::disable_timestamp ();
90 
91     DL((APP,"Service has been initialized\n"));
92 }
93 
94 void
95 TV_Test::
log_header(const string & msg_)96 log_header (const string& msg_)
97 {
98 	cout << msg_;
99 	DL((APP,"%s\n", msg_.c_str ()));
100 }
101 
102 void
103 TV_Test::
dump_all_formats(TimeVal & tv_,const string & msg_)104 dump_all_formats (TimeVal& tv_, const string& msg_)
105 {
106 	DL((APP, "\n"
107 		"\t%s\n"
108 		"\t===========================================\n"
109 		"\t sec = %d\n"
110 		"\t msec = %d\n"
111 		"\t millisec = %d\n"
112 		"\t fmtString()         = %s\n"
113 		"\t fmt_hh_mm_ss()      = %s\n",
114 		msg_.c_str (),
115 		tv_.sec (),
116 		tv_.msec (),
117 		tv_.millisec (),
118 		tv_.fmtString        ().c_str (),
119 		tv_.fmt_hh_mm_ss     ().c_str ()));
120 
121 	DL((APP,"\n"
122 		"\t fmt_hh_mm_ss_mls()  = %s\n"
123 		"\t fmt_mm_ss()         = %s\n"
124 		"\t fmt_mm_ss_mls()     = %s\n"
125 		"\t fmt_ss_mls()        = %s\n"
126 		"\t===========================================\n",
127 		tv_.fmt_hh_mm_ss_mls ().c_str (),
128 		tv_.fmt_mm_ss        ().c_str (),
129 		tv_.fmt_mm_ss_mls    ().c_str (),
130 		tv_.fmt_ss_mls       ().c_str ()
131 		   ));
132 }
133 
134 void
process_events()135 TV_Test::process_events ()
136 {
137     trace("TV_Test::process_events");
138 
139 	log_header ("1) Testing constructors ...");
140 	double result;
141 
142 	TimeVal tv0(TimeVal::gettimeofday ());		// today's date
143 	DL((APP,"Time now: %s -> %5.2f\n", tv0.fmtString().c_str (), double(tv0)));
144 
145 	TimeVal tv1(1, 0);	// 1 second
146 	DL((APP,"1 sec   : %s -> %5.2f\n", tv1.fmtString().c_str (), double(tv1)));
147 	result = tv1;
148 	Assure_exit (result == 1.0);
149 
150 	TimeVal tv2(2.5);	// 2 1/2 secs
151 	DL((APP,"2.5 secs: %s -> %5.2f\n", tv2.fmtString().c_str (), double(tv2)));
152 	result = tv2;
153 	Assure_exit (result == 2.5);
154 
155 	TimeVal tv_05 (.5);	// 1/2 of a second
156 	DL((APP,"0.5 secs: %s -> %5.2f\n",
157 		tv_05.fmtString().c_str (), double (tv_05)));
158 	result = tv_05;
159 	Assure_exit (result == 0.5);
160 
161 	cout << "ok\n";
162 
163 	log_header ("2) Testing conversion to/from struct timeval ...");
164 
165 	time_t  tt;
166 	time (&tt);
167 	struct timeval tval = { tt, 0 };
168 	TimeVal tv3(tval);
169 
170 	TimeVal tv4(tv3);
171 	tval = tv4;
172 	cout << "ok\n";
173 
174 	log_header ("3) Testing assignment opt ...");
175 
176 	TimeVal tv5;
177 	tv5 = tv4;
178 	Assure_exit (tv5 == tv4);
179 
180 	cout << "ok\n";
181 
182 	log_header ("4) Testing +/- opts ...");
183 
184 	tv5 += tv1;
185 	tv5 -= tv1;
186 	Assure_exit (tv4 == tv5);
187 
188 	cout << "ok\n";
189 
190 	log_header ("5) Testing logical opts ...");
191 
192 	Assure_exit (tv1 < tv2);
193 	Assure_exit (tv2 > tv1);
194 	Assure_exit (tv1 <= tv2);
195 	Assure_exit (tv2 >= tv1);
196 	Assure_exit (tv1 != tv2);
197 
198 	cout << "ok\n";
199 
200 	log_header ("6) Testing default formatting ...");
201 
202 	DL((APP, "GMT: %s\n", tv0.fmtString ().c_str ()));
203 	tv0.tz (TimeVal::loc);
204 	DL((APP, "EDT: %s\n", tv0.fmtString ().c_str ()));
205 
206 	cout << "ok\n";
207 
208 	log_header ("7) Testing user-specified formatting ...");
209 
210 	tv0.tz (TimeVal::gmt);
211 	DL((APP,"GMT: %s\n", tv0.fmtString ("%c").c_str ()));
212 
213 	tv0.tz (TimeVal::loc);
214 	DL((APP,"EDT: %s\n", tv0.fmtString ().c_str ()));
215 
216 	cout << "ok\n";
217 
218 	log_header ("8) Testing formatting functions ...");
219 
220 	TimeVal tv81 (27.348);
221 	dump_all_formats (tv81, "Formatting for 27 seconds 348 milliseconds:");
222 	tv81.dump_to_log ("tv81");
223 
224 	TimeVal tv82 (30);
225 	dump_all_formats (tv82, "Formatting for 30 secs");
226 	tv82.dump_to_log ("tv82");
227 
228 	TimeVal tv83 (0);
229 	dump_all_formats (tv83, "Formatting for 0 seconds");
230 	tv83.dump_to_log ("tv83");
231 
232 	cout << "ok\n";
233 
234 	log_header ("9) Testing default initialization ...");
235 
236 	if (tv83 == TimeVal::zeroTime ()) {
237 		DL((APP,"tv83 is equal to 0 - OK\n"));
238 		cout << "ok\n";
239 	}
240 	else {
241 		DL((APP,"Oops! tv83 is not equal to 0\n - ERROR\n"));
242 		cout << "error\n";
243 		set_exit_value (1);
244 	}
245 
246     m_reactor.stopReactor ();
247     DL((APP,"Service stopped!\n"));
248 }
249 
250 #ifdef HAVE_CONFIG_H
251 #    include "config.h"
252 #endif
253 
254 int
main(int argc,char * argv[])255 main (int argc, char* argv[])
256 {
257     static const char release[] = "VERSION";
258     int patch_level = 0;
259 
260 	cout << "= Running timeval_test Test =\n";
261 
262     TV_TEST->set_version (release, patch_level);
263     TV_TEST->set_author  ("Vladislav Grinchenko");
264 	TV_TEST->set_flags (GenServer::RMLOG);
265 
266     TV_TEST->init (&argc, argv, help_msg);
267 
268     TV_TEST->init_service ();
269     TV_TEST->process_events ();
270 
271 	cout << "Test "
272 		 << (TV_TEST->get_exit_value () == 0 ? "passed\n" : "failed\n");
273 
274     return TV_TEST->get_exit_value ();
275 }
276 
277