1 /*
2 * Copyright (c) 2024, Alliance for Open Media. All rights reserved
3 *
4 * This source code is subject to the terms of the BSD 3-Clause Clear License
5 * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
6 * License was not distributed with this source code in the LICENSE file, you
7 * can obtain it at www.aomedia.org/license/software-license/bsd-3-c-c. If the
8 * Alliance for Open Media Patent License 1.0 was not distributed with this
9 * source code in the PATENTS file, you can obtain it at
10 * www.aomedia.org/license/patent.
11 */
12
13 #include <cstddef>
14 #include <cstdint>
15 #include <vector>
16
17 #include "absl/status/status.h"
18 #include "absl/status/status_matchers.h"
19 #include "absl/types/span.h"
20 #include "gmock/gmock.h"
21 #include "gtest/gtest.h"
22 #include "iamf/cli/tests/cli_test_utils.h"
23
24 namespace iamf_tools {
25 namespace {
26
27 using ::absl_testing::IsOk;
28 using ::absl_testing::StatusIs;
29
30 using absl::StatusCode::kFailedPrecondition;
31
32 constexpr uint32_t kMaxInputTicks = 4;
33 constexpr uint32_t kMaxOutputTicks = 4;
34 constexpr size_t kNumChannels = 2;
35
TEST(GetOutputSamplesAsSpan,ReturnsEmptyAfterConstruction)36 TEST(GetOutputSamplesAsSpan, ReturnsEmptyAfterConstruction) {
37 const MockSampleProcessor mock_resampler(kMaxInputTicks, kNumChannels,
38 kMaxOutputTicks);
39 EXPECT_TRUE(mock_resampler.GetOutputSamplesAsSpan().empty());
40 }
41
TEST(GetOutputSamplesAsSpan,SizeMatchesNumValidTicks)42 TEST(GetOutputSamplesAsSpan, SizeMatchesNumValidTicks) {
43 EverySecondTickResampler every_second_tick_resampler(kMaxInputTicks,
44 kNumChannels);
45 EXPECT_THAT(
46 every_second_tick_resampler.PushFrame({{1, 2}, {3, 4}, {5, 6}, {7, 8}}),
47 IsOk());
48 EXPECT_EQ(every_second_tick_resampler.GetOutputSamplesAsSpan().size(), 2);
49
50 EXPECT_THAT(every_second_tick_resampler.PushFrame({{9, 10}, {11, 12}}),
51 IsOk());
52 EXPECT_EQ(every_second_tick_resampler.GetOutputSamplesAsSpan().size(), 1);
53
54 EXPECT_THAT(every_second_tick_resampler.Flush(), IsOk());
55 EXPECT_TRUE(every_second_tick_resampler.GetOutputSamplesAsSpan().empty());
56 }
57
TEST(PushFrame,ReturnsFailedPreconditionWhenCalledAfterFlush)58 TEST(PushFrame, ReturnsFailedPreconditionWhenCalledAfterFlush) {
59 MockSampleProcessor mock_resampler(kMaxInputTicks, kNumChannels,
60 kMaxOutputTicks);
61 EXPECT_THAT(mock_resampler.PushFrame({}), IsOk());
62 EXPECT_THAT(mock_resampler.Flush(), IsOk());
63
64 EXPECT_THAT(mock_resampler.PushFrame({}), StatusIs(kFailedPrecondition));
65 }
66
TEST(PushFrame,InvalidIfInputSpanHasTooManyTicks)67 TEST(PushFrame, InvalidIfInputSpanHasTooManyTicks) {
68 MockSampleProcessor mock_resampler(kMaxInputTicks, kNumChannels,
69 kMaxOutputTicks);
70 const std::vector<std::vector<int32_t>> kTooManyTicks(
71 kMaxInputTicks + 1, std::vector<int32_t>(kNumChannels));
72
73 EXPECT_FALSE(
74 mock_resampler.PushFrame(absl::MakeConstSpan(kTooManyTicks)).ok());
75 }
76
TEST(PushFrame,InvalidIfInputSpanHasTooFewChannels)77 TEST(PushFrame, InvalidIfInputSpanHasTooFewChannels) {
78 MockSampleProcessor mock_resampler(kMaxInputTicks, kNumChannels,
79 kMaxOutputTicks);
80 const std::vector<std::vector<int32_t>> kTooFewChannels(
81 kMaxInputTicks, std::vector<int32_t>(kNumChannels - 1));
82
83 EXPECT_FALSE(
84 mock_resampler.PushFrame(absl::MakeConstSpan(kTooFewChannels)).ok());
85 }
86
TEST(PushFrame,InvalidIfInputSpanHasTooManyChannels)87 TEST(PushFrame, InvalidIfInputSpanHasTooManyChannels) {
88 MockSampleProcessor mock_resampler(kMaxInputTicks, kNumChannels,
89 kMaxOutputTicks);
90 const std::vector<std::vector<int32_t>> kTooManyChannels(
91 kMaxInputTicks, std::vector<int32_t>(kNumChannels + 1));
92
93 EXPECT_FALSE(
94 mock_resampler.PushFrame(absl::MakeConstSpan(kTooManyChannels)).ok());
95 }
96
TEST(Flush,ReturnsFailedPreconditionWhenCalledTwice)97 TEST(Flush, ReturnsFailedPreconditionWhenCalledTwice) {
98 MockSampleProcessor mock_resampler(kMaxInputTicks, kNumChannels,
99 kMaxOutputTicks);
100 EXPECT_THAT(mock_resampler.Flush(), IsOk());
101
102 EXPECT_THAT(mock_resampler.Flush(), StatusIs(kFailedPrecondition));
103 }
104
105 } // namespace
106 } // namespace iamf_tools
107