1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "audio_math_tests"
18 
19 #include <audio_utils/safe_math.h>
20 
21 #include <gtest/gtest.h>
22 #include <utils/Log.h>
23 
TEST(audio_math_tests,safe_isnan)24 TEST(audio_math_tests, safe_isnan) {
25     const auto nf = std::nanf("");
26     const bool std_isnan = std::isnan(nf);
27     const bool eq_isnan = nf != nf;
28     const bool au_isnan = android::audio_utils::safe_isnan(nf);
29 
30 #ifndef FAST_MATH_ENABLED
31     EXPECT_TRUE(std_isnan);  // not always true for -ffast-math, assumed false
32     EXPECT_TRUE(eq_isnan);   // not always true for -ffast-math
33 #endif
34 
35     EXPECT_TRUE(au_isnan);
36     constexpr bool not_a_nan = android::audio_utils::safe_isnan(1.f);
37     EXPECT_FALSE(not_a_nan);
38     EXPECT_FALSE(android::audio_utils::safe_isnan(std::numeric_limits<float>::infinity()));
39     EXPECT_FALSE(android::audio_utils::safe_isnan(std::numeric_limits<float>::max()));
40     EXPECT_FALSE(android::audio_utils::safe_isnan(std::numeric_limits<float>::min()));
41 
42     EXPECT_TRUE(android::audio_utils::safe_isnan(nan("")));
43 
44     ALOGD("%s: std::isnan:%d  eq_isnan:%d  audio_utils::isnan:%d",
45             __func__, std_isnan, eq_isnan, au_isnan);
46 }
47 
TEST(audio_math_tests,safe_isinf)48 TEST(audio_math_tests, safe_isinf) {
49     const auto inf = std::numeric_limits<float>::infinity();
50     const bool std_isinf = std::isinf(inf);
51     const bool eq_isinf = inf == std::numeric_limits<float>::infinity();
52     const bool au_isinf = android::audio_utils::safe_isinf(inf);
53 
54 #ifndef FAST_MATH_ENABLED
55     EXPECT_TRUE(std_isinf);  // not always true for -ffast-math, assumed false
56 #endif
57 
58     EXPECT_TRUE(eq_isinf);
59     EXPECT_TRUE(au_isinf);
60     constexpr bool not_a_inf = android::audio_utils::safe_isinf(1.f);
61     EXPECT_FALSE(not_a_inf);
62     EXPECT_FALSE(android::audio_utils::safe_isinf(std::nanf("")));
63     EXPECT_FALSE(android::audio_utils::safe_isinf(std::numeric_limits<float>::max()));
64     EXPECT_FALSE(android::audio_utils::safe_isinf(std::numeric_limits<float>::min()));
65 
66     EXPECT_TRUE(android::audio_utils::safe_isinf(std::numeric_limits<double>::infinity()));
67 
68     ALOGD("%s: std::isinf:%d  eq_isinf:%d  audio_utils::isinf:%d",
69             __func__, std_isinf, eq_isinf, au_isinf);
70 }
71 
TEST(audio_math_tests,safe_add_sat)72 TEST(audio_math_tests, safe_add_sat) {
73     static_assert(INT_MAX == android::audio_utils::safe_add_sat(INT_MAX, 10));
74     static_assert(INT_MAX - 10 == android::audio_utils::safe_add_sat(INT_MAX, -10));
75     static_assert(INT_MIN == android::audio_utils::safe_add_sat(INT_MIN, -10));
76     static_assert(INT_MIN + 10 == android::audio_utils::safe_add_sat(INT_MIN, 10));
77     static_assert(UINT_MAX == android::audio_utils::safe_add_sat(UINT_MAX, 10U));
78     static_assert(UINT_MAX - 10 == android::audio_utils::safe_add_sat(UINT_MAX - 20, 10U));
79     static_assert(UINT_MAX - 10 == android::audio_utils::safe_add_sat(UINT_MAX - 20, 10U));
80 }
81