Merge tag 'v3.14.25' into backport/v3.14.24-ltsi-rc1+v3.14.25/snapshot-merge.wip
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / staging / ktap / test / function.kp
1 #!/usr/bin/env ktap
2
3 function failed() {
4         printf("failed\n");
5         exit(-1);
6 }
7
8 ### basic function call ###
9 function f1(a, b) {
10         return a + b
11 }
12
13 if (f1(2, 3) != 5) {
14         failed();
15 }
16
17 ### return string ###
18 function f2() {
19         return "function return"
20 }
21
22 if (f2() != "function return") {
23         failed();
24 }
25
26 ### mutli-value return ###
27 function f3(a, b) {
28         return a+b, a-b;
29 }
30
31 c, d = f3(2, 3);
32 if(c != 5 || d != -1) {
33         failed();
34 }
35
36
37 ### closure testing ###
38 function f4() {
39         f5 = function(a, b) {
40                 return a * b
41         }
42         return f5
43 }
44
45 local f = f4()
46 if (f(9, 9) != 81) {
47         failed();
48 }
49
50 ### closure with lexcial variable ###
51 # issue: variable cannot be local
52 i = 1
53 function f6() {
54         i = 5
55         f7 = function(a, b) {
56                 return a * b + i
57         }
58         return f7
59 }
60
61 f = f6()
62 if (f(9, 9) != 81 + i) {
63         failed();
64 }
65
66 i = 6
67 if (f(9, 9) != 81 + i) {
68         failed();
69 }
70
71 ### tail call
72 ### stack should not overflow in tail call mechanism
73 a = 0
74 function f8(i) {
75         if (i == 1000000) {
76                 a = 1000000
77                 return
78         }
79         # must add return here, otherwise stack overflow
80         return f8(i+1)
81 }
82
83 f8(0)
84 if (a != 1000000) {
85         failed();
86 }
87
88