Kevin DuBois | bb27bcd | 2019-04-02 14:34:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | #undef LOG_TAG |
| 18 | #define LOG_TAG "RegionSamplingTest" |
| 19 | |
| 20 | #include <gmock/gmock.h> |
| 21 | #include <gtest/gtest.h> |
| 22 | #include <array> |
| 23 | #include <limits> |
| 24 | |
| 25 | #include "RegionSamplingThread.h" |
| 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | struct RegionSamplingTest : testing::Test { |
| 30 | public: |
| 31 | static uint32_t constexpr kBlack = 0; |
| 32 | static uint32_t constexpr kWhite = std::numeric_limits<uint32_t>::max(); |
| 33 | static int constexpr kWidth = 98; |
| 34 | static int constexpr kStride = 100; |
| 35 | static int constexpr kHeight = 29; |
| 36 | std::array<uint32_t, kHeight * kStride> buffer; |
| 37 | Rect const whole_area{0, 0, kWidth, kHeight}; |
| 38 | }; |
| 39 | |
| 40 | TEST_F(RegionSamplingTest, calculate_mean_white) { |
| 41 | std::fill(buffer.begin(), buffer.end(), kWhite); |
| 42 | EXPECT_THAT(sampleArea(buffer.data(), kStride, whole_area), testing::FloatEq(1.0f)); |
| 43 | } |
| 44 | |
| 45 | TEST_F(RegionSamplingTest, calculate_mean_black) { |
| 46 | std::fill(buffer.begin(), buffer.end(), kBlack); |
| 47 | EXPECT_THAT(sampleArea(buffer.data(), kStride, whole_area), testing::FloatEq(0.0f)); |
| 48 | } |
| 49 | |
| 50 | TEST_F(RegionSamplingTest, calculate_mean_partial_region) { |
| 51 | auto const halfway_down = kHeight >> 1; |
| 52 | auto const half = halfway_down * kStride; |
| 53 | Rect const partial_region = {whole_area.left, whole_area.top, whole_area.right, |
| 54 | whole_area.top + halfway_down}; |
| 55 | std::fill(buffer.begin(), buffer.begin() + half, 0); |
| 56 | std::fill(buffer.begin() + half, buffer.end(), kWhite); |
| 57 | EXPECT_THAT(sampleArea(buffer.data(), kStride, partial_region), testing::FloatEq(0.0f)); |
| 58 | } |
| 59 | |
| 60 | TEST_F(RegionSamplingTest, calculate_mean_mixed_values) { |
| 61 | std::generate(buffer.begin(), buffer.end(), [n = 0]() mutable { |
| 62 | uint32_t const pixel = (n % std::numeric_limits<uint8_t>::max()) << ((n % 3) * CHAR_BIT); |
| 63 | n++; |
| 64 | return pixel; |
| 65 | }); |
| 66 | EXPECT_THAT(sampleArea(buffer.data(), kStride, whole_area), testing::FloatNear(0.083f, 0.01f)); |
| 67 | } |
| 68 | |
| 69 | TEST_F(RegionSamplingTest, bimodal_tiebreaker) { |
| 70 | std::generate(buffer.begin(), buffer.end(), |
| 71 | [n = 0]() mutable { return (n++ % 2) ? kBlack : kWhite; }); |
| 72 | // presently there's no tiebreaking strategy in place, accept either of the means |
| 73 | EXPECT_THAT(sampleArea(buffer.data(), kStride, whole_area), |
| 74 | testing::AnyOf(testing::FloatEq(1.0), testing::FloatEq(0.0f))); |
| 75 | } |
| 76 | |
| 77 | } // namespace android |