2021-07-03 12:18:13 -04:00
|
|
|
use super::query_context::test::{Def, UltraMinimal};
|
|
|
|
|
use crate::maybe_transmutable::MaybeTransmutableQuery;
|
2023-04-27 14:38:32 -07:00
|
|
|
use crate::{layout, Reason};
|
2021-07-03 12:18:13 -04:00
|
|
|
use itertools::Itertools;
|
|
|
|
|
|
|
|
|
|
mod bool {
|
2023-06-12 16:35:23 -07:00
|
|
|
use crate::Answer;
|
|
|
|
|
|
2021-07-03 12:18:13 -04:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn should_permit_identity_transmutation_tree() {
|
|
|
|
|
println!("{:?}", layout::Tree::<!, !>::bool());
|
|
|
|
|
let answer = crate::maybe_transmutable::MaybeTransmutableQuery::new(
|
|
|
|
|
layout::Tree::<Def, !>::bool(),
|
|
|
|
|
layout::Tree::<Def, !>::bool(),
|
|
|
|
|
(),
|
2022-08-18 19:39:14 +00:00
|
|
|
crate::Assume { alignment: false, lifetimes: false, validity: true, safety: false },
|
2021-07-03 12:18:13 -04:00
|
|
|
UltraMinimal,
|
|
|
|
|
)
|
|
|
|
|
.answer();
|
2023-06-12 16:35:23 -07:00
|
|
|
assert_eq!(answer, Answer::Yes);
|
2021-07-03 12:18:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn should_permit_identity_transmutation_dfa() {
|
|
|
|
|
let answer = crate::maybe_transmutable::MaybeTransmutableQuery::new(
|
|
|
|
|
layout::Dfa::<!>::bool(),
|
|
|
|
|
layout::Dfa::<!>::bool(),
|
|
|
|
|
(),
|
2022-08-18 19:39:14 +00:00
|
|
|
crate::Assume { alignment: false, lifetimes: false, validity: true, safety: false },
|
2021-07-03 12:18:13 -04:00
|
|
|
UltraMinimal,
|
|
|
|
|
)
|
|
|
|
|
.answer();
|
2023-06-12 16:35:23 -07:00
|
|
|
assert_eq!(answer, Answer::Yes);
|
2021-07-03 12:18:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn should_permit_validity_expansion_and_reject_contraction() {
|
|
|
|
|
let un = layout::Tree::<Def, !>::uninhabited();
|
|
|
|
|
let b0 = layout::Tree::<Def, !>::from_bits(0);
|
|
|
|
|
let b1 = layout::Tree::<Def, !>::from_bits(1);
|
|
|
|
|
let b2 = layout::Tree::<Def, !>::from_bits(2);
|
|
|
|
|
|
|
|
|
|
let alts = [b0, b1, b2];
|
|
|
|
|
|
|
|
|
|
let into_layout = |alts: Vec<_>| {
|
|
|
|
|
alts.into_iter().fold(layout::Tree::<Def, !>::uninhabited(), layout::Tree::<Def, !>::or)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let into_set = |alts: Vec<_>| {
|
|
|
|
|
#[cfg(feature = "rustc")]
|
2023-04-06 01:58:53 +00:00
|
|
|
let mut set = crate::Set::default();
|
2021-07-03 12:18:13 -04:00
|
|
|
#[cfg(not(feature = "rustc"))]
|
2023-04-06 01:58:53 +00:00
|
|
|
let mut set = std::collections::HashSet::new();
|
2021-07-03 12:18:13 -04:00
|
|
|
set.extend(alts);
|
|
|
|
|
set
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for src_alts in alts.clone().into_iter().powerset() {
|
|
|
|
|
let src_layout = into_layout(src_alts.clone());
|
|
|
|
|
let src_set = into_set(src_alts.clone());
|
|
|
|
|
|
|
|
|
|
for dst_alts in alts.clone().into_iter().powerset().filter(|alts| !alts.is_empty()) {
|
|
|
|
|
let dst_layout = into_layout(dst_alts.clone());
|
|
|
|
|
let dst_set = into_set(dst_alts.clone());
|
|
|
|
|
|
|
|
|
|
if src_set.is_subset(&dst_set) {
|
|
|
|
|
assert_eq!(
|
2023-06-12 16:35:23 -07:00
|
|
|
Answer::Yes,
|
2021-07-03 12:18:13 -04:00
|
|
|
MaybeTransmutableQuery::new(
|
|
|
|
|
src_layout.clone(),
|
|
|
|
|
dst_layout.clone(),
|
|
|
|
|
(),
|
|
|
|
|
crate::Assume { validity: false, ..crate::Assume::default() },
|
|
|
|
|
UltraMinimal,
|
|
|
|
|
)
|
|
|
|
|
.answer(),
|
|
|
|
|
"{:?} SHOULD be transmutable into {:?}",
|
|
|
|
|
src_layout,
|
|
|
|
|
dst_layout
|
|
|
|
|
);
|
|
|
|
|
} else if !src_set.is_disjoint(&dst_set) {
|
|
|
|
|
assert_eq!(
|
2023-06-12 16:35:23 -07:00
|
|
|
Answer::Yes,
|
2021-07-03 12:18:13 -04:00
|
|
|
MaybeTransmutableQuery::new(
|
|
|
|
|
src_layout.clone(),
|
|
|
|
|
dst_layout.clone(),
|
|
|
|
|
(),
|
|
|
|
|
crate::Assume { validity: true, ..crate::Assume::default() },
|
|
|
|
|
UltraMinimal,
|
|
|
|
|
)
|
|
|
|
|
.answer(),
|
|
|
|
|
"{:?} SHOULD be transmutable (assuming validity) into {:?}",
|
|
|
|
|
src_layout,
|
|
|
|
|
dst_layout
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
assert_eq!(
|
2023-06-12 16:35:23 -07:00
|
|
|
Answer::No(Reason::DstIsBitIncompatible),
|
2021-07-03 12:18:13 -04:00
|
|
|
MaybeTransmutableQuery::new(
|
|
|
|
|
src_layout.clone(),
|
|
|
|
|
dst_layout.clone(),
|
|
|
|
|
(),
|
|
|
|
|
crate::Assume { validity: false, ..crate::Assume::default() },
|
|
|
|
|
UltraMinimal,
|
|
|
|
|
)
|
|
|
|
|
.answer(),
|
|
|
|
|
"{:?} should NOT be transmutable into {:?}",
|
|
|
|
|
src_layout,
|
|
|
|
|
dst_layout
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|