diff options
| author | Federico Igne <git@federicoigne.com> | 2020-12-26 17:22:21 +0000 |
|---|---|---|
| committer | Federico Igne <git@federicoigne.com> | 2021-11-03 18:55:08 +0000 |
| commit | 4e2052c4d792540c2f742b2c2a081b11117ed41d (patch) | |
| tree | 6fdfbfd3b6f1c06cf613de2f6f052d39d93ec5e7 /nucleotide-count/tests/nucleotide-count.rs | |
| parent | 8a61a513dad5c22bb5596b8918a2d03755d08d1e (diff) | |
| download | exercism-4e2052c4d792540c2f742b2c2a081b11117ed41d.tar.gz exercism-4e2052c4d792540c2f742b2c2a081b11117ed41d.zip | |
[rust] Nucleotide Count
Diffstat (limited to 'nucleotide-count/tests/nucleotide-count.rs')
| -rw-r--r-- | nucleotide-count/tests/nucleotide-count.rs | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/nucleotide-count/tests/nucleotide-count.rs b/nucleotide-count/tests/nucleotide-count.rs new file mode 100644 index 0000000..8190580 --- /dev/null +++ b/nucleotide-count/tests/nucleotide-count.rs | |||
| @@ -0,0 +1,88 @@ | |||
| 1 | use nucleotide_count as dna; | ||
| 2 | |||
| 3 | use std::collections::HashMap; | ||
| 4 | |||
| 5 | fn process_nucleotidecounts_case(s: &str, pairs: &[(char, usize)]) { | ||
| 6 | // The reason for the awkward code in here is to ensure that the failure | ||
| 7 | // message for assert_eq! is as informative as possible. A simpler | ||
| 8 | // solution would simply check the length of the map, and then | ||
| 9 | // check for the presence and value of each key in the given pairs vector. | ||
| 10 | let mut m: HashMap<char, usize> = dna::nucleotide_counts(s).unwrap(); | ||
| 11 | for &(k, v) in pairs.iter() { | ||
| 12 | assert_eq!((k, m.remove(&k)), (k, Some(v))); | ||
| 13 | } | ||
| 14 | |||
| 15 | // may fail with a message that clearly shows all extra pairs in the map | ||
| 16 | assert_eq!(m.iter().collect::<Vec<(&char, &usize)>>(), vec![]); | ||
| 17 | } | ||
| 18 | |||
| 19 | #[test] | ||
| 20 | fn count_returns_result() { | ||
| 21 | assert!(dna::count('A', "").is_ok()); | ||
| 22 | } | ||
| 23 | |||
| 24 | #[test] | ||
| 25 | fn test_count_empty() { | ||
| 26 | assert_eq!(dna::count('A', ""), Ok(0)); | ||
| 27 | } | ||
| 28 | |||
| 29 | #[test] | ||
| 30 | fn count_invalid_nucleotide() { | ||
| 31 | assert_eq!(dna::count('X', "A"), Err('X')); | ||
| 32 | } | ||
| 33 | |||
| 34 | #[test] | ||
| 35 | fn count_invalid_dna() { | ||
| 36 | assert_eq!(dna::count('A', "AX"), Err('X')); | ||
| 37 | } | ||
| 38 | |||
| 39 | #[test] | ||
| 40 | fn test_count_repetitive_cytosine() { | ||
| 41 | assert_eq!(dna::count('C', "CCCCC"), Ok(5)); | ||
| 42 | } | ||
| 43 | |||
| 44 | #[test] | ||
| 45 | fn test_count_only_thymine() { | ||
| 46 | assert_eq!(dna::count('T', "GGGGGTAACCCGG"), Ok(1)); | ||
| 47 | } | ||
| 48 | |||
| 49 | #[test] | ||
| 50 | fn counts_returns_result() { | ||
| 51 | assert!(dna::nucleotide_counts("ACGT").is_ok()); | ||
| 52 | } | ||
| 53 | |||
| 54 | #[test] | ||
| 55 | fn test_empty_strand() { | ||
| 56 | process_nucleotidecounts_case("", &[('A', 0), ('T', 0), ('C', 0), ('G', 0)]); | ||
| 57 | } | ||
| 58 | |||
| 59 | #[test] | ||
| 60 | /// can count one nucleotide in single-character input | ||
| 61 | fn test_can_count_one_nucleotide_in_singlecharacter_input() { | ||
| 62 | process_nucleotidecounts_case("G", &[('A', 0), ('C', 0), ('G', 1), ('T', 0)]); | ||
| 63 | } | ||
| 64 | |||
| 65 | #[test] | ||
| 66 | fn test_strand_with_repeated_nucleotide() { | ||
| 67 | process_nucleotidecounts_case("GGGGGGG", &[('A', 0), ('T', 0), ('C', 0), ('G', 7)]); | ||
| 68 | } | ||
| 69 | |||
| 70 | #[test] | ||
| 71 | /// strand with multiple nucleotides | ||
| 72 | fn test_strand_with_multiple_nucleotides() { | ||
| 73 | process_nucleotidecounts_case( | ||
| 74 | "AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC", | ||
| 75 | &[('A', 20), ('T', 21), ('C', 12), ('G', 17)], | ||
| 76 | ); | ||
| 77 | } | ||
| 78 | |||
| 79 | #[test] | ||
| 80 | fn counts_invalid_nucleotide_results_in_err() { | ||
| 81 | assert_eq!(dna::nucleotide_counts("GGXXX"), Err('X')); | ||
| 82 | } | ||
| 83 | |||
| 84 | #[test] | ||
| 85 | /// strand with invalid nucleotides | ||
| 86 | fn test_strand_with_invalid_nucleotides() { | ||
| 87 | assert_eq!(dna::nucleotide_counts("AGXXACT"), Err('X'),); | ||
| 88 | } | ||
