1 // Copyright 2018 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/decoder/footprint.h"
16
17 #include <array>
18 #include <tuple>
19 #include <vector>
20
21 #include <gtest/gtest.h>
22
23 namespace astc_codec {
24
25 namespace {
26
TEST(FootprintTest,ParseAstcFootprintString)27 TEST(FootprintTest, ParseAstcFootprintString) {
28 using ASTCTestPair = std::pair<std::string, Footprint>;
29 const std::array<ASTCTestPair, Footprint::NumValidFootprints()>
30 valid_footprints {{
31 std::make_pair("4x4", Footprint::Get4x4()),
32 std::make_pair("5x4", Footprint::Get5x4()),
33 std::make_pair("5x5", Footprint::Get5x5()),
34 std::make_pair("6x5", Footprint::Get6x5()),
35 std::make_pair("6x6", Footprint::Get6x6()),
36 std::make_pair("8x5", Footprint::Get8x5()),
37 std::make_pair("8x6", Footprint::Get8x6()),
38 std::make_pair("8x8", Footprint::Get8x8()),
39 std::make_pair("10x5", Footprint::Get10x5()),
40 std::make_pair("10x6", Footprint::Get10x6()),
41 std::make_pair("10x8", Footprint::Get10x8()),
42 std::make_pair("10x10", Footprint::Get10x10()),
43 std::make_pair("12x10", Footprint::Get12x10()),
44 std::make_pair("12x12", Footprint::Get12x12())
45 }};
46
47 for (const auto& test : valid_footprints) {
48 base::Optional<Footprint> footprint = Footprint::Parse(test.first.c_str());
49 EXPECT_TRUE(footprint);
50 EXPECT_EQ(test.second, footprint.value());
51 }
52
53 EXPECT_DEBUG_DEATH(EXPECT_FALSE(Footprint::Parse("")), "");
54 EXPECT_DEBUG_DEATH(EXPECT_FALSE(Footprint::Parse("3")), "");
55 EXPECT_DEBUG_DEATH(EXPECT_FALSE(Footprint::Parse("x")), "");
56 // Validly formed but out-of-bounds dimensions do not assert, otherwise
57 // malformed ASTC files could crash the decoder in debug builds.
58 EXPECT_FALSE(Footprint::Parse("9999999999x10"));
59 EXPECT_DEBUG_DEATH(EXPECT_FALSE(Footprint::Parse("ax8")), "");
60 EXPECT_DEBUG_DEATH(EXPECT_FALSE(Footprint::Parse("2x3x4")), "");
61 EXPECT_DEBUG_DEATH(EXPECT_FALSE(Footprint::Parse("-3x4")), "");
62 EXPECT_FALSE(Footprint::Parse("10x4"));
63 }
64
TEST(FootprintTest,Bitrates)65 TEST(FootprintTest, Bitrates) {
66 EXPECT_NEAR(Footprint::Get4x4().Bitrate(), 8.f, 0.01f);
67 EXPECT_NEAR(Footprint::Get5x4().Bitrate(), 6.4f, 0.01f);
68 EXPECT_NEAR(Footprint::Get5x5().Bitrate(), 5.12f, 0.01f);
69 EXPECT_NEAR(Footprint::Get6x5().Bitrate(), 4.27f, 0.01f);
70 EXPECT_NEAR(Footprint::Get6x6().Bitrate(), 3.56f, 0.01f);
71 EXPECT_NEAR(Footprint::Get8x5().Bitrate(), 3.20f, 0.01f);
72 EXPECT_NEAR(Footprint::Get8x6().Bitrate(), 2.67f, 0.01f);
73 EXPECT_NEAR(Footprint::Get8x8().Bitrate(), 2.00f, 0.01f);
74 EXPECT_NEAR(Footprint::Get10x5().Bitrate(), 2.56f, 0.01f);
75 EXPECT_NEAR(Footprint::Get10x6().Bitrate(), 2.13f, 0.01f);
76 EXPECT_NEAR(Footprint::Get10x8().Bitrate(), 1.60f, 0.01f);
77 EXPECT_NEAR(Footprint::Get10x10().Bitrate(), 1.28f, 0.01f);
78 EXPECT_NEAR(Footprint::Get12x10().Bitrate(), 1.07f, 0.01f);
79 EXPECT_NEAR(Footprint::Get12x12().Bitrate(), 0.89f, 0.01f);
80 }
81
TEST(FootprintTest,StorageRequirements)82 TEST(FootprintTest, StorageRequirements) {
83 auto footprint = Footprint::Get10x8();
84 EXPECT_EQ(footprint.Width(), 10);
85 EXPECT_EQ(footprint.Height(), 8);
86
87 // If we have 8x8 blocks, then we have 64*16 = 1024 bytes.
88 EXPECT_EQ(footprint.StorageRequirements(80, 64), 1024);
89
90 // If our block is a little smaller this still counts because we need to
91 // cover a partial block with a fully encoded one.
92 EXPECT_EQ(footprint.StorageRequirements(79, 63), 1024);
93 }
94
95 } // namespace
96
97 } // namespace astc_codec
98