[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / cc / resources / resource_util_unittest.cc
blob1fabc3b4f6653f85c3decee1a39e44d059ca4883
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/logging.h"
6 #include "cc/resources/resource_util.h"
7 #include "testing/gtest/include/gtest/gtest.h"
9 namespace cc {
10 namespace {
12 struct TestFormat {
13 ResourceFormat format;
14 size_t expected_bytes;
15 size_t expected_bytes_aligned;
18 // Modify this constant as per TestFormat variables defined in following tests.
19 const int kTestFormats = 4;
21 class ResourceUtilTest : public testing::Test {
22 public:
23 void TestVerifyWidthInBytes(int width, const TestFormat* test_formats) {
24 for (int i = 0; i < kTestFormats; ++i) {
25 EXPECT_TRUE(ResourceUtil::VerifyWidthInBytes<size_t>(
26 width, test_formats[i].format));
30 void TestCheckedWidthInBytes(int width, const TestFormat* test_formats) {
31 for (int i = 0; i < kTestFormats; ++i) {
32 size_t bytes = ResourceUtil::CheckedWidthInBytes<size_t>(
33 width, test_formats[i].format);
34 EXPECT_EQ(bytes, test_formats[i].expected_bytes);
38 void TestUncheckedWidthInBytes(int width, const TestFormat* test_formats) {
39 for (int i = 0; i < kTestFormats; ++i) {
40 size_t bytes = ResourceUtil::UncheckedWidthInBytes<size_t>(
41 width, test_formats[i].format);
42 EXPECT_EQ(bytes, test_formats[i].expected_bytes);
46 void TestUncheckedWidthInBytesAligned(int width,
47 const TestFormat* test_formats) {
48 for (int i = 0; i < kTestFormats; ++i) {
49 size_t bytes = ResourceUtil::UncheckedWidthInBytesAligned<size_t>(
50 width, test_formats[i].format);
51 EXPECT_EQ(bytes, test_formats[i].expected_bytes_aligned);
55 void TestVerifySizeInBytes(const gfx::Size& size,
56 const TestFormat* test_formats) {
57 for (int i = 0; i < kTestFormats; ++i) {
58 EXPECT_TRUE(ResourceUtil::VerifySizeInBytes<size_t>(
59 size, test_formats[i].format));
63 void TestCheckedSizeInBytes(const gfx::Size& size,
64 const TestFormat* test_formats) {
65 for (int i = 0; i < kTestFormats; ++i) {
66 size_t bytes = ResourceUtil::CheckedSizeInBytes<size_t>(
67 size, test_formats[i].format);
68 EXPECT_EQ(bytes, test_formats[i].expected_bytes);
72 void TestUncheckedSizeInBytes(const gfx::Size& size,
73 const TestFormat* test_formats) {
74 for (int i = 0; i < kTestFormats; ++i) {
75 size_t bytes = ResourceUtil::UncheckedSizeInBytes<size_t>(
76 size, test_formats[i].format);
77 EXPECT_EQ(bytes, test_formats[i].expected_bytes);
81 void TestUncheckedSizeInBytesAligned(const gfx::Size& size,
82 const TestFormat* test_formats) {
83 for (int i = 0; i < kTestFormats; ++i) {
84 size_t bytes = ResourceUtil::UncheckedSizeInBytesAligned<size_t>(
85 size, test_formats[i].format);
86 EXPECT_EQ(bytes, test_formats[i].expected_bytes_aligned);
91 TEST_F(ResourceUtilTest, WidthInBytes) {
92 // Check bytes for even width.
93 int width = 10;
94 TestFormat test_formats[] = {
95 {RGBA_8888, 40, 40}, // for 32 bits
96 {RGBA_4444, 20, 20}, // for 16 bits
97 {ALPHA_8, 10, 12}, // for 8 bits
98 {ETC1, 5, 8} // for 4 bits
101 TestVerifyWidthInBytes(width, test_formats);
102 TestCheckedWidthInBytes(width, test_formats);
103 TestUncheckedWidthInBytes(width, test_formats);
104 TestUncheckedWidthInBytesAligned(width, test_formats);
106 // Check bytes for odd width.
107 int width_odd = 11;
108 TestFormat test_formats_odd[] = {
109 {RGBA_8888, 44, 44}, // for 32 bits
110 {RGBA_4444, 22, 24}, // for 16 bits
111 {ALPHA_8, 11, 12}, // for 8 bits
112 {ETC1, 6, 8} // for 4 bits
115 TestVerifyWidthInBytes(width_odd, test_formats_odd);
116 TestCheckedWidthInBytes(width_odd, test_formats_odd);
117 TestUncheckedWidthInBytes(width_odd, test_formats_odd);
118 TestUncheckedWidthInBytesAligned(width_odd, test_formats_odd);
121 TEST_F(ResourceUtilTest, SizeInBytes) {
122 // Check bytes for even size.
123 gfx::Size size(10, 10);
124 TestFormat test_formats[] = {
125 {RGBA_8888, 400, 400}, // for 32 bits
126 {RGBA_4444, 200, 200}, // for 16 bits
127 {ALPHA_8, 100, 120}, // for 8 bits
128 {ETC1, 50, 80} // for 4 bits
131 TestVerifySizeInBytes(size, test_formats);
132 TestCheckedSizeInBytes(size, test_formats);
133 TestUncheckedSizeInBytes(size, test_formats);
134 TestUncheckedSizeInBytesAligned(size, test_formats);
136 // Check bytes for odd size.
137 gfx::Size size_odd(11, 11);
138 TestFormat test_formats_odd[] = {
139 {RGBA_8888, 484, 484}, // for 32 bits
140 {RGBA_4444, 242, 264}, // for 16 bits
141 {ALPHA_8, 121, 132}, // for 8 bits
142 {ETC1, 66, 88} // for 4 bits
145 TestVerifySizeInBytes(size_odd, test_formats_odd);
146 TestCheckedSizeInBytes(size_odd, test_formats_odd);
147 TestUncheckedSizeInBytes(size_odd, test_formats_odd);
148 TestUncheckedSizeInBytesAligned(size_odd, test_formats_odd);
151 TEST_F(ResourceUtilTest, WidthInBytesOverflow) {
152 int width = 10;
153 // 10 * 16 = 160 bits, overflows in char, but fits in unsigned char.
154 EXPECT_FALSE(ResourceUtil::VerifyWidthInBytes<signed char>(width, RGBA_4444));
155 EXPECT_TRUE(
156 ResourceUtil::VerifyWidthInBytes<unsigned char>(width, RGBA_4444));
159 TEST_F(ResourceUtilTest, SizeInBytesOverflow) {
160 gfx::Size size(10, 10);
161 // 10 * 16 * 10 = 1600 bits, overflows in char, but fits in int.
162 EXPECT_FALSE(ResourceUtil::VerifySizeInBytes<signed char>(size, RGBA_4444));
163 EXPECT_TRUE(ResourceUtil::VerifySizeInBytes<int>(size, RGBA_4444));
166 } // namespace
167 } // namespace cc