1 /*
2  * Copyright (C) 2018 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 #include <stdint.h>
18 #include <stdlib.h>
19 
20 #include <limits>
21 #include <vector>
22 
23 #include <gtest/gtest.h>
24 
25 #include "minui/minui.h"
26 
TEST(GRSurfaceTest,Create_aligned)27 TEST(GRSurfaceTest, Create_aligned) {
28   auto surface = GRSurface::Create(9, 11, 9, 1);
29   ASSERT_TRUE(surface);
30   ASSERT_EQ(0, reinterpret_cast<uintptr_t>(surface->data()) % GRSurface::kSurfaceDataAlignment);
31   // data_size will be rounded up to the next multiple of GRSurface::kSurfaceDataAlignment.
32   ASSERT_EQ(0, surface->data_size() % GRSurface::kSurfaceDataAlignment);
33   ASSERT_GE(surface->data_size(), 11 * 9);
34 }
35 
TEST(GRSurfaceTest,Create_invalid_inputs)36 TEST(GRSurfaceTest, Create_invalid_inputs) {
37   ASSERT_FALSE(GRSurface::Create(9, 11, 0, 1));
38   ASSERT_FALSE(GRSurface::Create(9, 0, 9, 1));
39   ASSERT_FALSE(GRSurface::Create(0, 11, 9, 1));
40   ASSERT_FALSE(GRSurface::Create(9, 11, 9, 0));
41   ASSERT_FALSE(GRSurface::Create(9, 101, std::numeric_limits<size_t>::max() / 100, 1));
42 }
43 
TEST(GRSurfaceTest,Clone)44 TEST(GRSurfaceTest, Clone) {
45   auto image = GRSurface::Create(50, 10, 50, 1);
46   ASSERT_GE(image->data_size(), 10 * 50);
47   for (auto i = 0; i < image->data_size(); i++) {
48     image->data()[i] = rand() % 128;
49   }
50   auto image_copy = image->Clone();
51   ASSERT_EQ(image->data_size(), image_copy->data_size());
52   ASSERT_EQ(std::vector(image->data(), image->data() + image->data_size()),
53             std::vector(image_copy->data(), image_copy->data() + image->data_size()));
54 }
55