-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathtest_sparse_vec.rs
68 lines (59 loc) · 1.4 KB
/
test_sparse_vec.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
#[macro_use]
extern crate approx;
use algo::math::sparse_vector::SparseVector;
#[test]
fn dot() {
let (a, b) = create();
assert_relative_eq!(a.dot(&b).unwrap(), 0.3);
}
#[test]
fn magnitude() {
let (a, _) = create();
assert_relative_eq!(
a.magnitude(),
(0.5_f64.powf(2.0) + 0.75_f64.powf(2.0)).sqrt()
);
}
#[test]
fn scale() {
let (a, _) = create();
let c = a.scale(2.0);
let expect = vec![0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.5];
for (i, &v) in expect.iter().enumerate() {
assert_relative_eq!(v, c.get(i),);
}
}
#[test]
fn plus() {
let (a, b) = create();
let c = a + b;
let expect = vec![0.0, 0.0, 0.0, 1.1, 0.9, 0.0, 0.0, 0.0, 0.0, 0.75];
for (i, &v) in expect.iter().enumerate() {
assert_relative_eq!(v, c.get(i),);
}
}
#[test]
fn sub() {
let (a, b) = create();
let c = a - b;
let expect = vec![0.0, 0.0, 0.0, -0.1, -0.9, 0.0, 0.0, 0.0, 0.0, 0.75];
for (i, &v) in expect.iter().enumerate() {
assert_relative_eq!(v, c.get(i),);
}
}
#[test]
fn to_string() {
let (a, _) = create();
assert_eq!(a.to_string(), "(3, 0.5)(9, 0.75)");
}
fn create() -> (SparseVector, SparseVector) {
let mut a = SparseVector::new(10);
let mut b = SparseVector::new(10);
a.put(3, 0.5);
a.put(9, 0.75);
a.put(6, 0.11);
a.put(6, 0.0);
b.put(3, 0.6);
b.put(4, 0.9);
(a, b)
}