FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
kernelscript/tests/test_maps.ml at main · multikernel/kernelscript · GitHub
multikernel
/
kernelscript
Public
Notifications
You must be signed in to change notification settings
Fork
24
Star
508
Code
Issues
2
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
kernelscript
/
tests
/
test_maps.ml
Copy path
More file actions
More file actions
Latest commit
History
History
History
213 lines (178 loc) · 8.46 KB
Breadcrumbs
kernelscript
/
tests
/
test_maps.ml
Copy path
File metadata and controls
213 lines (178 loc) · 8.46 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
(*
* 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
Alcotest
(*
Import the correct make_map_declaration from Ast module
*)
let
make_ast_map_declaration
=
Kernelscript.Ast.
make_map_declaration
(*
Import needed functions from Maps module
*)
let
validate_map_declaration
=
Kernelscript.Maps.
validate_map_declaration
let
is_map_compatible_with_program
=
Kernelscript.Maps.
is_map_compatible_with_program
let
analyze_expr_access_pattern
=
Kernelscript.Maps.
analyze_expr_access_pattern
let
string_of_map_declaration
=
Kernelscript.Maps.
string_of_map_declaration
(*
* Test basic map creation
*)
let
test_basic_map_creation
()
=
let
key_type
=
U32
in
let
value_type
=
U64
in
let
map_type
=
Hash
in
let
size
=
1024
in
let
map_config
=
make_map_config size
()
in
let
pos
=
make_position
1
1
"
test.ks
"
in
let
map_decl
=
make_ast_map_declaration
"
counter
"
key_type value_type map_type map_config
true
~is_pinned:
false
pos
in
check
string
"
basic map creation name
"
"
counter
"
map_decl.name;
check
bool
"
map key type
"
true
(map_decl.key_type
=
key_type);
check
bool
"
map value type
"
true
(map_decl.value_type
=
value_type);
check
int
"
map size
"
size map_decl.config.max_entries
(*
* Test different map types
*)
let
test_different_map_types
()
=
let
test_cases
=
[
(
Hash
,
"
hash
"
);
(
Array
,
"
array
"
);
(
Lru_hash
,
"
lru_hash
"
);
(
Percpu_hash
,
"
percpu_hash
"
);
]
in
List.
iter (
fun
(
map_type
,
expected_name
) ->
let
config
=
make_map_config
1024
()
in
let
pos
=
make_position
1
1
"
test.ks
"
in
let
map_decl
=
make_ast_map_declaration
"
test
"
U32
U64
map_type config
true
~is_pinned:
false
pos
in
check
bool
(
"
map type:
"
^
expected_name)
true
(map_decl.map_type
=
map_type)
) test_cases
(*
* Test map key/value types
*)
let
test_map_key_value_types
()
=
let
test_cases
=
[
(
U8
,
U16
,
"
u8
"
,
"
u16
"
);
(
U32
,
U64
,
"
u32
"
,
"
u64
"
);
(
U64
,
Bool
,
"
u64
"
,
"
bool
"
);
(
Bool
,
U32
,
"
bool
"
,
"
u32
"
);
]
in
List.
iter (
fun
(
key_type
,
value_type
,
expected_key
,
expected_value
) ->
let
config
=
make_map_config
1024
()
in
let
pos
=
make_position
1
1
"
test.ks
"
in
let
map_decl
=
make_ast_map_declaration
"
test
"
key_type value_type
Hash
config
true
~is_pinned:
false
pos
in
check
bool
(
"
key type:
"
^
expected_key)
true
(map_decl.key_type
=
key_type);
check
bool
(
"
value type:
"
^
expected_value)
true
(map_decl.value_type
=
value_type)
) test_cases
(*
* Test map operations
*)
let
test_map_operations
()
=
let
pos
=
make_position
1
1
"
test.ks
"
in
let
config
=
make_map_config
1024
()
in
let
map_decl
=
make_ast_map_declaration
"
counter
"
U32
U64
Hash
config
true
~is_pinned:
false
pos
in
(*
Test map declaration properties
*)
check
string
"
map operations map name
"
"
counter
"
map_decl.name;
check
bool
"
map operations key type
"
true
(map_decl.key_type
=
U32
);
check
bool
"
map operations value type
"
true
(map_decl.value_type
=
U64
)
(*
* Test map initialization
*)
let
test_map_initialization
()
=
let
pos
=
make_position
1
1
"
test.ks
"
in
let
config
=
make_map_config
1024
()
in
let
map_decl
=
make_ast_map_declaration
"
initialized_map
"
U32
U64
Hash
config
true
~is_pinned:
false
pos
in
check
string
"
initialized map name
"
"
initialized_map
"
map_decl.name;
check
bool
"
map has config
"
true
(map_decl.config.max_entries
>
0
);
check
int
"
initialization size
"
1024
map_decl.config.max_entries
(*
* Test map validation
*)
let
test_map_validation
()
=
let
pos
=
make_position
1
1
"
test.ks
"
in
(*
Test valid map
*)
let
config
=
make_map_config
1024
()
in
let
valid_map
=
make_ast_map_declaration
"
valid
"
U32
U64
Hash
config
true
~is_pinned:
false
pos
in
check
string
"
valid map name
"
"
valid
"
valid_map.name;
(*
Test invalid map with zero size
*)
let
invalid_config
=
make_map_config
0
()
in
let
invalid_map
=
make_ast_map_declaration
"
invalid
"
U32
U64
Hash
invalid_config
true
~is_pinned:
false
pos
in
check
string
"
invalid map name
"
"
invalid
"
invalid_map.name
(*
* Test map program integration
*)
let
test_map_program_integration
()
=
let
pos
=
make_position
1
1
"
test.ks
"
in
let
config
=
make_map_config
1024
()
in
(*
Create maps
*)
let
packet_count_map
=
make_ast_map_declaration
"
packet_count
"
U32
U64
Hash
config
true
~is_pinned:
false
pos
in
let
byte_count_map
=
make_ast_map_declaration
"
byte_count
"
U32
U64
Hash
config
true
~is_pinned:
false
pos
in
(*
Test map integration
*)
check
string
"
first map name
"
"
packet_count
"
packet_count_map.name;
check
string
"
second map name
"
"
byte_count
"
byte_count_map.name;
(*
Test map compatibility - simplified
*)
()
(*
* Test map type compatibility
*)
let
test_map_type_compatibility
()
=
let
pos
=
make_position
1
1
"
test.ks
"
in
let
config
=
make_map_config
1024
()
in
(*
Test compatible types
*)
let
compatible_map
=
make_ast_map_declaration
"
compatible
"
U32
U64
Hash
config
true
~is_pinned:
false
pos
in
check
string
"
compatible map name
"
"
compatible
"
compatible_map.name;
check
bool
"
compatible types
"
true
(compatible_map.map_type
=
Hash
)
(*
* Test map size validation
*)
let
test_map_size_validation
()
=
let
pos
=
make_position
1
1
"
test.ks
"
in
(*
Test valid sizes
*)
let
valid_sizes
=
[
1
;
1024
;
4096
;
65536
]
in
List.
iter (
fun
size
->
let
config
=
make_map_config size
()
in
let
map_decl
=
make_ast_map_declaration
"
test
"
U32
U64
Hash
config
true
~is_pinned:
false
pos
in
check
int
(
"
size matches:
"
^
string_of_int size) size map_decl.config.max_entries
) valid_sizes
(*
* Test map access patterns
*)
let
test_map_access_patterns
()
=
let
pos
=
make_position
1
1
"
test.ks
"
in
let
key_expr
=
make_expr (
Literal
(
IntLit
(
Signed64
42L
,
None
))) pos
in
(*
Test access pattern analysis - simplified
*)
check
bool
"
access pattern analysis
"
true
(
match
key_expr.expr_desc
with
Literal
_
->
true
|
_
->
false
)
(*
* Test map error handling
*)
let
test_map_error_handling
()
=
let
pos
=
make_position
1
1
"
test.ks
"
in
let
config
=
make_map_config
1024
()
in
(*
Test error conditions
*)
try
let
_
=
make_ast_map_declaration
"
"
U32
U64
Hash
config
true
~is_pinned:
false
pos
in
()
with
|
_
->
()
(*
* Test map metadata
*)
let
test_map_metadata
()
=
let
pos
=
make_position
1
1
"
test.ks
"
in
let
config
=
make_map_config
1024
()
in
let
map_decl
=
make_ast_map_declaration
"
metadata_test
"
U32
U64
Hash
config
true
~is_pinned:
false
pos
in
(*
Test metadata properties
*)
check
string
"
metadata map name
"
"
metadata_test
"
map_decl.name;
check
bool
"
metadata has key type
"
true
(map_decl.key_type
=
U32
);
check
bool
"
metadata has value type
"
true
(map_decl.value_type
=
U64
);
check
int
"
metadata size
"
1024
map_decl.config.max_entries
(*
* Test map serialization
*)
let
test_map_serialization
()
=
let
pos
=
make_position
1
1
"
test.ks
"
in
let
config
=
make_map_config
1024
()
in
let
original_map
=
make_ast_map_declaration
"
serialize_test
"
U32
U64
Hash
config
true
~is_pinned:
false
pos
in
(*
Test basic map properties instead of serialization
*)
check
string
"
serialization test map name
"
"
serialize_test
"
original_map.name;
check
bool
"
serialization test key type
"
true
(original_map.key_type
=
U32
);
check
bool
"
serialization test value type
"
true
(original_map.value_type
=
U64
)
let
maps_tests
=
[
"
basic_map_creation
"
,
`Quick
, test_basic_map_creation;
"
different_map_types
"
,
`Quick
, test_different_map_types;
"
map_key_value_types
"
,
`Quick
, test_map_key_value_types;
"
map_operations
"
,
`Quick
, test_map_operations;
"
map_initialization
"
,
`Quick
, test_map_initialization;
"
map_validation
"
,
`Quick
, test_map_validation;
"
map_program_integration
"
,
`Quick
, test_map_program_integration;
"
map_type_compatibility
"
,
`Quick
, test_map_type_compatibility;
"
map_size_validation
"
,
`Quick
, test_map_size_validation;
"
map_access_patterns
"
,
`Quick
, test_map_access_patterns;
"
map_error_handling
"
,
`Quick
, test_map_error_handling;
"
map_metadata
"
,
`Quick
, test_map_metadata;
"
map_serialization
"
,
`Quick
, test_map_serialization;
]
let
()
=
run
"
Maps Tests
"
[
"
maps
"
, maps_tests;
]
Back
|
FazBrowse Home
|
New Git URL