~ubuntu-branches/ubuntu/quantal/mysql-workbench/quantal

« back to all changes in this revision

Viewing changes to library/grt/unit-tests/grtpp_lua_test.cpp

  • Committer: Package Import Robot
  • Author(s): Dmitry Smirnov
  • Date: 2012-03-01 21:57:30 UTC
  • Revision ID: package-import@ubuntu.com-20120301215730-o7y8av8y38n162ro
Tags: upstream-5.2.38+dfsg
ImportĀ upstreamĀ versionĀ 5.2.38+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public License as
 
6
 * published by the Free Software Foundation; version 2 of the
 
7
 * License.
 
8
 * 
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
12
 * GNU General Public License for more details.
 
13
 * 
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 
17
 * 02110-1301  USA
 
18
 */
 
19
 
 
20
#include "tut_stdafx.h"
 
21
 
 
22
#include "testgrt.h"
 
23
#include "grt_test_utility.h"
 
24
#include "grtpp_module_lua.h"
 
25
#include "structs.test.h"
 
26
 
 
27
 
 
28
BEGIN_TEST_DATA_CLASS(grtpp_lua_test)
 
29
public:
 
30
  GRT grt;
 
31
  LuaModuleLoader *loader;
 
32
END_TEST_DATA_CLASS
 
33
 
 
34
 
 
35
TEST_MODULE(grtpp_lua_test, "GRT: lua support");
 
36
 
 
37
TEST_FUNCTION(1)
 
38
{
 
39
  // init lua loader
 
40
 
 
41
  grt.add_module_loader(loader= new LuaModuleLoader(&grt));
 
42
  ensure("init lua loader", loader != NULL);
 
43
 
 
44
  grt.load_metaclasses("data/structs.test.xml");
 
45
  grt.end_loading_metaclasses();
 
46
  ensure_equals("load structs", grt.get_metaclasses().size(), 6U);
 
47
  
 
48
  ensure_equals("module count", grt.get_modules().size(), 0U);
 
49
  
 
50
  grt.load_module("../../library/grt/unit-tests/lua_module.lua", true);
 
51
  
 
52
  ensure_equals("module count after load", grt.get_modules().size(), 1U);
 
53
}
 
54
 
 
55
 
 
56
TEST_FUNCTION(2)
 
57
{
 
58
  test_BookRef object(&grt);
 
59
  LuaContext *ctx= loader->get_lua_context();
 
60
  
 
61
  ctx->push_wrap_value(object);
 
62
  
 
63
  ValueRef value= ctx->pop_value();
 
64
  ensure("value", value.is_valid());
 
65
  ensure("is object", test_BookRef::can_wrap(value));
 
66
//  ensure_equals("refcount", value.refcount(), 1);
 
67
 
 
68
  BaseListRef list(&grt);
 
69
  list.ginsert(object);
 
70
  
 
71
  ctx->push_wrap_value(list);
 
72
  value= ctx->pop_value();
 
73
  
 
74
  ensure("value", value.is_valid());
 
75
  ensure("is list", BaseListRef::can_wrap(value));
 
76
  ensure("list", value == list);
 
77
}
 
78
 
 
79
 
 
80
TEST_FUNCTION(3)
 
81
{
 
82
  test_BookRef object(&grt);
 
83
  
 
84
  LuaContext *ctx= loader->get_lua_context();
 
85
  
 
86
  ctx->push_wrap_value(object);
 
87
  
 
88
  {
 
89
    ObjectRef obj;
 
90
  
 
91
    ctx->pop_args("O", &obj);
 
92
    ensure("pop value", obj.is_valid());
 
93
    ctx->call_gc();
 
94
 
 
95
    ensure_equals("refcount", obj.refcount(), 2);
 
96
  }
 
97
  ensure_equals("final refcount", object.refcount(), 1);
 
98
}
 
99
 
 
100
 
 
101
TEST_FUNCTION(12)
 
102
{
 
103
  Module *test_module= grt.get_module("LuaModuleTest");
 
104
  ensure("get module LuaModuleTest", test_module!=NULL);
 
105
 
 
106
  BaseListRef args(&grt);
 
107
  ValueRef res;
 
108
 
 
109
  args.ginsert(IntegerRef(2));
 
110
  args.ginsert(IntegerRef(3));
 
111
  res= test_module->call_function("add2Numbers", args);
 
112
  ensure_equals("add2Numbers", (int)IntegerRef::cast_from(res), 5);
 
113
 
 
114
  IntegerListRef nlist(&grt);
 
115
 
 
116
  args= BaseListRef(&grt);
 
117
  args.ginsert(nlist);
 
118
  
 
119
  nlist.ginsert(IntegerRef(2));
 
120
  nlist.ginsert(IntegerRef(3));
 
121
  nlist.ginsert(IntegerRef(4));
 
122
  nlist.ginsert(IntegerRef(5));
 
123
 
 
124
  res= test_module->call_function("sumList", args);
 
125
 
 
126
  ensure_equals("sumList", (int)IntegerRef::cast_from(res), 14);
 
127
                             
 
128
}
 
129
 
 
130
/*
 
131
purpose:
 
132
  to avoid code duplication for checking of test_module functions like testArgInt, testArgDouble.
 
133
usage:
 
134
  TEST_FUNCTION(13)
 
135
*/
 
136
template <class GrtType, class StdType>
 
137
void test_num_arg(GRT &grt, Module *test_module, const char *func_name)
 
138
{
 
139
  BaseListRef args(&grt);
 
140
 
 
141
  GrtType v1((StdType)2.1);
 
142
  GrtType v2((StdType)3.1);
 
143
  args.ginsert(v1);
 
144
  args.ginsert(v2);
 
145
 
 
146
  check_module_function_return(test_module, func_name, args, GrtType(((StdType)v1)+((StdType)v2)));
 
147
}
 
148
 
 
149
/*
 
150
purpose:
 
151
  to avoid code duplication for checking of test_module functions like testArgIntList, testArgDoubleList.
 
152
logic description:
 
153
  create arithmethic progression of given length & pass it to mentioned functions.
 
154
usage:
 
155
  TEST_FUNCTION(13)
 
156
*/
 
157
template <class GrtType, class StdType>
 
158
void test_num_arg_list(GRT &grt, Module *test_module, const char *func_name, size_t count)
 
159
{
 
160
  BaseListRef args(&grt);
 
161
 
 
162
  Ref<GrtType> n= (StdType)1.1;
 
163
  Ref<GrtType> sum= (StdType)0;
 
164
 
 
165
  ListRef<GrtType> vlist(&grt);
 
166
  args.ginsert(vlist);
 
167
 
 
168
  for (size_t i= 0; i<count; i++)
 
169
    vlist.ginsert(Ref<GrtType>((sum= sum+(n= n+1), n)));
 
170
 
 
171
  args.ginsert(Ref<GrtType>((sum= sum+(n= n+1), n)));
 
172
 
 
173
  check_module_function_return(test_module, func_name, args, sum);
 
174
}
 
175
 
 
176
TEST_FUNCTION(13)
 
177
{ // test passing of all types
 
178
  Module *test_module= grt.get_module("LuaModuleTest");
 
179
  ensure("get module", test_module!=NULL);
 
180
 
 
181
  test_num_arg<IntegerRef, int>(grt, test_module, "testArgInt");
 
182
  test_num_arg<DoubleRef, double>(grt, test_module, "testArgDouble");
 
183
  test_num_arg_list<grt::internal::Integer, int>(grt, test_module, "testArgIntList", 4);
 
184
  test_num_arg_list<grt::internal::Double, double>(grt, test_module, "testArgDoubleList", 4);
 
185
 
 
186
  // testArgString
 
187
  {
 
188
    const std::string S1("unit-");
 
189
    const std::string S2("test");
 
190
 
 
191
    BaseListRef args(&grt, AnyType);
 
192
    args.ginsert(StringRef(S1));
 
193
    args.ginsert(StringRef(S2));
 
194
    check_module_function_return(test_module, "testArgString", args, StringRef(S1+S2));
 
195
  }
 
196
}
 
197
 
 
198
 
 
199
TEST_FUNCTION(14)
 
200
{ // test passing of all types
 
201
  Module *test_module= grt.get_module("LuaModuleTest");
 
202
 
 
203
  ensure("LuaModuleTest is not null", test_module != 0);  
 
204
  // testArgStringList
 
205
  {
 
206
    const int INDEX(2); // lua uses 1-based index
 
207
    const size_t LIST_COUNT(4);
 
208
    const std::string s[LIST_COUNT]= {"_0", "_1", "_2", "_3"};
 
209
 
 
210
    BaseListRef args(&grt);
 
211
    StringListRef list= list_from_array<grt::internal::String>(grt, s, LIST_COUNT);
 
212
    args.ginsert(list);
 
213
    args.ginsert(IntegerRef(INDEX+1));
 
214
    check_module_function_return(test_module, "testArgStringList", args, list.get(INDEX));
 
215
  }
 
216
}
 
217
 
 
218
TEST_FUNCTION(15)
 
219
{ // test passing of all types
 
220
  Module *test_module= grt.get_module("LuaModuleTest");
 
221
 
 
222
  ensure("LuaModuleTest is not null", test_module != 0);  
 
223
  // testArgObject
 
224
  {
 
225
    const char *MEMBER_NAME("name");
 
226
    const char *MEMBER_VAL("author");
 
227
    const char *S(" wrote some stuff");
 
228
    const char *OBJ_PATH("test.Author");
 
229
 
 
230
    BaseListRef args(&grt, AnyType);
 
231
    ObjectRef obj(grt.create_object<grt::internal::Object>(OBJ_PATH));
 
232
    args.ginsert(obj);
 
233
    obj.set_member(MEMBER_NAME, StringRef(MEMBER_VAL));
 
234
    args.ginsert(StringRef(S));
 
235
    check_module_function_return(test_module, "testArgObject", args, StringRef(std::string(MEMBER_VAL)+std::string(S)));
 
236
  }
 
237
}
 
238
 
 
239
TEST_FUNCTION(16)
 
240
{ // test passing of all types
 
241
  Module *test_module= grt.get_module("LuaModuleTest");
 
242
 
 
243
  ensure("LuaModuleTest is not null", test_module != 0);  
 
244
  // testArgObjectList
 
245
  {
 
246
    const char *OBJ_PATH("test.Author");
 
247
    const char *MEMBER_NAME("name");
 
248
    const size_t VAL_COUNT= 4;
 
249
    const size_t VAL_INDEX= 2;
 
250
    const std::string VA[VAL_COUNT]= {"author1", "author2", "author3", "author4"};
 
251
 
 
252
    BaseListRef args(&grt, AnyType);
 
253
    ObjectListRef list(&grt);
 
254
    args.ginsert(list);
 
255
    for (size_t n= 0; n<VAL_COUNT; n++)
 
256
    {
 
257
      ObjectRef obj(grt.create_object<grt::internal::Object>(OBJ_PATH));
 
258
      obj.set_member(MEMBER_NAME, StringRef(VA[n]));
 
259
      list.insert(obj);
 
260
    }
 
261
    args.ginsert(StringRef(VA[VAL_INDEX]));
 
262
    check_module_function_return(test_module, "testArgObjectList", args, StringRef(VA[VAL_INDEX]));
 
263
  }
 
264
}
 
265
 
 
266
TEST_FUNCTION(17)
 
267
{ // test passing of all types
 
268
  Module *test_module= grt.get_module("LuaModuleTest");
 
269
 
 
270
  ensure("LuaModuleTest is not null", test_module != 0);  
 
271
  // testArgDict
 
272
  {
 
273
    const size_t KEY_COUNT(4);
 
274
    const int INDEX(2);
 
275
    std::string k[KEY_COUNT]= {"id", "code", "name", "material"};
 
276
    std::string v[KEY_COUNT]= {"1", "001", "wrench", "metal"};
 
277
 
 
278
    BaseListRef args(&grt, AnyType);
 
279
    DictRef dict= dict_from_array<StringRef>(grt, k, v, KEY_COUNT);
 
280
    args.ginsert(dict);
 
281
    args.ginsert(StringRef(k[INDEX]));
 
282
    check_module_function_return(test_module, "testArgDict", args, StringRef(v[INDEX]));
 
283
  }
 
284
 
 
285
}
 
286
 
 
287
TEST_FUNCTION(18)
 
288
{ // test passing of all types
 
289
  Module *test_module= grt.get_module("LuaModuleTest");
 
290
 
 
291
  ensure("LuaModuleTest is not null", test_module != 0);  
 
292
  // testArgDictList
 
293
  {
 
294
    const size_t DICT_COUNT(4);
 
295
    const size_t KEY_COUNT(4);
 
296
    const size_t SEARCHED_DICT(1);
 
297
    const size_t NAME_KEY(2);
 
298
    std::string k[KEY_COUNT]= {"id","code","name","material"};
 
299
    std::string v[DICT_COUNT][KEY_COUNT]= {
 
300
      {"1","001","fork","metal"},
 
301
      {"2","002","ball","rubber"},
 
302
      {"3","003","table","wood"},
 
303
      {"4","004","window","glass"}};
 
304
 
 
305
    BaseListRef args(&grt, AnyType);
 
306
    DictRef dict_arr[DICT_COUNT];
 
307
    BaseListRef list(&grt);
 
308
    for (size_t n= 0; n<DICT_COUNT; n++)
 
309
    {
 
310
      dict_arr[n]= dict_from_array<StringRef>(grt, k, v[n], KEY_COUNT);
 
311
      list.ginsert(dict_arr[n]);
 
312
    }
 
313
    args.ginsert(list);
 
314
    args.ginsert(StringRef(v[SEARCHED_DICT][NAME_KEY]));
 
315
    check_module_function_return(test_module, "testArgDictList", args, DictRef(dict_arr[SEARCHED_DICT]));
 
316
  }
 
317
}
 
318
 
 
319
/*
 
320
usage:
 
321
  TEST_FUNCTION(20)
 
322
*/
 
323
void test_return_of_all_types(GRT& grt, Module* test_module, BaseListRef& args, bool grt_value_return)
 
324
{
 
325
  while (args.count())
 
326
    args.remove(0);
 
327
  args.ginsert(IntegerRef(grt_value_return?0:1));
 
328
 
 
329
  // testRetAny
 
330
  {
 
331
    int v[]= {grt_value_return?123456:12345};
 
332
    check_module_function_return(test_module, "testRetAny", args, list_from_array<grt::internal::Integer>(grt, v, 1));
 
333
  }
 
334
  check_module_function_return(test_module, "testRetInt", args, IntegerRef(123));
 
335
  check_module_function_return(test_module, "testRetDouble", args, DoubleRef(123.456));
 
336
  check_module_function_return(test_module, "testRetString", args, StringRef("hello"));
 
337
 
 
338
  check_module_function_return(test_module, "testRetObject", args, grt.create_object<grt::internal::Object>("test.Book"));
 
339
 
 
340
  // testRetDict
 
341
  {
 
342
    const size_t KEY_COUNT(3);
 
343
    std::string k1[KEY_COUNT]= {"k1","k2","k3"};
 
344
    std::string k0[KEY_COUNT]= {"key1","key2","key3"};
 
345
    std::string *k(grt_value_return?k0:k1);
 
346
    int v[KEY_COUNT]= {1,2,3};
 
347
 
 
348
    DictRef dict= dict_from_array<IntegerRef>(grt, k, v, KEY_COUNT);
 
349
    check_module_function_return(test_module, "testRetDict", args, dict);
 
350
  }
 
351
 
 
352
  // testRetList
 
353
  {
 
354
    BaseListRef list(&grt, AnyType);
 
355
    list.ginsert(IntegerRef(grt_value_return?2:1));
 
356
    list.ginsert(DoubleRef(grt_value_return?3.1:2.1));
 
357
    list.ginsert(StringRef(grt_value_return?"four":"three"));
 
358
    check_module_function_return(test_module, "testRetList", args, list);
 
359
  }
 
360
 
 
361
  // testRetIntList
 
362
  {
 
363
    const size_t LIST_COUNT(4);
 
364
    int v1[LIST_COUNT]= {1,2,3,4};
 
365
    int v0[LIST_COUNT]= {2,3,4,5};
 
366
    int *v(grt_value_return?v0:v1);
 
367
 
 
368
    IntegerListRef list= list_from_array<grt::internal::Integer>(grt, v, LIST_COUNT);
 
369
    check_module_function_return(test_module, "testRetIntList", args, list);
 
370
  }
 
371
 
 
372
  // testRetDoubleList
 
373
  {
 
374
    const size_t LIST_COUNT(4);
 
375
    double v1[]= {1.1, 2.2, 3.3, 4.4};
 
376
    double v0[]= {2.2, 3.3, 4.4, 5.5};
 
377
    double *v(grt_value_return?v0:v1);
 
378
 
 
379
    DoubleListRef list= list_from_array<grt::internal::Double>(grt, v, LIST_COUNT);
 
380
    check_module_function_return(test_module, "testRetDoubleList", args, list);
 
381
  }
 
382
 
 
383
  // testRetStringList
 
384
  {
 
385
    const size_t LIST_COUNT(4);
 
386
    std::string v1[]= {"one","two","three","four"};
 
387
    std::string v0[]= {"two","three","four","five"};
 
388
    std::string *v(grt_value_return?v0:v1);
 
389
 
 
390
    StringListRef list= list_from_array<grt::internal::String>(grt, v, LIST_COUNT);
 
391
    check_module_function_return(test_module, "testRetStringList", args, list);
 
392
  }
 
393
 
 
394
  // testRetObjectList
 
395
  {
 
396
    const char *OBJ_PATH("test.Book");
 
397
    const char *MEMBER_NAME("title");
 
398
    const size_t VAL_COUNT(3);
 
399
    const StringRef VA[VAL_COUNT]= {"Book1", "Book2", "Book3"};
 
400
 
 
401
    ObjectListRef list(&grt);
 
402
    for (size_t n= 0; n<VAL_COUNT; n++)
 
403
    {
 
404
      list.insert(grt.create_object<grt::internal::Object>(OBJ_PATH));
 
405
      list.get(n).set_member(MEMBER_NAME, VA[n]);
 
406
    }
 
407
    check_module_function_return(test_module, "testRetObjectList", args, list);
 
408
  }
 
409
 
 
410
  // testRetDictList
 
411
  {
 
412
    const size_t DICT_COUNT(2);
 
413
    const size_t KEY_COUNT(2);
 
414
    std::string k1[KEY_COUNT]= {"k1","k2"};
 
415
    std::string k0[KEY_COUNT]= {"key1","key2"};
 
416
    std::string *k(grt_value_return?k0:k1);
 
417
    int v[DICT_COUNT][KEY_COUNT]= {
 
418
      {1,2},
 
419
      {11,22}};
 
420
 
 
421
    DictRef dict_arr[DICT_COUNT];
 
422
    BaseListRef list(&grt);
 
423
    for (size_t n= 0; n<DICT_COUNT; n++)
 
424
    {
 
425
      dict_arr[n]= dict_from_array<IntegerRef>(grt, k, v[n], KEY_COUNT);
 
426
      list.ginsert(dict_arr[n]);
 
427
    }
 
428
    check_module_function_return(test_module, "testRetDictList", args, list);
 
429
  }
 
430
}
 
431
 
 
432
TEST_FUNCTION(20)
 
433
{ // test return of all types
 
434
  Module *test_module= grt.get_module("LuaModuleTest");
 
435
  ensure("get module", test_module!=NULL);
 
436
  BaseListRef args(&grt, AnyType);
 
437
 
 
438
  test_return_of_all_types(grt, test_module, args, false);
 
439
  test_return_of_all_types(grt, test_module, args, true);
 
440
}
 
441
 
 
442
 
 
443
TEST_FUNCTION(21)
 
444
{
 
445
  // test error catching
 
446
  Module *test_module= grt.get_module("LuaModuleTest");
 
447
  ensure("get module", test_module!=NULL);
 
448
 
 
449
  BaseListRef args(&grt);
 
450
  ValueRef res;
 
451
 
 
452
  args.ginsert(IntegerRef(2));
 
453
  args.ginsert(IntegerRef(3));
 
454
  try {
 
455
    res= test_module->call_function("errorFunc", args);
 
456
    ensure("lua module error handling", false);
 
457
  } catch (grt::module_error&) {
 
458
    // this is the correct exception to be raised
 
459
  } catch (...) {
 
460
    ensure("lua module error exception", false);
 
461
  }
 
462
}
 
463
 
 
464
 
 
465
TEST_FUNCTION(22)
 
466
{
 
467
  // some unit tests written in lua
 
468
  Module *test_module= grt.get_module("LuaModuleTest");
 
469
  ensure("LuaModuleTest is not null", test_module != 0);
 
470
  BaseListRef args(&grt, AnyType);  
 
471
 
 
472
  ValueRef res= test_module->call_function("doLuaUnitTests", args);
 
473
 
 
474
  ensure_equals("lua unit tests", IntegerRef::cast_from(res), 0);
 
475
}
 
476
 
 
477
 
 
478
END_TESTS