File: common_utils_test.cpp

package info (click to toggle)
libassa 3.5.1-8.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,424 kB
  • sloc: cpp: 15,703; sh: 12,083; makefile: 379; perl: 51
file content (261 lines) | stat: -rw-r--r-- 7,886 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// -*- c++ -*-
// Generated by assa-genesis
//------------------------------------------------------------------------------
// $Id: common_utils_test.cpp,v 1.10 2012/05/23 02:52:25 vlg Exp $
//------------------------------------------------------------------------------
//                            CommonUtils_test.cpp
//------------------------------------------------------------------------------
//  Copyright (c) 2002-2003,2005 by Vladislav Grinchenko
//
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; either version
//  2 of the License, or (at your option) any later version.
//
// Date   : Sat Oct  5 12:53:20 2002
//------------------------------------------------------------------------------
static const char help_msg[]=
"                                                                            \n"
" NAME:                                                                      \n"
"                                                                            \n"
"   common_utils_test                                                        \n"
"                                                                            \n"
" DESCRIPTION:                                                               \n"
"                                                                            \n"
"  Test for class CommonUtils.                                               \n"
"                                                                            \n"
" USAGE:                                                                     \n"
"                                                                            \n"
"   shell>  common_utils_test                                                \n"
"                                                                            \n"
" OPTIONS:                                                                   \n"
"                                                                            \n"
" -D, --log-file NAME     - Log file name                                    \n"
" -d, --log-stdout        - Log messages to standard output (default)        \n"
" -m, --mask              - Log mask                                         \n"
" -h, --help              - Print this messag                                \n"
" -v, --version           - Print version number                            \n";
//------------------------------------------------------------------------------

#ifdef HAVE_CONFIG_H
#    include "config.h"
#endif

#include <stdlib.h>				// setenv(3)
#include <string>
using std::string;

#include <assa/GenServer.h>
#include <assa/Singleton.h>
#include <assa/TimeVal.h>
#include <assa/MemDump.h>
#include <assa/CommonUtils.h>

using namespace ASSA;

enum { TEST = USR1 };
static const int OVERWRITE = 1;


class CommonUtils_test :
    public GenServer,
    public Singleton<CommonUtils_test>
{
public:
    CommonUtils_test ();

    virtual void init_service ();
    virtual void process_events ();
};

/* Useful definitions */

#define COMMONUTILS_TEST CommonUtils_test::get_instance()
#define REACTOR COMMONUTILS_TEST->get_reactor()

// Static declarations mandated by Singleton class
ASSA_DECL_SINGLETON(CommonUtils_test);

CommonUtils_test::
CommonUtils_test ()
{
    // ---Debugging---
    rm_opt ('z', "log-size"     );

    // ---Configuration---
    rm_opt ('f', "config-file"  );
    rm_opt ('n', "instance"     );
    rm_opt ('p', "port"         );

    // ---Process bookkeeping---
    rm_opt ('b', "daemon"       );
    rm_opt ('l', "pidfile"      );
    rm_opt ('L', "ommit-pidfile");

    /*---
     * Disable all debugging
     *---*/
    m_mask = TEST;
	m_log_file = "common_utils.log";
	m_log_stdout = "yes";
}


int
main (int argc, char* argv[])
{
    static const char release[] = "1.1";
    int patch_level = 0;

    COMMONUTILS_TEST->set_version (release, patch_level);
    COMMONUTILS_TEST->set_author  ("Vladislav Grinchenko");

    COMMONUTILS_TEST->init (&argc, argv, help_msg);

    COMMONUTILS_TEST->init_service ();
    COMMONUTILS_TEST->process_events ();

    return COMMONUTILS_TEST->get_exit_value ();
}

void
CommonUtils_test::
init_service ()
{
    Log::disable_timestamp ();
}

void
CommonUtils_test::
process_events ()
{
	//=================================================================
	DL((TEST, "Testing split () ...\n"));

	//	Separator: blank, tab, newline, formfeed, and carriage return.

	std::ostringstream oss;

	oss << "peaches" << ' '
		<< "roses"   << '\t'	// tab
		<< "beer"    << '\n'	// newline
		<< "bread"   << '\f'	// formfeed
		<< "butter"  << '\r'	// carriage return
		<< "friendship"
		<< std::ends;

	MemDump::dump_to_log (TEST, "Test string:",
						  oss.str ().c_str (), oss.str ().size ()-1);
	vector<string> vs;
	ASSA::Utils::split (oss.str ().c_str (), vs);

	if (vs.size () != 6) {
		set_exit_value (1);
		DL((TEST, "split() test failed\n"));
		return;
	}
	vector<string>::const_iterator cit = vs.begin ();
	while (cit != vs.end ()) {
		DL ((TEST,"%s\n", (*cit).c_str ()));
		cit++;
	}
	DL((TEST,"split() test passed\n"));

	//=================================================================
	DL((TEST,"\nTesting split_pair() ...\n"));
	string line ("ignorance=strength");
	string bad_line ("love piece");
	string lhs;
	string rhs;

	if (ASSA::Utils::split_pair (line, '=', lhs, rhs) < 0 ||
		lhs != "ignorance" || rhs != "strength")
	{
		set_exit_value (1);
		DL((TEST, "split_pair() test failed\n"));
		return;
	}

	if (ASSA::Utils::split_pair (bad_line, '=', lhs, rhs) == 0) {
		set_exit_value (1);
		DL((TEST, "split_pair() test failed\n"));
		return;
	}

	DL((TEST,"split_pair() test passed\n"));

	//=================================================================
	DL((TEST,"\nTesting [lr]trim() ...\n"));

	string sect_name ("[Default]");

	if (ASSA::Utils::ltrim (sect_name, "[") < 0 ||
		ASSA::Utils::rtrim (sect_name, "]") < 0 ||
		sect_name != "Default")
	{
		set_exit_value (1);
		DL((TEST, "[lr]trim() test failed\n"));
		DL((TEST, "\"Default\" != \"%s\"\n", sect_name.c_str ()));
		return;
	}

	DL((TEST,"[lr]trim() test passed\n"));

	//=================================================================
	DL((TEST,"\nTesting trim_sides() ...\n"));
	string option ("  mute  = \t true  ");

	if (ASSA::Utils::split_pair (option, '=', lhs, rhs) < 0) {
		set_exit_value (1);
		DL((TEST, "trim_sides() test failed\n"));
		return;
	}

	ASSA::Utils::trim_sides (lhs);
	ASSA::Utils::trim_sides (rhs);

	if (lhs != "mute" || rhs != "true") {
		set_exit_value (1);
		DL((TEST, "trim_sides() test failed\n"));
		DL((TEST, "lhs = \"%s\" (expected: \"mute\")\n", lhs.c_str ()));
		DL((TEST, "rhs = \"%s\" (expected: \"true\")\n", rhs.c_str ()));
		return;
	}


	DL((TEST,"trim_sides() test passed\n"));

	//=================================================================
	DL((TEST,"\nTesting strenv() ...\n"));

	::putenv ((char*) "AUTHOR=Vladislav Grinchenko");
	::putenv ((char*) "PROJECT=ASSA Library");

	string source ("$PROJECT is written by $AUTHOR");

	string expanded = ASSA::Utils::strenv (source.c_str ());

	if (expanded != "ASSA Library is written by Vladislav Grinchenko") {
		set_exit_value (1);
		DL((TEST, "setenv() test failed\n"));
		return;
	}

	m_default_config_file = "$HOME/.gnome/" + this->get_cmdline_name ();
	m_default_config_file = Utils::strenv (m_default_config_file.c_str ());

	string expected = ::getenv ("HOME");
	expected += "/.gnome/common_utils_test";

	if (expected != m_default_config_file) {
		set_exit_value (1);
		DL((TEST, "setenv() test failed\n"));
		DL((TEST,"expected: \"%s\"\n", expected.c_str ()));
		DL((TEST,"actual:   \"%s\"\n", m_default_config_file.c_str ()));
		return;
	}

	DL((TEST,"setenv() test passed\n"));
    m_reactor.stopReactor ();
}