test.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // use assert_no_alloc::*;
  2. // #[global_allocator]
  3. // static A: AllocDisabler = AllocDisabler;
  4. // // TODO: Get this working in miri later
  5. // #[cfg(all(feature = "warn_debug", debug_assertions))]
  6. // #[cfg_attr(miri, ignore)]
  7. // mod test {
  8. // use super::*;
  9. // use std::panic::catch_unwind;
  10. // // This is only a kludge; what we actually want to check is "will do_alloc() be optimized out?", e.g. due to
  11. // // compiler optimizations turned on in --release mode. We can't do that, the closest we can get is to check
  12. // // whether debug_assertions are disabled, which coincidentially also happens in release mode.
  13. // #[cfg(not(debug_assertions))]
  14. // compile_error!(
  15. // "The test suite only works in debug mode. Use `cargo test --features warn_debug`"
  16. // );
  17. // #[cfg(feature = "warn_debug")]
  18. // fn check_and_reset() -> bool {
  19. // let result = violation_count() > 0;
  20. // reset_violation_count();
  21. // result
  22. // }
  23. // // Provide a stub check_and_reset() function if warn_debug is disabled. This will never be compiled due to the
  24. // // compile_error!() above, but this stub ensures that the output will not be cluttered with spurious error
  25. // // messages.
  26. // #[cfg(not(feature = "warn_debug"))]
  27. // fn check_and_reset() -> bool {
  28. // unreachable!()
  29. // }
  30. // fn do_alloc() {
  31. // let _tmp: Box<u32> = Box::new(42);
  32. // }
  33. // #[test]
  34. // fn ok_noop() {
  35. // assert_eq!(check_and_reset(), false);
  36. // do_alloc();
  37. // assert_eq!(check_and_reset(), false);
  38. // }
  39. // #[test]
  40. // fn ok_simple() {
  41. // assert_eq!(check_and_reset(), false);
  42. // assert_no_alloc(|| {});
  43. // do_alloc();
  44. // assert_eq!(check_and_reset(), false);
  45. // }
  46. // #[test]
  47. // fn ok_nested() {
  48. // assert_eq!(check_and_reset(), false);
  49. // assert_no_alloc(|| {
  50. // assert_no_alloc(|| {});
  51. // });
  52. // do_alloc();
  53. // assert_eq!(check_and_reset(), false);
  54. // }
  55. // #[test]
  56. // fn forbidden_simple() {
  57. // assert_eq!(check_and_reset(), false);
  58. // assert_no_alloc(|| {
  59. // do_alloc();
  60. // });
  61. // assert_eq!(check_and_reset(), true);
  62. // }
  63. // #[test]
  64. // fn forbidden_in_nested() {
  65. // assert_eq!(check_and_reset(), false);
  66. // assert_no_alloc(|| {
  67. // assert_no_alloc(|| {
  68. // do_alloc();
  69. // });
  70. // });
  71. // assert_eq!(check_and_reset(), true);
  72. // }
  73. // #[test]
  74. // fn forbidden_after_nested() {
  75. // assert_eq!(check_and_reset(), false);
  76. // assert_no_alloc(|| {
  77. // assert_no_alloc(|| {});
  78. // do_alloc();
  79. // });
  80. // assert_eq!(check_and_reset(), true);
  81. // }
  82. // #[test]
  83. // fn unwind_ok() {
  84. // assert_eq!(check_and_reset(), false);
  85. // assert_no_alloc(|| {
  86. // let r = catch_unwind(|| {
  87. // assert_no_alloc(|| {
  88. // panic!();
  89. // });
  90. // });
  91. // assert!(r.is_err());
  92. // });
  93. // check_and_reset(); // unwinding might have allocated memory; we don't care about that.
  94. // do_alloc();
  95. // assert_eq!(check_and_reset(), false);
  96. // }
  97. // #[test]
  98. // fn unwind_nested() {
  99. // assert_eq!(check_and_reset(), false);
  100. // assert_no_alloc(|| {
  101. // let r = catch_unwind(|| {
  102. // assert_no_alloc(|| {
  103. // panic!();
  104. // });
  105. // });
  106. // assert!(r.is_err());
  107. // check_and_reset(); // unwinding might have allocated memory; we don't care about that.
  108. // do_alloc();
  109. // assert_eq!(check_and_reset(), true);
  110. // });
  111. // }
  112. // #[test]
  113. // fn unwind_nested2() {
  114. // assert_eq!(check_and_reset(), false);
  115. // assert_no_alloc(|| {
  116. // assert_no_alloc(|| {
  117. // let r = catch_unwind(|| {
  118. // assert_no_alloc(|| {
  119. // assert_no_alloc(|| {
  120. // panic!();
  121. // });
  122. // });
  123. // });
  124. // assert!(r.is_err());
  125. // check_and_reset(); // unwinding might have allocated memory; we don't care about that.
  126. // do_alloc();
  127. // assert_eq!(check_and_reset(), true);
  128. // });
  129. // });
  130. // check_and_reset(); // unwinding might have allocated memory; we don't care about that.
  131. // do_alloc();
  132. // assert_eq!(check_and_reset(), false);
  133. // }
  134. // }