basic.rs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. use generational_box::{GenerationalBox, Storage, SyncStorage, UnsyncStorage};
  2. /// # Example
  3. ///
  4. /// ```compile_fail
  5. /// let data = String::from("hello world");
  6. /// let owner = UnsyncStorage::owner();
  7. /// let key = owner.insert(&data);
  8. /// drop(data);
  9. /// assert_eq!(*key.read(), "hello world");
  10. /// ```
  11. #[allow(unused)]
  12. fn compile_fail() {}
  13. #[test]
  14. fn leaking_is_ok() {
  15. fn leaking_is_ok_test<S: Storage<String>>() {
  16. let data = String::from("hello world");
  17. let key;
  18. {
  19. // create an owner
  20. let owner = S::owner();
  21. // insert data into the store
  22. key = owner.insert(data);
  23. // don't drop the owner
  24. std::mem::forget(owner);
  25. }
  26. assert_eq!(
  27. key.try_read().as_deref().unwrap(),
  28. &"hello world".to_string()
  29. );
  30. }
  31. leaking_is_ok_test::<UnsyncStorage>();
  32. leaking_is_ok_test::<SyncStorage>();
  33. }
  34. #[test]
  35. fn drops() {
  36. fn drops_test<S: Storage<String>>() {
  37. let data = String::from("hello world");
  38. let key;
  39. {
  40. // create an owner
  41. let owner = S::owner();
  42. // insert data into the store
  43. key = owner.insert(data);
  44. // drop the owner
  45. }
  46. assert!(key.try_read().is_err());
  47. }
  48. drops_test::<UnsyncStorage>();
  49. drops_test::<SyncStorage>();
  50. }
  51. #[test]
  52. fn works() {
  53. fn works_test<S: Storage<i32>>() {
  54. let owner = S::owner();
  55. let key = owner.insert(1);
  56. assert_eq!(*key.read(), 1);
  57. }
  58. works_test::<UnsyncStorage>();
  59. works_test::<SyncStorage>();
  60. }
  61. #[test]
  62. fn insert_while_reading() {
  63. fn insert_while_reading_test<S: Storage<String> + Storage<&'static i32>>() {
  64. let owner = S::owner();
  65. let key;
  66. {
  67. let data: String = "hello world".to_string();
  68. key = owner.insert(data);
  69. }
  70. let value = key.read();
  71. owner.insert(&1);
  72. assert_eq!(*value, "hello world");
  73. }
  74. insert_while_reading_test::<UnsyncStorage>();
  75. insert_while_reading_test::<SyncStorage>();
  76. }
  77. #[test]
  78. #[should_panic]
  79. fn panics() {
  80. fn panics_test<S: Storage<i32>>() {
  81. let owner = S::owner();
  82. let key = owner.insert(1);
  83. drop(owner);
  84. assert_eq!(*key.read(), 1);
  85. }
  86. panics_test::<UnsyncStorage>();
  87. panics_test::<SyncStorage>();
  88. }
  89. #[test]
  90. fn fuzz() {
  91. fn maybe_owner_scope<S: Storage<String>>(
  92. valid_keys: &mut Vec<GenerationalBox<String, S>>,
  93. invalid_keys: &mut Vec<GenerationalBox<String, S>>,
  94. path: &mut Vec<u8>,
  95. ) {
  96. let branch_cutoff = 5;
  97. let children = if path.len() < branch_cutoff {
  98. rand::random::<u8>() % 4
  99. } else {
  100. rand::random::<u8>() % 2
  101. };
  102. for i in 0..children {
  103. let owner = S::owner();
  104. let key = owner.insert(format!("hello world {path:?}"));
  105. println!("created new box {key:?}");
  106. valid_keys.push(key);
  107. path.push(i);
  108. // read all keys
  109. println!("{:?}", path);
  110. for key in valid_keys.iter() {
  111. println!("reading {key:?}");
  112. let value = key.read();
  113. println!("{:?}", &*value);
  114. assert!(value.starts_with("hello world"));
  115. }
  116. for key in invalid_keys.iter() {
  117. println!("reading invalid {key:?}");
  118. assert!(key.try_read().is_err());
  119. }
  120. maybe_owner_scope(valid_keys, invalid_keys, path);
  121. let invalid = valid_keys.pop().unwrap();
  122. println!("popping {invalid:?}");
  123. invalid_keys.push(invalid);
  124. path.pop();
  125. }
  126. }
  127. for _ in 0..10 {
  128. maybe_owner_scope::<UnsyncStorage>(&mut Vec::new(), &mut Vec::new(), &mut Vec::new());
  129. }
  130. for _ in 0..10 {
  131. maybe_owner_scope::<SyncStorage>(&mut Vec::new(), &mut Vec::new(), &mut Vec::new());
  132. }
  133. }