Skip to content

Commit

Permalink
Add exhaustive canonical tests for enum configurations
Browse files Browse the repository at this point in the history
Test enum declarations with exhaustive configurations for the
following criterias:

1. truncated: is the enum width a multiple of 8 or not ?
    this characteristic has an impact for some language generators
2. complete: does the enum define all possible values ?
3. open: does the enum allow for unknown or undefined values ?
4. with range: does the enum use a tag range declarations ?
  • Loading branch information
hchataing committed Jan 2, 2024
1 parent 664c6bb commit c847947
Show file tree
Hide file tree
Showing 4 changed files with 338 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pdl-compiler/scripts/generate_cxx_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,9 @@ def generate_enum_declaration(decl: ast.EnumDeclaration) -> str:
enum_type = get_cxx_scalar_type(decl.width)
tag_decls = []
for t in decl.tags:
tag_decls.append(f"{t.id} = {hex(t.value)},")
# Exclude default tags: DEFAULT = ..
if t.value is not None:
tag_decls.append(f"{t.id} = {hex(t.value)},")

return dedent("""\
Expand All @@ -833,7 +835,9 @@ def generate_enum_to_text(decl: ast.EnumDeclaration) -> str:
enum_name = decl.id
tag_cases = []
for t in decl.tags:
tag_cases.append(f"case {enum_name}::{t.id}: return \"{t.id}\";")
# Exclude default tags: DEFAULT = ..
if t.value is not None:
tag_cases.append(f"case {enum_name}::{t.id}: return \"{t.id}\";")

return dedent("""\
Expand Down
7 changes: 7 additions & 0 deletions pdl-compiler/src/bin/generate-canonical-tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,13 @@ fn main() {
"Struct_FixedScalar_Field",
"Struct_Size_Field",
"Struct_Struct_Field",
"Enum_Incomplete_Truncated_Closed",
"Enum_Incomplete_Truncated_Open",
"Enum_Incomplete_Truncated_Closed_WithRange",
"Enum_Incomplete_Truncated_Open_WithRange",
"Enum_Complete_Truncated",
"Enum_Complete_Truncated_WithRange",
"Enum_Complete_WithRange",
],
&module_name,
);
Expand Down
98 changes: 98 additions & 0 deletions pdl-compiler/tests/canonical/le_test_file.pdl
Original file line number Diff line number Diff line change
Expand Up @@ -838,3 +838,101 @@ struct Struct_Optional_Struct_Field_ {
packet Struct_Optional_Struct_Field {
s: Struct_Optional_Struct_Field_,
}

// Enum declarations
//
// Test enum declarations with exhaustive configurations for the
// following criterias:
//
// 1. truncated: is the enum width a multiple of 8 or not ?
// this characteristic has an impact for some language generators
// 2. complete: does the enum define all possible values ?
// 3. open: does the enum allow for unknown or undefined values ?
// 4. with range: does the enum use a tag range declarations ?

enum Enum_Incomplete_Truncated_Closed_ : 3 {
A = 0,
B = 1,
}

packet Enum_Incomplete_Truncated_Closed {
e: Enum_Incomplete_Truncated_Closed_,
_reserved_ : 5,
}

enum Enum_Incomplete_Truncated_Open_ : 3 {
A = 0,
B = 1,
UNKNOWN = ..
}

packet Enum_Incomplete_Truncated_Open {
e: Enum_Incomplete_Truncated_Open_,
_reserved_ : 5,
}

enum Enum_Incomplete_Truncated_Closed_WithRange_ : 3 {
A = 0,
B = 1..6 {
X = 1,
Y = 2,
}
}

packet Enum_Incomplete_Truncated_Closed_WithRange {
e: Enum_Incomplete_Truncated_Closed_WithRange_,
_reserved_ : 5,
}

enum Enum_Incomplete_Truncated_Open_WithRange_ : 3 {
A = 0,
B = 1..6 {
X = 1,
Y = 2,
},
UNKNOWN = ..
}

packet Enum_Incomplete_Truncated_Open_WithRange {
e: Enum_Incomplete_Truncated_Open_WithRange_,
_reserved_ : 5,
}

enum Enum_Complete_Truncated_ : 3 {
A = 0,
B = 1,
C = 2,
D = 3,
E = 4,
F = 5,
G = 6,
H = 7,
}

packet Enum_Complete_Truncated {
e: Enum_Complete_Truncated_,
_reserved_ : 5,
}

enum Enum_Complete_Truncated_WithRange_ : 3 {
A = 0,
B = 1..7 {
X = 1,
Y = 2,
}
}

packet Enum_Complete_Truncated_WithRange {
e: Enum_Complete_Truncated_WithRange_,
_reserved_ : 5,
}

enum Enum_Complete_WithRange_ : 8 {
A = 0,
B = 1,
C = 2..255,
}

packet Enum_Complete_WithRange {
e: Enum_Complete_WithRange_,
}
227 changes: 227 additions & 0 deletions pdl-compiler/tests/canonical/le_test_vectors.json
Original file line number Diff line number Diff line change
Expand Up @@ -5095,5 +5095,232 @@
}
}
]
},
{
"packet": "Enum_Incomplete_Truncated_Closed",
"tests": [
{
"packed": "00",
"unpacked": {
"e": 0
}
},
{
"packed": "01",
"unpacked": {
"e": 1
}
}
]
},
{
"packet": "Enum_Incomplete_Truncated_Open",
"tests": [
{
"packed": "00",
"unpacked": {
"e": 0
}
},
{
"packed": "01",
"unpacked": {
"e": 1
}
},
{
"packed": "02",
"unpacked": {
"e": 2
}
}
]
},
{
"packet": "Enum_Incomplete_Truncated_Closed_WithRange",
"tests": [
{
"packed": "00",
"unpacked": {
"e": 0
}
},
{
"packed": "01",
"unpacked": {
"e": 1
}
},
{
"packed": "02",
"unpacked": {
"e": 2
}
}
]
},
{
"packet": "Enum_Incomplete_Truncated_Open_WithRange",
"tests": [
{
"packed": "00",
"unpacked": {
"e": 0
}
},
{
"packed": "01",
"unpacked": {
"e": 1
}
},
{
"packed": "02",
"unpacked": {
"e": 2
}
},
{
"packed": "03",
"unpacked": {
"e": 3
}
}
]
},
{
"packet": "Enum_Complete_Truncated",
"tests": [
{
"packed": "00",
"unpacked": {
"e": 0
}
},
{
"packed": "01",
"unpacked": {
"e": 1
}
},
{
"packed": "02",
"unpacked": {
"e": 2
}
},
{
"packed": "03",
"unpacked": {
"e": 3
}
},
{
"packed": "04",
"unpacked": {
"e": 4
}
},
{
"packed": "05",
"unpacked": {
"e": 5
}
},
{
"packed": "06",
"unpacked": {
"e": 6
}
},
{
"packed": "07",
"unpacked": {
"e": 7
}
}
]
},
{
"packet": "Enum_Complete_Truncated_WithRange",
"tests": [
{
"packed": "00",
"unpacked": {
"e": 0
}
},
{
"packed": "01",
"unpacked": {
"e": 1
}
},
{
"packed": "02",
"unpacked": {
"e": 2
}
},
{
"packed": "03",
"unpacked": {
"e": 3
}
},
{
"packed": "04",
"unpacked": {
"e": 4
}
},
{
"packed": "05",
"unpacked": {
"e": 5
}
},
{
"packed": "06",
"unpacked": {
"e": 6
}
},
{
"packed": "07",
"unpacked": {
"e": 7
}
}
]
},
{
"packet": "Enum_Complete_WithRange",
"tests": [
{
"packed": "00",
"unpacked": {
"e": 0
}
},
{
"packed": "01",
"unpacked": {
"e": 1
}
},
{
"packed": "02",
"unpacked": {
"e": 2
}
},
{
"packed": "ff",
"unpacked": {
"e": 255
}
}
]
}
]

0 comments on commit c847947

Please sign in to comment.