half_matrix

[Done] Triangular Matrix Library for Rust.
git clone https://git.jojolepro.com/half_matrix.git
Log | Files | Refs | README | LICENSE

commit e57b78b6052e93e5abd3e8cc94fd0762978c14a8
parent 067f587c125e66156eba0de14c2606da00fc27ba
Author: Joël Lupien (Jojolepro) <jojolepromain@gmail.com>
Date:   Tue,  1 Jan 2019 14:10:33 -0500

Fix not checking for index overflow

Diffstat:
MCargo.toml | 2+-
Msrc/lib.rs | 15+++++++++++++++
2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/Cargo.toml b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "half_matrix" -version = "0.2.0" +version = "0.2.1" authors = ["Joël Lupien (Jojolepro) <jojolepromain@gmail.com>"] edition = "2018" description = """ diff --git a/src/lib.rs b/src/lib.rs @@ -40,6 +40,7 @@ use hibitset::BitSet; #[derive(Debug, Clone)] pub struct HalfMatrix { size: u32, + index_size: u32, collision_matrix: BitSet, } @@ -54,6 +55,7 @@ impl HalfMatrix { let index_size = (size * (size + 1)) / 2; HalfMatrix { size, + index_size, collision_matrix: BitSet::with_capacity(index_size), } } @@ -91,6 +93,9 @@ impl HalfMatrix { let row_sum = (a * (a + 1)) / 2; let idx = row_sum + b; + + assert!(idx < self.index_size); + idx } } @@ -108,6 +113,16 @@ mod tests { assert_eq!(m.index_of(2,2), 5); } + #[should_panic] + #[test] + fn index_of_fail() { + // 4x4 + // C,C (2,2) + // Expected: 5 + let m = HalfMatrix::new(1); + m.index_of(0, 1); + } + #[test] fn index_calculation2() { // 4x4