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
  
367
  
368
  
369
  
370
  
371
  
372
  
373
  
374
  
375
  
376
  
377
  
378
  
379
  
380
  
381
  
382
  
383
  
384
  
385
  
386
  
387
  
388
  
389
  
390
  
391
  
392
  
393
  
394
  
395
  
396
  
397
  
398
  
399
  
400
  
401
  
402
  
403
  
404
  
405
  
406
  
407
  
408
  
409
  
410
  
411
  
412
  
413
  
414
  
415
  
416
  
417
  
418
  
419
  
420
  
421
  
422
  
423
  
424
  
425
  
426
  
427
  
428
  
429
  
430
  
431
  
432
  
433
  
434
  
435
  
436
  
437
  
438
  
439
  
440
  
441
  
442
  
443
  
444
  
445
  
446
  
447
  
448
  
449
  
450
  
451
  
452
  
453
  
454
  
455
  
456
  
457
  
458
  
459
  
460
  
461
  
462
  
463
  
464
  
465
  
466
  
467
  
468
  
469
  
470
  
471
  
472
  
473
  
474
  
475
  
int current_test, tests_failed; 
int verbose; 
 
constant single_thread = 0; 
// If this constant is set then the test will run in the backend 
// thread while all handler threads are on hold (which implies the 
// background_run queue is on hold as well). Otherwise tests are run 
// in one handler thread while another one is free to execute 
// background jobs right away. 
 
void create( int vb ) { verbose = vb; } 
 
 
string describe_test (function|string cb, array args) 
{ 
  if (!stringp (cb)) { 
    object describer = master()->Describer(); 
    return describer->describe (cb) + 
      "(" + describer->describe_comma_list (args, 512) + ")"; 
  } 
  if (sizeof (args)) 
    catch {return sprintf (cb, @args);}; 
  return cb; 
} 
 
string pad_to_column (string str, int col, string cont_prefix) 
{ 
  array(string) split = str / "\n"; 
  if (sizeof (split) > 1 && split[-1] == "") split = split[..<1]; 
  if (sizeof (split[-1]) > col) split += ({""}); 
  split[-1] += " " * (col - sizeof (split[-1])); 
  return split * ("\n" + cont_prefix); 
} 
 
void report_1st(function|string|array cb, array args, function check ) 
{ 
  if( !verbose ) 
    return; 
  int checkid = ' '; 
  if( check == check_error ) 
    checkid = '#'; 
  else if( check == check_false ) 
    checkid = '!'; 
  else if( check != check_is_configuration && 
           check == check_is_module ) 
    checkid = '~'; 
 
  if (arrayp (cb)) 
    // Got line number info. 
    report_error ("%3d  %s:%d:\n" 
                  "   %c %s  ", 
                  current_test, cb[0], cb[1], checkid, 
                  pad_to_column (describe_test (cb[2], args), 
                                 66, sprintf ("   %c ", checkid))); 
  else 
    report_error("%3d%c %s  ", current_test, checkid, 
                 pad_to_column (describe_test (cb, args), 
                                66, sprintf ("   %c ", checkid))); 
} 
 
string indent( int l, string what ) 
{ 
  array q = what/"\n"; 
  int trailing_nl = q[-1] == ""; 
  if (trailing_nl) q = q[..<1]; 
  string i = (" "*l+"|  "); 
  return i+q*("\n"+i) + (trailing_nl ? "\n" : ""); 
} 
 
void log (string msg, mixed... args) 
{ 
  if (sizeof (args)) msg = sprintf (msg, @args); 
  report_debug (indent (2, msg)); 
} 
 
void log_verbose (string msg, mixed... args) 
{ 
  if (!verbose) return; 
  if (sizeof (args)) msg = sprintf (msg, @args); 
  report_debug (indent (2, msg)); 
} 
 
string do_describe_error( mixed err ) 
{ 
  if (!stringp (err)) err = describe_backtrace (err); 
  if (has_suffix (err, "\n")) err = err[..<1]; 
  return indent(2, err) + "\n"; 
} 
 
void report_test_failure( mixed err, 
                          function|string|array cb, array args, int st ) 
{ 
  if( verbose )  
    report_debug(" ################ FAILED\n"); 
  else { 
    if (arrayp (cb)) { 
      // Got line number info. 
      report_debug (indent (2, sprintf ("################ %s:%d:   FAILED", 
                                        cb[0], cb[1])) + "\n" + 
                    do_describe_error(describe_test (cb[2], args))); 
    } 
    else 
      report_debug(indent (2, "################ " + 
                           describe_test (cb, args) + "   FAILED\n")); 
  } 
 
  if( err ) 
    report_debug( do_describe_error( err ) ); 
  report_debug ("\n"); 
  tests_failed++; 
} 
 
 
void report_test_ok( mixed err, function|string|array cb, array args, int st ) 
{ 
  if( verbose ) 
  { 
    int tt = (gethrtime()-st); 
    if( tt > 200000 ) 
      report_debug(" %4dms\n", tt/1000); 
    else 
      report_debug( "   PASS\n" ); 
    //   if( err ) report_error( do_describe_error( err ) ); 
  } 
} 
 
private mixed test_generic_low( function check_return, 
                                function|array cb, 
                                bool throw_error, 
                                mixed ... args ) 
{ 
  current_test++; 
  mixed result; 
  report_1st( cb, args, check_return ); 
  int st = gethrtime(); 
  function test_fn = arrayp (cb) ? cb[2] : cb; 
  mixed err = catch { 
    result = test_fn( @args ); 
  }; 
  if( err && throw_error ) { 
    report_test_failure( err, cb, args, st ); 
    throw( err ); 
  } 
  // Not all check-functions support the extra throw_error arg. 
  if ( check_return && throw_error ) { 
    check_return( result, err, cb, args,st, throw_error); 
  } 
  else if( check_return ) 
    check_return( result, err, cb, args,st ); 
  else if( err ) 
    report_test_failure( err, cb, args,st ); 
  else 
    report_test_ok( err, cb, args,st ); 
  return result; 
} 
 
mixed test_generic( function check_return, function|array cb, mixed ... args ) 
{ 
  return test_generic_low( check_return, cb, false, @args ); 
} 
 
mixed assert_generic( function check_return, function|array cb, mixed ... args ) 
{ 
  return test_generic_low( check_return, cb, true, @args ); 
} 
 
mixed test_really_generic( function check_return, function(void:mixed) test_fn, 
                           string|array test_text, array test_text_args, 
                           bool|void throw_error ) 
{ 
  current_test++; 
  mixed result; 
  int st = gethrtime(); 
  mixed err = catch { 
      result = test_fn(); 
  }; 
 
  // Write out the test after running it, since the macros change 
  // test_text_args in the test. 
  report_1st( test_text, test_text_args, check_return ); 
 
  if( err && throw_error ) { 
    report_test_failure( err, test_text, test_text_args, st ); 
    throw( err ); 
  } 
  if( check_return && throw_error ) 
     check_return( result, err, test_text, test_text_args, st, throw_error ); 
  else if( check_return ) 
    check_return( result, err, test_text, test_text_args,st ); 
  else if( err ) 
    report_test_failure( err, test_text, test_text_args,st ); 
  else 
    report_test_ok( err, test_text, test_text_args,st ); 
  return result; 
} 
 
 
void check_error( mixed res, mixed err, 
                  function|string|array cb, array args, int st, 
                  bool|void throw_error ) 
{ 
  if( err ) { 
    report_test_ok( err, cb, args, st ); 
  } 
  else { 
    report_test_failure( "Expected error", cb, args, st );  
    if( throw_error ) { 
      error( "Expected error but none was thrown.\n" ); 
    } 
  } 
} 
 
private void assert_throws_error( mixed res, mixed err, 
                                 function|string|array cb, array args, int st ) 
{ 
  check_error( res, err, cb, args, st, true ); 
} 
 
void check_is_module( mixed res, mixed err, 
                      function|string|array cb, array args, int st ) 
{ 
  if( err ) 
    report_test_failure( err, cb, args, st ); 
  else 
    if( !objectp(res) || !res->is_module || !res->my_configuration() ) 
      report_test_failure( sprintf("Got %O, expected module", res),cb,args, 
                         st); 
    else 
      report_test_ok( err, cb, args, st ); 
} 
 
void check_is_configuration( mixed res, mixed err, 
                             function|string|array cb, array args, int st) 
{ 
  if( err ) 
    report_test_failure( err, cb, args, st ); 
  else 
    if( !objectp(res) || !res->is_configuration ) 
      report_test_failure( sprintf("Got %O, expected configuration", res),cb,args, st); 
    else 
      report_test_ok( err, cb, args, st ); 
} 
 
void silent_check_true( mixed res, mixed err, 
                        function|string|array cb, array args, int st, 
                        bool|void throw_error ) 
{ 
  if( err || !res ) { 
    report_test_failure( err, cb, args, st ); 
    if( throw_error ) { 
      if( err ) { 
        throw( err ); 
      } else { 
        error( "Condition failed.\n" ); 
      } 
    } 
  } else { 
    report_test_ok( 0, cb, args, st ); 
  } 
} 
 
void check_true( mixed res, mixed err, 
                 function|string|array cb, array args, int st, 
                 bool|void throw_error ) 
{ 
  if( err ) { 
    report_test_failure( err, cb, args, st ); 
    if( throw_error ) { 
      throw( err ); 
    } 
  } 
  else if( !res ) { 
    report_test_failure( sprintf ("expected non-zero, got %O", res), 
                         cb, args, st); 
    if( throw_error ) { 
      error( sprintf ("expected non-zero, got %O\n", res) ); 
    } 
  } else { 
    report_test_ok( err, cb, args, st ); 
  } 
} 
 
private void assert_result_true( mixed res, mixed err, 
                                 function|string|array cb, array args, int st ) 
{ 
  check_true(res, err, cb, args, st, true); 
} 
 
 
void check_false( mixed res, mixed err, 
                  function|string|array cb, array args, int st, 
                  bool|void throw_error ) 
 
{ 
  if( err ) { 
    report_test_failure( err, cb, args, st ); 
    if( throw_error ) { 
      throw( err ); 
    } 
  } 
  else 
    if( res ) { 
      report_test_failure( sprintf("expected zero, got %O",res), cb, args, st); 
      if( throw_error ) { 
        error( sprintf("expected zero, got %O\n", res) ); 
      } 
    } 
    else 
      report_test_ok( err, cb, args, st ); 
} 
 
private void assert_result_false( mixed res, mixed err, 
                                  function|string|array cb, array args, int st ) 
{ 
  check_false( res, err, cb, args, st, true ); 
} 
 
function check_is( mixed m ) 
{ 
  return 
    lambda( mixed res, mixed err, function|string|array cb, array args, int st ) 
    { 
      if( err ) 
        report_test_failure( err, cb, args, st ); 
      else 
        if( res != m ) 
          report_test_failure(sprintf("Got %O, expected %O", res,m), 
                              cb,args,st); 
        else 
          report_test_ok( err, cb, args, st ); 
    }; 
} 
 
mixed pass( mixed arg ) 
{ 
  return arg; 
} 
 
function check_equal( mixed m ) 
{ 
  return 
    lambda( mixed res, mixed err, function|string|array cb, array args, int st, 
            bool|void throw_error ) 
    { 
      if( err ) { 
        report_test_failure( err, cb, args, st ); 
        if( throw_error ) { 
          throw( err ); 
        } 
      } else if( !equal( res, m )) { 
        string message = sprintf("Got %O, expected %O.\n", res,m); 
        report_test_failure( message, cb, args, st ); 
        if( throw_error ) { 
          error( message ); 
        } 
      } else { 
        report_test_ok( err, cb, args, st ); 
      } 
    }; 
} 
 
function check_not_equal( mixed m ) 
{ 
  return 
    lambda( mixed res, mixed err, function|string|array cb, array args, int st, 
            bool|void throw_error ) 
    { 
      if( err ) { 
        report_test_failure( err, cb, args, st ); 
        if( throw_error ) { 
          throw( err ); 
        } 
      } else if( equal( res, m )) { 
        string message = sprintf("Got %O, expected different value.\n", res); 
        report_test_failure( message, cb, args, st ); 
        if( throw_error ) { 
          error( message ); 
        } 
      } else { 
        report_test_ok( err, cb, args, st ); 
      } 
    }; 
} 
 
mixed cpp_test_true (string file, int line, function(void:mixed) test_fn, 
                     string test_text, array test_text_args) 
{ 
  return test_really_generic (silent_check_true, test_fn, 
                              ({file, line, test_text}), test_text_args); 
} 
 
mixed cpp_assert_true (string file, int line, function(void:mixed) test_fn, 
                       string test_text, array test_text_args) 
{ 
  return test_really_generic (silent_check_true, test_fn, 
                              ({file, line, test_text}), test_text_args, true); 
} 
 
mixed test( function|array f, mixed ... args ) 
{ 
  return test_generic( 0, f, @args ); 
} 
 
mixed assert( function|array f, mixed ... args ) 
{ 
  return assert_generic( 0, f, @args ); 
} 
 
mixed test_true( function|array f, mixed ... args ) 
{ 
  return test_generic( check_true, f, @args ); 
} 
 
mixed assert_true( function|array f, mixed ... args ) 
{ 
  return assert_generic( assert_result_true, f, @args ); 
} 
 
mixed test_false( function|array f, mixed ... args ) 
{ 
  return test_generic( check_false, f, @args ); 
} 
 
mixed assert_false( function|array f, mixed ... args ) 
{ 
  return assert_generic( assert_result_false, f, @args ); 
} 
 
mixed test_error( function|array f, mixed ... args ) 
{ 
  return test_generic( check_error, f, @args ); 
} 
 
mixed assert_error( function|array f, mixed ... args ) 
{ 
  return assert_generic( assert_throws_error, f, @args ); 
} 
 
mixed test_equal( mixed what, function|array f, mixed ... args ) 
{ 
  return test_generic( check_equal( what ), f, @args ); 
} 
 
mixed assert_equal( mixed what, function|array f, mixed ... args ) 
{ 
  return assert_generic( check_equal( what ), f, @args ); 
} 
 
mixed test_not_equal( mixed what, function|array f, mixed ... args ) 
{ 
  return test_generic( check_not_equal( what ), f, @args ); 
} 
 
mixed assert_not_equal( mixed what, function|array f, mixed ... args ) 
{ 
  return test_generic( check_not_equal( what ), f, @args ); 
} 
 
void run_tests( Configuration c ); 
 
void low_run_tests( Configuration c, 
                    function go_on ) 
{ 
  mixed err = catch { 
    run_tests( c ); 
  }; 
  if( err ) { 
    write( "################ " + describe_backtrace( err ) ); 
    go_on (++current_test, ++tests_failed); 
  } 
  else 
    go_on (++current_test, tests_failed); 
}