oop.ttcn 15.8 KB
Newer Older
1
/******************************************************************************
balaskoa's avatar
balaskoa committed
2
 * Copyright (c) 2000-2020 Ericsson Telecom AB
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
 *
 * Contributors:
 *   Baranyi, Botond
 *
 ******************************************************************************/
module oop {

type component CT {
  port PT pt;
}

type port PT message { inout integer; } with { extension "internal" }

20
21
type record of integer IntList;

22
23
24
25
///////////////////////////////////////////////////
///////////////////// CLASSES /////////////////////
///////////////////////////////////////////////////

26
27
28
29
30
type record Rec {
  charstring str,
  IntList list
}

31
32
33
34
35
type union Uni {
  integer i,
  charstring cs
}

36
37
type class BaseClass runs on CT mtc CT system CT {
  public const integer m_const := 1;
38
  private const IntList m_const2 := { 1, 2, 3 };
39
40
41
42
43
44
45
46
47
  /*protected*/ var charstring m_var;
  private template octetstring m_temp := ? length (1..4);
  public var template float m_var_temp;
  //private PT m_port; // port members not supported
  /*protected*/ timer m_timer;
  private timer m_timer_array[3];
  
  //public template integer m_temp_pard(integer p) := p; // parameterized template members not supported
  
48
  // this would also be the implicit constructor's header:
49
50
  create(integer p_const, IntList p_const2, charstring p_var,
         template octetstring p_temp, template float p_var_temp) {
51
52
53
    m_const := p_const;
    m_const := 2;
    m_var := p_var;
54
    m_const2[2] := p_const2[0];
55
56
57
    m_temp := p_temp;
    m_temp := *;
    m_var_temp := p_var_temp;
58
59
    //m_port := p_port;
    //m_timer := p_timer;
60
61
62
63
64
65
66
  }
  
  public function f(in integer x) return integer {
    m_var := m_var & int2str(x);
    return x;
  }
  
67
68
69
70
71
72
73
74
  public function f2(in integer x) return Rec {
    var Rec ret_val := { str := int2str(x), list := {} };
    for (var integer i := 0; i < x; i := i + 1) {
      ret_val.list[i] := i;
    }
    return ret_val;
  }
  
75
76
77
  public function get_var() return charstring {
    return m_var;
  }
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  
  public function this_test(in integer get_var, in charstring p_log_str) {
    var integer m_const := 9;
    if (this.m_const != 2) {
      setverdict(fail, "this.m_const = ", this.m_const);
    }
    if (this.get_var() != m_var) {
      setverdict(fail, "this.get_var() = ", this.get_var(), ", m_var = ", m_var);
    }
    if (log2str(this) != p_log_str) {
      setverdict(fail, "this = ", this, ", expected: ", p_log_str);
    }
    var BaseClass v_ref := this;
    if (not v_ref == this) {
      setverdict(fail, "equality failed");
    }
    var BaseClass v_ref2;
    v_ref2 := this;
    if (this != v_ref2) {
      setverdict(fail, "inequality failed");
    }
  }
100
101
102
103
104
105
}
finally {
  //pt.send(-1);
  log(m_var);
}

106
107
108
109
110
111
type class SubClass extends BaseClass {  
  create(integer p_const := 1, IntList p_const2, charstring p_var,
         template octetstring p_temp, template float p_var_temp)
         : BaseClass(p_const, p_const2, p_var, p_temp, p_var_temp) {
    m_const3 := 'FFFF'O;
    m_var := m_var & "x";
112
113
  }
  
114
115
  const octetstring m_const3 := 'AB'O; // the parser currently doesn't accept constants without initial value
  
116
  public function f(in integer x) return integer {
117
    return super.f(x) - 1;
118
119
120
121
  }
}

type class @final FinalClass extends SubClass {
122
123
  private const integer m_final_const := 1;
  private template charstring m_final_temp := ? length (1..4);
124
125
  private var float m_final_var := 1.0;
  private var template octetstring m_final_var_temp := ''O;
126
  
127
  public function @final f(in integer x) return integer {
128
129
130
131
132
133
134
135
136
    return super.f(x) + m_final_const + 1;
  }
  
  public function get_uni() return Uni {
    return { i := m_final_const };
  }
  
  public function get_uni_temp() return template Uni {
    return { cs := m_final_temp };
137
138
139
140
141
142
143
144
145
146
147
148
  }
}


type class @abstract AbstractClass {
  public function @abstract f_abs(inout integer x) return boolean;
  public function f_con(in charstring x) {
    log(x);
  }
}

type class ConcreteClass extends AbstractClass {
149
  create(octetstring p_const := 'AB'O) {
150
    // for compilation only, to make sure there are no name clashes between parameter default values
151
152
    log(p_const);
  }
153
154
155
156
157
158
  public function f_abs(inout integer x) return boolean {
    x := x + 1;
    return x > 0;
  }
}

159

160
161
162
163
164
165
166
167
168
169
170
type external class ExternalClass {
  public external function f_ext(in integer x) return charstring;
}

type class InternalClass extends ExternalClass {
  public function f_int() return integer {
    return 0;
  }
  public external function f_ext2();
}

171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
testcase tc_members_and_methods() runs on CT {
  var BaseClass v_base := BaseClass.create(4, { 1, 2, 4 }, "a", 'FF'O, (0.0 .. 10.0));
  var IntList il := { 1, 2, 4 };
  var template float tf := (0.0 .. 10.0);
  var BaseClass v_sub := SubClass.create(4, il, "a", 'FF'O, tf);
  var BaseClass v_final := FinalClass.create(4, il, "a", 'FF'O, tf, 8, "x", -1.5, *);
  
  log(v_base.f(6));
  log(v_sub.f(6));
  log(v_final.f(6));
  
  log(v_base.m_const);
  log(v_sub.m_var_temp);
  log(v_final.get_var());
  log(v_final.f2(3).list[1]);
  
187
  if (v_base.f(6) != 6) {
188
189
190
191
192
    setverdict(fail, "v_base.f(6) = ", v_base.f(6));
  }
  if (v_sub.f(6) != 5) {
    setverdict(fail, "v_sub.f(6) = ", v_sub.f(6));
  }
193
  if (v_final.f(6) != 14) {
194
    setverdict(fail, "v_final.f(6) = ", v_final.f(6));
195
  }
196
197
198
199
200
201
202
203
204
205
206
  
  if (log2str(v_base.f(6)) != "6") {
    setverdict(fail, "log2str(v_base.f(6)) = ", log2str(v_base.f(6)));
  }
  
  if (v_base.m_const != 2) {
    setverdict(fail, "v_base.m_const = ", v_base.m_const);
  }
  if (log2str(v_sub.m_var_temp) != "(0.000000 .. 10.000000)") {
    setverdict(fail, "v_sub.m_var_temp = ", v_sub.m_var_temp);
  }
207
  if (v_final.get_var() != "ax66") {
208
209
210
211
    setverdict(fail, "v_final.get_var() = ", v_final.get_var());
  }
  if (v_final.f2(3).list[1] != 1) {
    setverdict(fail, "v_final.f2(3).list[1] = ", v_final.f2(3).list[1]);
212
  }
213
214
215
  setverdict(pass);
}

216
217
218
219
220
221
222
223
224
225
226
testcase tc_logging() runs on CT {
  var BaseClass v_base := BaseClass.create(4, { 1, 2, 4 }, "a", 'FF'O, (0.0 .. 10.0));
  var IntList il := { 1, 2, 4 };
  var template float tf := (0.0 .. 10.0);
  var BaseClass v_sub := SubClass.create(4, il, "a", 'FF'O, tf);
  var BaseClass v_final := FinalClass.create(4, il, "a", 'FF'O, tf, 8, "x", -1.5, *);

  log(v_base);
  log(v_sub);
  log(v_final);
  
227
228
229
  var charstring v_base_str := "BaseClass: { m_const := 2, m_const2 := { 1, 2, 1 }, m_var := \"a\", m_temp := *, m_var_temp := (0.000000 .. 10.000000), m_timer := timer: { name: m_timer, default duration: none, state: inactive }, m_timer_array := { timer: { name: m_timer_array[0], default duration: none, state: inactive }, timer: { name: m_timer_array[1], default duration: none, state: inactive }, timer: { name: m_timer_array[2], default duration: none, state: inactive } } }";
  var charstring v_sub_str := "SubClass: { BaseClass: { m_const := 2, m_const2 := { 1, 2, 1 }, m_var := \"ax\", m_temp := *, m_var_temp := (0.000000 .. 10.000000), m_timer := timer: { name: m_timer, default duration: none, state: inactive }, m_timer_array := { timer: { name: m_timer_array[0], default duration: none, state: inactive }, timer: { name: m_timer_array[1], default duration: none, state: inactive }, timer: { name: m_timer_array[2], default duration: none, state: inactive } } }, m_const3 := 'FFFF'O }";
  var charstring v_final_str := "FinalClass: { SubClass: { BaseClass: { m_const := 2, m_const2 := { 1, 2, 1 }, m_var := \"ax\", m_temp := *, m_var_temp := (0.000000 .. 10.000000), m_timer := timer: { name: m_timer, default duration: none, state: inactive }, m_timer_array := { timer: { name: m_timer_array[0], default duration: none, state: inactive }, timer: { name: m_timer_array[1], default duration: none, state: inactive }, timer: { name: m_timer_array[2], default duration: none, state: inactive } } }, m_const3 := 'FFFF'O }, m_final_const := 8, m_final_temp := \"x\", m_final_var := -1.500000, m_final_var_temp := * }";
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
  
  if (log2str(v_base) != v_base_str) {
    setverdict(fail, "v_base: ", v_base);
  }
  if (log2str(v_sub) != v_sub_str) {
    setverdict(fail, "v_sub: ", v_sub);
  }
  if (log2str(v_final) != v_final_str) {
    setverdict(fail, "v_final: ", v_final);
  }
  setverdict(pass);
}

testcase tc_equality() runs on CT {
  var BaseClass v_base := BaseClass.create(4, { 1, 2, 4 }, "a", 'FF'O, (0.0 .. 10.0));
  var BaseClass v_base2 := BaseClass.create(4, { 1, 2, 4 }, "a", 'FF'O, (0.0 .. 10.0));
  if (v_base == v_base2) {
    setverdict(fail, "Equality of different objects failed");
  }
  if (not v_base != v_base2) {
    setverdict(fail, "Inquality of different objects failed");
  }
  
  var BaseClass v_ref := v_base;
  if (not v_ref == v_base) {
    setverdict(fail, "Equality of object and reference to object failed");
  }
  if (v_ref != v_base) {
    setverdict(fail, "Inequality of object and reference to object failed");
  }
  
  var BaseClass v_ref2 := v_ref;
  if (not v_ref2 == v_base) {
    setverdict(fail, "Equality of object and indirect reference to object failed");
  }
  if (v_ref2 != v_base) {
    setverdict(fail, "Inequality of object and indirect reference to object failed");
  }
  setverdict(pass);
}

testcase tc_null() runs on CT {
  var BaseClass v_base := BaseClass.create(4, { 1, 2, 4 }, "a", 'FF'O, (0.0 .. 10.0));
  var IntList il := { 1, 2, 4 };
  var template float tf := (0.0 .. 10.0);
  var SubClass v_sub := SubClass.create(4, il, "a", 'FF'O, tf);
  var FinalClass v_final := FinalClass.create(4, il, "a", 'FF'O, tf, 8, "x", -1.5, *);
  
  var BaseClass v_null := null;
  var BaseClass v_empty;
  
  log(v_null);
  log(v_empty);
  
  if (v_base == v_null) {
    setverdict(fail, "BaseClass equality with null reference failed");
  }
  if (v_base == v_empty) {
    setverdict(fail, "BaseClass equality with empty reference failed");
  }
  if (v_null != v_empty) {
    setverdict(fail, "null reference equality with empty reference failed");
  }
  if (v_base == null) {
    setverdict(fail, "BaseClass equality with null value failed");
  }
  if (null == v_final) {
    setverdict(fail, "FinalClass equality with null value failed");
  }
  if (v_null != null) {
    setverdict(fail, "null reference equality with null value failed");
  }
  if (null != v_empty) {
    setverdict(fail, "null value equality with empty reference failed");
  }
  setverdict(pass);
}

testcase tc_this() runs on CT {
  var BaseClass v_base := BaseClass.create(4, { 1, 2, 4 }, "a", 'FF'O, (0.0 .. 10.0));
  v_base.this_test(10, log2str(v_base));
  setverdict(pass);
}

testcase tc_references() runs on CT {
  var BaseClass v_base := BaseClass.create(4, { 1, 2, 4 }, "a", 'FF'O, 1.0);
  var SubClass v_sub := SubClass.create(4, { 1, 2, 4 }, "a", 'FF'O, 1.0);
  var FinalClass v_final := FinalClass.create(4, { 1, 2, 4 }, "a", 'FF'O, 1.0, 8, "x", -1.5, *);
318
  var BaseClass v_null := null;
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
  
  var BaseClass v_ref1;
  if (v_ref1 != null) {
    setverdict(fail, "#1");
  }
  v_ref1 := v_base;
  if (v_ref1 != v_base) {
    setverdict(fail, "#2");
  }
  var integer v_int := v_final.f(1);
  if (v_int != 9) {
    setverdict(fail, "#3, ", v_final.f(1));
  }
  v_int := v_final.f(2);
  if (v_int != 10) {
    setverdict(fail, "#4, ", v_final.f(2));
  }
  if (not isbound(v_final.f2(3).list[1])) {
    setverdict(fail, "#5");
  }
  if (not isvalue(v_base.m_var_temp)) {
    setverdict(fail, "#6");
  }
  if (not ispresent(v_base.m_var_temp)) {
    setverdict(fail, "#7");
  }
  if (ischosen(v_final.get_uni().cs)) {
    setverdict(fail, "#8");
  }
  if (not ischosen(v_final.get_uni_temp().cs)) {
    setverdict(fail, "#9");
  }
  if (v_final.f2(3).list[1] + v_final.f(-6) != v_base.f(3) * float2int(valueof(v_sub.m_var_temp))) {
    setverdict(fail, "#10, ", v_final.f2(3).list[1] + v_final.f(-6), " != ", v_base.f(3) * float2int(valueof(v_sub.m_var_temp)));
  }
  if (match(v_final.get_uni(), v_final.get_uni_temp())) {
    setverdict(fail, "#11, ", match(v_final.get_uni(), v_final.get_uni_temp()));
  }
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
  if (not isbound(v_sub)) {
    setverdict(fail, "#12");
  }
  if (isbound(v_null)) {
    setverdict(fail, "#13");
  }
  if (not isvalue(v_final)) {
    setverdict(fail, "#14");
  }
  if (isvalue(v_null)) {
    setverdict(fail, "#15");
  }
  if (not ispresent(v_base)) {
    setverdict(fail, "#16");
  }
  if (ispresent(v_null)) {
    setverdict(fail, "#17");
  }
375
376
377
  setverdict(pass);
}

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
type class Node {
  var integer data;
  var Node next;
}

function f_test(in Node p1, inout Node p2, out Node p3,
                in charstring p1_str, in charstring p2_str) return Node {
  if (log2str(p1) != p1_str) {
    setverdict(fail, "Invalid 'in' parameter value at start: ", p1, ", expected: ", p1_str);
  }
  if (log2str(p2) != p2_str) {
    setverdict(fail, "Invalid 'inout' parameter value at start: ", p2, ", expected: ", p2_str);
  }
  if (p3 != null) {
    setverdict(fail, "Invalid 'out' parameter value at start: ", p3, ", expected: null");
  }
  var Node tmp := p2;
  p3 := p1;
  p1 := null;
  p2 := null;
  return tmp;
}

testcase tc_function_pars_and_retval() runs on CT {
  var Node x := Node.create(1, Node.create(2, Node.create(3, null)));
  var Node y := Node.create(0, null);
  var Node z := Node.create(-1, null);
  var charstring x_str := log2str(x);
  var charstring y_str := log2str(y);
  var Node res := f_test(x, y, z, x_str, y_str);
  if (log2str(x) != x_str) {
    setverdict(fail, "Invalid 'in' parameter value at end: ", x, ", expected: ", x_str);
  }
  if (y != null) {
    setverdict(fail, "Invalid 'inout' parameter value at end: ", y, ", expected: null");
  }
  if (log2str(z) != x_str) {
    setverdict(fail, "Invalid 'out' parameter value at end: ", z, ", expected: ", x_str);
  }
  if (log2str(res) != y_str) {
    setverdict(fail, "Invalid return value: ", res, ", expected: ", y_str);
  }
  setverdict(pass);
}

type class Node2 {
  var object data;
  var Node2 next;
}

type class Something {
  public function toString() return universal charstring {
    return "Something";
  }
}

testcase tc_object() runs on CT {
  var object v_obj := Node.create(3, null);
  var Node v_node := Node.create(3, null);
  if (log2str(v_obj) != log2str(v_node)) {
    setverdict(fail, "v_obj: ", v_obj, ", v_node: ", v_node);
  }
  if (v_obj.toString() != "Object") {
    setverdict(fail, "v_obj.toString(): ", v_obj.toString());
  }
  if (v_obj.toString() != v_node.toString()) {
    setverdict(fail, "v_obj.toString(): ", v_obj.toString(), ", v_node.toString(): ", v_node.toString());
  }
  var object v_obj2 := Something.create;
  var Something v_smthn := Something.create;
  if (v_obj2.toString() != "Something") {
    setverdict(fail, "v_obj2.toString(): ", v_obj2.toString());
  }
  if (v_obj2.toString() != v_smthn.toString()) {
    setverdict(fail, "v_obj2.toString(): ", v_obj2.toString(), ", v_smthn.toString(): ", v_smthn.toString());
  }
  if (v_obj2.toString()[3] != "e") {
    setverdict(fail, "v_obj2.toString()[3]: ", v_obj2.toString()[3]);
  }
  var Node2 v_node2 := Node2.create(Something.create, Node2.create(Node.create(1, null), null));
  if (log2str(v_node2) != "Node2: { data := Something: {  }, next := Node2: { data := Node: { data := 1, next := null }, next := null } }") {
    setverdict(fail, "v_node2: ", v_node2);
  }
  setverdict(pass);
}

465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
type class Order {
  var integer b;
  var integer d;
  var integer c;
  var integer a;
}

testcase tc_order() runs on CT {
  var Order x := Order.create(1, 2, 3, 4);
  if (log2str(x) == "Order: { b := 1, d := 2, c := 3, a := 4 }") {
    setverdict(pass);
  }
  else {
    setverdict(fail, x);
  }
}

482
483
484
485
486
487
488
489
490
491
492
testcase tc_abstract() runs on CT {
  var AbstractClass v := ConcreteClass.create;
  var integer x := 0;
  if (v.f_abs(x) and x == 1) {
    setverdict(pass);
  }
  else {
    setverdict(fail);
  }
}

493

494
control {
495
  execute(tc_members_and_methods());
496
497
498
499
500
  execute(tc_logging());
  execute(tc_equality());
  execute(tc_null());
  execute(tc_this());
  execute(tc_references());
501
502
  execute(tc_function_pars_and_retval());
  execute(tc_object());
503
  execute(tc_order());
504
  execute(tc_abstract());
505
506
507
}

}