-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtest_string_to_array_unification.ml
More file actions
193 lines (175 loc) · 6.04 KB
/
Copy pathtest_string_to_array_unification.ml
File metadata and controls
193 lines (175 loc) · 6.04 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
(*
* 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.
*)
open Kernelscript.Ast
open Kernelscript.Type_checker
open Kernelscript.Parse
open Alcotest
(** Helper function to create test symbol table *)
let create_test_symbol_table ast =
Test_utils.Helpers.create_test_symbol_table ast
(** Test basic string to u8 array assignment *)
let test_string_to_u8_array_basic () =
let program_text = {|
struct TestStruct {
name: u8[16],
id: u32,
}
@xdp fn test_program(ctx: *xdp_md) -> xdp_action {
var obj = TestStruct {
name: "test_name", // String literal to u8[16] array
id: 42,
}
return XDP_PASS
}
|} in
try
let ast = parse_string program_text in
let symbol_table = create_test_symbol_table ast in
let (typed_ast, _typed_programs) = type_check_and_annotate_ast ~symbol_table:(Some symbol_table) ast in
check bool "type check produces declarations" true (List.length typed_ast > 0)
with
| exn -> fail ("String to u8 array basic test failed: " ^ Printexc.to_string exn)
(** Test string too long for array should fail *)
let test_string_too_long_for_array () =
let program_text = {|
struct TestStruct {
name: u8[4], // Small array
id: u32,
}
@xdp fn test_program(ctx: *xdp_md) -> xdp_action {
var obj = TestStruct {
name: "this_is_too_long", // String longer than 4 chars
id: 42,
}
return XDP_PASS
}
|} in
try
let ast = parse_string program_text in
let symbol_table = create_test_symbol_table ast in
let (_typed_ast, _typed_programs) = type_check_and_annotate_ast ~symbol_table:(Some symbol_table) ast in
fail "String too long for array should fail type checking"
with
| Type_error (_, _) -> ()
| exn -> fail ("Unexpected error: " ^ Printexc.to_string exn)
(** Test string exactly fits in array *)
let test_string_exact_fit_array () =
let program_text = {|
struct TestStruct {
name: u8[5], // Exactly 5 bytes
id: u32,
}
@xdp fn test_program(ctx: *xdp_md) -> xdp_action {
var obj = TestStruct {
name: "hello", // Exactly 5 chars
id: 42,
}
return XDP_PASS
}
|} in
try
let ast = parse_string program_text in
let symbol_table = create_test_symbol_table ast in
let (typed_ast, _typed_programs) = type_check_and_annotate_ast ~symbol_table:(Some symbol_table) ast in
check bool "type check produces declarations" true (List.length typed_ast > 0)
with
| exn -> fail ("String exact fit test failed: " ^ Printexc.to_string exn)
(** Test direct unify_types function *)
let test_unify_types_string_to_array () =
(* Test that string can unify with larger u8 array *)
let str_type = Str 10 in
let array_type = Array (U8, 16) in
(match unify_types str_type array_type with
| Some (Array (U8, 16)) -> ()
| _ -> fail "String should unify with larger u8 array");
(* Test that larger string cannot unify with smaller array *)
let large_str_type = Str 20 in
let small_array_type = Array (U8, 16) in
(match unify_types large_str_type small_array_type with
| None -> ()
| Some _ -> fail "Large string should not unify with smaller array");
(* Test that string cannot unify with non-u8 array *)
let str_type = Str 10 in
let u32_array_type = Array (U32, 16) in
(match unify_types str_type u32_array_type with
| None -> ()
| Some _ -> fail "String should not unify with non-u8 array")
(** Test multiple string assignments in same struct *)
let test_multiple_string_assignments () =
let program_text = {|
struct Config {
name: u8[16],
description: u8[32],
category: u8[8],
version: u32,
}
@xdp fn test_program(ctx: *xdp_md) -> xdp_action {
var cfg = Config {
name: "test_config",
description: "A test configuration",
category: "test",
version: 1,
}
return XDP_PASS
}
|} in
try
let ast = parse_string program_text in
let symbol_table = create_test_symbol_table ast in
let (typed_ast, _typed_programs) = type_check_and_annotate_ast ~symbol_table:(Some symbol_table) ast in
check bool "type check produces declarations" true (List.length typed_ast > 0)
with
| exn -> fail ("Multiple string assignments test failed: " ^ Printexc.to_string exn)
(** Test string assignment in nested structs *)
let test_nested_struct_string_assignment () =
let program_text = {|
struct Inner {
name: u8[16],
id: u32,
}
struct Outer {
inner: Inner,
label: u8[8],
}
@xdp fn test_program(ctx: *xdp_md) -> xdp_action {
var obj = Outer {
inner: Inner {
name: "inner_name",
id: 1,
},
label: "outer",
}
return XDP_PASS
}
|} in
try
let ast = parse_string program_text in
let symbol_table = create_test_symbol_table ast in
let (typed_ast, _typed_programs) = type_check_and_annotate_ast ~symbol_table:(Some symbol_table) ast in
check bool "type check produces declarations" true (List.length typed_ast > 0)
with
| exn -> fail ("Nested struct string assignment test failed: " ^ Printexc.to_string exn)
let tests = [
"string to u8 array basic", `Quick, test_string_to_u8_array_basic;
"string too long for array", `Quick, test_string_too_long_for_array;
"string exact fit in array", `Quick, test_string_exact_fit_array;
"unify_types string to array", `Quick, test_unify_types_string_to_array;
"multiple string assignments", `Quick, test_multiple_string_assignments;
"nested struct string assignment", `Quick, test_nested_struct_string_assignment;
]
let () = Alcotest.run "String to U8 Array Unification Tests" [
"string_to_array_tests", tests;
]