Last active
August 29, 2015 14:03
-
-
Save Denommus/89fcb4a51b640625de16 to your computer and use it in GitHub Desktop.
Second Rust dojo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extern crate rustc; | |
| use rustc::util::sha2::{Sha256, Digest}; | |
| struct File { | |
| path: String, | |
| contents: Vec<u8> | |
| } | |
| impl File { | |
| fn new(path: &str, contents: Vec<u8>) -> File { | |
| File { | |
| path: String::from_str(path), | |
| contents: contents | |
| } | |
| } | |
| } | |
| fn calculate_hash(file: &File) -> String { | |
| let mut digest = Sha256::new(); | |
| digest.input(file.contents.as_slice()); | |
| digest.result_str() | |
| } | |
| fn create_final_list<'a>(file_list: &'a Vec<File>) -> Vec<&'a str> { | |
| let mut aux_iter = file_list.iter(); | |
| let mut result = Vec::<&str>::new(); | |
| for i in file_list.iter() { | |
| if aux_iter | |
| .filter(|file| | |
| calculate_hash(i)==calculate_hash(*file)).count() == 1 | |
| { | |
| result.push(i.path.as_slice()); | |
| } | |
| aux_iter.next(); | |
| } | |
| result | |
| } | |
| #[test] | |
| fn test_produce_hash() { | |
| let file = File::new("foo", vec!(1,2,3)); | |
| assert_eq!(calculate_hash(&file), String::from_str("039058c6f2c0cb492c533b0\ | |
| a4d14ef77cc0f78abccced5287d84a1a2011cfb81")); | |
| } | |
| #[test] | |
| fn test_compare_two_equal_hashes() { | |
| let file=File::new("foo", vec!(1,2,3)); | |
| let file_compare=File::new("bar", vec!(1,2,3)); | |
| assert_eq!(calculate_hash(&file), calculate_hash(&file_compare)); | |
| } | |
| #[test] | |
| fn test_compare_two_not_equal_hashes() { | |
| let file=File::new("foo", vec!(1,2,4)); | |
| let file_compare=File::new("bar", vec!(1,2,3)); | |
| assert!(calculate_hash(&file) != calculate_hash(&file_compare)); | |
| } | |
| #[test] | |
| fn test_duplicated() { | |
| let files= vec!(File::new("foo", vec!(1,2,3)), | |
| File::new("bar", vec!(1,2,3)), | |
| File::new("far", vec!(1,2,3))); | |
| let final_list = create_final_list(&files); | |
| assert_eq!(final_list, vec!("far")); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
+1