blob: 51d6d7e1df4cd2eccc972edfd0a5d0f3e0ee56db [file] [log] [blame]
Kevin DuBoisbb27bcd2019-04-02 14:34:35 -07001/*
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
27namespace android {
28
29struct RegionSamplingTest : testing::Test {
30public:
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
40TEST_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
45TEST_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
50TEST_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
60TEST_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
69TEST_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