Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save WaffleLapkin/fe56599e559c0c9b540c6d00847bb94d to your computer and use it in GitHub Desktop.
Save WaffleLapkin/fe56599e559c0c9b540c6d00847bb94d to your computer and use it in GitHub Desktop.
patch for `array-init` that adds tests from `arraylib` which fail with `arraylib` under miri
From eff7dc0d51d64fa2c55c44f0244ab3474751f6f5 Mon Sep 17 00:00:00 2001
From: Waffle <waffle.lapkin@gmail.com>
Date: Fri, 20 Mar 2020 11:12:11 +0300
Subject: [PATCH] add tests from `arraylib` which fail under miri there
---
src/lib.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 58 insertions(+), 1 deletion(-)
diff --git a/src/lib.rs b/src/lib.rs
index fb2f6a5..4e18b48 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,4 +1,4 @@
-#![no_std]
+#![cfg_attr(not(test), no_std)]
//! The `array-init` crate allows you to initialize arrays
//! with an initializer closure that will be called
@@ -408,4 +408,61 @@ mod tests {
}
}
}
+
+
+
+
+
+
+
+
+ // added tests ============================================
+
+ use std::sync::Mutex;
+
+ /// Add `1` to mutex on drop
+ #[derive(Debug)]
+ struct DropCount<'a>(&'a Mutex<usize>);
+
+ impl<'a> Drop for DropCount<'a> {
+ fn drop(&mut self) {
+ let mut guard = self.0.lock().unwrap();
+ *guard += 1;
+ }
+ }
+
+ #[test]
+ fn drop_on_panic() {
+ let counter = Mutex::new(0);
+
+ let r = std::panic::catch_unwind(|| {
+ let _: [DropCount; 16] = array_init(|i| {
+ if i == 10 {
+ panic!()
+ } else {
+ DropCount(&counter)
+ }
+ });
+ });
+
+ assert!(r.is_err());
+ assert_eq!(*counter.lock().unwrap(), 10);
+ }
+
+ //#[cfg(not(miri))]
+ #[test]
+ fn drop_on_fail() {
+ let counter = Mutex::new(0);
+
+ let r: Result<[DropCount; 16], ()> = try_array_init(|i| {
+ if i == 10 {
+ Err(())
+ } else {
+ Ok(DropCount(&counter))
+ }
+ });
+
+ assert!(r.is_err());
+ assert_eq!(*counter.lock().unwrap(), 10);
+ }
}
--
2.25.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment