Skip to content

Commit

Permalink
integer range test coverage + fix for >isize::MAX ranges
Browse files Browse the repository at this point in the history
better coverage between top-level vs signed_ints member for various
ranges that map to primitives

fix for when >isize::MAX limits are used as we used `isize` when `i128`
would be better for covering all possibilities (`i64`, `u64`, `f64`)

float ranges now panic when they have a decimal part as we were silently
ignoring this. Issue created: #178
  • Loading branch information
rooooooooob committed Apr 17, 2023
1 parent d5b2f94 commit c2a2d32
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
25 changes: 15 additions & 10 deletions src/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,16 @@ fn parse_type_choices(
}
}

fn type2_to_number_literal(type2: &Type2) -> isize {
fn type2_to_number_literal(type2: &Type2) -> i128 {
match type2 {
Type2::UintValue { value, .. } => *value as isize,
Type2::IntValue { value, .. } => *value,
Type2::FloatValue { value, .. } => *value as isize,
Type2::UintValue { value, .. } => *value as i128,
Type2::IntValue { value, .. } => *value as i128,
Type2::FloatValue { value, .. } => {
// FloatToInt trait still experimental so just directly check
let as_int = *value as i128;
assert_eq!(as_int as f64, *value, "decimal not supported. Issue: https://github.com/dcSpark/cddl-codegen/issues/178");
as_int
},
_ => panic!(
"Value specified: {:?} must be a number literal to be used here",
type2
Expand Down Expand Up @@ -216,15 +221,15 @@ fn parse_control_operator(
match operator.operator {
RangeCtlOp::RangeOp { is_inclusive, .. } => {
let range_start = match type2 {
Type2::UintValue { value, .. } => *value as isize,
Type2::IntValue { value, .. } => *value,
Type2::FloatValue { value, .. } => *value as isize,
Type2::UintValue { value, .. } => *value as i128,
Type2::IntValue { value, .. } => *value as i128,
Type2::FloatValue { value, .. } => *value as i128,
_ => panic!("Number expected as range start. Found {:?}", type2),
};
let range_end = match operator.type2 {
Type2::UintValue { value, .. } => value as isize,
Type2::IntValue { value, .. } => value,
Type2::FloatValue { value, .. } => value as isize,
Type2::UintValue { value, .. } => value as i128,
Type2::IntValue { value, .. } => value as i128,
Type2::FloatValue { value, .. } => value as i128,
_ => unimplemented!("unsupported type in range control operator: {:?}", operator),
};
ControlOperator::Range((
Expand Down
14 changes: 7 additions & 7 deletions tests/core/input.cddl
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ u8 = uint .size 1
u16 = uint .le 65535
u32 = 0..4294967295
u64 = uint .size 8 ; 8 bytes
i8 = -128..127
i8 = -128 .. 127
i64 = int .size 8 ; 8 bytes

signed_ints = [
u_8: uint .size 1,
u_16: uint .le 65535,
u_32: 0..4294967295,
u_64: uint .size 8,
i_8: -128..127,
u_8: 0 .. 255,
u_16: uint .size 2,
u_32: uint .size 4,
u_64: uint .le 18446744073709551615,
i_8: int .size 1,
i_16: int .size 2,
i_32: int .size 4,
i_32: -2147483648 .. 2147483647,
i_64: int .size 8,
n_64: nint,
u64_max: 18446744073709551615,
Expand Down

0 comments on commit c2a2d32

Please sign in to comment.