Commit 6dfdd563 authored by Lucas Serrano's avatar Lucas Serrano
Browse files

Unitary test with google test

parent 4e0f4c14
GTEST_DIR := /usr/src/googletest/googletest
CPATH := ${BOOST_SIMD_ROOT}
ifneq ($(CPATH),)
CPATH := -I $(CPATH)
......@@ -8,11 +10,19 @@ CXXFLAGS = -O3 -march=native
all:
lib/gtest-all.o: $(GTEST_DIR)/src/gtest-all.cc
g++ -isystem $(GTEST_DIR)/include -I $(GTEST_DIR) -pthread -c $< -o $@
obj/unit_%.o: tests/unit_%.cpp fast_matrix.hpp
$(CXX) $(CPATH) $(CXXFLAGS) -isystem $(GTEST_DIR)/include -pthread $< -c -o $@
test: bin/tests
bin/tests: tests.cpp fast_matrix.hpp
$(CXX) $(CPATH) $(CXXFLAGS) $< -o $@
bin/tests: tests/run_all.cpp $(patsubst tests/unit_%.cpp,obj/unit_%.o,$(wildcard tests/unit_*.cpp)) fast_matrix.hpp lib/gtest-all.o
$(CXX) $(CPATH) $(CXXFLAGS) -isystem $(GTEST_DIR)/include -pthread $^ -o $@
$@
clean:
rm -f bin/*
rm -f lib/*
rm -f obj/*
#include<iostream>
#include<cstring>
#include<array>
#include<boost/simd/pack.hpp>
#include<boost/simd/constant/zero.hpp>
......@@ -284,10 +285,10 @@ class BaseMatrix {
return *this;
}
ElementType* dump_array() {
ElementType* returned_array = new ElementType[NRows*NCols];
std::array<ElementType, NRows*NCols> dump_array() const {
std::array<ElementType, NRows*NCols> returned_array;
if (NCols == VecSize) { // In this case there is no padding, we can copy directly the array
std::memcpy(returned_array, this->array, sizeof(T)*NRows*VecSize);
std::memcpy(&returned_array[0], this->array, sizeof(T)*NRows*VecSize);
}
else {
for (int i=0; i<NRows; i++) {
......
#include <gtest/gtest.h>
#include "../fast_matrix.hpp"
int main(int argc, char* argv[]) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
// Unitary tests for BaseMatrix class
#include <array>
#include <gtest/gtest.h>
#include "../fast_matrix.hpp"
TEST(BaseMatrixInstanciation, NoArguments) {
const std::array<float, 6> null_matrix = {0, 0, 0, 0, 0, 0};
BaseMatrix<float, 2, 3, 8> no_args_matrix;
ASSERT_EQ(no_args_matrix.dump_array(), null_matrix);
}
TEST(BaseMatrixInstanciation, ScalarArguments) {
const std::array<float, 9> diag_matrix = {5, 0, 0, 0, 5, 0, 0, 0, 5};
BaseMatrix<float, 3, 3, 8> scalar_arg_matrix(5);
ASSERT_EQ(scalar_arg_matrix.dump_array(), diag_matrix);
}
TEST(BaseMatrixInstanciation, ArrayArguments) {
const std::array<float, 6> array = {1, 2, 3, 4, 5, 6};
BaseMatrix<float, 3, 2, 8> array_3x2_float_matrix(&array[0]);
ASSERT_EQ(array_3x2_float_matrix.dump_array(), array);
BaseMatrix<float, 2, 3, 8> array_2x3_float_matrix(&array[0]);
ASSERT_EQ(array_2x3_float_matrix.dump_array(), array);
BaseMatrix<float, 1, 6, 8> array_1x6_float_matrix(&array[0]);
ASSERT_EQ(array_1x6_float_matrix.dump_array(), array);
BaseMatrix<float, 6, 1, 8> array_6x1_float_matrix(&array[0]);
ASSERT_EQ(array_6x1_float_matrix.dump_array(), array);
const std::array<double, 6> double_array = {1, 2, 3, 4, 5, 6};
BaseMatrix<double, 3, 2, 8> array_3x2_double_matrix(&double_array[0]);
ASSERT_EQ(array_3x2_double_matrix.dump_array(), double_array);
}
TEST(BaseMatrixAssignment, ArrayRValue) {
const std::array<float, 6> array = {1, 2, 3, 4, 5, 6};
BaseMatrix<float, 3, 2, 8> array_3x2_float_matrix;
array_3x2_float_matrix = &array[0];
ASSERT_EQ(array_3x2_float_matrix.dump_array(), array);
}
TEST(BaseMatrixAssignment, BaseMatrixRValue) {
const std::array<float, 6> array_right_matrix = {1, 2, 3, 4, 5, 6};
BaseMatrix<float, 3, 2, 8> left_matrix;
BaseMatrix<float, 3, 2, 8> right_matrix(&array_right_matrix[0]);
left_matrix = right_matrix;
ASSERT_EQ(left_matrix.dump_array(), right_matrix.dump_array());
}
// Unitary tests for arithmetics operators
#include <array>
#include <gtest/gtest.h>
#include "../fast_matrix.hpp"
TEST(MatrixAdd, Square) {
const std::array<float, 9> base_float_array = {1,2,3,4,5,6,7,8,9};
const std::array<float, 9> add_float_array = {1,1,1,1,1,1,1,1,1};
const std::array<float, 9> result_float_array = {2,3,4,5,6,7,8,9,10};
BaseMatrix<float, 3, 3, 8> base_float_mat(&base_float_array[0]);
BaseMatrix<float, 3, 3, 8> add_float_mat(&add_float_array[0]);
BaseMatrix<float, 3, 3, 8> result_float_mat;
matrix_add(base_float_mat, add_float_mat, result_float_mat);
EXPECT_EQ(result_float_mat.dump_array(), result_float_array);
const std::array<double, 9> base_double_array = {1,2,3,4,5,6,7,8,9};
const std::array<double, 9> add_double_array = {1,1,1,1,1,1,1,1,1};
const std::array<double, 9> result_double_array = {2,3,4,5,6,7,8,9,10};
BaseMatrix<double, 3, 3, 8> base_double_mat(&base_double_array[0]);
BaseMatrix<double, 3, 3, 8> add_double_mat(&add_double_array[0]);
BaseMatrix<double, 3, 3, 8> result_double_mat;
matrix_add(base_double_mat, add_double_mat, result_double_mat);
EXPECT_EQ(result_double_mat.dump_array(), result_double_array);
}
TEST(MatrixAdd, Rectangular2x3) {
const std::array<float, 6> base_float_array = {1,2,3,4,5,6};
const std::array<float, 6> add_float_array = {1,1,1,1,1,1};
const std::array<float, 6> result_float_array = {2,3,4,5,6,7};
BaseMatrix<float, 2, 3, 8> base_float_mat(&base_float_array[0]);
BaseMatrix<float, 2, 3, 8> add_float_mat(&add_float_array[0]);
BaseMatrix<float, 2, 3, 8> result_float_mat;
matrix_add(base_float_mat, add_float_mat, result_float_mat);
EXPECT_EQ(result_float_mat.dump_array(), result_float_array);
const std::array<double, 6> base_double_array = {1,2,3,4,5,6};
const std::array<double, 6> add_double_array = {1,1,1,1,1,1};
const std::array<double, 6> result_double_array = {2,3,4,5,6,7};
BaseMatrix<double, 2, 3, 8> base_double_mat(&base_double_array[0]);
BaseMatrix<double, 2, 3, 8> add_double_mat(&add_double_array[0]);
BaseMatrix<double, 2, 3, 8> result_double_mat;
matrix_add(base_double_mat, add_double_mat, result_double_mat);
EXPECT_EQ(result_double_mat.dump_array(), result_double_array);
}
TEST(MatrixSub, Square) {
const std::array<float, 9> base_float_array = {1,2,3,4,5,6,7,8,9};
const std::array<float, 9> sub_float_array = {1,1,1,1,1,1,1,1,1};
const std::array<float, 9> result_float_array = {0,1,2,3,4,5,6,7,8};
BaseMatrix<float, 3, 3, 8> base_float_mat(&base_float_array[0]);
BaseMatrix<float, 3, 3, 8> sub_float_mat(&sub_float_array[0]);
BaseMatrix<float, 3, 3, 8> result_float_mat;
matrix_sub(base_float_mat, sub_float_mat, result_float_mat);
EXPECT_EQ(result_float_mat.dump_array(), result_float_array);
const std::array<double, 9> base_double_array = {1,2,3,4,5,6,7,8,9};
const std::array<double, 9> sub_double_array = {1,1,1,1,1,1,1,1,1};
const std::array<double, 9> result_double_array = {0,1,2,3,4,5,6,7,8};
BaseMatrix<double, 3, 3, 8> base_double_mat(&base_double_array[0]);
BaseMatrix<double, 3, 3, 8> sub_double_mat(&sub_double_array[0]);
BaseMatrix<double, 3, 3, 8> result_double_mat;
matrix_sub(base_double_mat, sub_double_mat, result_double_mat);
EXPECT_EQ(result_double_mat.dump_array(), result_double_array);
}
TEST(MatrixSub, Rectangular2x3) {
const std::array<float, 6> base_float_array = {1,2,3,4,5,6};
const std::array<float, 6> sub_float_array = {1,1,1,1,1,1};
const std::array<float, 6> result_float_array = {0,1,2,3,4,5};
BaseMatrix<float, 2, 3, 8> base_float_mat(&base_float_array[0]);
BaseMatrix<float, 2, 3, 8> sub_float_mat(&sub_float_array[0]);
BaseMatrix<float, 2, 3, 8> result_float_mat;
matrix_sub(base_float_mat, sub_float_mat, result_float_mat);
EXPECT_EQ(result_float_mat.dump_array(), result_float_array);
const std::array<double, 6> base_double_array = {1,2,3,4,5,6};
const std::array<double, 6> sub_double_array = {1,1,1,1,1,1};
const std::array<double, 6> result_double_array = {0,1,2,3,4,5};
BaseMatrix<double, 2, 3, 8> base_double_mat(&base_double_array[0]);
BaseMatrix<double, 2, 3, 8> sub_double_mat(&sub_double_array[0]);
BaseMatrix<double, 2, 3, 8> result_double_mat;
matrix_sub(base_double_mat, sub_double_mat, result_double_mat);
EXPECT_EQ(result_double_mat.dump_array(), result_double_array);
}
TEST(MatrixMulMM, Square) {
const std::array<float, 9> left_float_array = {
1, 2, 0,
2, 5, 1,
1, 1, 0.1,
};
const std::array<float, 9> right_float_array = {
2, 2, 4,
1, 0, 0.5,
1, 1, 2,
};
const std::array<float, 9> result_float_array = {
4, 2, 5,
10, 5, 12.5,
3.1, 2.1, 4.7,
};
BaseMatrix<float, 3, 3, 8> left_float_mat(&left_float_array[0]);
BaseMatrix<float, 3, 3, 8> right_float_mat(&right_float_array[0]);
BaseMatrix<float, 3, 3, 8> result_float_mat;
matrix_mul_m_m(left_float_mat, right_float_mat, result_float_mat);
EXPECT_EQ(result_float_mat.dump_array(), result_float_array);
const std::array<double, 9> left_double_array = {
1, 2, 0,
2, 5, 1,
1, 1, 0.1,
};
const std::array<double, 9> right_double_array = {
2, 2, 4,
1, 0, 0.5,
1, 1, 2,
};
const std::array<double, 9> result_double_array = {
4, 2, 5,
10, 5, 12.5,
3.1, 2.1, 4.7,
};
BaseMatrix<double, 3, 3, 8> left_double_mat(&left_double_array[0]);
BaseMatrix<double, 3, 3, 8> right_double_mat(&right_double_array[0]);
BaseMatrix<double, 3, 3, 8> result_double_mat;
matrix_mul_m_m(left_double_mat, right_double_mat, result_double_mat);
EXPECT_EQ(result_double_mat.dump_array(), result_double_array);
}
TEST(MatrixMulMM, Rectangular2x3t3x2) {
const std::array<float, 6> left_float_array = {
1, 2, 3,
4, 5, 6,
};
const std::array<float, 6> right_float_array = {
7, 8,
9, 10,
11, 12,
};
const std::array<float, 4> result_float_array = {
58, 64,
139, 154,
};
BaseMatrix<float, 2, 3, 8> left_float_mat(&left_float_array[0]);
BaseMatrix<float, 3, 2, 8> right_float_mat(&right_float_array[0]);
BaseMatrix<float, 2, 2, 8> result_float_mat;
matrix_mul_m_m(left_float_mat, right_float_mat, result_float_mat);
EXPECT_EQ(result_float_mat.dump_array(), result_float_array);
const std::array<double, 6> left_double_array = {
1, 2, 3,
4, 5, 6,
};
const std::array<double, 6> right_double_array = {
7, 8,
9, 10,
11, 12,
};
const std::array<double, 4> result_double_array = {
58, 64,
139, 154,
};
BaseMatrix<double, 2, 3, 8> left_double_mat(&left_double_array[0]);
BaseMatrix<double, 3, 2, 8> right_double_mat(&right_double_array[0]);
BaseMatrix<double, 2, 2, 8> result_double_mat;
matrix_mul_m_m(left_double_mat, right_double_mat, result_double_mat);
EXPECT_EQ(result_double_mat.dump_array(), result_double_array);
}
TEST(MatrixMulMtM, Square) {
const std::array<float, 9> left_float_array = {
1, 2, 1,
2, 5, 1,
0, 1, 0.1,
};
const std::array<float, 9> right_float_array = {
2, 2, 4,
1, 0, 0.5,
1, 1, 2,
};
const std::array<float, 9> result_float_array = {
4, 2, 5,
10, 5, 12.5,
3.1, 2.1, 4.7,
};
BaseMatrix<float, 3, 3, 8> left_float_mat(&left_float_array[0]);
BaseMatrix<float, 3, 3, 8> right_float_mat(&right_float_array[0]);
BaseMatrix<float, 3, 3, 8> result_float_mat;
matrix_mul_mt_m(left_float_mat, right_float_mat, result_float_mat);
EXPECT_EQ(result_float_mat.dump_array(), result_float_array);
const std::array<double, 9> left_double_array = {
1, 2, 1,
2, 5, 1,
0, 1, 0.1,
};
const std::array<double, 9> right_double_array = {
2, 2, 4,
1, 0, 0.5,
1, 1, 2,
};
const std::array<double, 9> result_double_array = {
4, 2, 5,
10, 5, 12.5,
3.1, 2.1, 4.7,
};
BaseMatrix<double, 3, 3, 8> left_double_mat(&left_double_array[0]);
BaseMatrix<double, 3, 3, 8> right_double_mat(&right_double_array[0]);
BaseMatrix<double, 3, 3, 8> result_double_mat;
matrix_mul_mt_m(left_double_mat, right_double_mat, result_double_mat);
EXPECT_EQ(result_double_mat.dump_array(), result_double_array);
}
TEST(MatrixMulMtM, Rectangular2x3t3x2) {
const std::array<float, 6> left_float_array = {
1, 4,
2, 5,
3, 6,
};
const std::array<float, 6> right_float_array = {
7, 8,
9, 10,
11, 12,
};
const std::array<float, 4> result_float_array = {
58, 64,
139, 154,
};
BaseMatrix<float, 3, 2, 8> left_float_mat(&left_float_array[0]);
BaseMatrix<float, 3, 2, 8> right_float_mat(&right_float_array[0]);
BaseMatrix<float, 2, 2, 8> result_float_mat;
matrix_mul_mt_m(left_float_mat, right_float_mat, result_float_mat);
EXPECT_EQ(result_float_mat.dump_array(), result_float_array);
const std::array<double, 6> left_double_array = {
1, 4,
2, 5,
3, 6,
};
const std::array<double, 6> right_double_array = {
7, 8,
9, 10,
11, 12,
};
const std::array<double, 4> result_double_array = {
58, 64,
139, 154,
};
BaseMatrix<double, 3, 2, 8> left_double_mat(&left_double_array[0]);
BaseMatrix<double, 3, 2, 8> right_double_mat(&right_double_array[0]);
BaseMatrix<double, 2, 2, 8> result_double_mat;
matrix_mul_mt_m(left_double_mat, right_double_mat, result_double_mat);
EXPECT_EQ(result_double_mat.dump_array(), result_double_array);
}
TEST(MatrixMulMMt, Square) {
const std::array<float, 9> left_float_array = {
1, 2, 0,
2, 5, 1,
1, 1, 0.1,
};
const std::array<float, 9> right_float_array = {
2, 1, 1,
2, 0, 1,
4, 0.5, 2,
};
const std::array<float, 9> result_float_array = {
4, 2, 5,
10, 5, 12.5,
3.1, 2.1, 4.7,
};
BaseMatrix<float, 3, 3, 8> left_float_mat(&left_float_array[0]);
BaseMatrix<float, 3, 3, 8> right_float_mat(&right_float_array[0]);
BaseMatrix<float, 3, 3, 8> result_float_mat;
matrix_mul_m_mt(left_float_mat, right_float_mat, result_float_mat);
EXPECT_EQ(result_float_mat.dump_array(), result_float_array);
const std::array<double, 9> left_double_array = {
1, 2, 0,
2, 5, 1,
1, 1, 0.1,
};
const std::array<double, 9> right_double_array = {
2, 1, 1,
2, 0, 1,
4, 0.5, 2,
};
const std::array<double, 9> result_double_array = {
4, 2, 5,
10, 5, 12.5,
3.1, 2.1, 4.7,
};
BaseMatrix<double, 3, 3, 8> left_double_mat(&left_double_array[0]);
BaseMatrix<double, 3, 3, 8> right_double_mat(&right_double_array[0]);
BaseMatrix<double, 3, 3, 8> result_double_mat;
matrix_mul_m_mt(left_double_mat, right_double_mat, result_double_mat);
EXPECT_EQ(result_double_mat.dump_array(), result_double_array);
}
TEST(MatrixMulMMt, Rectangular2x3t3x2) {
const std::array<float, 6> left_float_array = {
1, 2, 3,
4, 5, 6,
};
const std::array<float, 6> right_float_array = {
7, 9, 11,
8, 10, 12
};
const std::array<float, 4> result_float_array = {
58, 64,
139, 154,
};
BaseMatrix<float, 2, 3, 8> left_float_mat(&left_float_array[0]);
BaseMatrix<float, 2, 3, 8> right_float_mat(&right_float_array[0]);
BaseMatrix<float, 2, 2, 8> result_float_mat;
matrix_mul_m_mt(left_float_mat, right_float_mat, result_float_mat);
EXPECT_EQ(result_float_mat.dump_array(), result_float_array);
const std::array<double, 6> left_double_array = {
1, 2, 3,
4, 5, 6,
};
const std::array<double, 6> right_double_array = {
7, 9, 11,
8, 10, 12
};
const std::array<double, 4> result_double_array = {
58, 64,
139, 154,
};
BaseMatrix<double, 2, 3, 8> left_double_mat(&left_double_array[0]);
BaseMatrix<double, 2, 3, 8> right_double_mat(&right_double_array[0]);
BaseMatrix<double, 2, 2, 8> result_double_mat;
matrix_mul_m_mt(left_double_mat, right_double_mat, result_double_mat);
EXPECT_EQ(result_double_mat.dump_array(), result_double_array);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment