• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 #include <array>
18 #include <climits>
19 #include <math.h>
20 
21 #include <audio_utils/sample.h>
22 #include <gtest/gtest.h>
23 
24 static_assert(sizeof(sample_minifloat_t) == sizeof(uint16_t),
25         "sizeof(sample_minifloat_t != sizeof(uint16_t");
26 
signum(float f)27 static constexpr int signum(float f)
28 {
29     return (f > 0) - (f < 0);
30 }
31 
TEST(audio_utils_sample,Convert)32 TEST(audio_utils_sample, Convert)
33 {
34     std::vector<float> fvec;
35     // verify minifloat <-> float is a bijection, and monotonic as float
36     for (int i = 0; i <= 0xFFFF; i++) {
37         // construct floats in order
38         const int val = i < 0x8000 ? 0xFFFF - i : i ^ 0x8000;
39         // TODO shouldn't depend on representation in order to skip negative zero
40         if (val == 0x8000) {
41             // This is an undefined value and so we won't test its behavior
42             continue;
43         }
44         // TODO reinterpret_cast<sample_minifloat_t>(val) fails
45         const sample_minifloat_t in = (sample_minifloat_t) val;
46         const float f = float_from_sample(in);
47         const sample_minifloat_t out = sample_from_float(f);
48         ASSERT_EQ(in, out);
49         fvec.push_back(f);
50     }
51     // no longer needed since we construct floats in order
52     // #include <algorithm>
53     // std::sort(fvec.begin(), fvec.end());
54     float prev = -2.0f;
55     for (auto curr : fvec) {
56         // LT instead of LE because no negative zero
57         ASSERT_LT(prev, curr);
58         int signum_prev = signum(prev);
59         int signum_curr = signum(curr);
60         ASSERT_LE(signum_prev, signum_curr);
61         if (signum_prev == signum_curr) {
62             // confirm ratio between adjacent values (3:45 of "Will it float?" video)
63             float ratio = curr / prev;
64             float lower, upper;
65             // normal
66             if (fabsf(curr) >= 0.001f) {
67                 upper = 1.005f;
68                 lower = 0.995f;
69             // denormal
70             } else {
71                 upper = 2.0f;
72                 lower = 0.5f;
73             }
74             ASSERT_GE(ratio, lower) << "prev " << prev << " curr " << curr;
75             ASSERT_LE(ratio, upper) << "prev " << prev << " curr " << curr;
76         }
77         prev = curr;
78     }
79     ASSERT_LT(prev, 2.0f);
80 }
81