From 5872bbc28cb60811b1527db042b4ede69f6880ed Mon Sep 17 00:00:00 2001 From: Oskar Weigl Date: Sun, 28 Oct 2018 16:00:25 -0700 Subject: [PATCH] Add slicing tests that are not pure row slicing I had a look at the implementation of `slice`, and I found it odd that it doesn't have a copy loop. The current implementation does a raw memcpy of the underlying contiguous row-major data. As far as I can tell, this could only slice along rows. Interestingly, I found that the tests only tested for slicing along rows, so this bug would go unnoticed. I added some tests that checks slicing along columns also. I have a feeling this would break, and we need to fix the implementation of `slice`. However I could be wrong, and hence I'm submitting these tests first to verify. --- test/slice.cpp | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/test/slice.cpp b/test/slice.cpp index 4c43e93c0a..ee1da077d5 100644 --- a/test/slice.cpp +++ b/test/slice.cpp @@ -9,15 +9,37 @@ int main() 4, 5, 6, 7, 8, 10 }; - float data_check[6] = { + SquareMatrix A(data); + + // Test row slicing + Matrix B_rowslice(A.slice<2, 3>(1, 0)); + float data_check_rowslice[6] = { 4, 5, 6, 7, 8, 10 }; - SquareMatrix A(data); - Matrix B_check(data_check); - Matrix B(A.slice<2, 3>(1, 0)); - TEST(isEqual(B, B_check)); + Matrix B_check_rowslice(data_check_rowslice); + TEST(isEqual(B_rowslice, B_check_rowslice)); + + // Test column slicing + Matrix B_colslice(A.slice<3, 2>(0, 1)); + float data_check_colslice[6] = { + 2, 3, + 5, 6, + 8, 10 + }; + Matrix B_check_colslice(data_check_colslice); + TEST(isEqual(B_colslice, B_check_colslice)); + // Test slicing both + Matrix B_bothslice(A.slice<2, 2>(1, 1)); + float data_check_bothslice[4] = { + 5, 6, + 8, 10 + }; + Matrix B_check_bothslice(data_check_bothslice); + TEST(isEqual(B_bothslice, B_check_bothslice)); + + //Test block writing float data_2[4] = { 11, 12, 13, 14