From 5d8024db291fdf69ad3610d44e4473163ca90a35 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 14 Jul 2017 23:17:32 +0200 Subject: [PATCH 1/4] Add test whether link removal works --- libimagentrylink/src/internal.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/libimagentrylink/src/internal.rs b/libimagentrylink/src/internal.rs index 99205a3a..487d81af 100644 --- a/libimagentrylink/src/internal.rs +++ b/libimagentrylink/src/internal.rs @@ -989,5 +989,27 @@ mod test { } + #[test] + fn test_link_deleting() { + setup_logging(); + let store = get_store(); + + let mut e1 = store.retrieve(PathBuf::from("1")).unwrap(); + let mut e2 = store.retrieve(PathBuf::from("2")).unwrap(); + + assert_eq!(e1.get_internal_links().unwrap().collect::>().len(), 0); + assert_eq!(e2.get_internal_links().unwrap().collect::>().len(), 0); + + assert!(e1.add_internal_link(&mut e2).is_ok()); + + assert_eq!(e1.get_internal_links().unwrap().collect::>().len(), 1); + assert_eq!(e2.get_internal_links().unwrap().collect::>().len(), 1); + + assert!(e1.remove_internal_link(&mut e2).is_ok()); + + assert_eq!(e1.get_internal_links().unwrap().collect::>().len(), 0); + assert_eq!(e2.get_internal_links().unwrap().collect::>().len(), 0); + } + } From 4d67726af9c685931df74e46f21cb44ae8b49e18 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 14 Jul 2017 23:21:38 +0200 Subject: [PATCH 2/4] Add test for multi-linking checks --- libimagentrylink/src/internal.rs | 45 ++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/libimagentrylink/src/internal.rs b/libimagentrylink/src/internal.rs index 487d81af..62003777 100644 --- a/libimagentrylink/src/internal.rs +++ b/libimagentrylink/src/internal.rs @@ -1011,5 +1011,50 @@ mod test { assert_eq!(e2.get_internal_links().unwrap().collect::>().len(), 0); } + #[test] + fn test_link_deleting_multiple_links() { + setup_logging(); + let store = get_store(); + + let mut e1 = store.retrieve(PathBuf::from("1")).unwrap(); + let mut e2 = store.retrieve(PathBuf::from("2")).unwrap(); + let mut e3 = store.retrieve(PathBuf::from("3")).unwrap(); + + assert_eq!(e1.get_internal_links().unwrap().collect::>().len(), 0); + assert_eq!(e2.get_internal_links().unwrap().collect::>().len(), 0); + assert_eq!(e3.get_internal_links().unwrap().collect::>().len(), 0); + + assert!(e1.add_internal_link(&mut e2).is_ok()); // 1-2 + assert!(e1.add_internal_link(&mut e3).is_ok()); // 1-2, 1-3 + + assert_eq!(e1.get_internal_links().unwrap().collect::>().len(), 2); + assert_eq!(e2.get_internal_links().unwrap().collect::>().len(), 1); + assert_eq!(e3.get_internal_links().unwrap().collect::>().len(), 1); + + assert!(e2.add_internal_link(&mut e3).is_ok()); // 1-2, 1-3, 2-3 + + assert_eq!(e1.get_internal_links().unwrap().collect::>().len(), 2); + assert_eq!(e2.get_internal_links().unwrap().collect::>().len(), 2); + assert_eq!(e3.get_internal_links().unwrap().collect::>().len(), 2); + + assert!(e1.remove_internal_link(&mut e2).is_ok()); // 1-3, 2-3 + + assert_eq!(e1.get_internal_links().unwrap().collect::>().len(), 1); + assert_eq!(e2.get_internal_links().unwrap().collect::>().len(), 1); + assert_eq!(e3.get_internal_links().unwrap().collect::>().len(), 2); + + assert!(e1.remove_internal_link(&mut e3).is_ok()); // 2-3 + + assert_eq!(e1.get_internal_links().unwrap().collect::>().len(), 0); + assert_eq!(e2.get_internal_links().unwrap().collect::>().len(), 1); + assert_eq!(e3.get_internal_links().unwrap().collect::>().len(), 1); + + assert!(e2.remove_internal_link(&mut e3).is_ok()); + + assert_eq!(e1.get_internal_links().unwrap().collect::>().len(), 0); + assert_eq!(e2.get_internal_links().unwrap().collect::>().len(), 0); + assert_eq!(e3.get_internal_links().unwrap().collect::>().len(), 0); + } + } From 0215817262687fa2d1b3901502fdcd50b2264eef Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 15 Jul 2017 00:05:05 +0200 Subject: [PATCH 3/4] Add quickcheck-based test for link removal --- libimagentrylink/Cargo.toml | 3 ++ libimagentrylink/src/internal.rs | 79 ++++++++++++++++++++++++++++++++ libimagentrylink/src/lib.rs | 4 ++ 3 files changed, 86 insertions(+) diff --git a/libimagentrylink/Cargo.toml b/libimagentrylink/Cargo.toml index 193f5108..67723efc 100644 --- a/libimagentrylink/Cargo.toml +++ b/libimagentrylink/Cargo.toml @@ -23,6 +23,9 @@ rust-crypto = "0.2" env_logger = "0.3" is-match = "0.1" +[dev-dependencies] +quickcheck = "0.3" + [dependencies.libimagstore] path = "../libimagstore" diff --git a/libimagentrylink/src/internal.rs b/libimagentrylink/src/internal.rs index 62003777..9507698f 100644 --- a/libimagentrylink/src/internal.rs +++ b/libimagentrylink/src/internal.rs @@ -1056,5 +1056,84 @@ mod test { assert_eq!(e3.get_internal_links().unwrap().collect::>().len(), 0); } + quickcheck! { + + // This creates `removals.len() * 10` entries in the store and links all of them to a + // test entry. + // After that, each entry from the to-link-entries list which is hit by an index from + // `removals` is unlinked again and a number of checks run for the testing entry: + // + // * Are all other entries still linked (two-way-check) + // * Is the number of links in the testing entry smaller than before + // * Is the just unlinked entry not linked + // + // I'm not sure whether using quickcheck is the right approach here, but it is the easiest + // way to create a random number of to-remove-entries where I don't know which ones get + // removed. + fn test_random_linking(removals: Vec) -> bool { + use std::collections::hash_map::DefaultHasher; + use std::hash::Hash; + use std::hash::Hasher; + use itertools::Itertools; + + setup_logging(); + + let num_removals = removals.len(); + let num_tmp_entries = num_removals * 10; + let removals = removals + .into_iter() + .map(|r| r % num_tmp_entries) + .unique() + .collect::>(); + + let store = get_store(); + + let test_entry_name = { + let mut hasher = DefaultHasher::new(); + removals.hash(&mut hasher); + let name_ext : u64 = hasher.finish(); + format!("test-{}", name_ext) + }; + + let mut test_entry = store.retrieve(PathBuf::from(test_entry_name)).unwrap(); + + let mut tmp_entries = Vec::with_capacity(num_tmp_entries); + + for i in 0..num_tmp_entries { + let mut entry = store.retrieve(PathBuf::from(format!("tmp-{}", i))).unwrap(); + assert!(test_entry.add_internal_link(&mut entry).is_ok(), + format!("Adding entry did not work: {:?} -> {:?}", test_entry, entry)); + tmp_entries.push(entry); + } + + let link_count = test_entry.get_internal_links().unwrap().count(); + + assert_eq!(link_count, num_tmp_entries, + "Numbers of links in test_entry should be: {:?} but is {:?}: {:?}", + num_tmp_entries, link_count, + test_entry.get_internal_links().unwrap().collect::>()); + + for index in removals { + let num_links = test_entry.get_internal_links().unwrap().count(); + + debug!("Removing link: {:?}", tmp_entries[index]); + assert!(test_entry.remove_internal_link(&mut tmp_entries[index]).is_ok(), + format!("Removing link to test entry failed: {:?}", tmp_entries[index])); + + let link_count = test_entry.get_internal_links().unwrap().count(); + assert_eq!(link_count, num_links - 1, + "Numbers of links in test_entry should be: {:?} but is {:?}: {:?}", + num_links - 1, link_count, + test_entry.get_internal_links().unwrap().collect::>()); + + for internal_link in test_entry.get_internal_links().unwrap() { + assert!(tmp_entries.iter().any(|tmp| internal_link.eq_store_id(tmp.get_location()))); + } + } + + true + } + } + } diff --git a/libimagentrylink/src/lib.rs b/libimagentrylink/src/lib.rs index 2ed8b6ba..eab4ea9e 100644 --- a/libimagentrylink/src/lib.rs +++ b/libimagentrylink/src/lib.rs @@ -42,6 +42,10 @@ extern crate crypto; #[cfg(test)] extern crate env_logger; +#[cfg(test)] +#[macro_use] +extern crate quickcheck; + #[macro_use] extern crate libimagstore; #[macro_use] extern crate libimagerror; extern crate libimagutil; From 84fa51d4fe3c59184ba93b20d9a717b8b8064f6f Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 26 Aug 2017 12:29:18 +0200 Subject: [PATCH 4/4] Revert "Add quickcheck-based test for link removal" This reverts commit 0215817262687fa2d1b3901502fdcd50b2264eef. The quickcheck tests push the build times up very much, so we revert this patch to keep the idea of the test in the history, but not having it in the source. Maybe we can reuse this patch someday. --- libimagentrylink/Cargo.toml | 3 -- libimagentrylink/src/internal.rs | 79 -------------------------------- libimagentrylink/src/lib.rs | 4 -- 3 files changed, 86 deletions(-) diff --git a/libimagentrylink/Cargo.toml b/libimagentrylink/Cargo.toml index 67723efc..193f5108 100644 --- a/libimagentrylink/Cargo.toml +++ b/libimagentrylink/Cargo.toml @@ -23,9 +23,6 @@ rust-crypto = "0.2" env_logger = "0.3" is-match = "0.1" -[dev-dependencies] -quickcheck = "0.3" - [dependencies.libimagstore] path = "../libimagstore" diff --git a/libimagentrylink/src/internal.rs b/libimagentrylink/src/internal.rs index 9507698f..62003777 100644 --- a/libimagentrylink/src/internal.rs +++ b/libimagentrylink/src/internal.rs @@ -1056,84 +1056,5 @@ mod test { assert_eq!(e3.get_internal_links().unwrap().collect::>().len(), 0); } - quickcheck! { - - // This creates `removals.len() * 10` entries in the store and links all of them to a - // test entry. - // After that, each entry from the to-link-entries list which is hit by an index from - // `removals` is unlinked again and a number of checks run for the testing entry: - // - // * Are all other entries still linked (two-way-check) - // * Is the number of links in the testing entry smaller than before - // * Is the just unlinked entry not linked - // - // I'm not sure whether using quickcheck is the right approach here, but it is the easiest - // way to create a random number of to-remove-entries where I don't know which ones get - // removed. - fn test_random_linking(removals: Vec) -> bool { - use std::collections::hash_map::DefaultHasher; - use std::hash::Hash; - use std::hash::Hasher; - use itertools::Itertools; - - setup_logging(); - - let num_removals = removals.len(); - let num_tmp_entries = num_removals * 10; - let removals = removals - .into_iter() - .map(|r| r % num_tmp_entries) - .unique() - .collect::>(); - - let store = get_store(); - - let test_entry_name = { - let mut hasher = DefaultHasher::new(); - removals.hash(&mut hasher); - let name_ext : u64 = hasher.finish(); - format!("test-{}", name_ext) - }; - - let mut test_entry = store.retrieve(PathBuf::from(test_entry_name)).unwrap(); - - let mut tmp_entries = Vec::with_capacity(num_tmp_entries); - - for i in 0..num_tmp_entries { - let mut entry = store.retrieve(PathBuf::from(format!("tmp-{}", i))).unwrap(); - assert!(test_entry.add_internal_link(&mut entry).is_ok(), - format!("Adding entry did not work: {:?} -> {:?}", test_entry, entry)); - tmp_entries.push(entry); - } - - let link_count = test_entry.get_internal_links().unwrap().count(); - - assert_eq!(link_count, num_tmp_entries, - "Numbers of links in test_entry should be: {:?} but is {:?}: {:?}", - num_tmp_entries, link_count, - test_entry.get_internal_links().unwrap().collect::>()); - - for index in removals { - let num_links = test_entry.get_internal_links().unwrap().count(); - - debug!("Removing link: {:?}", tmp_entries[index]); - assert!(test_entry.remove_internal_link(&mut tmp_entries[index]).is_ok(), - format!("Removing link to test entry failed: {:?}", tmp_entries[index])); - - let link_count = test_entry.get_internal_links().unwrap().count(); - assert_eq!(link_count, num_links - 1, - "Numbers of links in test_entry should be: {:?} but is {:?}: {:?}", - num_links - 1, link_count, - test_entry.get_internal_links().unwrap().collect::>()); - - for internal_link in test_entry.get_internal_links().unwrap() { - assert!(tmp_entries.iter().any(|tmp| internal_link.eq_store_id(tmp.get_location()))); - } - } - - true - } - } - } diff --git a/libimagentrylink/src/lib.rs b/libimagentrylink/src/lib.rs index eab4ea9e..2ed8b6ba 100644 --- a/libimagentrylink/src/lib.rs +++ b/libimagentrylink/src/lib.rs @@ -42,10 +42,6 @@ extern crate crypto; #[cfg(test)] extern crate env_logger; -#[cfg(test)] -#[macro_use] -extern crate quickcheck; - #[macro_use] extern crate libimagstore; #[macro_use] extern crate libimagerror; extern crate libimagutil;