#[test_only] module std::option_tests { use std::option; use std::vector; #[test] fun option_none_is_none() { let none = option::none(); assert!(option::is_none(&none), 0); assert!(!option::is_some(&none), 1); } #[test] fun option_some_is_some() { let some = option::some(5); assert!(!option::is_none(&some), 0); assert!(option::is_some(&some), 1); } #[test] fun option_contains() { let none = option::none(); let some = option::some(5); let some_other = option::some(6); assert!(option::contains(&some, &5), 0); assert!(option::contains(&some_other, &6), 1); assert!(!option::contains(&none, &5), 2); assert!(!option::contains(&some_other, &5), 3); } #[test] fun option_borrow_some() { let some = option::some(5); let some_other = option::some(6); assert!(*option::borrow(&some) == 5, 3); assert!(*option::borrow(&some_other) == 6, 4); } #[test] #[expected_failure(abort_code = 0x40001)] fun option_borrow_none() { option::borrow(&option::none()); } #[test] fun borrow_mut_some() { let some = option::some(1); let ref = option::borrow_mut(&mut some); *ref = 10; assert!(*option::borrow(&some) == 10, 0); } #[test] #[expected_failure(abort_code = 0x40001)] fun borrow_mut_none() { option::borrow_mut(&mut option::none()); } #[test] fun borrow_with_default() { let none = option::none(); let some = option::some(5); assert!(*option::borrow_with_default(&some, &7) == 5, 0); assert!(*option::borrow_with_default(&none, &7) == 7, 1); } #[test] fun get_with_default() { let none = option::none(); let some = option::some(5); assert!(option::get_with_default(&some, 7) == 5, 0); assert!(option::get_with_default(&none, 7) == 7, 1); } #[test] fun extract_some() { let opt = option::some(1); assert!(option::extract(&mut opt) == 1, 0); assert!(option::is_none(&opt), 1); } #[test] #[expected_failure(abort_code = 0x40001)] fun extract_none() { option::extract(&mut option::none()); } #[test] fun swap_some() { let some = option::some(5); assert!(option::swap(&mut some, 1) == 5, 0); assert!(*option::borrow(&some) == 1, 1); } #[test] fun swap_or_fill_some() { let some = option::some(5); assert!(option::swap_or_fill(&mut some, 1) == option::some(5), 0); assert!(*option::borrow(&some) == 1, 1); } #[test] fun swap_or_fill_none() { let none = option::none(); assert!(option::swap_or_fill(&mut none, 1) == option::none(), 0); assert!(*option::borrow(&none) == 1, 1); } #[test] #[expected_failure(abort_code = 0x40001)] fun swap_none() { option::swap(&mut option::none(), 1); } #[test] fun fill_none() { let none = option::none(); option::fill(&mut none, 3); assert!(option::is_some(&none), 0); assert!(*option::borrow(&none) == 3, 1); } #[test] #[expected_failure(abort_code = 0x40000)] fun fill_some() { option::fill(&mut option::some(3), 0); } #[test] fun destroy_with_default() { assert!(option::destroy_with_default(option::none(), 4) == 4, 0); assert!(option::destroy_with_default(option::some(4), 5) == 4, 1); } #[test] fun destroy_some() { assert!(option::destroy_some(option::some(4)) == 4, 0); } #[test] #[expected_failure(abort_code = 0x40001)] fun destroy_some_none() { option::destroy_some(option::none()); } #[test] fun destroy_none() { option::destroy_none(option::none()); } #[test] #[expected_failure(abort_code = 0x40000)] fun destroy_none_some() { option::destroy_none(option::some(0)); } #[test] fun into_vec_some() { let v = option::to_vec(option::some(0)); assert!(vector::length(&v) == 1, 0); let x = vector::pop_back(&mut v); assert!(x == 0, 1); } #[test] fun into_vec_none() { let v: vector = option::to_vec(option::none()); assert!(vector::is_empty(&v), 0); } }