Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion rust/src/instructions/math/vector/arithmetic/addv_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ pub fn addv_values(
found: get_type_name(value.clone()),
});
}
if matches!(
num_type,
PrimitiveTypes::Sht | PrimitiveTypes::Int | PrimitiveTypes::Lng | PrimitiveTypes::Oct
) && matches!(
value,
Value::Float16(_) | Value::Float32(_) | Value::Float64(_)
) {
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::Integer),
found: get_type_name(value.clone()),
});
}
}
Ok(match num_type {
PrimitiveTypes::Sht => Value::Array(addv_i16in(&arr_a, &arr_b)),
Expand All @@ -63,7 +76,7 @@ pub fn addv_values(
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::All),
found: num_type.directive(),
found: "unknown",
});
}
})
Expand Down Expand Up @@ -91,6 +104,23 @@ mod tests {
Value::Array(Arc::new(values))
}
#[test]
fn rejects_float_elements_for_integer_directives() {
let left = array(vec![Value::Float32(1.0)]);
let right = array(vec![Value::Int32(2)]);
assert!(matches!(
addv_values(left.clone(), right.clone(), PrimitiveTypes::Int, 24),
Err(VMError::TypeMismatch {
ip: 24,
expected: "Integer",
found: "Float"
})
));
let mut stack = Stack::from_vec(vec![left, right]);
let original = stack.clone();
assert!(addv_func(&mut stack, PrimitiveTypes::Int, 25).is_err());
assert_eq!(stack, original);
}
#[test]
fn reports_type_mismatch_without_mutating_stack() {
let mut stack = Stack::from_vec(vec![Value::Bool(false), array(vec![Value::Int32(1)])]);
let original = stack.clone();
Expand Down
30 changes: 28 additions & 2 deletions rust/src/instructions/math/vector/arithmetic/cosv_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ pub fn cosv_values(a_val: Value, num_type: PrimitiveTypes, ip: usize) -> Result<
found: get_type_name(a_val.clone()),
})?;
for value in arr_a.iter() {
if !value.is_number() {
if !matches!(
value,
Value::Float16(_) | Value::Float32(_) | Value::Float64(_)
) {
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::Float),
Expand All @@ -41,7 +44,7 @@ pub fn cosv_values(a_val: Value, num_type: PrimitiveTypes, ip: usize) -> Result<
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::Float),
found: expected_type(num_type, ExpectedCategory::All),
found: "unknown",
});
}
})
Expand All @@ -64,6 +67,29 @@ mod tests {
Value::Array(Arc::new(values))
}
#[test]
fn requires_float_elements_and_accepts_cross_width_values() {
assert!(
cosv_values(
array(vec![Value::Float16(half::f16::ZERO)]),
PrimitiveTypes::Dbl,
24
)
.is_ok()
);
for invalid in [Value::Int64(1), Value::Bool(false)] {
let value = array(vec![invalid.clone()]);
assert!(matches!(
cosv_values(value.clone(), PrimitiveTypes::Flt, 25),
Err(VMError::TypeMismatch { ip: 25, expected: "Float", found: actual })
if actual == get_type_name(invalid.clone())
));
let mut stack = Stack::from_vec(vec![value]);
let original = stack.clone();
assert!(cosv_func(&mut stack, PrimitiveTypes::Flt, 26).is_err());
assert_eq!(stack, original);
}
}
#[test]
fn reports_type_mismatch_without_mutating_stack() {
let mut stack = Stack::from_vec(vec![Value::Bool(false)]);
let original = stack.clone();
Expand Down
32 changes: 31 additions & 1 deletion rust/src/instructions/math/vector/arithmetic/divv_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ pub fn divv_values(
found: get_type_name(value.clone()),
});
}
if matches!(
num_type,
PrimitiveTypes::Sht | PrimitiveTypes::Int | PrimitiveTypes::Lng | PrimitiveTypes::Oct
) && matches!(
value,
Value::Float16(_) | Value::Float32(_) | Value::Float64(_)
) {
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::Integer),
found: get_type_name(value.clone()),
});
}
}
Ok(match num_type {
PrimitiveTypes::Sht => Value::Array(divv_i16in(&arr_a, &arr_b)),
Expand All @@ -61,7 +74,7 @@ pub fn divv_values(
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::All),
found: num_type.directive(),
found: "unknown",
});
}
})
Expand Down Expand Up @@ -89,6 +102,23 @@ mod tests {
Value::Array(Arc::new(values))
}
#[test]
fn rejects_float_elements_for_integer_directives() {
let left = array(vec![Value::Int64(2)]);
let right = array(vec![Value::Float32(1.0)]);
assert!(matches!(
divv_values(left.clone(), right.clone(), PrimitiveTypes::Lng, 24),
Err(VMError::TypeMismatch {
ip: 24,
expected: "Long",
found: "Float"
})
));
let mut stack = Stack::from_vec(vec![left, right]);
let original = stack.clone();
assert!(divv_func(&mut stack, PrimitiveTypes::Lng, 25).is_err());
assert_eq!(stack, original);
}
#[test]
fn reports_type_mismatch_without_mutating_stack() {
let mut stack = Stack::from_vec(vec![Value::Bool(false), array(vec![Value::Int32(1)])]);
let original = stack.clone();
Expand Down
32 changes: 31 additions & 1 deletion rust/src/instructions/math/vector/arithmetic/modv_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ pub fn modv_values(
found: get_type_name(value.clone()),
});
}
if matches!(
num_type,
PrimitiveTypes::Sht | PrimitiveTypes::Int | PrimitiveTypes::Lng | PrimitiveTypes::Oct
) && matches!(
value,
Value::Float16(_) | Value::Float32(_) | Value::Float64(_)
) {
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::Integer),
found: get_type_name(value.clone()),
});
}
}
Ok(match num_type {
PrimitiveTypes::Sht => Value::Array(modv_i16in(&arr_a, &arr_b)),
Expand All @@ -61,7 +74,7 @@ pub fn modv_values(
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::All),
found: num_type.directive(),
found: "unknown",
});
}
})
Expand Down Expand Up @@ -89,6 +102,23 @@ mod tests {
Value::Array(Arc::new(values))
}
#[test]
fn rejects_float_elements_for_integer_directives() {
let left = array(vec![Value::Float64(2.0)]);
let right = array(vec![Value::Int128(1)]);
assert!(matches!(
modv_values(left.clone(), right.clone(), PrimitiveTypes::Oct, 24),
Err(VMError::TypeMismatch {
ip: 24,
expected: "Octa",
found: "Double"
})
));
let mut stack = Stack::from_vec(vec![left, right]);
let original = stack.clone();
assert!(modv_func(&mut stack, PrimitiveTypes::Oct, 25).is_err());
assert_eq!(stack, original);
}
#[test]
fn reports_type_mismatch_without_mutating_stack() {
let mut stack = Stack::from_vec(vec![Value::Bool(false), array(vec![Value::Int32(1)])]);
let original = stack.clone();
Expand Down
32 changes: 31 additions & 1 deletion rust/src/instructions/math/vector/arithmetic/mulv_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ pub fn mulv_values(
found: get_type_name(value.clone()),
});
}
if matches!(
num_type,
PrimitiveTypes::Sht | PrimitiveTypes::Int | PrimitiveTypes::Lng | PrimitiveTypes::Oct
) && matches!(
value,
Value::Float16(_) | Value::Float32(_) | Value::Float64(_)
) {
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::Integer),
found: get_type_name(value.clone()),
});
}
}
Ok(match num_type {
PrimitiveTypes::Sht => Value::Array(mulv_i16in(&arr_a, &arr_b)),
Expand All @@ -61,7 +74,7 @@ pub fn mulv_values(
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::All),
found: expected_type(num_type, ExpectedCategory::All),
found: "unknown",
});
}
})
Expand Down Expand Up @@ -89,6 +102,23 @@ mod tests {
Value::Array(Arc::new(values))
}
#[test]
fn rejects_float_elements_for_integer_directives() {
let left = array(vec![Value::Float16(half::f16::ONE)]);
let right = array(vec![Value::Int16(2)]);
assert!(matches!(
mulv_values(left.clone(), right.clone(), PrimitiveTypes::Sht, 24),
Err(VMError::TypeMismatch {
ip: 24,
expected: "Short",
found: "Half"
})
));
let mut stack = Stack::from_vec(vec![left, right]);
let original = stack.clone();
assert!(mulv_func(&mut stack, PrimitiveTypes::Sht, 25).is_err());
assert_eq!(stack, original);
}
#[test]
fn reports_type_mismatch_without_mutating_stack() {
let mut stack = Stack::from_vec(vec![Value::Bool(false), array(vec![Value::Int32(1)])]);
let original = stack.clone();
Expand Down
31 changes: 30 additions & 1 deletion rust/src/instructions/math/vector/arithmetic/negv_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ pub fn negv_values(a_val: Value, num_type: PrimitiveTypes, ip: usize) -> Result<
found: get_type_name(value.clone()),
});
}
if matches!(
num_type,
PrimitiveTypes::Sht | PrimitiveTypes::Int | PrimitiveTypes::Lng | PrimitiveTypes::Oct
) && matches!(
value,
Value::Float16(_) | Value::Float32(_) | Value::Float64(_)
) {
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::Integer),
found: get_type_name(value.clone()),
});
}
}
Ok(match num_type {
PrimitiveTypes::Sht => Value::Array(negv_i16in(&arr_a)),
Expand All @@ -44,7 +57,7 @@ pub fn negv_values(a_val: Value, num_type: PrimitiveTypes, ip: usize) -> Result<
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::All),
found: num_type.directive(),
found: "unknown",
});
}
})
Expand All @@ -67,6 +80,22 @@ mod tests {
Value::Array(Arc::new(values))
}
#[test]
fn rejects_float_elements_for_integer_directives() {
let value = array(vec![Value::Float32(1.0)]);
assert!(matches!(
negv_values(value.clone(), PrimitiveTypes::Int, 24),
Err(VMError::TypeMismatch {
ip: 24,
expected: "Integer",
found: "Float"
})
));
let mut stack = Stack::from_vec(vec![value]);
let original = stack.clone();
assert!(negv_func(&mut stack, PrimitiveTypes::Int, 25).is_err());
assert_eq!(stack, original);
}
#[test]
fn reports_type_mismatch_without_mutating_stack() {
let mut stack = Stack::from_vec(vec![Value::Bool(false)]);
let original = stack.clone();
Expand Down
32 changes: 30 additions & 2 deletions rust/src/instructions/math/vector/arithmetic/powfv_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ pub fn powfv_values(
});
}
for value in arr_a.iter().chain(arr_b.iter()) {
if !value.is_number() {
if !matches!(
value,
Value::Float16(_) | Value::Float32(_) | Value::Float64(_)
) {
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::Float),
Expand All @@ -58,7 +61,7 @@ pub fn powfv_values(
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::Float),
found: expected_type(num_type, ExpectedCategory::All),
found: "unknown",
});
}
})
Expand Down Expand Up @@ -89,6 +92,31 @@ mod tests {
Value::Array(Arc::new(values))
}
#[test]
fn requires_float_elements_and_accepts_cross_width_values() {
assert!(
powfv_values(
array(vec![Value::Float16(half::f16::from_f32(2.0))]),
array(vec![Value::Float64(3.0)]),
PrimitiveTypes::Flt,
24
)
.is_ok()
);
for invalid in [Value::Int32(2), Value::Bool(false)] {
let left = array(vec![invalid.clone()]);
let right = array(vec![Value::Float32(3.0)]);
assert!(matches!(
powfv_values(left.clone(), right.clone(), PrimitiveTypes::Flt, 25),
Err(VMError::TypeMismatch { ip: 25, expected: "Float", found: actual })
if actual == get_type_name(invalid.clone())
));
let mut stack = Stack::from_vec(vec![left, right]);
let original = stack.clone();
assert!(powfv_func(&mut stack, PrimitiveTypes::Flt, 26).is_err());
assert_eq!(stack, original);
}
}
#[test]
fn reports_type_mismatch_without_mutating_stack() {
let mut stack = Stack::from_vec(vec![Value::Bool(false), array(vec![Value::Int32(1)])]);
let original = stack.clone();
Expand Down
Loading
Loading