-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtest_enum.ml
More file actions
633 lines (541 loc) · 24.8 KB
/
Copy pathtest_enum.ml
File metadata and controls
633 lines (541 loc) · 24.8 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
(*
* 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.Symbol_table
open Kernelscript.Type_checker
open Kernelscript.Parse
open Alcotest
let dummy_pos = { line = 1; column = 1; filename = "test_enum.ml" }
(** Test enum auto-assignment functionality *)
let test_enum_auto_assignment () =
let process_enum_values values =
let rec process_values acc current_value = function
| [] -> List.rev acc
| (const_name, None) :: rest ->
(* Auto-assign current value *)
let processed_value = (const_name, Some (Signed64 (Int64.of_int current_value))) in
process_values (processed_value :: acc) (current_value + 1) rest
| (const_name, Some explicit_value) :: rest ->
(* Use explicit value and update current value *)
let processed_value = (const_name, Some explicit_value) in
let explicit_int = Int64.to_int (IntegerValue.to_int64 explicit_value) in
process_values (processed_value :: acc) (explicit_int + 1) rest
in
process_values [] 0 values
in
(* Test case 1: All auto-assigned values *)
let values1 = [("TCP", None); ("UDP", None); ("ICMP", None)] in
let result1 = process_enum_values values1 in
let expected1 = [("TCP", Some 0); ("UDP", Some 1); ("ICMP", Some 2)] in
check (list (pair string (option int))) "auto assignment" expected1 (List.map (fun (name, opt) -> (name, Option.map (fun v -> Int64.to_int (IntegerValue.to_int64 v)) opt)) result1);
(* Test case 2: Mixed explicit and auto values *)
let values2 = [("TCP", Some (Signed64 6L)); ("UDP", Some (Signed64 17L)); ("ICMP", None); ("UNKNOWN", None)] in
let result2 = process_enum_values values2 in
let expected2 = [("TCP", Some 6); ("UDP", Some 17); ("ICMP", Some 18); ("UNKNOWN", Some 19)] in
check (list (pair string (option int))) "mixed assignment" expected2 (List.map (fun (name, opt) -> (name, Option.map (fun v -> Int64.to_int (IntegerValue.to_int64 v)) opt)) result2);
(* Test case 3: Auto values with explicit override *)
let values3 = [("FIRST", None); ("SECOND", Some (Signed64 10L)); ("THIRD", None)] in
let result3 = process_enum_values values3 in
let expected3 = [("FIRST", Some 0); ("SECOND", Some 10); ("THIRD", Some 11)] in
check (list (pair string (option int))) "auto with override" expected3 (List.map (fun (name, opt) -> (name, Option.map (fun v -> Int64.to_int (IntegerValue.to_int64 v)) opt)) result3)
(** Test enum parsing and symbol table integration *)
let test_enum_symbol_table () =
let symbol_table = create_symbol_table () in
(* Create enum definition *)
let enum_values = [("XDP_ABORTED", Some (Signed64 0L)); ("XDP_DROP", Some (Signed64 1L)); ("XDP_PASS", Some (Signed64 2L))] in
let enum_def = EnumDef ("xdp_action", enum_values, { line = 1; column = 1; filename = "test" }) in
(* Add to symbol table *)
add_type_def symbol_table enum_def;
(* Verify enum type is registered *)
let enum_symbol = lookup_symbol symbol_table "xdp_action" in
check bool "enum type found" true (enum_symbol <> None);
(match enum_symbol with
| Some symbol ->
(match symbol.kind with
| TypeDef (EnumDef (name, values, _)) ->
check string "enum name" "xdp_action" name;
check int "enum value count" 3 (List.length values)
| _ -> check bool "wrong symbol kind" false true)
| None -> check bool "enum symbol not found" false true);
(* Verify enum constants are registered *)
let const1 = lookup_symbol symbol_table "XDP_ABORTED" in
let const2 = lookup_symbol symbol_table "XDP_DROP" in
let const3 = lookup_symbol symbol_table "XDP_PASS" in
check bool "enum constant 1 found" true (const1 <> None);
check bool "enum constant 2 found" true (const2 <> None);
check bool "enum constant 3 found" true (const3 <> None)
(** Test enum type checking and unification *)
let test_enum_type_checking () =
let empty_symbol_table = Kernelscript.Symbol_table.create_symbol_table () in
(* Add enum type to context *)
let enum_values = [("XDP_PASS", Some (Signed64 2L)); ("XDP_DROP", Some (Signed64 1L))] in
let enum_def = EnumDef ("xdp_action", enum_values, { line = 1; column = 1; filename = "test" }) in
let enum_type = Enum "xdp_action" in
let ctx = create_context empty_symbol_table [] in (* Provide empty AST for tests *)
Hashtbl.replace ctx.types "xdp_action" enum_def;
(* Test enum-integer unification *)
let unify_result1 = unify_types enum_type U32 in
check bool "enum unifies with u32" true (unify_result1 = Some U32);
let unify_result2 = unify_types U32 enum_type in
check bool "u32 unifies with enum" true (unify_result2 = Some U32);
(* Test enum-enum unification *)
let same_enum = Enum "xdp_action" in
let unify_result3 = unify_types enum_type same_enum in
check bool "enum unifies with same enum" true (unify_result3 = Some enum_type);
let different_enum = Enum "TcAction" in
let unify_result4 = unify_types enum_type different_enum in
check bool "enum doesn't unify with different enum" true (unify_result4 = None);
(* Test enum with non-integer types *)
let unify_result5 = unify_types enum_type Bool in
check bool "enum doesn't unify with bool" true (unify_result5 = None)
(** Test enum constant lookup and validation *)
let test_enum_constants () =
let symbol_table = create_symbol_table () in
(* Add enum with constants *)
let enum_values = [("PROTOCOL_TCP", Some (Signed64 6L)); ("PROTOCOL_UDP", Some (Signed64 17L)); ("PROTOCOL_ICMP", Some (Signed64 1L))] in
let enum_def = EnumDef ("Protocol", enum_values, { line = 1; column = 1; filename = "test" }) in
add_type_def symbol_table enum_def;
(* Test constant lookup *)
let tcp_const = lookup_symbol symbol_table "PROTOCOL_TCP" in
check bool "TCP constant found" true (tcp_const <> None);
(match tcp_const with
| Some symbol ->
(match symbol.kind with
| EnumConstant (enum_name, Some value) ->
check string "constant enum name" "Protocol" enum_name;
check int "TCP value" 6 (Int64.to_int (IntegerValue.to_int64 value))
| _ -> check bool "wrong constant kind" false true)
| None -> check bool "TCP constant not found" false true);
(* Test invalid constant lookup *)
let invalid_const = lookup_symbol symbol_table "INVALID" in
check bool "invalid constant not found" true (invalid_const = None)
(** Test enum code generation *)
let test_enum_code_generation () =
(* Test enum definition generation for eBPF C *)
let enum_name = "xdp_action" in
let enum_values = [("XDP_ABORTED", 0); ("XDP_DROP", 1); ("XDP_PASS", 2); ("XDP_TX", 3)] in
(* Simulate code generation *)
let generate_enum_c enum_name values =
let header = Printf.sprintf "enum %s {" enum_name in
let constants = List.mapi (fun i (name, value) ->
let comma = if i = List.length values - 1 then "" else "," in
Printf.sprintf " %s = %d%s" name value comma
) values in
let footer = "};" in
String.concat "\n" (header :: constants @ [footer])
in
let generated = generate_enum_c enum_name enum_values in
let expected_lines = [
"enum xdp_action {";
" XDP_ABORTED = 0,";
" XDP_DROP = 1,";
" XDP_PASS = 2,";
" XDP_TX = 3";
"};"
] in
let expected = String.concat "\n" expected_lines in
check string "enum C generation" expected generated
(** Test enum usage in expressions *)
let test_enum_expressions () =
let symbol_table = create_symbol_table () in
(* Add enum *)
let enum_values = [("XDP_PASS", Some (Signed64 2L)); ("XDP_DROP", Some (Signed64 1L))] in
let enum_def = EnumDef ("xdp_action", enum_values, { line = 1; column = 1; filename = "test" }) in
add_type_def symbol_table enum_def;
(* Verify the constant can be looked up *)
let symbol = lookup_symbol symbol_table "XDP_PASS" in
check bool "enum constant accessible" true (symbol <> None);
match symbol with
| Some s ->
(match s.kind with
| EnumConstant (_, Some value) ->
check int "enum constant value" 2 (Int64.to_int (IntegerValue.to_int64 value))
| _ -> check bool "wrong symbol type" false true)
| None -> check bool "enum constant not found" false true
(** Test enum edge cases *)
let test_enum_edge_cases () =
(* Test empty enum *)
let empty_enum = EnumDef ("Empty", [], { line = 1; column = 1; filename = "test" }) in
let symbol_table = create_symbol_table () in
add_type_def symbol_table empty_enum;
let empty_symbol = lookup_symbol symbol_table "Empty" in
check bool "empty enum registered" true (empty_symbol <> None);
(* Test enum with duplicate names (should be handled by symbol table) *)
let duplicate_values = [("SAME", Some (Signed64 1L)); ("SAME", Some (Signed64 2L))] in
let duplicate_enum = EnumDef ("Duplicate", duplicate_values, { line = 1; column = 1; filename = "test" }) in
(* This should either succeed (last wins) or fail gracefully *)
try
add_type_def symbol_table duplicate_enum;
(* If it succeeds, verify the behavior *)
let dup_symbol = lookup_symbol symbol_table "SAME" in
check bool "duplicate handled" true (dup_symbol <> None)
with
| Symbol_error _ ->
(* If it fails, that's also acceptable behavior *)
()
(** Test enum with large values *)
let test_enum_large_values () =
let large_values = [
("SMALL", None);
("MEDIUM", Some (Signed64 1000L));
("LARGE", Some (Signed64 65535L));
("VERY_LARGE", Some (Signed64 4294967295L)) (* Max u32 *)
] in
let process_enum_values values =
let rec process_values acc current_value = function
| [] -> List.rev acc
| (const_name, None) :: rest ->
let processed_value = (const_name, Some (Signed64 (Int64.of_int current_value))) in
process_values (processed_value :: acc) (current_value + 1) rest
| (const_name, Some explicit_value) :: rest ->
let processed_value = (const_name, Some explicit_value) in
let explicit_int = Int64.to_int (IntegerValue.to_int64 explicit_value) in
process_values (processed_value :: acc) (explicit_int + 1) rest
in
process_values [] 0 values
in
let result = process_enum_values large_values in
let expected = [
("SMALL", Some 0);
("MEDIUM", Some 1000);
("LARGE", Some 65535);
("VERY_LARGE", Some 4294967295)
] in
check (list (pair string (option int))) "large values handled" expected (List.map (fun (name, opt) -> (name, Option.map (fun v -> Int64.to_int (IntegerValue.to_int64 v)) opt)) result)
(** Test enum constant preservation in IR generation *)
let test_enum_ir_preservation () =
let open Kernelscript.Ir in
let open Kernelscript.Ir_generator in
(* Create a symbol table with enum constants *)
let symbol_table = create_symbol_table () in
let enum_values = [("TCP", Some (Signed64 6L)); ("UDP", Some (Signed64 17L)); ("ICMP", Some (Signed64 1L))] in
let enum_def = EnumDef ("IpProtocol", enum_values, { line = 1; column = 1; filename = "test" }) in
add_type_def symbol_table enum_def;
(* Create AST identifier expression for enum constant *)
let tcp_identifier = make_expr (Identifier "TCP") dummy_pos in
(* Generate IR from AST *)
let ctx = create_context symbol_table in
let ir_value = lower_expression ctx tcp_identifier in
(* Verify that the IR contains IREnumConstant, not IRLiteral *)
(match ir_value.value_desc with
| IREnumConstant (enum_name, constant_name, numeric_value) ->
check string "IR enum name" "IpProtocol" enum_name;
check string "IR constant name" "TCP" constant_name;
check int "IR constant value" 6 (Int64.to_int (IntegerValue.to_int64 numeric_value))
| IRLiteral _ -> check bool "should not be IRLiteral" false true
| _ -> check bool "wrong IR value type" false true)
(** Test enum constant preservation in C code generation *)
let test_enum_c_code_preservation () =
let open Kernelscript.Ebpf_c_codegen in
let open Kernelscript.Ir in
(* Create IREnumConstant value *)
let enum_constant = make_ir_value (IREnumConstant ("IpProtocol", "TCP", Signed64 6L)) IRU32 dummy_pos in
(* Create a simple eBPF context *)
let ctx = create_c_context () in
(* Generate C code *)
let c_code = generate_c_value ctx enum_constant in
(* Verify that C code contains the constant name, not numeric value *)
check string "C code uses constant name" "TCP" c_code;
(* Test that it doesn't generate numeric value *)
check bool "C code doesn't use numeric value" true (c_code <> "6")
(** Test enum definition inclusion using symbol table *)
let test_enum_definition_inclusion () =
(* Create a symbol table with enum definition *)
let symbol_table = create_symbol_table () in
let enum_values = [("TCP", Some (Signed64 6L)); ("UDP", Some (Signed64 17L)); ("ICMP", Some (Signed64 1L))] in
let enum_def = EnumDef ("IpProtocol", enum_values, { line = 1; column = 1; filename = "test" }) in
add_type_def symbol_table enum_def;
(* Test that the enum can be looked up from symbol table *)
let tcp_symbol = lookup_symbol symbol_table "TCP" in
check bool "TCP enum constant found in symbol table" true (tcp_symbol <> None);
(* Verify enum constant has correct value *)
(match tcp_symbol with
| Some symbol ->
(match symbol.kind with
| EnumConstant (enum_name, Some value) ->
check string "enum name" "IpProtocol" enum_name;
check int "TCP value" 6 (Int64.to_int (IntegerValue.to_int64 value))
| _ -> check bool "wrong symbol kind" false true)
| None -> check bool "TCP symbol not found" false true)
(** Test match expression with enum constants parsing *)
let test_match_enum_constants () =
(* Create symbol table with enum *)
let symbol_table = create_symbol_table () in
let enum_values = [("TCP", Some (Signed64 6L)); ("UDP", Some (Signed64 17L)); ("ICMP", Some (Signed64 1L))] in
let enum_def = EnumDef ("IpProtocol", enum_values, { line = 1; column = 1; filename = "test" }) in
add_type_def symbol_table enum_def;
(* Test that enum constants can be looked up *)
let tcp_symbol = lookup_symbol symbol_table "TCP" in
check bool "TCP enum constant found" true (tcp_symbol <> None);
(* Verify enum constant structure for match patterns *)
(match tcp_symbol with
| Some symbol ->
(match symbol.kind with
| EnumConstant (enum_name, Some value) ->
check string "match enum name" "IpProtocol" enum_name;
check string "match constant name" "TCP" "TCP";
check int "match constant value" 6 (Int64.to_int (IntegerValue.to_int64 value))
| _ -> check bool "wrong symbol kind for match" false true)
| None -> check bool "TCP symbol not found for match" false true)
(** Test that enum constants are NOT converted to numeric literals *)
let test_enum_not_numeric_literals () =
let open Kernelscript.Ir in
let open Kernelscript.Ir_generator in
(* Create symbol table with enum *)
let symbol_table = create_symbol_table () in
let enum_values = [("TCP", Some (Signed64 6L)); ("UDP", Some (Signed64 17L))] in
let enum_def = EnumDef ("IpProtocol", enum_values, { line = 1; column = 1; filename = "test" }) in
add_type_def symbol_table enum_def;
(* Create AST identifier for enum constant *)
let tcp_expr = make_expr (Identifier "TCP") dummy_pos in
(* Generate IR *)
let ctx = create_context symbol_table in
let ir_value = lower_expression ctx tcp_expr in
(* Verify it's NOT IRLiteral *)
(match ir_value.value_desc with
| IRLiteral _ -> check bool "should not be IRLiteral" false true
| IREnumConstant _ -> ()
| _ -> check bool "unexpected IR value type" false true)
(** Test complete enum preservation pipeline *)
let test_enum_preservation_pipeline () =
let open Kernelscript.Ebpf_c_codegen in
let open Kernelscript.Ir in
(* Create symbol table with enum *)
let symbol_table = create_symbol_table () in
let enum_values = [("XDP_PASS", Some (Signed64 2L)); ("XDP_DROP", Some (Signed64 1L))] in
let enum_def = EnumDef ("XdpAction", enum_values, { line = 1; column = 1; filename = "test" }) in
add_type_def symbol_table enum_def;
(* Create IR with enum constant *)
let enum_constant = make_ir_value (IREnumConstant ("XdpAction", "XDP_PASS", Signed64 2L)) IRU32 dummy_pos in
(* Create context for C generation *)
let ctx = create_c_context () in
(* Test C code generation *)
let c_code = generate_c_value ctx enum_constant in
check string "C code preserves enum constant" "XDP_PASS" c_code;
(* Test symbol table preservation *)
let pass_symbol = lookup_symbol symbol_table "XDP_PASS" in
check bool "XDP_PASS symbol found" true (pass_symbol <> None);
let drop_symbol = lookup_symbol symbol_table "XDP_DROP" in
check bool "XDP_DROP symbol found" true (drop_symbol <> None)
(** Test userspace enum preservation *)
let test_userspace_enum_preservation () =
let open Kernelscript.Userspace_codegen in
let open Kernelscript.Ir in
(* Create IREnumConstant value *)
let enum_constant = make_ir_value (IREnumConstant ("Protocol", "HTTP", Signed64 80L)) IRU32 dummy_pos in
(* Create a simple userspace context *)
let ctx = create_userspace_context () in
(* Generate userspace C code *)
let c_code = generate_c_value_from_ir ctx enum_constant in
(* Verify that userspace code also preserves enum constant names *)
check string "userspace C code uses constant name" "HTTP" c_code;
check bool "userspace C code doesn't use numeric value" true (c_code <> "80")
(** Test negative enum parsing - regression test for negative integer parsing bug *)
let test_negative_enum_parsing () =
let source = {|
enum test_enum {
NEGATIVE = -1,
ZERO = 0,
POSITIVE = 1,
LARGE_NEGATIVE = -999
}
|} in
let ast = parse_string source in
let enum_def = match ast with
| [TypeDef (EnumDef (name, variants, _))] ->
check string "enum name" "test_enum" name;
variants
| _ -> failwith "Expected single enum declaration"
in
(* Check that all values are parsed correctly *)
let expected = [
("NEGATIVE", Some (-1));
("ZERO", Some 0);
("POSITIVE", Some 1);
("LARGE_NEGATIVE", Some (-999))
] in
check (list (pair string (option int))) "enum variants" expected (List.map (fun (name, opt) -> (name, Option.map (fun v -> Int64.to_int (IntegerValue.to_int64 v)) opt)) enum_def)
let test_mixed_positive_negative_enum () =
let source = {|
enum mixed_values {
NEG_FIRST = -5,
ZERO = 0,
POS_EXPLICIT = 42,
NEG_AGAIN = -100,
AUTO_ASSIGNED,
POSITIVE_AFTER = 200
}
|} in
let ast = parse_string source in
let enum_def = match ast with
| [TypeDef (EnumDef (name, variants, _))] ->
check string "enum name" "mixed_values" name;
variants
| _ -> failwith "Expected single enum declaration"
in
(* Check that negative values are parsed correctly alongside positive values *)
let expected = [
("NEG_FIRST", Some (-5));
("ZERO", Some 0);
("POS_EXPLICIT", Some 42);
("NEG_AGAIN", Some (-100));
("AUTO_ASSIGNED", None); (* Auto-assigned value *)
("POSITIVE_AFTER", Some 200)
] in
check (list (pair string (option int))) "mixed enum variants" expected (List.map (fun (name, opt) -> (name, Option.map (fun v -> Int64.to_int (IntegerValue.to_int64 v)) opt)) enum_def)
let test_tc_action_enum () =
let source = {|
enum tc_action {
TC_ACT_UNSPEC = -1,
TC_ACT_OK = 0,
TC_ACT_RECLASSIFY = 1,
TC_ACT_SHOT = 2,
TC_ACT_PIPE = 3,
TC_ACT_STOLEN = 4,
TC_ACT_QUEUED = 5,
TC_ACT_REPEAT = 6,
TC_ACT_REDIRECT = 7,
TC_ACT_TRAP = 8,
}
|} in
let ast = parse_string source in
let enum_def = match ast with
| [TypeDef (EnumDef (name, variants, _))] ->
check string "enum name" "tc_action" name;
variants
| _ -> failwith "Expected single enum declaration"
in
(* Check the specific tc_action enum that was failing *)
let expected = [
("TC_ACT_UNSPEC", Some (-1));
("TC_ACT_OK", Some 0);
("TC_ACT_RECLASSIFY", Some 1);
("TC_ACT_SHOT", Some 2);
("TC_ACT_PIPE", Some 3);
("TC_ACT_STOLEN", Some 4);
("TC_ACT_QUEUED", Some 5);
("TC_ACT_REPEAT", Some 6);
("TC_ACT_REDIRECT", Some 7);
("TC_ACT_TRAP", Some 8)
] in
check (list (pair string (option int))) "tc_action variants" expected (List.map (fun (name, opt) -> (name, Option.map (fun v -> Int64.to_int (IntegerValue.to_int64 v)) opt)) enum_def)
let test_edge_case_negative_values () =
let source = {|
enum edge_cases {
VERY_NEGATIVE = -2147483648,
NEGATIVE_ONE = -1,
ZERO = 0,
POSITIVE_ONE = 1,
VERY_POSITIVE = 2147483647
}
|} in
let ast = parse_string source in
let enum_def = match ast with
| [TypeDef (EnumDef (name, variants, _))] ->
check string "enum name" "edge_cases" name;
variants
| _ -> failwith "Expected single enum declaration"
in
(* Check edge case values including minimum/maximum int32 values *)
let expected = [
("VERY_NEGATIVE", Some (-2147483648));
("NEGATIVE_ONE", Some (-1));
("ZERO", Some 0);
("POSITIVE_ONE", Some 1);
("VERY_POSITIVE", Some 2147483647)
] in
check (list (pair string (option int))) "edge case variants" expected (List.map (fun (name, opt) -> (name, Option.map (fun v -> Int64.to_int (IntegerValue.to_int64 v)) opt)) enum_def)
(** Test enum as array index *)
let test_enum_array_index () =
let source = {|
enum Protocol {
TCP = 6,
UDP = 17,
ICMP = 1
}
var protocol_stats : percpu_array<Protocol, u32>(32)
@helper
fn test_enum_index() -> u32 {
var proto = TCP
var count = protocol_stats[proto]
if (count != null) {
return count
} else {
return 0
}
}
@xdp fn packet_handler(ctx: *xdp_md) -> xdp_action {
return XDP_PASS
}
|} in
try
(* Parse the source *)
let ast = parse_string source in
(* Build symbol table with XDP builtin types *)
let symbol_table = Test_utils.Helpers.create_test_symbol_table ast in
(* Type check the AST *)
let _typed_ast = type_check_and_annotate_ast ~symbol_table:(Some symbol_table) ast in
(* If we reach here, type checking succeeded *)
()
with
| Type_error (msg, _) when String.contains msg 'A' && String.contains msg 'r' ->
(* If we get "Array index must be integer type" error, the test fails *)
check bool ("enum array index should be allowed: " ^ msg) false true
| Type_error (_, _) ->
(* Other type errors are acceptable for this test *)
()
| Parse_error (msg, _) ->
check bool ("parse error: " ^ msg) false true
| e ->
check bool ("unexpected error: " ^ Printexc.to_string e) false true
(** Main test suite *)
let () =
run "Enum Tests" [
"auto_assignment", [
test_case "basic auto assignment" `Quick test_enum_auto_assignment;
];
"symbol_table", [
test_case "enum symbol table integration" `Quick test_enum_symbol_table;
test_case "enum constants lookup" `Quick test_enum_constants;
];
"type_checking", [
test_case "enum type unification" `Quick test_enum_type_checking;
test_case "enum expressions" `Quick test_enum_expressions;
test_case "enum as array index" `Quick test_enum_array_index;
];
"code_generation", [
test_case "enum C code generation" `Quick test_enum_code_generation;
];
"edge_cases", [
test_case "enum edge cases" `Quick test_enum_edge_cases;
test_case "large enum values" `Quick test_enum_large_values;
];
"negative_parsing", [
test_case "basic negative enum parsing" `Quick test_negative_enum_parsing;
test_case "mixed positive and negative enum" `Quick test_mixed_positive_negative_enum;
test_case "tc_action enum parsing (regression test)" `Quick test_tc_action_enum;
test_case "edge case negative values" `Quick test_edge_case_negative_values;
];
"enum_preservation_bug_fix", [
test_case "enum constants preserved in IR" `Quick test_enum_ir_preservation;
test_case "enum constants preserved in C code" `Quick test_enum_c_code_preservation;
test_case "enum definitions included in generated code" `Quick test_enum_definition_inclusion;
test_case "match expressions with enum constants" `Quick test_match_enum_constants;
test_case "enum constants not converted to numeric literals" `Quick test_enum_not_numeric_literals;
test_case "complete enum preservation pipeline" `Quick test_enum_preservation_pipeline;
test_case "userspace enum preservation" `Quick test_userspace_enum_preservation;
];
]