File: test_helpers.h

package info (click to toggle)
mysql-gui-tools 5.0r14%2BopenSUSE-2.1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 116,956 kB
  • ctags: 48,715
  • sloc: sql: 341,918; pascal: 276,698; ansic: 91,020; cpp: 90,451; objc: 33,236; sh: 29,481; yacc: 10,756; xml: 10,589; java: 10,079; php: 2,806; python: 2,092; makefile: 1,783; perl: 4
file content (366 lines) | stat: -rw-r--r-- 10,834 bytes parent folder | download
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#ifndef _TEST_HELPERS_H_
#define _TEST_HELPERS_H_

#include <vector>
#include <list>
#include <stdexcept>
#include <fstream>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

#include <my_global.h>
#include <mysql.h>

// needed for class Auto_release
#include "myx_public_interface.h"

/*
#define ENUM_SIZE_EQUALS_INT_SIZE(enm) (sizeof(enm) == sizeof(int))

#if !ENUM_SIZE_EQUALS_INT_SIZE(library_error)
#error sizeof(enum) < sizeof(int)
#endif
*/

/*
  Global_test_parameters keeps global test environment runtime parameters
  such as test serer connection information
*/

class Global_test_parameters
{
public:
  // result output mode
  enum Output_mode { DEBUGGER, CONSOLE };

private:
  // connection parameters
  const char *host_name;
  short port;
  const char *user_name;
  const char *password;
  std::vector <std::string> pathes;

  // reporting parameters
  bool rep_passed;
  Output_mode out_mode;
  bool wait_on_finish;

  void print_usage(const char *exe_name);
public:
  Global_test_parameters(int argc, char *argv[]);
  ~Global_test_parameters();

  std::string get_full_filename(const std::string& name);
  const char *get_host_name() const;
  short get_port() const;
  const char *get_user_name() const;
  const char *get_password() const;
  bool report_passed() const;
  Output_mode get_output_mode() const;
  bool wait() { return wait_on_finish; };
};

class Resultset;

/*
  Test_connection encapsulates one active test server connection.
  This class serves as factory for result set instances.
*/

class Test_connection
{
  MYSQL *mysql;

public:
   Test_connection(const char *host, short port, const char *user, const char *pass);
  ~Test_connection();

  // Try to avoid using get_mysql() if that's possible. if you lack a function that uses server connection
  // consider adding it to the Test_connection class.
  MYSQL* get_mysql(void) { return mysql; };

  void query(const std::string& query);

  // In multi_query the query string can contain several delimited statements and DELIMITER statements.
  void multi_query(std::istream& is);
  void multi_query(const std::string& q);
  Resultset* new_resultset(const std::string& query);
};

class Resultset
{
  std::vector<char**> rows;
  MYSQL_RES *res;
  int column_count;
  MYSQL_FIELD* fields;
  bool is_active;

  friend bool operator == (const Resultset &rs1, const Resultset &rs2);

protected:
  bool fetch_resultset();

public:
  Resultset(MYSQL_RES *resultset);
  ~Resultset();

  bool compare_to(const Resultset &resultset);
  char *escape(const char *str);
  int get_column_count(void) {return column_count; };
  MYSQL_FIELD* get_column_fields(void) {return fields; };
  bool load(const std::string &file);
  bool save(const std::string &file);
  char *unescape(const char *str);
};

// Every TEST_FUNCTION is a member of a Test_object_base-derived class instance. The class could contain any shared info.
//  Note: template class is fake, it's just to get different template specializations.
namespace tut
{

template<class> class Test_object_base
{
};

}

// Test_group class encapsulates testgroup-wide information. For example it keeps a server connection
// which can be shared among the tests in group.
template<class module>
class Test_group : public tut::test_group<tut::Test_object_base<module> >
{
  Test_connection *c_;
  Global_test_parameters **params_;

  void inc_if_disabled();

public:
  Test_group(Global_test_parameters **params, const char* name, const char *desc)
    : tut::test_group<tut::Test_object_base<module> >(name), c_(NULL), params_(params)
  {
    inc_if_disabled();
  }

  virtual ~Test_group()
  {
    delete c_;
  }

  Test_connection *get_connection()
  {
    if (c_ == NULL)
    {
      c_= new Test_connection(
        (*params_)->get_host_name(),
        (*params_)->get_port(),
        (*params_)->get_user_name(),
        (*params_)->get_password());
    }
    return c_;
  }

  std::string get_full_filename(const std::string& name)
  {
    return (*params_)->get_full_filename(name);
  }

  bool compare_files(const std::string& created, const std::string& reference)
  {
    std::fstream file1(created.c_str(), std::ios::in | std::ios::binary);
    std::string name2 = (*params_)->get_full_filename(reference);
    std::fstream file2(name2.c_str(), std::ios::in | std::ios::binary);
    int line_number= 0;
    std::string line1, line2;

    while (!file1.eof() && !file2.eof()) {

      line_number++;
      getline(file1, line1, '\n');
      getline(file2, line2, '\n');

      // Remove a carriage return character if there is one at the end.
      if (line1[line1.length() - 1] == '\r')
         line1.erase(line1.end() - 1);
      if (line2[line2.length() - 1] == '\r')
         line2.erase(line2.end() - 1);

      // Now compare the lines.
      if (line1 != line2) {
        std::cout << "Files differ at line " << line_number << '\n';
        return false;
      }
    }

    if (!file1.eof() || !file2.eof()) {
      std::cout << "Files have different line count.\n";
      return false;
    }
    return true;
  }

  bool compare_files_old(const std::string& created, const std::string& reference)
  {
    std::fstream file1(created.c_str(), std::ios::in | std::ios::binary);
    std::string name2 = (*params_)->get_full_filename(reference);
    std::fstream file2(name2.c_str(), std::ios::in | std::ios::binary);
    int line= 0;

    bool result= true;
    if (file1 && file2)
    {
      unsigned char *data1= new char[200000];
      unsigned char *data2= new char[200000];
      int current_offset= 0;

      while (!file1.eof() && !file2.eof())
      {
        file1.read(data1, 200000);
        int size1= file1.gcount();
        file2.read(data2, 200000);
        int size2= file2.gcount();
        if (size1 != size2)
        {
          int count= size1 < size2 ? size1 : size2;
          int i= 0;
          while ((count > 0) && (data1[i] == data2[i]))
          {
            --count;
            ++i;
          };

          int point= current_offset + i;
          std::cout << "Data differs at position: " << point << '\n';
          result= false;
          break;
        }
        else
        {

          int i= 0;
          while ((size1 > 0) && (data1[i] == data2[i]))
          {
            --size1;
            ++i;
          };
          if (size1 > 0)
          {
            int point= current_offset + i;
            std::cout << "Data differs at position: " << point << '\n';
            result= false;
            break;
          };
        };

        current_offset += size1;
      };

      delete[] data1;
      delete[] data2;
    }
    else
      result= false;
      
    if (!file1.eof() || !file2.eof())
      result= false;

    file1.close();
    file2.close();
    
    return result;
  }
};

template<class module> void Test_group<module>::inc_if_disabled()
{
  set_disabled(false);
}

#ifdef _WINDOWS
template<> void Test_group<void>::inc_if_disabled()
{
  inc_disabled_groups_count();
  set_disabled(true);
}
#else
template<> void Test_group<void>::inc_if_disabled();
#endif

// Auto_release can be used to automatically free dynamically allocated objects. The usage pattern is the following:
//
// void f()
// {
//   Auto_release ar;
//   char *c= ar.add_g_free((char *)g_malloc(1000));
// }
//
// now c will be automatically freed (g_free-d in this case) when ar goes out of scope.
//
// At the moment only a limited set of datatypes is supported, so feel free to extend the class as needed.

class Auto_release
{
  std::list<char *> pointers;
  std::list<char *> cpp_pointers;
  std::list<MYX_DBM_SERVER_VERSION *> versions;
  std::list<MYX_DBM_TABLE_DATA *> tabledatas;
  std::list<MYX_DBM_DATATYPES *> datatypes;
  std::list<MYX_SCHEMA_STORED_PROCEDURES *> schema_sps;
  std::list<MYX_RESULTSET *> resultsets;
  std::list<MYX_TABLE_EXPORTER_INFO *> expinfos;
  std::list<MYX_ENGINES *> engines;

  typedef std::pair<Test_connection *, std::string> Connection_sql_pair;

  std::list<Connection_sql_pair> sql_list;

public:

  char *add_g_free(char *p) { pointers.push_back(p); return p; }
  char *add_delete(char *p) { cpp_pointers.push_back(p); return p; }
  MYX_DBM_SERVER_VERSION *add(MYX_DBM_SERVER_VERSION *v) { versions.push_back(v); return v; }
  MYX_DBM_TABLE_DATA *add(MYX_DBM_TABLE_DATA *d) { tabledatas.push_back(d); return d; }
  MYX_DBM_DATATYPES *add(MYX_DBM_DATATYPES *t) { datatypes.push_back(t); return t; }
  MYX_SCHEMA_STORED_PROCEDURES *add(MYX_SCHEMA_STORED_PROCEDURES *sp) { schema_sps.push_back(sp); return sp; }
  MYX_RESULTSET *add(MYX_RESULTSET *rs) { resultsets.push_back(rs); return rs; }
  MYX_TABLE_EXPORTER_INFO *add(MYX_TABLE_EXPORTER_INFO *info) { expinfos.push_back(info); return info; }
  MYX_ENGINES *add(MYX_ENGINES *data) { engines.push_back(data); return data; }

  void add_sql(Test_connection *conn, const char *filename) { sql_list.push_back(Connection_sql_pair(conn, std::string(filename))); }


  // TODO: check for NULL before passing to myx_free_*
  ~Auto_release()
  {
    for(std::list<char *>::iterator it= pointers.begin(); it != pointers.end(); it++)
      g_free(*it);
    for(std::list<char *>::iterator it= cpp_pointers.begin(); it != cpp_pointers.end(); it++)
      delete *it;
    for(std::list<MYX_DBM_SERVER_VERSION *>::iterator it= versions.begin(); it != versions.end(); it++)
      myx_dbm_free_server_version(*it);
    for(std::list<MYX_DBM_TABLE_DATA *>::iterator it= tabledatas.begin(); it != tabledatas.end(); it++)
      myx_dbm_free_table_data(*it);
    for(std::list<MYX_DBM_DATATYPES *>::iterator it= datatypes.begin(); it != datatypes.end(); it++)
      if (*it) myx_free_datatype(*it);
    for(std::list<MYX_SCHEMA_STORED_PROCEDURES *>::iterator it= schema_sps.begin(); it != schema_sps.end(); it++)
      myx_free_schema_sps(*it);
    for(std::list<MYX_RESULTSET *>::iterator it= resultsets.begin(); it != resultsets.end(); it++)
      myx_query_free_resultset(*it);
    for(std::list<MYX_TABLE_EXPORTER_INFO *>::iterator it= expinfos.begin(); it != expinfos.end(); it++)
      myx_free_table_exporter_info(*it);

    for(std::list<Connection_sql_pair>::iterator it= sql_list.begin(); it != sql_list.end(); it++)
    {
      Connection_sql_pair pair= *it;
      pair.first->multi_query(pair.second);
    }

    for(std::list<MYX_ENGINES *>::iterator it= engines.begin(); it != engines.end(); it++)
      if (*it) myx_free_engines(*it);
  }
};

#endif // _TEST_HELPERS_H_