File: test_gt_math.cpp

package info (click to toggle)
gridtools 2.3.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 29,480 kB
  • sloc: cpp: 228,792; python: 17,561; javascript: 9,164; ansic: 4,101; sh: 850; makefile: 231; f90: 201
file content (150 lines) | stat: -rw-r--r-- 4,351 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * GridTools
 *
 * Copyright (c) 2014-2023, ETH Zurich
 * All rights reserved.
 *
 * Please, refer to the LICENSE file in the root directory.
 * SPDX-License-Identifier: BSD-3-Clause
 */
#include <gridtools/common/gt_math.hpp>

#include <gtest/gtest.h>

#include <gridtools/common/host_device.hpp>

#include <verifier.hpp>

using namespace gridtools;

template <class Value>
bool GT_FUNCTION test_pow(Value val, Value result) {
    return expect_with_threshold(math::pow(val, val), result);
}

template <typename Value>
bool GT_FUNCTION test_log(Value val, Value result) {
    return expect_with_threshold(math::log(val), result);
}

template <typename Value>
bool GT_FUNCTION test_exp(Value val, Value result) {
    return expect_with_threshold(math::exp(val), result);
}

static_assert(std::is_same_v<decltype(math::fabs(4.0f)), float>);
static_assert(std::is_same_v<decltype(math::fabs(4.0)), double>);
#ifndef __CUDA_ARCH__
static_assert(std::is_same_v<decltype(math::fabs((long double)4)), long double>);
#endif
static_assert(std::is_same_v<decltype(math::fabs((int)4)), double>);

bool GT_FUNCTION test_fabs() {
    if (!expect_with_threshold(math::fabs(5.6), 5.6, 1e-14))
        return false;
    else if (!expect_with_threshold(math::fabs(-5.6), 5.6, 1e-14))
        return false;
    else if (!expect_with_threshold(math::fabs(-5.6f), 5.6f, 1e-14))
        return false;
    else if (!expect_with_threshold(math::fabs(-5), (double)5, 1e-14))
        return false;
#ifndef __CUDA_ARCH__
    else if (!expect_with_threshold(math::fabs((long double)-5), (long double)5., 1e-14))
        return false;
#endif
    else
        return true;
}

static_assert(std::is_same_v<decltype(math::abs(4.0f)), float>);
static_assert(std::is_same_v<decltype(math::abs(4.0)), double>);
#ifndef __CUDA_ARCH__
static_assert(std::is_same_v<decltype(math::abs((long double)4)), long double>);
#endif

// int overloads
static_assert(std::is_same_v<decltype(math::abs((int)4)), int>);
static_assert(std::is_same_v<decltype(math::abs((long)4)), long>);
static_assert(std::is_same_v<decltype(math::abs((long long)4)), long long>);

GT_FUNCTION bool test_abs() {
    // float overloads
    if (math::abs(5.6) != 5.6)
        return false;
    else if (math::abs(-5.6) != 5.6)
        return false;
    else if (math::abs(-5.6f) != 5.6f)
        return false;
    else if (math::abs(-5) != 5)
        return false;
    else
        return true;
}

namespace {
    TEST(math, test_min) {
        EXPECT_TRUE(math::min(5, 2, 7) == 2);
        EXPECT_TRUE(math::min(5, -1) == -1);

        ASSERT_EQ(math::min(5.3, 22.0, 7.7), 5.3);
    }

    TEST(math, test_min_ref) {
        // checking returned by const &
        double a = 3.5;
        double b = 2.3;
        double const &min = math::min(a, b);
        ASSERT_EQ(min, 2.3);
        b = 8;
        ASSERT_EQ(min, 8);
    }

    TEST(math, test_max) {
        EXPECT_TRUE(math::max(5, 2, 7) == 7);
        EXPECT_TRUE(math::max(5, -1) == 5);

        ASSERT_EQ(math::max(5.3, 22.0, 7.7), 22.0);
    }

    TEST(math, test_max_ref) {
        // checking returned by const &
        double a = 3.5;
        double b = 2.3;
        double const &max = math::max(a, b);

        ASSERT_EQ(max, 3.5);
        a = 8;
        ASSERT_EQ(max, 8);
    }

    TEST(math, test_fabs) { EXPECT_TRUE(test_fabs()); }

    TEST(math, test_abs) { EXPECT_TRUE(test_abs()); }

    TEST(math, test_log) {
        EXPECT_TRUE(test_log<double>(2.3, std::log(2.3)));
        EXPECT_TRUE(test_log<float>(2.3f, std::log(2.3f)));
    }

    TEST(math, test_exp) {
        EXPECT_TRUE(test_exp<double>(2.3, std::exp(2.3)));
        EXPECT_TRUE(test_exp<float>(2.3f, std::exp(2.3f)));
    }

    TEST(math, test_pow) {
        EXPECT_TRUE(test_pow<double>(2.3, std::pow(2.3, 2.3)));
        EXPECT_TRUE(test_pow<float>(2.3f, std::pow(2.3f, 2.3f)));
    }

    TEST(math, test_fmod) {
        EXPECT_FLOAT_EQ(math::fmod(3.7f, 1.2f), std::fmod(3.7f, 1.2f));
        EXPECT_DOUBLE_EQ(math::fmod(3.7, 1.2), std::fmod(3.7, 1.2));
        EXPECT_DOUBLE_EQ(math::fmod(3.7l, 1.2l), std::fmod(3.7l, 1.2l));
    }

    TEST(math, test_trunc) {
        EXPECT_FLOAT_EQ(math::trunc(3.7f), std::trunc(3.7f));
        EXPECT_DOUBLE_EQ(math::trunc(3.7), std::trunc(3.7));
        EXPECT_DOUBLE_EQ(math::trunc(3.7l), std::trunc(3.7l));
    }
} // namespace