/
unit_test.rs
69 lines (63 loc) · 1.45 KB
/
unit_test.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use ariprog::{
common_difference,
common_difference_and_first_term,
first_term,
how_many_terms,
nth_term,
insert_arithmetic_means,
NthTerm
};
#[test]
fn it_gets_common_difference() {
assert_eq!(common_difference(15.0, 10.0), 5.0);
assert_eq!(common_difference(10.0, 8.0), 2.0);
}
#[test]
fn it_gets_nth_term() {
assert_eq!(nth_term(1.0, 2.0, 20.0), 39.0);
assert_eq!(nth_term(0.0, 2.0, 103.0), 204.0);
}
#[test]
fn it_gets_first_term() {
assert_eq!(first_term(39.0, 2.0, 20.0), 1.0);
assert_eq!(first_term(204.0, 2.0, 103.0), 0.0);
}
#[test]
fn it_inserts_arithmetic_means() {
assert_eq!(
insert_arithmetic_means(6, 14.0, 98.0),
vec![14.0, 26.0, 38.0, 50.0, 62.0, 74.0, 86.0, 98.0]
);
assert_eq!(
insert_arithmetic_means(3, 1.0, 13.0),
vec![1.0, 4.0, 7.0, 10.0, 13.0]
);
}
#[test]
fn it_gets_the_number_of_terms() {
assert_eq!(
how_many_terms(5.0, 785.0, 5.0),
157
);
assert_eq!(
how_many_terms(0.0, 1000.0, 2.0),
501
);
}
#[test]
fn it_gets_common_difference_and_first_term() {
assert_eq!(
common_difference_and_first_term(
NthTerm { value: 64.0, position: 8.0 },
NthTerm { value: 32.0, position: 4.0 }
),
(8.0, 8.0)
);
assert_eq!(
common_difference_and_first_term(
NthTerm { value: 1000.0, position: 501.0 },
NthTerm { value: 998.0, position: 500.0 }
),
(2.0, 0.0)
)
}