-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtest_ir_function_system.ml
More file actions
311 lines (273 loc) · 9.63 KB
/
Copy pathtest_ir_function_system.ml
File metadata and controls
311 lines (273 loc) · 9.63 KB
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
(*
* Copyright 2025 Multikernel Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*)
(** Test IR Function System *)
open Kernelscript.Ast
open Kernelscript.Ir
open Kernelscript.Ir_function_system
open Kernelscript.Parse
open Alcotest
(** Test data *)
let create_test_function name is_main params ret_type =
{
func_name = name;
parameters = params;
return_type = ret_type;
basic_blocks = [
{
label = "entry";
instructions = [
{
instr_desc = IRReturn None;
instr_stack_usage = 0;
bounds_checks = [];
verifier_hints = [];
instr_pos = { line = 1; column = 1; filename = "test" };
}
];
successors = [];
predecessors = [];
stack_usage = 0;
loop_depth = 0;
reachable = true;
block_id = 0;
}
];
total_stack_usage = 0;
max_loop_depth = 0;
calls_helper_functions = [];
visibility = Public;
is_main;
func_pos = { line = 1; column = 1; filename = "test.ks" };
tail_call_targets = [];
tail_call_index_map = Hashtbl.create 16;
is_tail_callable = false;
func_program_type = None;
func_target = None;
}
let create_test_program () =
let main_func = create_test_function "main" true
[("ctx", IRStruct ("xdp_md", []))]
(Some (IREnum ("xdp_action", []))) in
{
name = "test_program";
program_type = Xdp;
entry_function = main_func;
ir_pos = { line = 1; column = 1; filename = "test" };
}
(** Test Function Signature Validation *)
let test_valid_main_signature _ =
let main_func = create_test_function "main" true
[("ctx", IRStruct ("xdp_md", []))]
(Some (IREnum ("xdp_action", []))) in
let sig_info = validate_function_signature main_func in
check bool "Main function should be valid" true sig_info.is_valid;
check string "Function name" "main" sig_info.func_name;
check bool "Should be marked as main" true sig_info.is_main
let test_invalid_main_signature _ =
let invalid_func = {
func_name = "main";
parameters = []; (* Missing context parameter *)
return_type = Some (IREnum ("xdp_action", []));
basic_blocks = [];
total_stack_usage = 0;
max_loop_depth = 0;
calls_helper_functions = [];
visibility = Public;
is_main = true;
func_pos = { line = 1; column = 1; filename = "test.ks" };
tail_call_targets = [];
tail_call_index_map = Hashtbl.create 16;
is_tail_callable = false;
func_program_type = None;
func_target = None;
} in
let sig_info = validate_function_signature invalid_func in
check bool "Invalid main function should be invalid" true (not sig_info.is_valid);
check string "Function name" "main" sig_info.func_name;
check bool "Should be marked as main" true sig_info.is_main
let test_too_many_parameters _ =
let func_with_many_params = create_test_function "test" false
[("a", IRU32); ("b", IRU32); ("c", IRU32); ("d", IRU32); ("e", IRU32); ("f", IRU32)]
(Some IRU32) in
let sig_info = validate_function_signature func_with_many_params in
check bool "Function with too many params should be invalid" false sig_info.is_valid;
check bool "Should have parameter count error" true
(List.exists (fun err -> String.length err > 0 && err.[0] = 'T') sig_info.validation_errors)
(** Test Complete Function System Analysis *)
let test_simple_analysis _ =
let prog = create_test_program () in
let analysis = analyze_ir_program_simple prog in
check int "signature validations count" 1 (List.length analysis.signature_validations);
check bool "Analysis should contain summary" true (String.length analysis.analysis_summary > 0)
(** Test basic function system operations *)
let test_basic_function_system () =
let prog = create_test_program () in
let analysis = analyze_ir_program_simple prog in
check int "signature validations count" 1 (List.length analysis.signature_validations);
check bool "Analysis should contain summary" true (String.length analysis.analysis_summary > 0)
(** Test function registration *)
let test_function_registration () =
let program_text = {|
@helper
fn helper(x: u32, y: u32) -> u32 {
return x + y
}
@xdp fn test(ctx: *xdp_md) -> xdp_action {
var result = helper(10, 20)
return 2
}
|} in
try
let ast = parse_string program_text in
let st = Kernelscript.Symbol_table.build_symbol_table ast in
check bool "function registration test" true (Kernelscript.Symbol_table.lookup_symbol st "helper" <> None)
with
| e -> fail ("Failed to test function registration: " ^ Printexc.to_string e)
(** Test function signature validation *)
let test_function_signature_validation () =
let program_text = {|
@helper
fn valid_function(x: u32, y: u32) -> u32 {
return x + y
}
@xdp fn test(ctx: *xdp_md) -> xdp_action {
var result = valid_function(10, 20)
return 2
}
|} in
try
let ast = parse_string program_text in
let st = Kernelscript.Symbol_table.build_symbol_table ast in
check bool "function signature validation test" true (Kernelscript.Symbol_table.lookup_symbol st "valid_function" <> None)
with
| e -> fail ("Failed to test function signature validation: " ^ Printexc.to_string e)
(** Test function call resolution *)
let test_function_call_resolution () =
let program_text = {|
@helper
fn multiply(x: u32, y: u32) -> u32 {
return x * y
}
@xdp fn test(ctx: *xdp_md) -> xdp_action {
var result = multiply(10, 2)
return 2
}
|} in
try
let ast = parse_string program_text in
let st = Kernelscript.Symbol_table.build_symbol_table ast in
check bool "function call resolution test" true (Kernelscript.Symbol_table.lookup_symbol st "multiply" <> None)
with
| e -> fail ("Failed to test function call resolution: " ^ Printexc.to_string e)
(** Test recursive function detection *)
let test_recursive_function_detection () =
let program_text = {|
@helper
fn helper() -> u32 {
return 42
}
@xdp fn test(ctx: *xdp_md) -> xdp_action {
var result = helper()
return 2
}
|} in
try
let ast = parse_string program_text in
let st = Kernelscript.Symbol_table.build_symbol_table ast in
check bool "recursive function detection test" true (Kernelscript.Symbol_table.lookup_symbol st "helper" <> None)
with
| e -> fail ("Failed to test recursive function detection: " ^ Printexc.to_string e)
(** Test function dependency analysis *)
let test_function_dependency_analysis () =
let program_text = {|
@helper
fn level1() -> u32 {
return 10
}
@xdp fn test(ctx: *xdp_md) -> xdp_action {
var result = level1()
return 2
}
|} in
try
let ast = parse_string program_text in
let st = Kernelscript.Symbol_table.build_symbol_table ast in
check bool "function dependency analysis test" true (Kernelscript.Symbol_table.lookup_symbol st "level1" <> None)
with
| e -> fail ("Failed to test function dependency analysis: " ^ Printexc.to_string e)
(** Test function optimization *)
let test_function_optimization () =
let program_text = {|
@helper
fn simple_math(x: u32) -> u32 {
return x * 2
}
@xdp fn test(ctx: *xdp_md) -> xdp_action {
var const_val = 10
var result = simple_math(const_val)
return 2
}
|} in
try
let ast = parse_string program_text in
let st = Kernelscript.Symbol_table.build_symbol_table ast in
check bool "function optimization test" true (Kernelscript.Symbol_table.lookup_symbol st "simple_math" <> None)
with
| e -> fail ("Failed to test function optimization: " ^ Printexc.to_string e)
(** Test comprehensive function system *)
let test_comprehensive_function_system () =
let program_text = {|
@helper
fn add(x: u32, y: u32) -> u32 {
return x + y
}
@helper
fn multiply(x: u32, y: u32) -> u32 {
return x * y
}
@xdp fn test(ctx: *xdp_md) -> xdp_action {
var a = 10
var b = 20
var sum = add(a, b)
var product = multiply(sum, 2)
return 2
}
|} in
try
let ast = parse_string program_text in
let st = Kernelscript.Symbol_table.build_symbol_table ast in
check bool "comprehensive function system test" true (Kernelscript.Symbol_table.lookup_symbol st "add" <> None && Kernelscript.Symbol_table.lookup_symbol st "multiply" <> None)
with
| e -> fail ("Failed to test comprehensive function system: " ^ Printexc.to_string e)
(** Test Suite *)
let function_system_tests = [
"test_valid_main_signature", `Quick, test_valid_main_signature;
"test_invalid_main_signature", `Quick, test_invalid_main_signature;
"test_too_many_parameters", `Quick, test_too_many_parameters;
"test_simple_analysis", `Quick, test_simple_analysis;
"test_basic_function_system", `Quick, test_basic_function_system;
"test_function_registration", `Quick, test_function_registration;
"test_function_signature_validation", `Quick, test_function_signature_validation;
"test_function_call_resolution", `Quick, test_function_call_resolution;
"test_recursive_function_detection", `Quick, test_recursive_function_detection;
"test_function_dependency_analysis", `Quick, test_function_dependency_analysis;
"test_function_optimization", `Quick, test_function_optimization;
"test_comprehensive_function_system", `Quick, test_comprehensive_function_system;
]
let () =
run "IR Function System Tests" [
"function_system", function_system_tests;
]