From 26a029d407be480d791972afb5975cf62c9360a6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 02:47:55 +0200 Subject: Adding upstream version 124.0.1. Signed-off-by: Daniel Baumann --- .../tests/compile-fail-serde/deprecation.rs | 20 + .../tests/compile-fail-serde/deprecation.stderr | 17 + .../enumset/tests/compile-fail/explicit_repr.rs | 17 + .../tests/compile-fail/explicit_repr.stderr | 45 ++ .../rust/enumset/tests/compile-fail/syntax.rs | 39 ++ .../rust/enumset/tests/compile-fail/syntax.stderr | 31 ++ .../rust/enumset/tests/compile-fail/variants.rs | 41 ++ .../enumset/tests/compile-fail/variants.stderr | 41 ++ .../rust/enumset/tests/compile-pass/no_imports.rs | 50 ++ .../rust/enumset/tests/compile-pass/no_std.rs | 50 ++ third_party/rust/enumset/tests/conversions.rs | 360 ++++++++++++++ third_party/rust/enumset/tests/ops.rs | 530 +++++++++++++++++++++ third_party/rust/enumset/tests/repr.rs | 23 + third_party/rust/enumset/tests/serde.rs | 137 ++++++ third_party/rust/enumset/tests/trybuild.rs | 11 + 15 files changed, 1412 insertions(+) create mode 100644 third_party/rust/enumset/tests/compile-fail-serde/deprecation.rs create mode 100644 third_party/rust/enumset/tests/compile-fail-serde/deprecation.stderr create mode 100644 third_party/rust/enumset/tests/compile-fail/explicit_repr.rs create mode 100644 third_party/rust/enumset/tests/compile-fail/explicit_repr.stderr create mode 100644 third_party/rust/enumset/tests/compile-fail/syntax.rs create mode 100644 third_party/rust/enumset/tests/compile-fail/syntax.stderr create mode 100644 third_party/rust/enumset/tests/compile-fail/variants.rs create mode 100644 third_party/rust/enumset/tests/compile-fail/variants.stderr create mode 100644 third_party/rust/enumset/tests/compile-pass/no_imports.rs create mode 100644 third_party/rust/enumset/tests/compile-pass/no_std.rs create mode 100644 third_party/rust/enumset/tests/conversions.rs create mode 100644 third_party/rust/enumset/tests/ops.rs create mode 100644 third_party/rust/enumset/tests/repr.rs create mode 100644 third_party/rust/enumset/tests/serde.rs create mode 100644 third_party/rust/enumset/tests/trybuild.rs (limited to 'third_party/rust/enumset/tests') diff --git a/third_party/rust/enumset/tests/compile-fail-serde/deprecation.rs b/third_party/rust/enumset/tests/compile-fail-serde/deprecation.rs new file mode 100644 index 0000000000..72c19e29db --- /dev/null +++ b/third_party/rust/enumset/tests/compile-fail-serde/deprecation.rs @@ -0,0 +1,20 @@ +#![deny(deprecated)] + +use enumset::*; +use serde_derive::*; + +#[derive(Serialize, Deserialize, EnumSetType, Debug)] +#[enumset(serialize_as_map)] +#[serde(crate="enumset::__internal::serde")] +pub enum MapEnum { + A, B, C, D, E, F, G, H, +} + +#[derive(Serialize, Deserialize, EnumSetType, Debug)] +#[enumset(serialize_as_list)] +#[serde(crate="enumset::__internal::serde")] +pub enum ListEnum { + A, B, C, D, E, F, G, H, +} + +fn main() {} diff --git a/third_party/rust/enumset/tests/compile-fail-serde/deprecation.stderr b/third_party/rust/enumset/tests/compile-fail-serde/deprecation.stderr new file mode 100644 index 0000000000..d514000952 --- /dev/null +++ b/third_party/rust/enumset/tests/compile-fail-serde/deprecation.stderr @@ -0,0 +1,17 @@ +error: use of deprecated constant `_::__enumset_derive__generated_warnings::_w`: #[enumset(serialize_as_map)] is deprecated. Use `#[enumset(serialize_repr = "map")]` instead. + --> tests/compile-fail-serde/deprecation.rs:7:11 + | +7 | #[enumset(serialize_as_map)] + | ^^^^^^^^^^^^^^^^ + | +note: the lint level is defined here + --> tests/compile-fail-serde/deprecation.rs:1:9 + | +1 | #![deny(deprecated)] + | ^^^^^^^^^^ + +error: use of deprecated constant `_::__enumset_derive__generated_warnings::_w`: #[enumset(serialize_as_list)] is deprecated. Use `#[enumset(serialize_repr = "list")]` instead. + --> tests/compile-fail-serde/deprecation.rs:14:11 + | +14 | #[enumset(serialize_as_list)] + | ^^^^^^^^^^^^^^^^^ diff --git a/third_party/rust/enumset/tests/compile-fail/explicit_repr.rs b/third_party/rust/enumset/tests/compile-fail/explicit_repr.rs new file mode 100644 index 0000000000..445cc61136 --- /dev/null +++ b/third_party/rust/enumset/tests/compile-fail/explicit_repr.rs @@ -0,0 +1,17 @@ +use enumset::*; + +#[derive(EnumSetType)] +enum OkayEnumButCantUseFromRepr { + Variant, +} + +#[derive(EnumSetType)] +#[enumset(repr = "array")] +enum OkayEnumButCantUseFromReprArray { + Variant, +} + +fn main() { + EnumSet::::from_repr(1); + EnumSet::::from_repr([1]); +} diff --git a/third_party/rust/enumset/tests/compile-fail/explicit_repr.stderr b/third_party/rust/enumset/tests/compile-fail/explicit_repr.stderr new file mode 100644 index 0000000000..93979448c9 --- /dev/null +++ b/third_party/rust/enumset/tests/compile-fail/explicit_repr.stderr @@ -0,0 +1,45 @@ +error[E0599]: the function or associated item `from_repr` exists for struct `EnumSet`, but its trait bounds were not satisfied + --> tests/compile-fail/explicit_repr.rs:15:44 + | +4 | enum OkayEnumButCantUseFromRepr { + | ------------------------------- + | | + | doesn't satisfy `<_ as EnumSetTypePrivate>::Repr = ::Repr` + | doesn't satisfy `OkayEnumButCantUseFromRepr: EnumSetTypeWithRepr` +... +15 | EnumSet::::from_repr(1); + | ^^^^^^^^^ function or associated item cannot be called on `EnumSet` due to unsatisfied trait bounds + | + = note: the following trait bounds were not satisfied: + `OkayEnumButCantUseFromRepr: EnumSetTypeWithRepr` + `::Repr = ::Repr` + which is required by `OkayEnumButCantUseFromRepr: EnumSetTypeWithRepr` +note: the trait `EnumSetTypeWithRepr` must be implemented + --> src/traits.rs + | + | / pub unsafe trait EnumSetTypeWithRepr: + | | EnumSetType + EnumSetTypePrivate::Repr> + | |________________________________________________________________________________^ + +error[E0599]: the function or associated item `from_repr` exists for struct `EnumSet`, but its trait bounds were not satisfied + --> tests/compile-fail/explicit_repr.rs:16:49 + | +10 | enum OkayEnumButCantUseFromReprArray { + | ------------------------------------ + | | + | doesn't satisfy `<_ as EnumSetTypePrivate>::Repr = ::Repr` + | doesn't satisfy `_: EnumSetTypeWithRepr` +... +16 | EnumSet::::from_repr([1]); + | ^^^^^^^^^ function or associated item cannot be called on `EnumSet` due to unsatisfied trait bounds + | + = note: the following trait bounds were not satisfied: + `OkayEnumButCantUseFromReprArray: EnumSetTypeWithRepr` + `::Repr = ::Repr` + which is required by `OkayEnumButCantUseFromReprArray: EnumSetTypeWithRepr` +note: the trait `EnumSetTypeWithRepr` must be implemented + --> src/traits.rs + | + | / pub unsafe trait EnumSetTypeWithRepr: + | | EnumSetType + EnumSetTypePrivate::Repr> + | |________________________________________________________________________________^ diff --git a/third_party/rust/enumset/tests/compile-fail/syntax.rs b/third_party/rust/enumset/tests/compile-fail/syntax.rs new file mode 100644 index 0000000000..33ff972101 --- /dev/null +++ b/third_party/rust/enumset/tests/compile-fail/syntax.rs @@ -0,0 +1,39 @@ +use enumset::*; + +#[derive(EnumSetType)] +#[repr(usize)] +enum BadRepr { + Variant, +} + +#[derive(EnumSetType)] +#[repr(usize)] +enum GenericEnum { + Variant, + FieldBlah(T), +} + +#[derive(EnumSetType)] +struct BadItemType { + +} + +#[derive(EnumSetType)] +#[enumset(repr = "u8", repr = "u16")] +enum MultipleReprs { + Variant, +} + +#[derive(EnumSetType)] +#[enumset(repr = "abcdef")] +enum InvalidRepr { + Variant, +} + +#[derive(EnumSetType)] +#[enumset(serialize_repr = "abcdef")] +enum InvalidSerdeRepr { + Variant, +} + +fn main() {} \ No newline at end of file diff --git a/third_party/rust/enumset/tests/compile-fail/syntax.stderr b/third_party/rust/enumset/tests/compile-fail/syntax.stderr new file mode 100644 index 0000000000..bd929d53e9 --- /dev/null +++ b/third_party/rust/enumset/tests/compile-fail/syntax.stderr @@ -0,0 +1,31 @@ +error: `#[derive(EnumSetType)]` cannot be used on enums with type parameters. + --> tests/compile-fail/syntax.rs:11:17 + | +11 | enum GenericEnum { + | ^^^ + +error: `#[derive(EnumSetType)]` may only be used on enums + --> tests/compile-fail/syntax.rs:17:1 + | +17 | / struct BadItemType { +18 | | +19 | | } + | |_^ + +error: Duplicate field `repr` + --> tests/compile-fail/syntax.rs:22:24 + | +22 | #[enumset(repr = "u8", repr = "u16")] + | ^^^^^^^^^^^^ + +error: `abcdef` is not a valid internal enumset representation. + --> tests/compile-fail/syntax.rs:28:18 + | +28 | #[enumset(repr = "abcdef")] + | ^^^^^^^^ + +error: `abcdef` is not a valid serialized representation. + --> tests/compile-fail/syntax.rs:34:28 + | +34 | #[enumset(serialize_repr = "abcdef")] + | ^^^^^^^^ diff --git a/third_party/rust/enumset/tests/compile-fail/variants.rs b/third_party/rust/enumset/tests/compile-fail/variants.rs new file mode 100644 index 0000000000..202d34028f --- /dev/null +++ b/third_party/rust/enumset/tests/compile-fail/variants.rs @@ -0,0 +1,41 @@ +use enumset::*; + +#[derive(EnumSetType)] +enum NegativeVariant { + Variant = -1, +} + +#[derive(EnumSetType)] +enum HasFields { + Variant(u32), +} + +#[derive(EnumSetType)] +#[enumset(repr = "u128")] +enum BadExplicitRepr { + Variant = 128, +} + +#[derive(EnumSetType)] +#[enumset(serialize_repr = "u8")] +enum BadSerializationRepr { + Variant = 8, +} + +#[derive(EnumSetType)] +enum NonLiteralRepr { + Variant = 1 + 1, +} + +#[derive(EnumSetType)] +#[enumset(repr = "u16")] +enum BadMemRepr { + Variant = 16, +} + +#[derive(EnumSetType)] +enum ExcessiveReprSize { + Variant = 0xFFFFFFD0, +} + +fn main() {} diff --git a/third_party/rust/enumset/tests/compile-fail/variants.stderr b/third_party/rust/enumset/tests/compile-fail/variants.stderr new file mode 100644 index 0000000000..635fa56c86 --- /dev/null +++ b/third_party/rust/enumset/tests/compile-fail/variants.stderr @@ -0,0 +1,41 @@ +error: Enum discriminants must not be negative. + --> tests/compile-fail/variants.rs:5:15 + | +5 | Variant = -1, + | ^^ + +error: `#[derive(EnumSetType)]` can only be used on fieldless enums. + --> tests/compile-fail/variants.rs:10:5 + | +10 | Variant(u32), + | ^^^^^^^^^^^^ + +error: `repr` is too small to contain the largest discriminant. + --> tests/compile-fail/variants.rs:16:5 + | +16 | Variant = 128, + | ^^^^^^^^^^^^^ + +error: `serialize_repr` is too small to contain the largest discriminant. + --> tests/compile-fail/variants.rs:22:5 + | +22 | Variant = 8, + | ^^^^^^^^^^^ + +error: Enum discriminants must be literal expressions. + --> tests/compile-fail/variants.rs:27:5 + | +27 | Variant = 1 + 1, + | ^^^^^^^^^^^^^^^ + +error: `repr` is too small to contain the largest discriminant. + --> tests/compile-fail/variants.rs:33:5 + | +33 | Variant = 16, + | ^^^^^^^^^^^^ + +error: Maximum discriminant allowed is `0xFFFFFFBF`. + --> tests/compile-fail/variants.rs:38:5 + | +38 | Variant = 0xFFFFFFD0, + | ^^^^^^^^^^^^^^^^^^^^ diff --git a/third_party/rust/enumset/tests/compile-pass/no_imports.rs b/third_party/rust/enumset/tests/compile-pass/no_imports.rs new file mode 100644 index 0000000000..ce5bbd1265 --- /dev/null +++ b/third_party/rust/enumset/tests/compile-pass/no_imports.rs @@ -0,0 +1,50 @@ +#![no_std] +#![allow(dead_code)] + +extern crate std as __renamed_std; // so we don't have compile issues, but ::std still errors. + +use enumset::EnumSetType; + +#[derive(EnumSetType)] +pub enum EmptyEnum { } + +#[derive(EnumSetType)] +pub enum Enum1 { + A, +} + +#[derive(EnumSetType)] +pub enum SmallEnum { + A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, +} +#[derive(EnumSetType)] +pub enum Enum128 { + A, B, C, D, E, F, G, H, _8, _9, _10, _11, _12, _13, _14, _15, + _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, + _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, + _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, _62, _63, + _64, _65, _66, _67, _68, _69, _70, _71, _72, _73, _74, _75, _76, _77, _78, _79, + _80, _81, _82, _83, _84, _85, _86, _87, _88, _89, _90, _91, _92, _93, _94, _95, + _96, _97, _98, _99, _100, _101, _102, _103, _104, _105, _106, _107, _108, _109, + _110, _111, _112, _113, _114, _115, _116, _117, _118, _119, _120, _121, _122, + _123, _124, _125, _126, _127, +} +#[derive(EnumSetType)] +pub enum SparseEnum { + A = 0xA, B = 20, C = 30, D = 40, E = 50, F = 60, G = 70, H = 80, +} + +#[repr(u32)] +#[derive(EnumSetType)] +pub enum ReprEnum { + A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, +} +#[repr(C)] +#[derive(EnumSetType)] +pub enum ReprEnum4 { + A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, +} + +pub fn main() { + +} diff --git a/third_party/rust/enumset/tests/compile-pass/no_std.rs b/third_party/rust/enumset/tests/compile-pass/no_std.rs new file mode 100644 index 0000000000..9d389a4acd --- /dev/null +++ b/third_party/rust/enumset/tests/compile-pass/no_std.rs @@ -0,0 +1,50 @@ +#![no_std] +#![allow(dead_code)] + +extern crate std as __renamed_std; // so we don't have compile issues, but ::std still errors. + +use enumset::*; + +#[derive(EnumSetType)] +pub enum EmptyEnum { } + +#[derive(EnumSetType)] +pub enum Enum1 { + A, +} + +#[derive(EnumSetType)] +pub enum SmallEnum { + A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, +} +#[derive(EnumSetType)] +pub enum Enum128 { + A, B, C, D, E, F, G, H, _8, _9, _10, _11, _12, _13, _14, _15, + _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, + _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, + _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, _62, _63, + _64, _65, _66, _67, _68, _69, _70, _71, _72, _73, _74, _75, _76, _77, _78, _79, + _80, _81, _82, _83, _84, _85, _86, _87, _88, _89, _90, _91, _92, _93, _94, _95, + _96, _97, _98, _99, _100, _101, _102, _103, _104, _105, _106, _107, _108, _109, + _110, _111, _112, _113, _114, _115, _116, _117, _118, _119, _120, _121, _122, + _123, _124, _125, _126, _127, +} +#[derive(EnumSetType)] +pub enum SparseEnum { + A = 0xA, B = 20, C = 30, D = 40, E = 50, F = 60, G = 70, H = 80, +} + +#[repr(u32)] +#[derive(EnumSetType)] +pub enum ReprEnum { + A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, +} +#[repr(C)] +#[derive(EnumSetType)] +pub enum ReprEnum4 { + A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, +} + +pub fn main() { + +} diff --git a/third_party/rust/enumset/tests/conversions.rs b/third_party/rust/enumset/tests/conversions.rs new file mode 100644 index 0000000000..054ef8f8b8 --- /dev/null +++ b/third_party/rust/enumset/tests/conversions.rs @@ -0,0 +1,360 @@ +use enumset::*; + +macro_rules! read_slice { + ($set:expr, $size:expr) => {{ + let mut arr = [0; $size]; + $set.copy_into_slice(&mut arr); + arr + }}; +} + +macro_rules! try_read_slice { + ($set:expr, $size:expr) => {{ + let mut arr = [0; $size]; + match $set.try_copy_into_slice(&mut arr) { + Some(()) => Some(arr), + None => None, + } + }}; +} + +macro_rules! read_slice_truncated { + ($set:expr, $size:expr) => {{ + let mut arr = [0; $size]; + $set.copy_into_slice_truncated(&mut arr); + arr + }}; +} + +#[derive(EnumSetType, Debug)] +pub enum Enum8 { + A, B, C, D, E, F, G, + // H omitted for non-existent bit test +} + +#[derive(EnumSetType, Debug)] +pub enum Enum16 { + A, B, C, D, E=8, F, G, H, +} + +#[derive(EnumSetType, Debug)] +pub enum Enum32 { + A, B, C, D, E=16, F, G, H, +} + +#[derive(EnumSetType, Debug)] +pub enum Enum64 { + A, B, C, D, E=32, F, G, H, +} + +#[derive(EnumSetType, Debug)] +pub enum Enum128 { + A, B, C, D, E=64, F, G, H, +} + +#[derive(EnumSetType, Debug)] +pub enum Enum192 { + A, B, C, D, E=128, F, G, H, +} + +#[derive(EnumSetType, Debug)] +pub enum Enum256 { + A, B, C, D, E=192, F, G, H, +} + +macro_rules! check_simple_conversion { + ($mod:ident, $e:ident) => { + mod $mod { + use super::*; + + #[test] + fn to_integer() { + assert_eq!(7, + ($e::A | $e::B | $e::C).as_u8()); + assert_eq!(7, + ($e::A | $e::B | $e::C).as_u16()); + assert_eq!(7, + ($e::A | $e::B | $e::C).as_u32()); + assert_eq!(7, + ($e::A | $e::B | $e::C).as_u64()); + assert_eq!(7, + ($e::A | $e::B | $e::C).as_u128()); + assert_eq!(7, + ($e::A | $e::B | $e::C).as_usize()); + } + + #[test] + fn try_from_integer() { + assert_eq!(Some($e::A | $e::B | $e::C), + EnumSet::try_from_u8(7)); + assert_eq!(None, + EnumSet::<$e>::try_from_u8(7 | (1 << 7))); + assert_eq!(None, + EnumSet::<$e>::try_from_u16(7 | (1 << 15))); + assert_eq!(None, + EnumSet::<$e>::try_from_u32(7 | (1 << 31))); + assert_eq!(None, + EnumSet::<$e>::try_from_usize(7 | (1 << 31))); + assert_eq!(None, + EnumSet::<$e>::try_from_u64(7 | (1 << 63))); + assert_eq!(None, + EnumSet::<$e>::try_from_u128(7 | (1 << 127))); + } + + #[test] + fn from_integer_truncated() { + assert_eq!($e::A | $e::B | $e::C, + EnumSet::from_u8_truncated(7)); + assert_eq!($e::A | $e::B | $e::C, + EnumSet::from_u8_truncated(7 | (1 << 7))); + assert_eq!($e::A | $e::B | $e::C, + EnumSet::from_u16_truncated(7 | (1 << 15))); + assert_eq!($e::A | $e::B | $e::C, + EnumSet::from_u32_truncated(7 | (1 << 31))); + assert_eq!($e::A | $e::B | $e::C, + EnumSet::from_usize_truncated(7 | (1 << 31))); + assert_eq!($e::A | $e::B | $e::C, + EnumSet::from_u64_truncated(7 | (1 << 63))); + assert_eq!($e::A | $e::B | $e::C, + EnumSet::from_u128_truncated(7 | (1 << 127))); + } + + #[test] + fn basic_to_array() { + // array tests + assert_eq!(($e::A | $e::B | $e::C).as_array_truncated(), + []); + assert_eq!(EnumSet::<$e>::EMPTY.as_array_truncated(), + []); + assert_eq!(($e::A | $e::B | $e::C).as_array(), + [7]); + assert_eq!(($e::A | $e::B | $e::C).as_array(), + [7, 0]); + assert_eq!(($e::A | $e::B | $e::C).as_array(), + [7, 0, 0]); + assert_eq!(($e::A | $e::B | $e::C).as_array(), + [7, 0, 0, 0]); + assert_eq!(($e::A | $e::B | $e::C).as_array(), + [7, 0, 0, 0, 0]); + + // slice tests + assert_eq!(read_slice!($e::A | $e::B | $e::C, 1), + [7]); + assert_eq!(read_slice!($e::A | $e::B | $e::C, 2), + [7, 0]); + assert_eq!(read_slice!($e::A | $e::B | $e::C, 3), + [7, 0, 0]); + assert_eq!(read_slice!($e::A | $e::B | $e::C, 4), + [7, 0, 0, 0]); + assert_eq!(read_slice!($e::A | $e::B | $e::C, 5), + [7, 0, 0, 0, 0]); + + // slice tests truncated + assert_eq!(read_slice_truncated!($e::A | $e::B | $e::C, 1), + [7]); + assert_eq!(read_slice_truncated!($e::A | $e::B | $e::C, 2), + [7, 0]); + assert_eq!(read_slice_truncated!($e::A | $e::B | $e::C, 3), + [7, 0, 0]); + assert_eq!(read_slice_truncated!($e::A | $e::B | $e::C, 4), + [7, 0, 0, 0]); + assert_eq!(read_slice_truncated!($e::A | $e::B | $e::C, 5), + [7, 0, 0, 0, 0]); + } + + #[test] + fn basic_from_array() { + // array tests + assert_eq!(EnumSet::<$e>::EMPTY, + EnumSet::<$e>::from_array([])); + assert_eq!($e::A | $e::B | $e::C, + EnumSet::<$e>::from_array([7])); + assert_eq!($e::A | $e::B | $e::C, + EnumSet::<$e>::from_array([7, 0, 0])); + assert_eq!($e::A | $e::B | $e::C, + EnumSet::<$e>::from_array([7, 0, 0, 0])); + assert_eq!($e::A | $e::B | $e::C, + EnumSet::<$e>::from_array([7, 0, 0, 0, 0])); + + // array tests + assert_eq!(EnumSet::<$e>::EMPTY, + EnumSet::<$e>::from_slice(&[])); + assert_eq!($e::A | $e::B | $e::C, + EnumSet::<$e>::from_slice(&[7])); + assert_eq!($e::A | $e::B | $e::C, + EnumSet::<$e>::from_slice(&[7, 0, 0])); + assert_eq!($e::A | $e::B | $e::C, + EnumSet::<$e>::from_slice(&[7, 0, 0, 0])); + assert_eq!($e::A | $e::B | $e::C, + EnumSet::<$e>::from_slice(&[7, 0, 0, 0, 0])); + } + + #[test] + fn basic_from_array_truncated() { + // array tests + assert_eq!(EnumSet::<$e>::EMPTY, + EnumSet::<$e>::from_array_truncated([])); + assert_eq!($e::A | $e::B | $e::C, + EnumSet::<$e>::from_array_truncated([7 | (1 << 31)])); + assert_eq!($e::A | $e::B | $e::C, + EnumSet::<$e>::from_array_truncated([7, 0, 16])); + assert_eq!($e::A | $e::B | $e::C, + EnumSet::<$e>::from_array_truncated([7, 0, 0, 16])); + assert_eq!($e::A | $e::B | $e::C, + EnumSet::<$e>::from_array_truncated([7, 0, 0, 0, 16])); + + // array tests + assert_eq!(EnumSet::<$e>::EMPTY, + EnumSet::<$e>::from_slice_truncated(&[])); + assert_eq!($e::A | $e::B | $e::C, + EnumSet::<$e>::from_slice_truncated(&[7 | (1 << 31)])); + assert_eq!($e::A | $e::B | $e::C, + EnumSet::<$e>::from_slice_truncated(&[7, 0, 16])); + assert_eq!($e::A | $e::B | $e::C, + EnumSet::<$e>::from_slice_truncated(&[7, 0, 0, 16])); + assert_eq!($e::A | $e::B | $e::C, + EnumSet::<$e>::from_slice_truncated(&[7, 0, 0, 0, 16])); + } + + #[test] + #[should_panic] + fn fail_from_u8() { + EnumSet::<$e>::from_u8(7 | (1 << 7)); + } + + #[test] + #[should_panic] + fn fail_from_u16() { + EnumSet::<$e>::from_u16(7 | (1 << 15)); + } + + #[test] + #[should_panic] + fn fail_from_u32() { + EnumSet::<$e>::from_u32(7 | (1 << 31)); + } + + #[test] + #[should_panic] + fn fail_from_usize() { + EnumSet::<$e>::from_usize(7 | (1 << 31)); + } + + #[test] + #[should_panic] + fn fail_from_u64() { + EnumSet::<$e>::from_u64(7 | (1 << 63)); + } + + #[test] + #[should_panic] + fn fail_from_u128() { + EnumSet::<$e>::from_u128(7 | (1 << 127)); + } + + #[test] + #[should_panic] + fn fail_to_array() { + ($e::A | $e::B | $e::C).as_array::<0>(); + } + + #[test] + #[should_panic] + fn fail_to_slice() { + read_slice!($e::A | $e::B | $e::C, 0); + } + + #[test] + #[should_panic] + fn fail_from_array_1() { + EnumSet::<$e>::from_array([7 | (1 << 63), 0, 0, 0]); + } + + #[test] + #[should_panic] + fn fail_from_slice_1() { + EnumSet::<$e>::from_slice(&[7 | (1 << 63), 0, 0, 0]); + } + + #[test] + #[should_panic] + fn fail_from_array_2() { + EnumSet::<$e>::from_array([7, 0, 0, 0, 1]); + } + + #[test] + #[should_panic] + fn fail_from_slice_2() { + EnumSet::<$e>::from_slice(&[7, 0, 0, 0, 1]); + } + } + }; +} + +check_simple_conversion!(enum_8_simple, Enum8); +check_simple_conversion!(enum_16_simple, Enum16); +check_simple_conversion!(enum_32_simple, Enum32); +check_simple_conversion!(enum_64_simple, Enum64); +check_simple_conversion!(enum_128_simple, Enum128); +check_simple_conversion!(enum_192_simple, Enum192); +check_simple_conversion!(enum_256_simple, Enum256); + +macro_rules! check_oversized_64 { + ($mod:ident, $e:ident) => { + mod $mod { + use super::*; + + #[test] + fn downcast_to_u64() { + assert_eq!(Some(7), ($e::A | $e::B | $e::C).try_as_u64()); + assert_eq!(Some([7]), ($e::A | $e::B | $e::C).try_as_array()); + assert_eq!(Some([7]), try_read_slice!($e::A | $e::B | $e::C, 1)); + + assert_eq!(None, ($e::E | $e::F | $e::G).try_as_u64()); + assert_eq!(None, ($e::E | $e::F | $e::G).try_as_array::<1>()); + assert_eq!(None, try_read_slice!($e::E | $e::F | $e::G, 1)); + } + + #[test] + fn downcast_to_u64_truncated() { + assert_eq!(0, ($e::E | $e::F | $e::G).as_u64_truncated()); + assert_eq!([0], ($e::E | $e::F | $e::G).as_array_truncated()); + assert_eq!([0], read_slice_truncated!($e::E | $e::F | $e::G, 1)); + } + + #[test] + #[should_panic] + fn fail_to_u64() { + ($e::E | $e::F | $e::G).as_u64(); + } + } + }; +} + +check_oversized_64!(enum_128_oversized_64, Enum128); +check_oversized_64!(enum_192_oversized_64, Enum192); +check_oversized_64!(enum_256_oversized_64, Enum256); + +macro_rules! check_oversized_128 { + ($mod:ident, $e:ident) => { + mod $mod { + use super::*; + + #[test] + fn downcast_to_u128() { + assert_eq!(Some(7), ($e::A | $e::B | $e::C).try_as_u128()); + assert_eq!(None, ($e::E | $e::F | $e::G).try_as_u128()); + assert_eq!(0, ($e::E | $e::F | $e::G).as_u128_truncated()); + } + + #[test] + #[should_panic] + fn fail_to_u128() { + ($e::E | $e::F | $e::G).as_u128(); + } + } + }; +} + +check_oversized_128!(enum_192_oversized_128, Enum192); +check_oversized_128!(enum_256_oversized_128, Enum256); diff --git a/third_party/rust/enumset/tests/ops.rs b/third_party/rust/enumset/tests/ops.rs new file mode 100644 index 0000000000..f9eef88f11 --- /dev/null +++ b/third_party/rust/enumset/tests/ops.rs @@ -0,0 +1,530 @@ +#![allow(dead_code)] + +use enumset::*; +use std::collections::{HashSet, BTreeSet}; + +#[derive(EnumSetType, Debug)] +pub enum EmptyEnum { } + +#[derive(EnumSetType, Debug)] +pub enum Enum1 { + A, +} + +#[derive(EnumSetType, Debug)] +pub enum SmallEnum { + A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, +} +#[derive(Clone, Copy, Debug, EnumSetType, Eq, PartialEq)] +#[enumset(no_super_impls)] +pub enum SmallEnumExplicitDerive { + A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, +} +#[derive(EnumSetType, Debug)] +#[enumset(repr = "u128")] +pub enum LargeEnum { + _00, _01, _02, _03, _04, _05, _06, _07, + _10, _11, _12, _13, _14, _15, _16, _17, + _20, _21, _22, _23, _24, _25, _26, _27, + _30, _31, _32, _33, _34, _35, _36, _37, + _40, _41, _42, _43, _44, _45, _46, _47, + _50, _51, _52, _53, _54, _55, _56, _57, + _60, _61, _62, _63, _64, _65, _66, _67, + _70, _71, _72, _73, _74, _75, _76, _77, + A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, +} +#[derive(EnumSetType, Debug)] +pub enum Enum8 { + A, B, C, D, E, F, G, H, +} +#[derive(EnumSetType, Debug)] +pub enum Enum128 { + A, B, C, D, E, F, G, H, _8, _9, _10, _11, _12, _13, _14, _15, + _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, + _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, + _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, _62, _63, + _64, _65, _66, _67, _68, _69, _70, _71, _72, _73, _74, _75, _76, _77, _78, _79, + _80, _81, _82, _83, _84, _85, _86, _87, _88, _89, _90, _91, _92, _93, _94, _95, + _96, _97, _98, _99, _100, _101, _102, _103, _104, _105, _106, _107, _108, _109, + _110, _111, _112, _113, _114, _115, _116, _117, _118, _119, _120, _121, _122, + _123, _124, _125, _126, _127, +} +#[derive(EnumSetType, Debug)] +pub enum SparseEnum { + A = 0xA, B = 20, C = 30, D = 40, E = 50, F = 60, G = 70, H = 80, +} + +#[repr(u32)] +#[derive(EnumSetType, Debug)] +pub enum ReprEnum { + A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, +} +#[repr(u64)] +#[derive(EnumSetType, Debug)] +pub enum ReprEnum2 { + A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, +} +#[repr(isize)] +#[derive(EnumSetType, Debug)] +pub enum ReprEnum3 { + A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, +} +#[repr(C)] +#[derive(EnumSetType, Debug)] +pub enum ReprEnum4 { + A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, +} +#[derive(EnumSetType, Debug)] +pub enum GiantEnum { + A = 100, B = 200, C = 300, D = 400, E = 500, F = 600, G = 700, H = 800, +} +#[derive(EnumSetType, Debug)] +#[enumset(repr = "array")] +pub enum SmallArrayEnum { + A, B, C, D, E, F, G, H +} +#[derive(EnumSetType, Debug)] +#[enumset(repr = "array")] +pub enum MarginalArrayEnumS2 { + A, B, C, D, E, F, G, H, Marginal = 64, +} +#[derive(EnumSetType, Debug)] +#[enumset(repr = "array")] +pub enum MarginalArrayEnumS2H { + A = 64, B, C, D, E, F, G, H, Marginal = 127, +} +#[derive(EnumSetType, Debug)] +#[enumset(repr = "array")] +pub enum MarginalArrayEnumS3 { + A, B, C, D, E, F, G, H, Marginal = 128, +} + +macro_rules! test_variants { + ($enum_name:ident $all_empty_test:ident $($variant:ident,)*) => { + #[test] + fn $all_empty_test() { + let all = EnumSet::<$enum_name>::all(); + let empty = EnumSet::<$enum_name>::empty(); + + $( + assert!(!empty.contains($enum_name::$variant)); + assert!(all.contains($enum_name::$variant)); + )* + } + } +} +test_variants! { SmallEnum small_enum_all_empty + A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, +} +test_variants! { SmallEnumExplicitDerive small_enum_explicit_derive_all_empty + A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, +} +test_variants! { LargeEnum large_enum_all_empty + _00, _01, _02, _03, _04, _05, _06, _07, + _10, _11, _12, _13, _14, _15, _16, _17, + _20, _21, _22, _23, _24, _25, _26, _27, + _30, _31, _32, _33, _34, _35, _36, _37, + _40, _41, _42, _43, _44, _45, _46, _47, + _50, _51, _52, _53, _54, _55, _56, _57, + _60, _61, _62, _63, _64, _65, _66, _67, + _70, _71, _72, _73, _74, _75, _76, _77, + A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, +} +test_variants! { SparseEnum sparse_enum_all_empty + A, B, C, D, E, F, G, +} + +macro_rules! test_enum { + ($e:ident, $mem_size:expr) => { + const CONST_SET: EnumSet<$e> = enum_set!($e::A | $e::C); + const CONST_1_SET: EnumSet<$e> = enum_set!($e::A); + const EMPTY_SET: EnumSet<$e> = EnumSet::EMPTY; + #[test] + fn const_set() { + assert_eq!(CONST_SET.len(), 2); + assert_eq!(CONST_1_SET.len(), 1); + assert!(CONST_SET.contains($e::A)); + assert!(CONST_SET.contains($e::C)); + assert!(EMPTY_SET.is_empty()); + } + + #[test] + fn basic_add_remove() { + let mut set = EnumSet::new(); + set.insert($e::A); + set.insert($e::B); + set.insert($e::C); + assert_eq!(set, $e::A | $e::B | $e::C); + set.remove($e::B); + assert_eq!(set, $e::A | $e::C); + set.insert($e::D); + assert_eq!(set, $e::A | $e::C | $e::D); + set.insert_all($e::F | $e::E | $e::G); + assert_eq!(set, $e::A | $e::C | $e::D | $e::F | $e::E | $e::G); + set.remove_all($e::A | $e::D | $e::G); + assert_eq!(set, $e::C | $e::F | $e::E); + assert!(!set.is_empty()); + set.clear(); + assert!(set.is_empty()); + } + + #[test] + fn already_present_element() { + let mut set = EnumSet::new(); + assert!(set.insert($e::A)); + assert!(!set.insert($e::A)); + set.remove($e::A); + assert!(set.insert($e::A)); + } + + #[test] + fn empty_is_empty() { + assert_eq!(EnumSet::<$e>::empty().len(), 0) + } + + #[test] + fn all_len() { + assert_eq!(EnumSet::<$e>::all().len(), EnumSet::<$e>::variant_count() as usize) + } + + #[test] + fn iter_test() { + let mut set = EnumSet::new(); + set.insert($e::A); + set.insert($e::B); + set.extend($e::C | $e::E); + + let mut set_2 = EnumSet::new(); + let vec: Vec<_> = set.iter().collect(); + for val in vec { + assert!(!set_2.contains(val)); + set_2.insert(val); + } + assert_eq!(set, set_2); + + let mut set_3 = EnumSet::new(); + for val in set { + assert!(!set_3.contains(val)); + set_3.insert(val); + } + assert_eq!(set, set_3); + + let mut set_4 = EnumSet::new(); + let vec: EnumSet<_> = set.into_iter().map(EnumSet::only).collect(); + for val in vec { + assert!(!set_4.contains(val)); + set_4.insert(val); + } + assert_eq!(set, set_4); + + let mut set_5 = EnumSet::new(); + let vec: EnumSet<_> = set.iter().collect(); + for val in vec { + assert!(!set_5.contains(val)); + set_5.insert(val); + } + assert_eq!(set, set_5); + } + + #[test] + fn empty_iter_test() { + for _ in EnumSet::<$e>::new() { + panic!("should not happen"); + } + } + + #[test] + fn iter_ordering_test() { + let set_a = $e::A | $e::B | $e::E; + let vec_a: Vec<_> = set_a.iter().collect(); + assert_eq!(vec_a, &[$e::A, $e::B, $e::E]); + let vec_a_rev: Vec<_> = set_a.iter().rev().collect(); + assert_eq!(vec_a_rev, &[$e::E, $e::B, $e::A]); + + let set_b = $e::B | $e::C | $e::D | $e::G; + let vec_b: Vec<_> = set_b.iter().collect(); + assert_eq!(vec_b, &[$e::B, $e::C, $e::D, $e::G]); + let vec_b_rev: Vec<_> = set_b.iter().rev().collect(); + assert_eq!(vec_b_rev, &[$e::G, $e::D, $e::C, $e::B]); + } + + fn check_iter_size_hint(set: EnumSet<$e>) { + let count = set.len(); + + // check for forward iteration + { + let mut itr = set.iter(); + for idx in 0 .. count { + assert_eq!(itr.size_hint(), (count-idx, Some(count-idx))); + assert_eq!(itr.len(), count-idx); + assert!(itr.next().is_some()); + } + assert_eq!(itr.size_hint(), (0, Some(0))); + assert_eq!(itr.len(), 0); + } + + // check for backwards iteration + { + let mut itr = set.iter().rev(); + for idx in 0 .. count { + assert_eq!(itr.size_hint(), (count-idx, Some(count-idx))); + assert_eq!(itr.len(), count-idx); + assert!(itr.next().is_some()); + } + assert_eq!(itr.size_hint(), (0, Some(0))); + assert_eq!(itr.len(), 0); + } + } + #[test] + fn test_iter_size_hint() { + check_iter_size_hint(EnumSet::<$e>::new()); + check_iter_size_hint(EnumSet::<$e>::all()); + let mut set = EnumSet::new(); + set.insert($e::A); + set.insert($e::C); + set.insert($e::E); + check_iter_size_hint(set); + } + + #[test] + fn iter_ops_test() { + let set = $e::A | $e::B | $e::C | $e::E; + let set2 = set.iter().filter(|&v| v != $e::B).collect::>(); + assert_eq!(set2, $e::A | $e::C | $e::E); + } + + #[test] + fn basic_ops_test() { + assert_eq!(($e::A | $e::B) | ($e::B | $e::C), $e::A | $e::B | $e::C); + assert_eq!(($e::A | $e::B) & ($e::B | $e::C), $e::B); + assert_eq!(($e::A | $e::B) ^ ($e::B | $e::C), $e::A | $e::C); + assert_eq!(($e::A | $e::B) - ($e::B | $e::C), $e::A); + assert_eq!($e::A | !$e::A, EnumSet::<$e>::all()); + } + + #[test] + fn mutable_ops_test() { + let mut set = $e::A | $e::B; + assert_eq!(set, $e::A | $e::B); + set |= $e::C | $e::D; + assert_eq!(set, $e::A | $e::B | $e::C | $e::D); + set -= $e::C; + assert_eq!(set, $e::A | $e::B | $e::D); + set ^= $e::B | $e::E; + assert_eq!(set, $e::A | $e::D | $e::E); + set &= $e::A | $e::E | $e::F; + assert_eq!(set, $e::A | $e::E); + } + + #[test] + fn basic_set_status() { + assert!(($e::A | $e::B | $e::C).is_disjoint($e::D | $e::E | $e::F)); + assert!(!($e::A | $e::B | $e::C | $e::D).is_disjoint($e::D | $e::E | $e::F)); + assert!(($e::A | $e::B).is_subset($e::A | $e::B | $e::C)); + assert!(!($e::A | $e::D).is_subset($e::A | $e::B | $e::C)); + } + + #[test] + fn debug_impl() { + assert_eq!(format!("{:?}", $e::A | $e::B | $e::D), "EnumSet(A | B | D)"); + } + + #[test] + fn to_from_bits() { + let value = $e::A | $e::C | $e::D | $e::F | $e::E | $e::G; + if EnumSet::<$e>::bit_width() < 128 { + assert_eq!(EnumSet::from_u128(value.as_u128()), value); + } + if EnumSet::<$e>::bit_width() < 64 { + assert_eq!(EnumSet::from_u64(value.as_u64()), value); + } + if EnumSet::<$e>::bit_width() < 32 { + assert_eq!(EnumSet::from_u32(value.as_u32()), value); + } + if EnumSet::<$e>::bit_width() < 16 { + assert_eq!(EnumSet::from_u16(value.as_u16()), value); + } + if EnumSet::<$e>::bit_width() < 8 { + assert_eq!(EnumSet::from_u8(value.as_u8()), value); + } + } + + #[test] + #[should_panic] + fn too_many_bits() { + if EnumSet::<$e>::variant_count() == 128 { + panic!("(test skipped)") + } + EnumSet::<$e>::from_u128(!0); + } + + #[test] + fn match_const_test() { + match CONST_SET { + CONST_SET => { /* ok */ } + _ => panic!("match fell through?"), + } + } + + #[test] + fn set_test() { + const SET_TEST_A: EnumSet<$e> = enum_set!($e::A | $e::B | $e::C); + const SET_TEST_B: EnumSet<$e> = enum_set!($e::A | $e::B | $e::D); + const SET_TEST_C: EnumSet<$e> = enum_set!($e::A | $e::B | $e::E); + const SET_TEST_D: EnumSet<$e> = enum_set!($e::A | $e::B | $e::F); + const SET_TEST_E: EnumSet<$e> = enum_set!($e::A | $e::B | $e::G); + macro_rules! test_set { + ($set:ident) => {{ + assert!(!$set.contains(&SET_TEST_A)); + assert!(!$set.contains(&SET_TEST_B)); + assert!(!$set.contains(&SET_TEST_C)); + assert!(!$set.contains(&SET_TEST_D)); + assert!(!$set.contains(&SET_TEST_E)); + $set.insert(SET_TEST_A); + $set.insert(SET_TEST_C); + assert!($set.contains(&SET_TEST_A)); + assert!(!$set.contains(&SET_TEST_B)); + assert!($set.contains(&SET_TEST_C)); + assert!(!$set.contains(&SET_TEST_D)); + assert!(!$set.contains(&SET_TEST_E)); + $set.remove(&SET_TEST_C); + $set.remove(&SET_TEST_D); + assert!($set.contains(&SET_TEST_A)); + assert!(!$set.contains(&SET_TEST_B)); + assert!(!$set.contains(&SET_TEST_C)); + assert!(!$set.contains(&SET_TEST_D)); + assert!(!$set.contains(&SET_TEST_E)); + $set.insert(SET_TEST_A); + $set.insert(SET_TEST_D); + assert!($set.contains(&SET_TEST_A)); + assert!(!$set.contains(&SET_TEST_B)); + assert!(!$set.contains(&SET_TEST_C)); + assert!($set.contains(&SET_TEST_D)); + assert!(!$set.contains(&SET_TEST_E)); + }} + } + + let mut hash_set = HashSet::new(); + test_set!(hash_set); + + let mut tree_set = BTreeSet::new(); + test_set!(tree_set); + } + + #[test] + fn sum_test() { + let target = $e::A | $e::B | $e::D | $e::E | $e::G | $e::H; + + let list_a = [$e::A | $e::B, $e::D | $e::E, $e::G | $e::H]; + let sum_a: EnumSet<$e> = list_a.iter().map(|x| *x).sum(); + assert_eq!(target, sum_a); + let sum_b: EnumSet<$e> = list_a.iter().sum(); + assert_eq!(target, sum_b); + + let list_b = [$e::A, $e::B, $e::D, $e::E, $e::G, $e::H]; + let sum_c: EnumSet<$e> = list_b.iter().map(|x| *x).sum(); + assert_eq!(target, sum_c); + let sum_d: EnumSet<$e> = list_b.iter().sum(); + assert_eq!(target, sum_d); + } + + #[test] + fn check_size() { + assert_eq!(::std::mem::size_of::>(), $mem_size); + } + } +} +macro_rules! tests { + ($m:ident, $($tt:tt)*) => { mod $m { use super::*; $($tt)*; } } +} + +tests!(small_enum, test_enum!(SmallEnum, 4)); +tests!(small_enum_explicit_derive, test_enum!(SmallEnumExplicitDerive, 4)); +tests!(large_enum, test_enum!(LargeEnum, 16)); +tests!(enum8, test_enum!(Enum8, 1)); +tests!(enum128, test_enum!(Enum128, 16)); +tests!(sparse_enum, test_enum!(SparseEnum, 16)); +tests!(repr_enum_u32, test_enum!(ReprEnum, 4)); +tests!(repr_enum_u64, test_enum!(ReprEnum2, 4)); +tests!(repr_enum_isize, test_enum!(ReprEnum3, 4)); +tests!(repr_enum_c, test_enum!(ReprEnum4, 4)); +tests!(giant_enum, test_enum!(GiantEnum, 104)); +tests!(small_array_enum, test_enum!(SmallArrayEnum, 8)); +tests!(marginal_array_enum_s2, test_enum!(MarginalArrayEnumS2, 16)); +tests!(marginal_array_enum_s2h, test_enum!(MarginalArrayEnumS2H, 16)); +tests!(marginal_array_enum_s3, test_enum!(MarginalArrayEnumS3, 24)); + +#[derive(EnumSetType, Debug)] +pub enum ThresholdEnum { + A = 1, B, C, D, + U8 = 0, U16 = 8, U32 = 16, U64 = 32, U128 = 64, +} +macro_rules! bits_tests { + ( + $mod_name:ident, $threshold_expr:expr, ($($too_big_expr:expr),*), $ty:ty, + $to:ident $try_to:ident $to_truncated:ident + $from:ident $try_from:ident $from_truncated:ident + ) => { + mod $mod_name { + use super::*; + use crate::ThresholdEnum::*; + + #[test] + fn to_from_basic() { + for &mask in &[ + $threshold_expr | B | C | D, + $threshold_expr | A | D, + $threshold_expr | B | C, + ] { + assert_eq!(mask, EnumSet::::$from(mask.$to())); + assert_eq!(mask.$to_truncated(), mask.$to()); + assert_eq!(Some(mask.$to()), mask.$try_to()) + } + } + + #[test] + #[should_panic] + fn from_invalid() { + let invalid_mask: $ty = 0x80; + EnumSet::::$from(invalid_mask); + } + + #[test] + fn try_from_invalid() { + assert!(EnumSet::::$try_from(0xFF).is_none()); + } + + $( + #[test] + fn try_to_overflow() { + let set: EnumSet = $too_big_expr.into(); + assert!(set.$try_to().is_none()); + } + )* + + #[test] + fn truncated_overflow() { + let trunc_invalid = EnumSet::::$from_truncated(0xFE); + assert_eq!(A | B | C | D, trunc_invalid); + $( + let set: EnumSet = $too_big_expr | A; + assert_eq!(2, set.$to_truncated()); + )* + } + } + } +} + +bits_tests!(test_u8_bits, U8, (U16), u8, + as_u8 try_as_u8 as_u8_truncated from_u8 try_from_u8 from_u8_truncated); +bits_tests!(test_u16_bits, U16, (U32), u16, + as_u16 try_as_u16 as_u16_truncated from_u16 try_from_u16 from_u16_truncated); +bits_tests!(test_u32_bits, U32, (U64), u32, + as_u32 try_as_u32 as_u32_truncated from_u32 try_from_u32 from_u32_truncated); +bits_tests!(test_u64_bits, U64, (U128), u64, + as_u64 try_as_u64 as_u64_truncated from_u64 try_from_u64 from_u64_truncated); +bits_tests!(test_u128_bits, U128, (), u128, + as_u128 try_as_u128 as_u128_truncated from_u128 try_from_u128 from_u128_truncated); +bits_tests!(test_usize_bits, U32, (U128), usize, + as_usize try_as_usize as_usize_truncated + from_usize try_from_usize from_usize_truncated); \ No newline at end of file diff --git a/third_party/rust/enumset/tests/repr.rs b/third_party/rust/enumset/tests/repr.rs new file mode 100644 index 0000000000..c7d9d7baa6 --- /dev/null +++ b/third_party/rust/enumset/tests/repr.rs @@ -0,0 +1,23 @@ +use enumset::*; + +#[derive(EnumSetType, Debug)] +#[enumset(repr = "u16")] +enum ReprEnum { + A, B, C, D, E, F, G, H, +} + +#[test] +fn test() { + let mut set = EnumSet::::new(); + set.insert(ReprEnum::B); + set.insert(ReprEnum::F); + + let repr: u16 = set.as_repr(); + assert_eq!( + (1 << 1) | (1 << 5), + repr, + ); + + let set2 = unsafe { EnumSet::::from_repr_unchecked(repr) }; + assert_eq!(set, set2); +} diff --git a/third_party/rust/enumset/tests/serde.rs b/third_party/rust/enumset/tests/serde.rs new file mode 100644 index 0000000000..42c1cab06c --- /dev/null +++ b/third_party/rust/enumset/tests/serde.rs @@ -0,0 +1,137 @@ +#![cfg(feature = "serde")] +#![allow(dead_code)] + +use enumset::*; +use serde_derive::*; + +// Test resistance against shadowed types. +type Some = (); +type None = (); +type Result = (); + +#[derive(Serialize, Deserialize, EnumSetType, Debug)] +#[enumset(serialize_repr = "list")] +#[serde(crate="serde2")] +pub enum ListEnum { + A, B, C, D, E, F, G, H, +} + +#[derive(Serialize, Deserialize, EnumSetType, Debug)] +#[enumset(serialize_repr = "map")] +#[serde(crate="serde2")] +pub enum MapEnum { + A, B, C, D, E, F, G, H, +} + +#[derive(EnumSetType, Debug)] +#[enumset(serialize_repr = "array")] +pub enum ArrayEnum { + A, B, C, D, E, F, G, H, +} + +#[derive(EnumSetType, Debug)] +pub enum LargeEnum { + A, B, C, D, E=200, F, G, H, +} + +#[derive(EnumSetType, Debug)] +#[enumset(serialize_repr = "u128")] +pub enum ReprEnum { + A, B, C, D, E, F, G, H, +} + +#[derive(EnumSetType, Debug)] +#[enumset(serialize_repr = "u128", serialize_deny_unknown)] +pub enum DenyUnknownEnum { + A, B, C, D, E, F, G, H, +} + +macro_rules! serde_test_simple { + ($e:ident, $ser_size:expr) => { + const VALUES: &[EnumSet<$e>] = &[ + enum_set!(), + enum_set!($e::A | $e::C | $e::D | $e::F | $e::E | $e::G), + enum_set!($e::A), + enum_set!($e::H), + enum_set!($e::A | $e::B), + enum_set!($e::A | $e::B | $e::C | $e::D), + enum_set!($e::A | $e::B | $e::C | $e::D | $e::F | $e::G | $e::H), + enum_set!($e::G | $e::H), + enum_set!($e::E | $e::F | $e::G | $e::H), + ]; + + #[test] + fn serialize_deserialize_test_bincode() { + for &value in VALUES { + let serialized = bincode::serialize(&value).unwrap(); + let deserialized = bincode::deserialize::>(&serialized).unwrap(); + assert_eq!(value, deserialized); + if $ser_size != !0 { + assert_eq!(serialized.len(), $ser_size); + } + } + } + + #[test] + fn serialize_deserialize_test_json() { + for &value in VALUES { + let serialized = serde_json::to_string(&value).unwrap(); + let deserialized = serde_json::from_str::>(&serialized).unwrap(); + assert_eq!(value, deserialized); + } + } + } +} +macro_rules! serde_test { + ($e:ident, $ser_size:expr) => { + serde_test_simple!($e, $ser_size); + + #[test] + fn deserialize_all_test() { + let serialized = bincode::serialize(&!0u128).unwrap(); + let deserialized = bincode::deserialize::>(&serialized).unwrap(); + assert_eq!(EnumSet::<$e>::all(), deserialized); + } + } +} +macro_rules! tests { + ($m:ident, $($tt:tt)*) => { mod $m { use super::*; $($tt)*; } } +} + +#[test] +fn test_deny_unknown() { + let serialized = bincode::serialize(&!0u128).unwrap(); + let deserialized = bincode::deserialize::>(&serialized); + assert!(deserialized.is_err()); +} + +#[test] +fn test_json_reprs_basic() { + assert_eq!(ListEnum::A | ListEnum::C | ListEnum::F, + serde_json::from_str::>(r#"["A","C","F"]"#).unwrap()); + assert_eq!(MapEnum::A | MapEnum::C | MapEnum::F, + serde_json::from_str::>(r#"{"A":true,"C":true,"F":true}"#).unwrap()); + assert_eq!(ReprEnum::A | ReprEnum::C | ReprEnum::D, + serde_json::from_str::>("13").unwrap()); + assert_eq!(r#"["A","C","F"]"#, + serde_json::to_string(&(ListEnum::A | ListEnum::C | ListEnum::F)).unwrap()); + assert_eq!(r#"{"A":true,"C":true,"F":true}"#, + serde_json::to_string(&(MapEnum::A | MapEnum::C | MapEnum::F)).unwrap()); + assert_eq!("13", + serde_json::to_string(&(ReprEnum::A | ReprEnum::C | ReprEnum::D)).unwrap()); +} + +#[test] +fn test_json_reprs_edge_cases() { + assert_eq!(MapEnum::A | MapEnum::C | MapEnum::F, + serde_json::from_str::>(r#"{"D":false,"A":true,"E":false,"C":true,"F":true}"#).unwrap()); + assert_eq!(LargeEnum::A | LargeEnum::B | LargeEnum::C | LargeEnum::D, + serde_json::from_str::>(r#"[15]"#).unwrap()); +} + +tests!(list_enum, serde_test_simple!(ListEnum, !0)); +tests!(map_enum, serde_test_simple!(MapEnum, !0)); +tests!(array_enum, serde_test_simple!(ArrayEnum, !0)); +tests!(large_enum, serde_test_simple!(LargeEnum, !0)); +tests!(repr_enum, serde_test!(ReprEnum, 16)); +tests!(deny_unknown_enum, serde_test_simple!(DenyUnknownEnum, 16)); diff --git a/third_party/rust/enumset/tests/trybuild.rs b/third_party/rust/enumset/tests/trybuild.rs new file mode 100644 index 0000000000..cd3ad2ec35 --- /dev/null +++ b/third_party/rust/enumset/tests/trybuild.rs @@ -0,0 +1,11 @@ +#[rustversion::nightly] +#[test] +fn ui() { + let t = trybuild::TestCases::new(); + + t.compile_fail("tests/compile-fail/*.rs"); + t.pass("tests/compile-pass/*.rs"); + + #[cfg(feature = "serde")] + t.compile_fail("tests/compile-fail-serde/*.rs"); +} -- cgit v1.2.3