Skip to content

Commit

Permalink
test_utils test
Browse files Browse the repository at this point in the history
  • Loading branch information
yhmtsai committed Nov 4, 2024
1 parent 95a2dd2 commit 39b79d5
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 40 deletions.
18 changes: 11 additions & 7 deletions core/test/utils/array_generator_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,30 @@ template <typename T>
class ArrayGenerator : public ::testing::Test {
protected:
using value_type = T;
using check_type = double;

ArrayGenerator() : exec(gko::ReferenceExecutor::create())
{
array = gko::test::generate_random_array<T>(
500, std::normal_distribution<gko::remove_complex<T>>(20.0, 5.0),
500, std::normal_distribution<>(20.0, 5.0),
std::default_random_engine(42), exec);
}

std::shared_ptr<const gko::Executor> exec;
gko::array<T> array;

template <typename InputIterator, typename ValueType, typename Closure>
ValueType get_nth_moment(int n, ValueType c, InputIterator sample_start,
InputIterator sample_end, Closure closure_op)
check_type get_nth_moment(int n, ValueType c, InputIterator sample_start,
InputIterator sample_end, Closure closure_op)
{
using std::pow;
ValueType res = 0;
ValueType num_elems = 0;
check_type res = 0;
check_type num_elems = 0;
while (sample_start != sample_end) {
auto tmp = *(sample_start++);
res += pow(closure_op(tmp) - c, n);
res += pow(static_cast<check_type>(closure_op(tmp)) -
static_cast<check_type>(c),
n);
num_elems += 1;
}
return res / num_elems;
Expand All @@ -62,7 +65,8 @@ class ArrayGenerator : public ::testing::Test {
}
};

TYPED_TEST_SUITE(ArrayGenerator, gko::test::ValueTypes, TypenameNameGenerator);
TYPED_TEST_SUITE(ArrayGenerator, gko::test::ValueTypesWithHalf,
TypenameNameGenerator);


TYPED_TEST(ArrayGenerator, OutputHasCorrectSize)
Expand Down
49 changes: 29 additions & 20 deletions core/test/utils/matrix_generator_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,32 @@ template <typename T>
class MatrixGenerator : public ::testing::Test {
protected:
using value_type = T;
using check_type = double;
using real_type = gko::remove_complex<T>;
using mtx_type = gko::matrix::Dense<T>;

MatrixGenerator()
: exec(gko::ReferenceExecutor::create()),
mtx(gko::test::generate_random_matrix<mtx_type>(
500, 100, std::normal_distribution<real_type>(50, 5),
std::normal_distribution<real_type>(20.0, 5.0),
500, 100, std::normal_distribution<>(50, 5),
std::normal_distribution<>(20.0, 5.0),
std::default_random_engine(42), exec)),
dense_mtx(gko::test::generate_random_dense_matrix<value_type>(
500, 100, std::normal_distribution<real_type>(20.0, 5.0),
500, 100, std::normal_distribution<>(20.0, 5.0),
std::default_random_engine(41), exec)),
l_mtx(gko::test::generate_random_lower_triangular_matrix<mtx_type>(
4, true, std::normal_distribution<real_type>(50, 5),
std::normal_distribution<real_type>(20.0, 5.0),
4, true, std::normal_distribution<>(50, 5),
std::normal_distribution<>(20.0, 5.0),
std::default_random_engine(42), exec)),
u_mtx(gko::test::generate_random_upper_triangular_matrix<mtx_type>(
4, true, std::normal_distribution<real_type>(50, 5),
std::normal_distribution<real_type>(20.0, 5.0),
4, true, std::normal_distribution<>(50, 5),
std::normal_distribution<>(20.0, 5.0),
std::default_random_engine(42), exec)),
lower_bandwidth(2),
upper_bandwidth(3),
band_mtx(gko::test::generate_random_band_matrix<mtx_type>(
100, lower_bandwidth, upper_bandwidth,
std::normal_distribution<real_type>(20.0, 5.0),
std::normal_distribution<>(20.0, 5.0),
std::default_random_engine(42), exec)),
nnz_per_row_sample(500, 0),
values_sample(0),
Expand Down Expand Up @@ -96,15 +97,17 @@ class MatrixGenerator : public ::testing::Test {


template <typename InputIterator, typename ValueType, typename Closure>
ValueType get_nth_moment(int n, ValueType c, InputIterator sample_start,
InputIterator sample_end, Closure closure_op)
check_type get_nth_moment(int n, ValueType c, InputIterator sample_start,
InputIterator sample_end, Closure closure_op)
{
using std::pow;
ValueType res = 0;
ValueType num_elems = 0;
check_type res = 0;
check_type num_elems = 0;
while (sample_start != sample_end) {
auto tmp = *(sample_start++);
res += pow(closure_op(tmp) - c, n);
res += pow(static_cast<check_type>(closure_op(tmp)) -
static_cast<check_type>(c),
n);
num_elems += 1;
}
return res / num_elems;
Expand All @@ -128,7 +131,8 @@ class MatrixGenerator : public ::testing::Test {
}
};

TYPED_TEST_SUITE(MatrixGenerator, gko::test::ValueTypes, TypenameNameGenerator);
TYPED_TEST_SUITE(MatrixGenerator, gko::test::ValueTypesWithHalf,
TypenameNameGenerator);


TYPED_TEST(MatrixGenerator, OutputHasCorrectSize)
Expand Down Expand Up @@ -247,7 +251,7 @@ TYPED_TEST(MatrixGenerator, CanGenerateTridiagMatrix)
{
using T = typename TestFixture::value_type;
using Dense = typename TestFixture::mtx_type;
auto dist = std::normal_distribution<gko::remove_complex<T>>(0, 1);
auto dist = std::normal_distribution<>(0, 1);
auto engine = std::default_random_engine(42);
auto lower = gko::test::detail::get_rand_value<T>(dist, engine);
auto diag = gko::test::detail::get_rand_value<T>(dist, engine);
Expand All @@ -271,18 +275,23 @@ TYPED_TEST(MatrixGenerator, CanGenerateTridiagInverseMatrix)
{
using T = typename TestFixture::value_type;
using Dense = typename TestFixture::mtx_type;
auto dist = std::normal_distribution<gko::remove_complex<T>>(0, 1);
auto dist = std::normal_distribution<>(0, 1);
auto engine = std::default_random_engine(42);
auto lower = gko::test::detail::get_rand_value<T>(dist, engine);
auto upper = gko::test::detail::get_rand_value<T>(dist, engine);
// make diagonally dominant
auto diag = std::abs(gko::test::detail::get_rand_value<T>(dist, engine)) +
std::abs(lower) + std::abs(upper);
auto diag = gko::abs(gko::test::detail::get_rand_value<T>(dist, engine)) +
gko::abs(lower) + gko::abs(upper);
gko::size_type size = 50;
if (std::is_same_v<gko::remove_complex<T>, gko::half>) {
// half precision can only handle the inverse of small matrix.
size = 5;
}

auto mtx = gko::test::generate_tridiag_matrix<Dense>(
50, {lower, diag, upper}, this->exec);
size, {lower, diag, upper}, this->exec);
auto inv_mtx = gko::test::generate_tridiag_inverse_matrix<Dense>(
50, {lower, diag, upper}, this->exec);
size, {lower, diag, upper}, this->exec);

auto result = Dense::create(this->exec, mtx->get_size());
inv_mtx->apply(mtx, result);
Expand Down
11 changes: 6 additions & 5 deletions core/test/utils/matrix_utils_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class MatrixUtils : public ::testing::Test {
MatrixUtils()
: exec(gko::ReferenceExecutor::create()),
data(gko::test::generate_random_matrix_data<value_type, int>(
500, 500, std::normal_distribution<real_type>(50, 5),
std::normal_distribution<real_type>(20.0, 5.0),
500, 500, std::normal_distribution<>(50, 5),
std::normal_distribution<>(20.0, 5.0),
std::default_random_engine(42))),
rectangular_data(gko::dim<2>(500, 100))
{}
Expand All @@ -41,7 +41,8 @@ class MatrixUtils : public ::testing::Test {
mtx_data rectangular_data;
};

TYPED_TEST_SUITE(MatrixUtils, gko::test::ValueTypes, TypenameNameGenerator);
TYPED_TEST_SUITE(MatrixUtils, gko::test::ValueTypesWithHalf,
TypenameNameGenerator);


TYPED_TEST(MatrixUtils, MakeSymmetricThrowsError)
Expand Down Expand Up @@ -241,7 +242,7 @@ TYPED_TEST(MatrixUtils, MakeHpdMatrixCorrectly)
TYPED_TEST(MatrixUtils, MakeHpdMatrixWithRatioCorrectly)
{
using T = typename TestFixture::value_type;
gko::remove_complex<T> ratio = 1.00001;
gko::remove_complex<T> ratio = 1.01;
auto cpy_data = this->data;

gko::utils::make_hpd(this->data, ratio);
Expand Down Expand Up @@ -276,7 +277,7 @@ TYPED_TEST(MatrixUtils, MakeSpdMatrixCorrectly)
TYPED_TEST(MatrixUtils, MakeSpdMatrixWithRatioCorrectly)
{
using T = typename TestFixture::value_type;
gko::remove_complex<T> ratio = 1.00001;
gko::remove_complex<T> ratio = 1.01;
auto cpy_data = this->data;

gko::utils::make_spd(this->data, ratio);
Expand Down
2 changes: 1 addition & 1 deletion core/test/utils/unsort_matrix_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class UnsortMatrix : public ::testing::Test {
std::unique_ptr<Coo> coo_empty;
};

TYPED_TEST_SUITE(UnsortMatrix, gko::test::ValueIndexTypes,
TYPED_TEST_SUITE(UnsortMatrix, gko::test::ValueIndexTypesWithHalf,
PairTypenameNameGenerator);


Expand Down
16 changes: 10 additions & 6 deletions core/test/utils/value_generator_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,22 @@ template <typename T>
class ValueGenerator : public ::testing::Test {
protected:
using value_type = T;
using check_type = double;

ValueGenerator() {}

template <typename InputIterator, typename ValueType, typename Closure>
ValueType get_nth_moment(int n, ValueType c, InputIterator sample_start,
InputIterator sample_end, Closure closure_op)
check_type get_nth_moment(int n, ValueType c, InputIterator sample_start,
InputIterator sample_end, Closure closure_op)
{
using std::pow;
ValueType res = 0;
ValueType num_elems = 0;
check_type res = 0;
check_type num_elems = 0;
while (sample_start != sample_end) {
auto tmp = *(sample_start++);
res += pow(closure_op(tmp) - c, n);
res += pow(static_cast<check_type>(closure_op(tmp)) -
static_cast<check_type>(c),
n);
num_elems += 1;
}
return res / num_elems;
Expand All @@ -56,7 +59,8 @@ class ValueGenerator : public ::testing::Test {
}
};

TYPED_TEST_SUITE(ValueGenerator, gko::test::ValueTypes, TypenameNameGenerator);
TYPED_TEST_SUITE(ValueGenerator, gko::test::ValueTypesWithHalf,
TypenameNameGenerator);


TYPED_TEST(ValueGenerator, OutputHasCorrectAverageAndDeviation)
Expand Down
3 changes: 2 additions & 1 deletion reference/test/utils/assertions_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ namespace {
template <typename T>
class MatricesNear : public ::testing::Test {};

TYPED_TEST_SUITE(MatricesNear, gko::test::ValueTypes, TypenameNameGenerator);
TYPED_TEST_SUITE(MatricesNear, gko::test::ValueTypesWithHalf,
TypenameNameGenerator);


TYPED_TEST(MatricesNear, CanPassAnyMatrixType)
Expand Down

0 comments on commit 39b79d5

Please sign in to comment.