Skip to content

Commit

Permalink
test: add more tests for edge cases for const parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
tohrnii committed Dec 1, 2022
1 parent 2e43644 commit 1f00061
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
8 changes: 4 additions & 4 deletions parser/src/ast/boundary_constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ pub enum BoundaryExpr {
Const(u64),
/// Represents any named constant or variable.
Elem(Identifier),
/// Represents an element inside a constant or variable vector. The index is the index of
/// this value inside the vector.
/// Represents an element inside a constant or variable vector. [VectorAccess] contains the
/// name of the vector and the index of the element to access.
VecElem(VectorAccess),
/// Represents an element inside a constant or variable matrix. Indices idx_row and idx_col
/// are the indices of this value inside the matrix.
/// Represents an element inside a constant or variable matrix. [MatrixAccess] contains the
/// name of the matrix and indices of the element to access.
MatrixElem(MatrixAccess),
/// Represents a random value provided by the verifier. The inner value is the index of this
/// random value in the array of all random values.
Expand Down
7 changes: 7 additions & 0 deletions parser/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,17 @@ pub struct VectorAccess {
}

impl VectorAccess {
/// Creates a new [VectorAccess] instance with the specified identifier name and index.
pub fn new(name: Identifier, idx: u64) -> Self {
Self { name, idx }
}

/// Returns the name of the vector.
pub fn name(&self) -> &str {
&self.name.0
}

/// Returns the index of the element in the vector.
pub fn idx(&self) -> u64 {
self.idx
}
Expand All @@ -101,6 +104,7 @@ pub struct MatrixAccess {
}

impl MatrixAccess {
/// Creates a new [MatrixAccess] instance with the specified identifier name and index.
pub fn new(name: Identifier, col_idx: u64, row_idx: u64) -> Self {
Self {
name,
Expand All @@ -109,14 +113,17 @@ impl MatrixAccess {
}
}

/// Returns the name of the matrix.
pub fn name(&self) -> &str {
&self.name.0
}

/// Returns the column index of the element in the matrix.
pub fn col_idx(&self) -> u64 {
self.col_idx
}

/// Returns the row index of the element in the matrix.
pub fn row_idx(&self) -> u64 {
self.row_idx
}
Expand Down
58 changes: 58 additions & 0 deletions parser/src/parser/tests/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ fn constants_matrices() {
build_parse_test!(source).expect_ast(expected);
}

#[test]
fn const_matrix_unequal_number_of_cols() {
// This is invalid since the number of columns for the two rows are unequal. However this
// validation happens at the IR level.
// TODO: Add columns mismatch validation to IR
let source = "constants:
A: [[1, 2], [3, 4, 5]]";
let expected = Source(vec![SourceSection::Constants(vec![Constant::new(
Identifier("A".to_string()),
ConstantType::Matrix(vec![vec![1, 2], vec![3, 4, 5]]),
)])]);
build_parse_test!(source).expect_ast(expected);
}

#[test]
fn error_empty_constants_section() {
let source = "
Expand All @@ -91,3 +105,47 @@ fn err_lowercase_constant_name() {
));
build_parse_test!(source).expect_error(error);
}

#[test]
fn err_consts_with_non_int_values() {
let source = "constants:
A: a
B: 2";
build_parse_test!(source).expect_unrecognized_token();
}

#[test]
fn err_const_vectors_with_non_int_values() {
let source = "constants:
A: [1, a]
B: [2, 4]";
build_parse_test!(source).expect_unrecognized_token();
}

#[test]
fn err_vector_with_trailing_comma() {
let source = "constants:
A: [1, ]";
build_parse_test!(source).expect_unrecognized_token();
}

#[test]
fn err_matrix_with_trailing_comma() {
let source = "constants:
A: [[1, 2], ]";
build_parse_test!(source).expect_unrecognized_token();
}

#[test]
fn err_matrix_mixed_element_types() {
let source = "constants:
A: [1, [1, 2]]";
build_parse_test!(source).expect_unrecognized_token();
}

#[test]
fn err_invalid_matrix_element() {
let source = "constants:
A: [[1, 2], [3, [4, 5]]]";
build_parse_test!(source).expect_unrecognized_token();
}

0 comments on commit 1f00061

Please sign in to comment.