Skip to content

Commit 7877d63

Browse files
committed
Add RNA transcription assignment in Rust
1 parent 0b469a7 commit 7877d63

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#[deriving(Eq)]
2+
struct RibonucleicAcid {
3+
nucleotides: ~str
4+
}
5+
6+
impl RibonucleicAcid {
7+
pub fn new(nucleotides: ~str) -> RibonucleicAcid { RibonucleicAcid { nucleotides: nucleotides } }
8+
}
9+
10+
impl ToStr for RibonucleicAcid {
11+
fn to_str(&self) -> ~str {
12+
self.nucleotides.to_str()
13+
}
14+
}
15+
16+
struct DeoxyribonucleicAcid {
17+
nucleotides: ~str
18+
}
19+
20+
impl DeoxyribonucleicAcid {
21+
pub fn new(nucleotides: ~str) -> DeoxyribonucleicAcid { DeoxyribonucleicAcid { nucleotides: nucleotides } }
22+
23+
pub fn to_rna(&self) -> RibonucleicAcid {
24+
let rna_nucleotides = self.nucleotides.map_chars(|chr| if chr == 'T' { 'U' } else { chr } );
25+
26+
RibonucleicAcid { nucleotides: rna_nucleotides }
27+
}
28+
}
29+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#[link(name = "rna-transcription", vers = "1.0")];
2+
#[crate_type = "lib"];
3+
4+
extern mod std;
5+
6+
mod dna;
7+
8+
#[test]
9+
#[should_fail]
10+
fn test_acid_equals_acid() {
11+
assert_eq!(dna::RibonucleicAcid::new(~"CGA"), dna::RibonucleicAcid::new(~"CGA"));
12+
assert!(dna::RibonucleicAcid::new(~"CGA") != dna::RibonucleicAcid::new(~"AGC"));
13+
}
14+
15+
#[test]
16+
#[should_fail]
17+
fn test_transcribes_cytidine_unchanged() {
18+
assert_eq!(dna::RibonucleicAcid::new(~"C"), dna::DeoxyribonucleicAcid::new(~"C").to_rna());
19+
}
20+
21+
#[test]
22+
#[should_fail]
23+
fn test_transcribes_guanosine_unchanged() {
24+
assert_eq!(dna::RibonucleicAcid::new(~"G"), dna::DeoxyribonucleicAcid::new(~"G").to_rna());
25+
}
26+
27+
#[test]
28+
#[should_fail]
29+
fn test_transcribes_adenosine_unchanged() {
30+
assert_eq!(dna::RibonucleicAcid::new(~"A"), dna::DeoxyribonucleicAcid::new(~"A").to_rna());
31+
}
32+
33+
#[test]
34+
#[should_fail]
35+
fn test_transcribes_thymidine_to_uracil() {
36+
assert_eq!(dna::RibonucleicAcid::new(~"U"), dna::DeoxyribonucleicAcid::new(~"T").to_rna());
37+
}
38+
39+
#[test]
40+
#[should_fail]
41+
fn test_transcribes_all_occurrences_of_thymidine_to_uracil() {
42+
assert_eq!(dna::RibonucleicAcid::new(~"ACGUGGUCUUAA"), dna::DeoxyribonucleicAcid::new(~"ACGTGGTCTTAA").to_rna())
43+
}
44+
45+
#[test]
46+
#[should_fail]
47+
fn test_acid_converts_to_string() {
48+
assert_eq!(dna::RibonucleicAcid::new(~"AGC").to_str(), ~"AGC");
49+
assert_eq!(dna::RibonucleicAcid::new(~"CGA").to_str(), ~"CGA");
50+
}

0 commit comments

Comments
 (0)