| 1 |
set test "func_overload" |
| 2 |
|
| 3 |
if {![installtest_p]} {untested $test; return} |
| 4 |
|
| 5 |
proc run_test {test script expected} { |
| 6 |
set success 0 |
| 7 |
spawn stap -g -e $script |
| 8 |
expect { |
| 9 |
-timeout 30 |
| 10 |
-re "$expected" {pass $test} |
| 11 |
default {fail $test} |
| 12 |
} |
| 13 |
catch { close }; catch { wait } |
| 14 |
} |
| 15 |
|
| 16 |
# unconditional next |
| 17 |
set script1 { |
| 18 |
function f() { |
| 19 |
next |
| 20 |
return "first function" |
| 21 |
} |
| 22 |
|
| 23 |
function f() { |
| 24 |
return "second function" |
| 25 |
} |
| 26 |
|
| 27 |
probe begin { |
| 28 |
println(f()) |
| 29 |
exit() |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
# overload using global variable |
| 34 |
set script2 { |
| 35 |
global x = 0 |
| 36 |
|
| 37 |
function f() { |
| 38 |
if (x == 100) |
| 39 |
return "first function" |
| 40 |
else |
| 41 |
next |
| 42 |
} |
| 43 |
|
| 44 |
function f() { |
| 45 |
return "second function" |
| 46 |
} |
| 47 |
|
| 48 |
probe begin { |
| 49 |
println(f()) |
| 50 |
x = 100 |
| 51 |
println(f()) |
| 52 |
exit() |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
# overload using function arguments |
| 57 |
set script3 { |
| 58 |
function f(x, y) { |
| 59 |
if (x == 500 || y == "hello") |
| 60 |
return "first function" |
| 61 |
else |
| 62 |
next |
| 63 |
} |
| 64 |
|
| 65 |
function f(x, y) { |
| 66 |
return "second function" |
| 67 |
} |
| 68 |
|
| 69 |
probe begin { |
| 70 |
println(f(200, "asd")) |
| 71 |
println(f(900, "hello")) |
| 72 |
exit() |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
# overload using argument and global variable |
| 77 |
set script4 { |
| 78 |
global x = 0 |
| 79 |
|
| 80 |
function f(y) { |
| 81 |
if (x == 100 || y == 200) |
| 82 |
return "first function" |
| 83 |
else |
| 84 |
next |
| 85 |
} |
| 86 |
|
| 87 |
function f(x) { |
| 88 |
return "second function" |
| 89 |
} |
| 90 |
|
| 91 |
probe begin { |
| 92 |
println(f(0)) |
| 93 |
println(f(200)) |
| 94 |
x = 100 |
| 95 |
println(f(0)) |
| 96 |
exit() |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
# priority ordering |
| 101 |
set script5 { |
| 102 |
function f():1 %{ |
| 103 |
STAP_NEXT; |
| 104 |
STAP_PRINTF("first function\n"); |
| 105 |
%} |
| 106 |
|
| 107 |
function f():3 { |
| 108 |
println("second function") |
| 109 |
} |
| 110 |
|
| 111 |
function f():2 %{ |
| 112 |
STAP_PRINTF("third function\n"); |
| 113 |
%} |
| 114 |
|
| 115 |
probe begin { |
| 116 |
f() |
| 117 |
exit() |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
# compile time arity overloading |
| 122 |
set script6 { |
| 123 |
function adder(x) { |
| 124 |
return x |
| 125 |
} |
| 126 |
|
| 127 |
function adder(x, y) { |
| 128 |
return x + y |
| 129 |
} |
| 130 |
|
| 131 |
function adder(x, y, z) { |
| 132 |
return x + y + z |
| 133 |
} |
| 134 |
|
| 135 |
probe begin { |
| 136 |
println(adder(1)) |
| 137 |
println(adder(1, 2)) |
| 138 |
println(adder(1, 2, 3)) |
| 139 |
exit() |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
# runtime overload and compile time overload |
| 144 |
set script7 { |
| 145 |
function adder(x) { |
| 146 |
if (x != 1) |
| 147 |
return 1000 |
| 148 |
else |
| 149 |
next |
| 150 |
} |
| 151 |
|
| 152 |
function adder(x) { |
| 153 |
return x |
| 154 |
} |
| 155 |
|
| 156 |
function adder(x, y) { |
| 157 |
return x + y |
| 158 |
} |
| 159 |
|
| 160 |
function adder(x, y, z) { |
| 161 |
return x + y + z |
| 162 |
} |
| 163 |
|
| 164 |
probe begin { |
| 165 |
println(adder(0)) |
| 166 |
println(adder(1)) |
| 167 |
println(adder(1, 2)) |
| 168 |
println(adder(1, 2, 3)) |
| 169 |
exit() |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
# all functions exhausted runtime error |
| 174 |
set script8 { |
| 175 |
function f() { |
| 176 |
next |
| 177 |
return 0 |
| 178 |
} |
| 179 |
|
| 180 |
probe begin { |
| 181 |
println(f()) |
| 182 |
exit() |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
run_test unconditional_next $script1 "second function\r\n" |
| 187 |
run_test runtime_global $script2 "second function\r\nfirst function\r\n" |
| 188 |
run_test runtime_arguments $script3 "second function\r\nfirst function\r\n" |
| 189 |
run_test runtime_conditions $script4 "second function\r\nfirst function\r\nfirst function\r\n" |
| 190 |
run_test runtime_priority $script5 "third function\r\n" |
| 191 |
run_test arity $script6 "1\r\n3\r\n6\r\n" |
| 192 |
run_test mix $script7 "1000\r\n1\r\n3\r\n6\r\n" |
| 193 |
run_test overload_exception $script8 "all functions exhausted" |