| 1 |
#![allow(dead_code)] |
| 2 |
|
| 3 |
use enumset::*; |
| 4 |
use std::collections::{HashSet, BTreeSet}; |
| 5 |
|
| 6 |
#[derive(EnumSetType, Debug)] |
| 7 |
pub enum EmptyEnum { } |
| 8 |
|
| 9 |
#[derive(EnumSetType, Debug)] |
| 10 |
pub enum Enum1 { |
| 11 |
A, |
| 12 |
} |
| 13 |
|
| 14 |
#[derive(EnumSetType, Debug)] |
| 15 |
pub enum SmallEnum { |
| 16 |
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, |
| 17 |
} |
| 18 |
#[derive(Clone, Copy, Debug, EnumSetType, Eq, PartialEq)] |
| 19 |
#[enumset(no_super_impls)] |
| 20 |
pub enum SmallEnumExplicitDerive { |
| 21 |
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, |
| 22 |
} |
| 23 |
#[derive(EnumSetType, Debug)] |
| 24 |
#[enumset(repr = "u128")] |
| 25 |
pub enum LargeEnum { |
| 26 |
_00, _01, _02, _03, _04, _05, _06, _07, |
| 27 |
_10, _11, _12, _13, _14, _15, _16, _17, |
| 28 |
_20, _21, _22, _23, _24, _25, _26, _27, |
| 29 |
_30, _31, _32, _33, _34, _35, _36, _37, |
| 30 |
_40, _41, _42, _43, _44, _45, _46, _47, |
| 31 |
_50, _51, _52, _53, _54, _55, _56, _57, |
| 32 |
_60, _61, _62, _63, _64, _65, _66, _67, |
| 33 |
_70, _71, _72, _73, _74, _75, _76, _77, |
| 34 |
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, |
| 35 |
} |
| 36 |
#[derive(EnumSetType, Debug)] |
| 37 |
pub enum Enum8 { |
| 38 |
A, B, C, D, E, F, G, H, |
| 39 |
} |
| 40 |
#[derive(EnumSetType, Debug)] |
| 41 |
pub enum Enum128 { |
| 42 |
A, B, C, D, E, F, G, H, _8, _9, _10, _11, _12, _13, _14, _15, |
| 43 |
_16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, |
| 44 |
_32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, |
| 45 |
_48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, _62, _63, |
| 46 |
_64, _65, _66, _67, _68, _69, _70, _71, _72, _73, _74, _75, _76, _77, _78, _79, |
| 47 |
_80, _81, _82, _83, _84, _85, _86, _87, _88, _89, _90, _91, _92, _93, _94, _95, |
| 48 |
_96, _97, _98, _99, _100, _101, _102, _103, _104, _105, _106, _107, _108, _109, |
| 49 |
_110, _111, _112, _113, _114, _115, _116, _117, _118, _119, _120, _121, _122, |
| 50 |
_123, _124, _125, _126, _127, |
| 51 |
} |
| 52 |
#[derive(EnumSetType, Debug)] |
| 53 |
pub enum SparseEnum { |
| 54 |
A = 0xA, B = 20, C = 30, D = 40, E = 50, F = 60, G = 70, H = 80, |
| 55 |
} |
| 56 |
|
| 57 |
#[repr(u32)] |
| 58 |
#[derive(EnumSetType, Debug)] |
| 59 |
pub enum ReprEnum { |
| 60 |
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, |
| 61 |
} |
| 62 |
#[repr(u64)] |
| 63 |
#[derive(EnumSetType, Debug)] |
| 64 |
pub enum ReprEnum2 { |
| 65 |
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, |
| 66 |
} |
| 67 |
#[repr(isize)] |
| 68 |
#[derive(EnumSetType, Debug)] |
| 69 |
pub enum ReprEnum3 { |
| 70 |
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, |
| 71 |
} |
| 72 |
#[repr(C)] |
| 73 |
#[derive(EnumSetType, Debug)] |
| 74 |
pub enum ReprEnum4 { |
| 75 |
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, |
| 76 |
} |
| 77 |
#[derive(EnumSetType, Debug)] |
| 78 |
pub enum GiantEnum { |
| 79 |
A = 100, B = 200, C = 300, D = 400, E = 500, F = 600, G = 700, H = 800, |
| 80 |
} |
| 81 |
#[derive(EnumSetType, Debug)] |
| 82 |
#[enumset(repr = "array")] |
| 83 |
pub enum SmallArrayEnum { |
| 84 |
A, B, C, D, E, F, G, H |
| 85 |
} |
| 86 |
#[derive(EnumSetType, Debug)] |
| 87 |
#[enumset(repr = "array")] |
| 88 |
pub enum MarginalArrayEnumS2 { |
| 89 |
A, B, C, D, E, F, G, H, Marginal = 64, |
| 90 |
} |
| 91 |
#[derive(EnumSetType, Debug)] |
| 92 |
#[enumset(repr = "array")] |
| 93 |
pub enum MarginalArrayEnumS2H { |
| 94 |
A = 64, B, C, D, E, F, G, H, Marginal = 127, |
| 95 |
} |
| 96 |
#[derive(EnumSetType, Debug)] |
| 97 |
#[enumset(repr = "array")] |
| 98 |
pub enum MarginalArrayEnumS3 { |
| 99 |
A, B, C, D, E, F, G, H, Marginal = 128, |
| 100 |
} |
| 101 |
|
| 102 |
macro_rules! test_variants { |
| 103 |
($enum_name:ident $all_empty_test:ident $($variant:ident,)*) => { |
| 104 |
#[test] |
| 105 |
fn $all_empty_test() { |
| 106 |
let all = EnumSet::<$enum_name>::all(); |
| 107 |
let empty = EnumSet::<$enum_name>::empty(); |
| 108 |
|
| 109 |
$( |
| 110 |
assert!(!empty.contains($enum_name::$variant)); |
| 111 |
assert!(all.contains($enum_name::$variant)); |
| 112 |
)* |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
test_variants! { SmallEnum small_enum_all_empty |
| 117 |
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, |
| 118 |
} |
| 119 |
test_variants! { SmallEnumExplicitDerive small_enum_explicit_derive_all_empty |
| 120 |
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, |
| 121 |
} |
| 122 |
test_variants! { LargeEnum large_enum_all_empty |
| 123 |
_00, _01, _02, _03, _04, _05, _06, _07, |
| 124 |
_10, _11, _12, _13, _14, _15, _16, _17, |
| 125 |
_20, _21, _22, _23, _24, _25, _26, _27, |
| 126 |
_30, _31, _32, _33, _34, _35, _36, _37, |
| 127 |
_40, _41, _42, _43, _44, _45, _46, _47, |
| 128 |
_50, _51, _52, _53, _54, _55, _56, _57, |
| 129 |
_60, _61, _62, _63, _64, _65, _66, _67, |
| 130 |
_70, _71, _72, _73, _74, _75, _76, _77, |
| 131 |
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, |
| 132 |
} |
| 133 |
test_variants! { SparseEnum sparse_enum_all_empty |
| 134 |
A, B, C, D, E, F, G, |
| 135 |
} |
| 136 |
|
| 137 |
macro_rules! test_enum { |
| 138 |
($e:ident, $mem_size:expr) => { |
| 139 |
const CONST_SET: EnumSet<$e> = enum_set!($e::A | $e::C); |
| 140 |
const CONST_1_SET: EnumSet<$e> = enum_set!($e::A); |
| 141 |
const EMPTY_SET: EnumSet<$e> = EnumSet::EMPTY; |
| 142 |
#[test] |
| 143 |
fn const_set() { |
| 144 |
assert_eq!(CONST_SET.len(), 2); |
| 145 |
assert_eq!(CONST_1_SET.len(), 1); |
| 146 |
assert!(CONST_SET.contains($e::A)); |
| 147 |
assert!(CONST_SET.contains($e::C)); |
| 148 |
assert!(EMPTY_SET.is_empty()); |
| 149 |
} |
| 150 |
|
| 151 |
#[test] |
| 152 |
fn basic_add_remove() { |
| 153 |
let mut set = EnumSet::new(); |
| 154 |
set.insert($e::A); |
| 155 |
set.insert($e::B); |
| 156 |
set.insert($e::C); |
| 157 |
assert_eq!(set, $e::A | $e::B | $e::C); |
| 158 |
set.remove($e::B); |
| 159 |
assert_eq!(set, $e::A | $e::C); |
| 160 |
set.insert($e::D); |
| 161 |
assert_eq!(set, $e::A | $e::C | $e::D); |
| 162 |
set.insert_all($e::F | $e::E | $e::G); |
| 163 |
assert_eq!(set, $e::A | $e::C | $e::D | $e::F | $e::E | $e::G); |
| 164 |
set.remove_all($e::A | $e::D | $e::G); |
| 165 |
assert_eq!(set, $e::C | $e::F | $e::E); |
| 166 |
assert!(!set.is_empty()); |
| 167 |
set.clear(); |
| 168 |
assert!(set.is_empty()); |
| 169 |
} |
| 170 |
|
| 171 |
#[test] |
| 172 |
fn already_present_element() { |
| 173 |
let mut set = EnumSet::new(); |
| 174 |
assert!(set.insert($e::A)); |
| 175 |
assert!(!set.insert($e::A)); |
| 176 |
set.remove($e::A); |
| 177 |
assert!(set.insert($e::A)); |
| 178 |
} |
| 179 |
|
| 180 |
#[test] |
| 181 |
fn empty_is_empty() { |
| 182 |
assert_eq!(EnumSet::<$e>::empty().len(), 0) |
| 183 |
} |
| 184 |
|
| 185 |
#[test] |
| 186 |
fn all_len() { |
| 187 |
assert_eq!(EnumSet::<$e>::all().len(), EnumSet::<$e>::variant_count() as usize) |
| 188 |
} |
| 189 |
|
| 190 |
#[test] |
| 191 |
fn iter_test() { |
| 192 |
let mut set = EnumSet::new(); |
| 193 |
set.insert($e::A); |
| 194 |
set.insert($e::B); |
| 195 |
set.extend($e::C | $e::E); |
| 196 |
|
| 197 |
let mut set_2 = EnumSet::new(); |
| 198 |
let vec: Vec<_> = set.iter().collect(); |
| 199 |
for val in vec { |
| 200 |
assert!(!set_2.contains(val)); |
| 201 |
set_2.insert(val); |
| 202 |
} |
| 203 |
assert_eq!(set, set_2); |
| 204 |
|
| 205 |
let mut set_3 = EnumSet::new(); |
| 206 |
for val in set { |
| 207 |
assert!(!set_3.contains(val)); |
| 208 |
set_3.insert(val); |
| 209 |
} |
| 210 |
assert_eq!(set, set_3); |
| 211 |
|
| 212 |
let mut set_4 = EnumSet::new(); |
| 213 |
let vec: EnumSet<_> = set.into_iter().map(EnumSet::only).collect(); |
| 214 |
for val in vec { |
| 215 |
assert!(!set_4.contains(val)); |
| 216 |
set_4.insert(val); |
| 217 |
} |
| 218 |
assert_eq!(set, set_4); |
| 219 |
|
| 220 |
let mut set_5 = EnumSet::new(); |
| 221 |
let vec: EnumSet<_> = set.iter().collect(); |
| 222 |
for val in vec { |
| 223 |
assert!(!set_5.contains(val)); |
| 224 |
set_5.insert(val); |
| 225 |
} |
| 226 |
assert_eq!(set, set_5); |
| 227 |
} |
| 228 |
|
| 229 |
#[test] |
| 230 |
fn empty_iter_test() { |
| 231 |
for _ in EnumSet::<$e>::new() { |
| 232 |
panic!("should not happen"); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
#[test] |
| 237 |
fn iter_ordering_test() { |
| 238 |
let set_a = $e::A | $e::B | $e::E; |
| 239 |
let vec_a: Vec<_> = set_a.iter().collect(); |
| 240 |
assert_eq!(vec_a, &[$e::A, $e::B, $e::E]); |
| 241 |
let vec_a_rev: Vec<_> = set_a.iter().rev().collect(); |
| 242 |
assert_eq!(vec_a_rev, &[$e::E, $e::B, $e::A]); |
| 243 |
|
| 244 |
let set_b = $e::B | $e::C | $e::D | $e::G; |
| 245 |
let vec_b: Vec<_> = set_b.iter().collect(); |
| 246 |
assert_eq!(vec_b, &[$e::B, $e::C, $e::D, $e::G]); |
| 247 |
let vec_b_rev: Vec<_> = set_b.iter().rev().collect(); |
| 248 |
assert_eq!(vec_b_rev, &[$e::G, $e::D, $e::C, $e::B]); |
| 249 |
} |
| 250 |
|
| 251 |
fn check_iter_size_hint(set: EnumSet<$e>) { |
| 252 |
let count = set.len(); |
| 253 |
|
| 254 |
// check for forward iteration |
| 255 |
{ |
| 256 |
let mut itr = set.iter(); |
| 257 |
for idx in 0 .. count { |
| 258 |
assert_eq!(itr.size_hint(), (count-idx, Some(count-idx))); |
| 259 |
assert_eq!(itr.len(), count-idx); |
| 260 |
assert!(itr.next().is_some()); |
| 261 |
} |
| 262 |
assert_eq!(itr.size_hint(), (0, Some(0))); |
| 263 |
assert_eq!(itr.len(), 0); |
| 264 |
} |
| 265 |
|
| 266 |
// check for backwards iteration |
| 267 |
{ |
| 268 |
let mut itr = set.iter().rev(); |
| 269 |
for idx in 0 .. count { |
| 270 |
assert_eq!(itr.size_hint(), (count-idx, Some(count-idx))); |
| 271 |
assert_eq!(itr.len(), count-idx); |
| 272 |
assert!(itr.next().is_some()); |
| 273 |
} |
| 274 |
assert_eq!(itr.size_hint(), (0, Some(0))); |
| 275 |
assert_eq!(itr.len(), 0); |
| 276 |
} |
| 277 |
} |
| 278 |
#[test] |
| 279 |
fn test_iter_size_hint() { |
| 280 |
check_iter_size_hint(EnumSet::<$e>::new()); |
| 281 |
check_iter_size_hint(EnumSet::<$e>::all()); |
| 282 |
let mut set = EnumSet::new(); |
| 283 |
set.insert($e::A); |
| 284 |
set.insert($e::C); |
| 285 |
set.insert($e::E); |
| 286 |
check_iter_size_hint(set); |
| 287 |
} |
| 288 |
|
| 289 |
#[test] |
| 290 |
fn iter_ops_test() { |
| 291 |
let set = $e::A | $e::B | $e::C | $e::E; |
| 292 |
let set2 = set.iter().filter(|&v| v != $e::B).collect::<EnumSet<_>>(); |
| 293 |
assert_eq!(set2, $e::A | $e::C | $e::E); |
| 294 |
} |
| 295 |
|
| 296 |
#[test] |
| 297 |
fn basic_ops_test() { |
| 298 |
assert_eq!(($e::A | $e::B) | ($e::B | $e::C), $e::A | $e::B | $e::C); |
| 299 |
assert_eq!(($e::A | $e::B) & ($e::B | $e::C), $e::B); |
| 300 |
assert_eq!(($e::A | $e::B) ^ ($e::B | $e::C), $e::A | $e::C); |
| 301 |
assert_eq!(($e::A | $e::B) - ($e::B | $e::C), $e::A); |
| 302 |
assert_eq!($e::A | !$e::A, EnumSet::<$e>::all()); |
| 303 |
} |
| 304 |
|
| 305 |
#[test] |
| 306 |
fn mutable_ops_test() { |
| 307 |
let mut set = $e::A | $e::B; |
| 308 |
assert_eq!(set, $e::A | $e::B); |
| 309 |
set |= $e::C | $e::D; |
| 310 |
assert_eq!(set, $e::A | $e::B | $e::C | $e::D); |
| 311 |
set -= $e::C; |
| 312 |
assert_eq!(set, $e::A | $e::B | $e::D); |
| 313 |
set ^= $e::B | $e::E; |
| 314 |
assert_eq!(set, $e::A | $e::D | $e::E); |
| 315 |
set &= $e::A | $e::E | $e::F; |
| 316 |
assert_eq!(set, $e::A | $e::E); |
| 317 |
} |
| 318 |
|
| 319 |
#[test] |
| 320 |
fn basic_set_status() { |
| 321 |
assert!(($e::A | $e::B | $e::C).is_disjoint($e::D | $e::E | $e::F)); |
| 322 |
assert!(!($e::A | $e::B | $e::C | $e::D).is_disjoint($e::D | $e::E | $e::F)); |
| 323 |
assert!(($e::A | $e::B).is_subset($e::A | $e::B | $e::C)); |
| 324 |
assert!(!($e::A | $e::D).is_subset($e::A | $e::B | $e::C)); |
| 325 |
} |
| 326 |
|
| 327 |
#[test] |
| 328 |
fn debug_impl() { |
| 329 |
assert_eq!(format!("{:?}", $e::A | $e::B | $e::D), "EnumSet(A | B | D)"); |
| 330 |
} |
| 331 |
|
| 332 |
#[test] |
| 333 |
fn to_from_bits() { |
| 334 |
let value = $e::A | $e::C | $e::D | $e::F | $e::E | $e::G; |
| 335 |
if EnumSet::<$e>::bit_width() < 128 { |
| 336 |
assert_eq!(EnumSet::from_u128(value.as_u128()), value); |
| 337 |
} |
| 338 |
if EnumSet::<$e>::bit_width() < 64 { |
| 339 |
assert_eq!(EnumSet::from_u64(value.as_u64()), value); |
| 340 |
} |
| 341 |
if EnumSet::<$e>::bit_width() < 32 { |
| 342 |
assert_eq!(EnumSet::from_u32(value.as_u32()), value); |
| 343 |
} |
| 344 |
if EnumSet::<$e>::bit_width() < 16 { |
| 345 |
assert_eq!(EnumSet::from_u16(value.as_u16()), value); |
| 346 |
} |
| 347 |
if EnumSet::<$e>::bit_width() < 8 { |
| 348 |
assert_eq!(EnumSet::from_u8(value.as_u8()), value); |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
#[test] |
| 353 |
#[should_panic] |
| 354 |
fn too_many_bits() { |
| 355 |
if EnumSet::<$e>::variant_count() == 128 { |
| 356 |
panic!("(test skipped)") |
| 357 |
} |
| 358 |
EnumSet::<$e>::from_u128(!0); |
| 359 |
} |
| 360 |
|
| 361 |
#[test] |
| 362 |
fn match_const_test() { |
| 363 |
match CONST_SET { |
| 364 |
CONST_SET => { /* ok */ } |
| 365 |
_ => panic!("match fell through?"), |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
#[test] |
| 370 |
fn set_test() { |
| 371 |
const SET_TEST_A: EnumSet<$e> = enum_set!($e::A | $e::B | $e::C); |
| 372 |
const SET_TEST_B: EnumSet<$e> = enum_set!($e::A | $e::B | $e::D); |
| 373 |
const SET_TEST_C: EnumSet<$e> = enum_set!($e::A | $e::B | $e::E); |
| 374 |
const SET_TEST_D: EnumSet<$e> = enum_set!($e::A | $e::B | $e::F); |
| 375 |
const SET_TEST_E: EnumSet<$e> = enum_set!($e::A | $e::B | $e::G); |
| 376 |
macro_rules! test_set { |
| 377 |
($set:ident) => {{ |
| 378 |
assert!(!$set.contains(&SET_TEST_A)); |
| 379 |
assert!(!$set.contains(&SET_TEST_B)); |
| 380 |
assert!(!$set.contains(&SET_TEST_C)); |
| 381 |
assert!(!$set.contains(&SET_TEST_D)); |
| 382 |
assert!(!$set.contains(&SET_TEST_E)); |
| 383 |
$set.insert(SET_TEST_A); |
| 384 |
$set.insert(SET_TEST_C); |
| 385 |
assert!($set.contains(&SET_TEST_A)); |
| 386 |
assert!(!$set.contains(&SET_TEST_B)); |
| 387 |
assert!($set.contains(&SET_TEST_C)); |
| 388 |
assert!(!$set.contains(&SET_TEST_D)); |
| 389 |
assert!(!$set.contains(&SET_TEST_E)); |
| 390 |
$set.remove(&SET_TEST_C); |
| 391 |
$set.remove(&SET_TEST_D); |
| 392 |
assert!($set.contains(&SET_TEST_A)); |
| 393 |
assert!(!$set.contains(&SET_TEST_B)); |
| 394 |
assert!(!$set.contains(&SET_TEST_C)); |
| 395 |
assert!(!$set.contains(&SET_TEST_D)); |
| 396 |
assert!(!$set.contains(&SET_TEST_E)); |
| 397 |
$set.insert(SET_TEST_A); |
| 398 |
$set.insert(SET_TEST_D); |
| 399 |
assert!($set.contains(&SET_TEST_A)); |
| 400 |
assert!(!$set.contains(&SET_TEST_B)); |
| 401 |
assert!(!$set.contains(&SET_TEST_C)); |
| 402 |
assert!($set.contains(&SET_TEST_D)); |
| 403 |
assert!(!$set.contains(&SET_TEST_E)); |
| 404 |
}} |
| 405 |
} |
| 406 |
|
| 407 |
let mut hash_set = HashSet::new(); |
| 408 |
test_set!(hash_set); |
| 409 |
|
| 410 |
let mut tree_set = BTreeSet::new(); |
| 411 |
test_set!(tree_set); |
| 412 |
} |
| 413 |
|
| 414 |
#[test] |
| 415 |
fn sum_test() { |
| 416 |
let target = $e::A | $e::B | $e::D | $e::E | $e::G | $e::H; |
| 417 |
|
| 418 |
let list_a = [$e::A | $e::B, $e::D | $e::E, $e::G | $e::H]; |
| 419 |
let sum_a: EnumSet<$e> = list_a.iter().map(|x| *x).sum(); |
| 420 |
assert_eq!(target, sum_a); |
| 421 |
let sum_b: EnumSet<$e> = list_a.iter().sum(); |
| 422 |
assert_eq!(target, sum_b); |
| 423 |
|
| 424 |
let list_b = [$e::A, $e::B, $e::D, $e::E, $e::G, $e::H]; |
| 425 |
let sum_c: EnumSet<$e> = list_b.iter().map(|x| *x).sum(); |
| 426 |
assert_eq!(target, sum_c); |
| 427 |
let sum_d: EnumSet<$e> = list_b.iter().sum(); |
| 428 |
assert_eq!(target, sum_d); |
| 429 |
} |
| 430 |
|
| 431 |
#[test] |
| 432 |
fn check_size() { |
| 433 |
assert_eq!(::std::mem::size_of::<EnumSet<$e>>(), $mem_size); |
| 434 |
} |
| 435 |
} |
| 436 |
} |
| 437 |
macro_rules! tests { |
| 438 |
($m:ident, $($tt:tt)*) => { mod $m { use super::*; $($tt)*; } } |
| 439 |
} |
| 440 |
|
| 441 |
tests!(small_enum, test_enum!(SmallEnum, 4)); |
| 442 |
tests!(small_enum_explicit_derive, test_enum!(SmallEnumExplicitDerive, 4)); |
| 443 |
tests!(large_enum, test_enum!(LargeEnum, 16)); |
| 444 |
tests!(enum8, test_enum!(Enum8, 1)); |
| 445 |
tests!(enum128, test_enum!(Enum128, 16)); |
| 446 |
tests!(sparse_enum, test_enum!(SparseEnum, 16)); |
| 447 |
tests!(repr_enum_u32, test_enum!(ReprEnum, 4)); |
| 448 |
tests!(repr_enum_u64, test_enum!(ReprEnum2, 4)); |
| 449 |
tests!(repr_enum_isize, test_enum!(ReprEnum3, 4)); |
| 450 |
tests!(repr_enum_c, test_enum!(ReprEnum4, 4)); |
| 451 |
tests!(giant_enum, test_enum!(GiantEnum, 104)); |
| 452 |
tests!(small_array_enum, test_enum!(SmallArrayEnum, 8)); |
| 453 |
tests!(marginal_array_enum_s2, test_enum!(MarginalArrayEnumS2, 16)); |
| 454 |
tests!(marginal_array_enum_s2h, test_enum!(MarginalArrayEnumS2H, 16)); |
| 455 |
tests!(marginal_array_enum_s3, test_enum!(MarginalArrayEnumS3, 24)); |
| 456 |
|
| 457 |
#[derive(EnumSetType, Debug)] |
| 458 |
pub enum ThresholdEnum { |
| 459 |
A = 1, B, C, D, |
| 460 |
U8 = 0, U16 = 8, U32 = 16, U64 = 32, U128 = 64, |
| 461 |
} |
| 462 |
macro_rules! bits_tests { |
| 463 |
( |
| 464 |
$mod_name:ident, $threshold_expr:expr, ($($too_big_expr:expr),*), $ty:ty, |
| 465 |
$to:ident $try_to:ident $to_truncated:ident |
| 466 |
$from:ident $try_from:ident $from_truncated:ident |
| 467 |
) => { |
| 468 |
mod $mod_name { |
| 469 |
use super::*; |
| 470 |
use crate::ThresholdEnum::*; |
| 471 |
|
| 472 |
#[test] |
| 473 |
fn to_from_basic() { |
| 474 |
for &mask in &[ |
| 475 |
$threshold_expr | B | C | D, |
| 476 |
$threshold_expr | A | D, |
| 477 |
$threshold_expr | B | C, |
| 478 |
] { |
| 479 |
assert_eq!(mask, EnumSet::<ThresholdEnum>::$from(mask.$to())); |
| 480 |
assert_eq!(mask.$to_truncated(), mask.$to()); |
| 481 |
assert_eq!(Some(mask.$to()), mask.$try_to()) |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
#[test] |
| 486 |
#[should_panic] |
| 487 |
fn from_invalid() { |
| 488 |
let invalid_mask: $ty = 0x80; |
| 489 |
EnumSet::<ThresholdEnum>::$from(invalid_mask); |
| 490 |
} |
| 491 |
|
| 492 |
#[test] |
| 493 |
fn try_from_invalid() { |
| 494 |
assert!(EnumSet::<ThresholdEnum>::$try_from(0xFF).is_none()); |
| 495 |
} |
| 496 |
|
| 497 |
$( |
| 498 |
#[test] |
| 499 |
fn try_to_overflow() { |
| 500 |
let set: EnumSet<ThresholdEnum> = $too_big_expr.into(); |
| 501 |
assert!(set.$try_to().is_none()); |
| 502 |
} |
| 503 |
)* |
| 504 |
|
| 505 |
#[test] |
| 506 |
fn truncated_overflow() { |
| 507 |
let trunc_invalid = EnumSet::<ThresholdEnum>::$from_truncated(0xFE); |
| 508 |
assert_eq!(A | B | C | D, trunc_invalid); |
| 509 |
$( |
| 510 |
let set: EnumSet<ThresholdEnum> = $too_big_expr | A; |
| 511 |
assert_eq!(2, set.$to_truncated()); |
| 512 |
)* |
| 513 |
} |
| 514 |
} |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
bits_tests!(test_u8_bits, U8, (U16), u8, |
| 519 |
as_u8 try_as_u8 as_u8_truncated from_u8 try_from_u8 from_u8_truncated); |
| 520 |
bits_tests!(test_u16_bits, U16, (U32), u16, |
| 521 |
as_u16 try_as_u16 as_u16_truncated from_u16 try_from_u16 from_u16_truncated); |
| 522 |
bits_tests!(test_u32_bits, U32, (U64), u32, |
| 523 |
as_u32 try_as_u32 as_u32_truncated from_u32 try_from_u32 from_u32_truncated); |
| 524 |
bits_tests!(test_u64_bits, U64, (U128), u64, |
| 525 |
as_u64 try_as_u64 as_u64_truncated from_u64 try_from_u64 from_u64_truncated); |
| 526 |
bits_tests!(test_u128_bits, U128, (), u128, |
| 527 |
as_u128 try_as_u128 as_u128_truncated from_u128 try_from_u128 from_u128_truncated); |
| 528 |
bits_tests!(test_usize_bits, U32, (U128), usize, |
| 529 |
as_usize try_as_usize as_usize_truncated |
| 530 |
from_usize try_from_usize from_usize_truncated); |