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
  
int current_test, tests_failed; 
int verbose = 1; 
 
string describe_arglist( array args ) 
{ 
  array res = ({}); 
  foreach( args, mixed arg ) 
    if( mappingp(arg) || arrayp(arg) ) 
      res+=({sprintf("%t<%d>",arg,sizeof(arg))}); 
    else if( objectp( arg ) ) 
      if( arg->is_module ) 
        res += ({ sprintf("%s",arg->my_configuration()->otomod[arg])}); 
      else if( arg->is_configuration ) 
        res += ({ sprintf("%s", arg->name ) }); 
      else 
        res += ({ sprintf("%O", arg ) }); 
    else 
      res+=({sprintf("%O",arg)}); 
  return res * ", "; 
} 
 
void report_1st(function cb, array args, function check ) 
{ 
  int checkid = ' '; 
  if( check == check_error ) 
    checkid = '#'; 
  else if( check == check_false ) 
    checkid = '!'; 
  else if( check != check_is_configuration && 
           check == check_is_module ) 
    checkid = '~'; 
 
  report_error("  Test %3d %c%-40s  ", current_test, 
               checkid,sprintf("%O("+describe_arglist( args )+")",cb)[..39] 
               ); 
} 
 
string do_describe_error( mixed err ) 
{ 
  if( stringp( err ) ) 
    return err + (strlen(err)?(err[-1] == '\n' ? "": "\n" ):""); 
  err = (array)err; 
  err[1] = err[1][sizeof(err[1])-3..]; 
  return describe_backtrace( err ); 
} 
 
void report_test_failure( mixed err, function cb, array args, int st ) 
{ 
  report_error("FAILED\n"); 
  if( err ) report_error( do_describe_error( err ) ); 
  tests_failed++; 
} 
 
 
void report_test_ok( mixed err, function cb, array args, int st ) 
{ 
  report_error("OK [%dms]\n", (gethrtime()-st)/1000); 
//   if( err ) report_error( do_describe_error( err ) ); 
} 
 
 
mixed do_test( function check_return, function cb, mixed ... args ) 
{ 
  current_test++; 
  mixed result; 
  report_1st( cb, args, check_return ); 
  int st = gethrtime(); 
  mixed err = catch { 
    result = cb( @args ); 
  }; 
  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; 
} 
 
 
void check_error( mixed res, mixed err, function cb, array args,int st ) 
{ 
  if( err ) 
    report_test_ok( err, cb, args, st ); 
  else 
    report_test_failure( "Expected error", cb, args, st );  
} 
 
void check_is_module( mixed res, mixed err, function 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 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 check_true( mixed res, mixed err, function cb, array args, int st ) 
{ 
  if( err ) 
    report_test_failure( err, cb, args, st ); 
  else 
    if( !res ) 
      report_test_failure( "expected non-zero", cb, args, st); 
    else 
      report_test_ok( err, cb, args, st ); 
} 
 
void check_false( mixed res, mixed err, function cb, array args, int st ) 
{ 
  if( err ) 
    report_test_failure( err, cb, args, st ); 
  else 
    if( res ) 
      report_test_failure( sprintf("expected zero, got %O",res), cb, args, st); 
    else 
      report_test_ok( err, cb, args, st ); 
} 
 
 
function check_is( mixed m ) 
{ 
  return 
    lambda( mixed res, mixed err, function 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 cb, array args, int st ) 
    { 
      if( err ) 
        report_test_failure( err, cb, args, st ); 
      else 
        if( !equal( res, m )) 
          report_test_failure(sprintf("Got %O, expected %O", res,m), 
                              cb,args,st); 
        else 
          report_test_ok( err, cb, args, st ); 
    }; 
} 
 
function check_not_equal( mixed m ) 
{ 
  return 
    lambda( mixed res, mixed err, function cb, array args, int st ) 
    { 
      if( err ) 
        report_test_failure( err, cb, args, st ); 
      else 
        if( equal( res, m )) 
          report_test_failure(sprintf("Got %O, expected %O", res,m), 
                              cb,args,st); 
        else 
          report_test_ok( err, cb, args, st ); 
    }; 
}