Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
  • Loading branch information
acl-cqc committed Aug 2, 2023
1 parent e16cdb4 commit 7ab58ae
Showing 1 changed file with 43 additions and 4 deletions.
47 changes: 43 additions & 4 deletions src/ops/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,17 +330,21 @@ impl CustomConst for YamlConst {
#[cfg(test)]
mod test {
use cool_asserts::assert_matches;
use serde_yaml::Value;

use super::{typecheck::ConstIntError, Const, ConstValue};
use super::{typecheck::ConstIntError, Const, ConstValue, YamlConst};
use crate::{
builder::{BuildError, Container, DFGBuilder, Dataflow, DataflowHugr},
builder::{BuildError, DFGBuilder, Dataflow, DataflowHugr},
classic_row, type_row,
types::{ClassicType, SimpleRow, SimpleType},
values::{ConstTypeError, HashableValue, ValueOfType},
types::simple::Container,
types::type_param::TypeArg,
types::{ClassicType, CustomType, HashableType, SimpleRow, SimpleType, TypeTag},
values::{ConstTypeError, CustomCheckFail, HashableValue, ValueOfType},
};

#[test]
fn test_predicate() -> Result<(), BuildError> {
use crate::builder::Container;
let pred_rows = vec![
classic_row![ClassicType::i64(), ClassicType::F64],
type_row![],
Expand Down Expand Up @@ -406,4 +410,39 @@ mod test {
Err(ConstTypeError::TupleWrongLength)
);
}

#[test]
fn test_yaml_const() {
let typ_int = CustomType::new(
"mytype",
vec![TypeArg::ClassicType(ClassicType::Hashable(
HashableType::Int(8),
))],
"myrsrc",
TypeTag::Hashable,
);
let val = ConstValue::Opaque((Box::new(YamlConst {
typ: typ_int.clone(),
value: Value::Number(6.into()),
}),));
let SimpleType::Classic(classic_t) = typ_int.clone().into()
else {panic!("Hashable CustomType returned as non-Classic");};
assert_matches!(classic_t, ClassicType::Hashable(_));
val.check_type(&classic_t).unwrap();

// This misrepresents the CustomType, so doesn't really "have to work".
// But just as documentation of current behaviour:
val.check_type(&ClassicType::Container(Container::Opaque(typ_int.clone())))
.unwrap();

let typ_float = CustomType::new(
"mytype",
vec![TypeArg::ClassicType(ClassicType::F64)],
"myrsrc",
TypeTag::Hashable,
);
let t: SimpleType = typ_float.clone().into();
assert_matches!(val.check_type(&t.try_into().unwrap()),
Err(ConstTypeError::CustomCheckFail(CustomCheckFail::TypeMismatch(a, b))) => a == typ_int && b == typ_float);
}
}

0 comments on commit 7ab58ae

Please sign in to comment.