Browse Source

Add tests to the `lv` module.

- Remove the `test_` prefix.
ticki 8 years ago
parent
commit
e89d82d0cf
13 changed files with 105 additions and 28 deletions
  1. 3 3
      src/arena.rs
  2. 77 0
      src/bk/lv.rs
  3. 5 5
      src/block.rs
  4. 3 3
      src/brk.rs
  5. 3 3
      src/cell.rs
  6. 2 2
      src/fail.rs
  7. 3 3
      src/lazy_init.rs
  8. 2 2
      src/ptr.rs
  9. 1 1
      src/random.rs
  10. 1 1
      src/sync.rs
  11. 2 2
      src/take.rs
  12. 2 2
      src/tls.rs
  13. 1 1
      src/vec.rs

+ 3 - 3
src/arena.rs

@@ -175,7 +175,7 @@ mod test {
     }
 
     #[test]
-    fn test_integers() {
+    fn integers() {
         let mut arena = make();
 
         let mut n = arena.alloc(200).unwrap();
@@ -196,7 +196,7 @@ mod test {
     }
 
     #[test]
-    fn test_oom() {
+    fn oom() {
         let mut arena = make();
 
         // Make the arena run dry.
@@ -228,7 +228,7 @@ mod test {
     }
 
     #[test]
-    fn test_cross_arena() {
+    fn cross_arena() {
         let mut arena1 = make();
         let mut arena2 = make();
 

+ 77 - 0
src/bk/lv.rs

@@ -46,6 +46,7 @@ impl Level {
         }
     }
 
+    #[inline]
     pub fn above(self) -> Option<Level> {
         // TODO: Find a way to eliminate this branch.
         if self == Level::max() {
@@ -132,6 +133,7 @@ impl Iterator for LevelIter {
 ///
 /// This is used to prevent bound checks, since the bound is encoded into the indexing type, and
 /// thus statically ensured.
+#[derive(Default)]
 pub struct Array<T> {
     inner: [T; LEVELS],
 }
@@ -149,3 +151,78 @@ impl<T> ops::IndexMut<Level> for Array {
         self.inner.get_unchecked_mut(lv.0)
     }
 }
+
+#[cfg(test)]
+mod test {
+    use super::*;
+
+    #[test]
+    fn level_generation_dist() {
+        // The number of generated `None`s.
+        let mut nones = 0;
+        // Occurences of each level.
+        let mut occ = Array::default();
+        // Simulate tousand level generations.
+        for _ in 0..1000 {
+            if let Some(lv) = Level::generate() {
+                // Increment the occurence counter.
+                occ[lv] += 1;
+            } else {
+                // Increment the `None` counter.
+                nones += 1;
+            }
+        }
+
+        // Ensure that the number of `None`s is within the expected margin.
+        assert!((490..510).contains(nones));
+
+        let mut expected = 250;
+        for lv in Iter::all() {
+            // Ensure that the occurences of `lv` is within the expected margin.
+            assert!((expected - 10..expected + 10).contains(occ[lv]));
+        }
+    }
+
+    #[test]
+    fn above() {
+        assert_eq!(Level::max().above(), None);
+        assert_eq!(Level::min().above().unwrap() as usize, 1);
+    }
+
+    #[test]
+    fn iter() {
+        assert!(Iter::all().eq(0..Level::max() as usize));
+        assert!(Iter::non_bottom().eq(1..Level::max() as usize));
+    }
+
+    #[test]
+    fn array_max_index() {
+        assert_eq!(Array::<&str>::default()[Level::max()], "");
+        assert_eq!(Array::<u32>::default()[Level::max()], 0);
+        assert_eq!(&mut Array::<&str>::default()[Level::max()], &mut "");
+        assert_eq!(&mut Array::<u32>::default()[Level::max()], &mut 0);
+    }
+
+    #[test]
+    fn array_iter() {
+        let mut arr = Array::default();
+        for lv in Iter::all() {
+            arr[lv] = lv as usize;
+        }
+
+        for lv in Iter::all() {
+            assert_eq!(arr[lv], lv as usize);
+
+            for lv in Iter::start_at(lv) {
+                assert_eq!(arr[lv], lv as usize);
+            }
+            for lv in Iter::all().to(lv) {
+                assert_eq!(arr[lv], lv as usize);
+            }
+        }
+
+        for lv in Iter::non_bottom() {
+            assert_eq!(arr[lv], lv as usize);
+        }
+    }
+}

+ 5 - 5
src/block.rs

@@ -321,7 +321,7 @@ mod test {
     use prelude::*;
 
     #[test]
-    fn test_array() {
+    fn array() {
         let arr = b"Lorem ipsum dolor sit amet";
         let block = unsafe {
             Block::from_raw_parts(Pointer::new(arr.as_ptr()), arr.len())
@@ -341,7 +341,7 @@ mod test {
     }
 
     #[test]
-    fn test_merge() {
+    fn merge() {
         let arr = b"Lorem ipsum dolor sit amet";
         let block = unsafe {
             Block::from_raw_parts(Pointer::new(arr.as_ptr()), arr.len())
@@ -357,7 +357,7 @@ mod test {
 
     #[test]
     #[should_panic]
-    fn test_oob() {
+    fn oob() {
         let arr = b"lorem";
         let block = unsafe {
             Block::from_raw_parts(Pointer::new(arr.as_ptr()), arr.len())
@@ -368,7 +368,7 @@ mod test {
     }
 
     #[test]
-    fn test_mutate() {
+    fn mutate() {
         let mut arr = [0u8, 2, 0, 0, 255, 255];
 
         let block = unsafe {
@@ -382,7 +382,7 @@ mod test {
     }
 
     #[test]
-    fn test_empty_lr() {
+    fn empty_lr() {
         let arr = b"Lorem ipsum dolor sit amet";
         let block = unsafe {
             Block::from_raw_parts(Pointer::new(arr.as_ptr()), arr.len())

+ 3 - 3
src/brk.rs

@@ -195,7 +195,7 @@ mod test {
     use super::*;
 
     #[test]
-    fn test_ordered() {
+    fn ordered() {
         let brk = lock().canonical_brk(20, 1);
 
         assert!(brk.0 <= brk.1);
@@ -203,7 +203,7 @@ mod test {
     }
 
     #[test]
-    fn test_brk_grow_up() {
+    fn brk_grow_up() {
         unsafe {
             let brk1 = lock().sbrk(5).unwrap();
             let brk2 = lock().sbrk(100).unwrap();
@@ -213,7 +213,7 @@ mod test {
     }
 
     #[test]
-    fn test_brk_right_segment_change() {
+    fn brk_right_segment_change() {
         unsafe {
             let brk1 = lock().sbrk(5).unwrap();
             let brk2 = lock().sbrk(100).unwrap();

+ 3 - 3
src/cell.rs

@@ -47,7 +47,7 @@ impl<T> MoveCell<T> {
             // This is safe due to the `&mut self`, enforcing the guarantee of uniqueness. This
             // will thus not alias it for the lifetime of that reference.
             &mut *self.inner.get()
-        }
+        })
     }
 }
 
@@ -56,14 +56,14 @@ mod test {
     use super::*;
 
     #[test]
-    fn test_replace() {
+    fn replace() {
         let cell = MoveCell::new(200);
         assert_eq!(cell.replace(300), 200);
         assert_eq!(cell.replace(4), 300);
     }
 
     #[test]
-    fn test_get() {
+    fn get() {
         let mut cell = MoveCell::new(200);
         assert_eq!(*cell.get(), 200);
 

+ 2 - 2
src/fail.rs

@@ -88,7 +88,7 @@ mod test {
 
     #[test]
     #[should_panic]
-    fn test_panic_oom() {
+    fn panic_oom() {
         fn panic() -> ! {
             panic!("cats are not cute.");
         }
@@ -100,7 +100,7 @@ mod test {
     #[test]
     #[should_panic]
     #[cfg(feature = "tls")]
-    fn test_panic_thread_oom() {
+    fn panic_thread_oom() {
         fn infinite() -> ! {
             #[allow(empty_loop)]
             loop {}

+ 3 - 3
src/lazy_init.rs

@@ -77,7 +77,7 @@ mod test {
     use core::cell::Cell;
 
     #[test]
-    fn test_init() {
+    fn init() {
         let mut lazy = LazyInit::new(|| 300);
 
         assert_eq!(*lazy.get(), 300);
@@ -86,7 +86,7 @@ mod test {
     }
 
     #[test]
-    fn test_into_inner() {
+    fn into_inner() {
         let mut lazy = LazyInit::new(|| 300);
 
         *lazy.get() = 442;
@@ -94,7 +94,7 @@ mod test {
     }
 
     #[test]
-    fn test_laziness() {
+    fn laziness() {
         let is_called = Cell::new(false);
         let mut lazy = LazyInit::new(|| is_called.set(true));
         assert!(!is_called.get());

+ 2 - 2
src/ptr.rs

@@ -219,7 +219,7 @@ mod test {
     use super::*;
 
     #[test]
-    fn test_pointer() {
+    fn pointer() {
         let mut x = [b'a', b'b'];
 
         unsafe {
@@ -239,7 +239,7 @@ mod test {
     }
 
     #[test]
-    fn test_empty() {
+    fn empty() {
         assert_eq!(*Pointer::<u8>::empty() as usize, 1);
     }
 }

+ 1 - 1
src/random.rs

@@ -47,7 +47,7 @@ mod test {
     use super::*;
 
     #[test]
-    fn test_distinct() {
+    fn distinct() {
         assert!(get() != get());
         assert!(get() != get());
         assert!(get() != get());

+ 1 - 1
src/sync.rs

@@ -101,7 +101,7 @@ mod test {
     use super::*;
 
     #[test]
-    fn test_mutex() {
+    fn mutex() {
         let mutex = Mutex::new(3);
         assert_eq!(*mutex.lock(), 3);
 

+ 2 - 2
src/take.rs

@@ -52,14 +52,14 @@ mod test {
     use core::cell::Cell;
 
     #[test]
-    fn test_replace_with() {
+    fn replace_with() {
         let mut x = Some("test");
         replace_with(&mut x, |_| None);
         assert!(x.is_none());
     }
 
     #[test]
-    fn test_replace_with_2() {
+    fn replace_with_2() {
         let is_called = Cell::new(false);
         let mut x = 2;
         replace_with(&mut x, |_| {

+ 2 - 2
src/tls.rs

@@ -89,14 +89,14 @@ mod test {
     use cell::MoveCell;
 
     #[test]
-    fn test_tls() {
+    fn tls() {
         tls!(static HELLO: &'static str = "hello");
 
         HELLO.with(|x| assert_eq!(x, "hello"));
     }
 
     #[test]
-    fn test_mutability() {
+    fn mutability() {
         tls!(static HELLO: MoveCell<u32> = MoveCell::new(3));
 
         HELLO.with(|x| assert_eq!(x.replace(4), 3));

+ 1 - 1
src/vec.rs

@@ -216,7 +216,7 @@ mod test {
     use prelude::*;
 
     #[test]
-    fn test_vec() {
+    fn vec() {
         let mut buffer = [b'a'; 32];
         let mut vec = unsafe {
             Vec::from_raw_parts(