Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 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> |
Tao Bao | 63b59dc | 2018-10-31 15:23:04 -0700 | [diff] [blame] | 18 | #include <stdlib.h> |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 19 | |
Tao Bao | dd78982 | 2018-11-26 16:28:07 -0800 | [diff] [blame] | 20 | #include <limits> |
Tao Bao | 63b59dc | 2018-10-31 15:23:04 -0700 | [diff] [blame] | 21 | #include <vector> |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 22 | |
| 23 | #include <gtest/gtest.h> |
| 24 | |
| 25 | #include "minui/minui.h" |
| 26 | |
| 27 | TEST(GRSurfaceTest, Create_aligned) { |
Tao Bao | dd78982 | 2018-11-26 16:28:07 -0800 | [diff] [blame] | 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 | |
| 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)); |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 42 | } |
Tao Bao | 63b59dc | 2018-10-31 15:23:04 -0700 | [diff] [blame] | 43 | |
| 44 | TEST(GRSurfaceTest, Clone) { |
Tao Bao | dd78982 | 2018-11-26 16:28:07 -0800 | [diff] [blame] | 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++) { |
Tao Bao | 63b59dc | 2018-10-31 15:23:04 -0700 | [diff] [blame] | 48 | image->data()[i] = rand() % 128; |
| 49 | } |
| 50 | auto image_copy = image->Clone(); |
Tao Bao | dd78982 | 2018-11-26 16:28:07 -0800 | [diff] [blame] | 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())); |
Tao Bao | 63b59dc | 2018-10-31 15:23:04 -0700 | [diff] [blame] | 54 | } |