Skip to content

Commit c6e8a42

Browse files
mangelatsLuthaf
authored andcommitted
Add tests for indexing the generated SoA
1 parent 2c64769 commit c6e8a42

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

tests/index.rs

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
mod particles;
2+
use self::particles::{Particle, ParticleVec, ParticleRef};
3+
4+
5+
#[test]
6+
fn test_usize() {
7+
let mut aos = Vec::new();
8+
let mut soa = ParticleVec::new();
9+
10+
let particle = Particle::new(String::from("Na"), 56.0);
11+
aos.push(particle.clone());
12+
soa.push(particle.clone());
13+
14+
// SoaIndex
15+
assert_eq!(soa.get(0).unwrap().name, &aos.get(0).unwrap().name);
16+
assert_eq!(soa.get(0).unwrap().mass, &aos.get(0).unwrap().mass);
17+
assert_eq!(aos.get(1), None);
18+
assert_eq!(soa.get(1), None);
19+
20+
unsafe {
21+
assert_eq!(soa.get_unchecked(0).name, &aos.get_unchecked(0).name);
22+
assert_eq!(soa.get_unchecked(0).mass, &aos.get_unchecked(0).mass);
23+
}
24+
25+
assert_eq!(soa.index(0).name, &aos[0].name);
26+
assert_eq!(soa.index(0).mass, &aos[0].mass);
27+
28+
29+
// SoaIndexMut
30+
assert_eq!(soa.get_mut(0).unwrap().name, &aos.get_mut(0).unwrap().name);
31+
assert_eq!(soa.get_mut(0).unwrap().mass, &aos.get_mut(0).unwrap().mass);
32+
assert_eq!(aos.get_mut(1), None);
33+
assert_eq!(soa.get_mut(1), None);
34+
35+
unsafe {
36+
assert_eq!(soa.get_unchecked_mut(0).name, &aos.get_unchecked_mut(0).name);
37+
assert_eq!(soa.get_unchecked_mut(0).mass, &aos.get_unchecked_mut(0).mass);
38+
}
39+
40+
assert_eq!(soa.index_mut(0).name, &aos[0].name);
41+
assert_eq!(soa.index_mut(0).mass, &aos[0].mass);
42+
43+
44+
*soa.index_mut(0).mass -= 1.;
45+
assert_eq!(soa.get(0).map(|p| *p.mass), Some(particle.mass - 1.));
46+
47+
*soa.get_mut(0).unwrap().mass += 1.;
48+
assert_eq!(soa.get(0).map(|p| *p.mass), Some(particle.mass));
49+
}
50+
51+
fn eq_its<'a, I1, I2>(i1: I1, i2: I2)
52+
where
53+
I1: Iterator<Item = ParticleRef<'a>>,
54+
I2: Iterator<Item = &'a Particle>,
55+
{
56+
for (p1, p2) in i1.zip(i2) {
57+
assert_eq!(*p1.mass, p2.mass);
58+
}
59+
}
60+
61+
#[test]
62+
fn test_ranges() {
63+
let mut particles = Vec::new();
64+
particles.push(Particle::new(String::from("Cl"), 1.0));
65+
particles.push(Particle::new(String::from("Na"), 2.0));
66+
particles.push(Particle::new(String::from("Br"), 3.0));
67+
particles.push(Particle::new(String::from("Zn"), 4.0));
68+
69+
let mut soa = ParticleVec::new();
70+
71+
for particle in particles.iter() {
72+
soa.push(particle.clone());
73+
}
74+
75+
eq_its(soa.iter(), particles.iter());
76+
77+
// All tests from here are the same only changing the range
78+
79+
let range = 0..1;
80+
eq_its(soa.get(range.clone()).unwrap().iter(), particles.get(range.clone()).unwrap().iter());
81+
unsafe { eq_its(soa.get_unchecked(range.clone()).iter(), particles.get_unchecked(range.clone()).iter()); }
82+
eq_its(soa.index(range.clone()).iter(), particles[range.clone()].iter());
83+
eq_its(soa.get_mut(range.clone()).unwrap().iter(), particles.get(range.clone()).unwrap().iter());
84+
unsafe { eq_its(soa.get_unchecked_mut(range.clone()).iter(), particles.get_unchecked(range.clone()).iter()); }
85+
eq_its(soa.index_mut(range.clone()).iter(), particles[range.clone()].iter());
86+
87+
let range = ..3;
88+
eq_its(soa.get(range.clone()).unwrap().iter(), particles.get(range.clone()).unwrap().iter());
89+
unsafe { eq_its(soa.get_unchecked(range.clone()).iter(), particles.get_unchecked(range.clone()).iter()); }
90+
eq_its(soa.index(range.clone()).iter(), particles[range.clone()].iter());
91+
eq_its(soa.get_mut(range.clone()).unwrap().iter(), particles.get(range.clone()).unwrap().iter());
92+
unsafe { eq_its(soa.get_unchecked_mut(range.clone()).iter(), particles.get_unchecked(range.clone()).iter()); }
93+
eq_its(soa.index_mut(range.clone()).iter(), particles[range.clone()].iter());
94+
95+
let range = 1..;
96+
eq_its(soa.get(range.clone()).unwrap().iter(), particles.get(range.clone()).unwrap().iter());
97+
unsafe { eq_its(soa.get_unchecked(range.clone()).iter(), particles.get_unchecked(range.clone()).iter()); }
98+
eq_its(soa.index(range.clone()).iter(), particles[range.clone()].iter());
99+
eq_its(soa.get_mut(range.clone()).unwrap().iter(), particles.get(range.clone()).unwrap().iter());
100+
unsafe { eq_its(soa.get_unchecked_mut(range.clone()).iter(), particles.get_unchecked(range.clone()).iter()); }
101+
eq_its(soa.index_mut(range.clone()).iter(), particles[range.clone()].iter());
102+
103+
let range = ..;
104+
eq_its(soa.get(range.clone()).unwrap().iter(), particles.get(range.clone()).unwrap().iter());
105+
unsafe { eq_its(soa.get_unchecked(range.clone()).iter(), particles.get_unchecked(range.clone()).iter()); }
106+
eq_its(soa.index(range.clone()).iter(), particles[range.clone()].iter());
107+
eq_its(soa.get_mut(range.clone()).unwrap().iter(), particles.get(range.clone()).unwrap().iter());
108+
unsafe { eq_its(soa.get_unchecked_mut(range.clone()).iter(), particles.get_unchecked(range.clone()).iter()); }
109+
eq_its(soa.index_mut(range.clone()).iter(), particles[range.clone()].iter());
110+
111+
let range = 0..=1;
112+
eq_its(soa.get(range.clone()).unwrap().iter(), particles.get(range.clone()).unwrap().iter());
113+
unsafe { eq_its(soa.get_unchecked(range.clone()).iter(), particles.get_unchecked(range.clone()).iter()); }
114+
eq_its(soa.index(range.clone()).iter(), particles[range.clone()].iter());
115+
eq_its(soa.get_mut(range.clone()).unwrap().iter(), particles.get(range.clone()).unwrap().iter());
116+
unsafe { eq_its(soa.get_unchecked_mut(range.clone()).iter(), particles.get_unchecked(range.clone()).iter()); }
117+
eq_its(soa.index_mut(range.clone()).iter(), particles[range.clone()].iter());
118+
119+
let range = ..=2;
120+
eq_its(soa.get(range.clone()).unwrap().iter(), particles.get(range.clone()).unwrap().iter());
121+
unsafe { eq_its(soa.get_unchecked(range.clone()).iter(), particles.get_unchecked(range.clone()).iter()); }
122+
eq_its(soa.index(range.clone()).iter(), particles[range.clone()].iter());
123+
eq_its(soa.get_mut(range.clone()).unwrap().iter(), particles.get(range.clone()).unwrap().iter());
124+
unsafe { eq_its(soa.get_unchecked_mut(range.clone()).iter(), particles.get_unchecked(range.clone()).iter()); }
125+
eq_its(soa.index_mut(range.clone()).iter(), particles[range.clone()].iter());
126+
127+
}

0 commit comments

Comments
 (0)