| 1 |
use crate::WasmValue; |
| 2 |
|
| 3 |
use super::{Type, Value}; |
| 4 |
|
| 5 |
#[test] |
| 6 |
fn simple_value_round_trips() { |
| 7 |
for val in [ |
| 8 |
Value::make_bool(true), |
| 9 |
Value::make_u8(u8::MAX), |
| 10 |
Value::make_u16(u16::MAX), |
| 11 |
Value::make_u32(u32::MAX), |
| 12 |
Value::make_u64(u64::MAX), |
| 13 |
Value::make_s8(i8::MIN), |
| 14 |
Value::make_s16(i16::MIN), |
| 15 |
Value::make_s32(i32::MIN), |
| 16 |
Value::make_s64(i64::MIN), |
| 17 |
Value::make_char('☃'), |
| 18 |
Value::make_string("str".into()), |
| 19 |
] { |
| 20 |
test_value_round_trip(val) |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
#[test] |
| 25 |
fn float_round_trips() { |
| 26 |
for (float32, float64) in [ |
| 27 |
(0.0, 0.0), |
| 28 |
(f32::MIN, f64::MIN), |
| 29 |
(f32::MIN_POSITIVE, f64::MIN_POSITIVE), |
| 30 |
(f32::MAX, f64::MAX), |
| 31 |
(f32::EPSILON, f64::EPSILON), |
| 32 |
(f32::INFINITY, f64::INFINITY), |
| 33 |
(f32::NEG_INFINITY, f64::NEG_INFINITY), |
| 34 |
] { |
| 35 |
test_value_round_trip(Value::make_f32(float32)); |
| 36 |
test_value_round_trip(Value::make_f64(float64)); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
#[test] |
| 41 |
fn list_round_trips() { |
| 42 |
let ty = Type::list(Type::U8); |
| 43 |
test_value_round_trip(Value::make_list(&ty, []).unwrap()); |
| 44 |
test_value_round_trip(Value::make_list(&ty, [Value::make_u8(1)]).unwrap()); |
| 45 |
test_value_round_trip(Value::make_list(&ty, [Value::make_u8(1), Value::make_u8(2)]).unwrap()); |
| 46 |
} |
| 47 |
|
| 48 |
#[test] |
| 49 |
fn record_round_trip() { |
| 50 |
let option_ty = Type::option(Type::U8); |
| 51 |
let record_ty = |
| 52 |
Type::record([("field-a", Type::BOOL), ("field-b", option_ty.clone())]).unwrap(); |
| 53 |
let record_val = Value::make_record( |
| 54 |
&record_ty, |
| 55 |
[ |
| 56 |
("field-a", Value::make_bool(true)), |
| 57 |
("field-b", Value::make_option(&option_ty, None).unwrap()), |
| 58 |
], |
| 59 |
) |
| 60 |
.unwrap(); |
| 61 |
test_value_round_trip(record_val) |
| 62 |
} |
| 63 |
|
| 64 |
#[test] |
| 65 |
fn tuple_round_trip() { |
| 66 |
let ty = Type::tuple([Type::BOOL, Type::U8]).unwrap(); |
| 67 |
let val = Value::make_tuple(&ty, [Value::make_bool(true), Value::make_u8(1)]).unwrap(); |
| 68 |
test_value_round_trip(val); |
| 69 |
} |
| 70 |
|
| 71 |
#[test] |
| 72 |
fn variant_round_trips() { |
| 73 |
let ty = Type::variant([("off", None), ("on", Some(Type::U8))]).unwrap(); |
| 74 |
test_value_round_trip(Value::make_variant(&ty, "off", None).unwrap()); |
| 75 |
test_value_round_trip(Value::make_variant(&ty, "on", Some(Value::make_u8(1))).unwrap()); |
| 76 |
} |
| 77 |
|
| 78 |
#[test] |
| 79 |
fn enum_round_trips() { |
| 80 |
let ty = Type::enum_ty(["north", "east", "south", "west"]).unwrap(); |
| 81 |
test_value_round_trip(Value::make_enum(&ty, "north").unwrap()); |
| 82 |
test_value_round_trip(Value::make_enum(&ty, "south").unwrap()); |
| 83 |
} |
| 84 |
|
| 85 |
#[test] |
| 86 |
fn option_round_trips() { |
| 87 |
let ty = Type::option(Type::U8); |
| 88 |
test_value_round_trip(Value::make_option(&ty, Some(Value::make_u8(1))).unwrap()); |
| 89 |
test_value_round_trip(Value::make_option(&ty, None).unwrap()); |
| 90 |
} |
| 91 |
|
| 92 |
#[test] |
| 93 |
fn result_round_trips() { |
| 94 |
let no_payloads = Type::result(None, None); |
| 95 |
let both_payloads = Type::result(Some(Type::U8), Some(Type::STRING)); |
| 96 |
let ok_only = Type::result(Some(Type::U8), None); |
| 97 |
let err_only = Type::result(None, Some(Type::STRING)); |
| 98 |
for (ty, payload) in [ |
| 99 |
(&no_payloads, Ok(None)), |
| 100 |
(&no_payloads, Err(None)), |
| 101 |
(&both_payloads, Ok(Some(Value::make_u8(1)))), |
| 102 |
(&both_payloads, Err(Some(Value::make_string("oops".into())))), |
| 103 |
(&ok_only, Ok(Some(Value::make_u8(1)))), |
| 104 |
(&ok_only, Err(None)), |
| 105 |
(&err_only, Ok(None)), |
| 106 |
(&err_only, Err(Some(Value::make_string("oops".into())))), |
| 107 |
] { |
| 108 |
let val = Value::make_result(ty, payload).unwrap(); |
| 109 |
test_value_round_trip(val); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
#[test] |
| 114 |
fn flags_round_trips() { |
| 115 |
let ty = Type::flags(["read", "write", "execute"]).unwrap(); |
| 116 |
test_value_round_trip(Value::make_flags(&ty, []).unwrap()); |
| 117 |
test_value_round_trip(Value::make_flags(&ty, ["write"]).unwrap()); |
| 118 |
test_value_round_trip(Value::make_flags(&ty, ["read", "execute"]).unwrap()); |
| 119 |
} |
| 120 |
|
| 121 |
fn local_ty(val: &Value) -> Type { |
| 122 |
use crate::value::{TypeEnum, ValueEnum}; |
| 123 |
match &val.0 { |
| 124 |
ValueEnum::Bool(_) => Type::BOOL, |
| 125 |
ValueEnum::S8(_) => Type::S8, |
| 126 |
ValueEnum::S16(_) => Type::S16, |
| 127 |
ValueEnum::S32(_) => Type::S32, |
| 128 |
ValueEnum::S64(_) => Type::S64, |
| 129 |
ValueEnum::U8(_) => Type::U8, |
| 130 |
ValueEnum::U16(_) => Type::U16, |
| 131 |
ValueEnum::U32(_) => Type::U32, |
| 132 |
ValueEnum::U64(_) => Type::U64, |
| 133 |
ValueEnum::F32(_) => Type::F32, |
| 134 |
ValueEnum::F64(_) => Type::F64, |
| 135 |
ValueEnum::Char(_) => Type::CHAR, |
| 136 |
ValueEnum::String(_) => Type::STRING, |
| 137 |
ValueEnum::List(inner) => Type(TypeEnum::List(inner.ty.clone())), |
| 138 |
ValueEnum::Record(inner) => Type(TypeEnum::Record(inner.ty.clone())), |
| 139 |
ValueEnum::Tuple(inner) => Type(TypeEnum::Tuple(inner.ty.clone())), |
| 140 |
ValueEnum::Variant(inner) => Type(TypeEnum::Variant(inner.ty.clone())), |
| 141 |
ValueEnum::Enum(inner) => Type(TypeEnum::Enum(inner.ty.clone())), |
| 142 |
ValueEnum::Option(inner) => Type(TypeEnum::Option(inner.ty.clone())), |
| 143 |
ValueEnum::Result(inner) => Type(TypeEnum::Result(inner.ty.clone())), |
| 144 |
ValueEnum::Flags(inner) => Type(TypeEnum::Flags(inner.ty.clone())), |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
fn test_value_round_trip(val: Value) { |
| 149 |
let ty = local_ty(&val); |
| 150 |
let serialized = crate::to_string(&val).unwrap(); |
| 151 |
let deserialized: Value = crate::from_str(&ty, &serialized) |
| 152 |
.unwrap_or_else(|err| panic!("failed to deserialize {serialized:?}: {err}")); |
| 153 |
assert_eq!(deserialized, val, "for {val:?}"); |
| 154 |
} |