Flutter Engine
The Flutter Engine
mapping_unittests.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter 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.
4
5#include "flutter/fml/mapping.h"
6#include "flutter/testing/testing.h"
7
8namespace fml {
9
10TEST(MallocMapping, EmptyContructor) {
11 MallocMapping mapping;
12 ASSERT_EQ(nullptr, mapping.GetMapping());
13 ASSERT_EQ(0u, mapping.GetSize());
14}
15
16TEST(MallocMapping, NotEmptyContructor) {
17 size_t length = 10;
18 MallocMapping mapping(reinterpret_cast<uint8_t*>(malloc(length)), length);
19 ASSERT_NE(nullptr, mapping.GetMapping());
20 ASSERT_EQ(length, mapping.GetSize());
21}
22
23TEST(MallocMapping, MoveConstructor) {
24 size_t length = 10;
25 MallocMapping mapping(reinterpret_cast<uint8_t*>(malloc(length)), length);
26 MallocMapping moved = std::move(mapping);
27
28 ASSERT_EQ(nullptr,
29 mapping.GetMapping()); // NOLINT(clang-analyzer-cplusplus.Move,
30 // bugprone-use-after-move)
31 ASSERT_EQ(0u, mapping.GetSize());
32 ASSERT_NE(nullptr, moved.GetMapping());
33 ASSERT_EQ(length, moved.GetSize());
34}
35
37 size_t length = 10;
38 MallocMapping mapping(reinterpret_cast<uint8_t*>(malloc(length)), length);
39 memset(const_cast<uint8_t*>(mapping.GetMapping()), 0xac, mapping.GetSize());
40 MallocMapping copied =
41 MallocMapping::Copy(mapping.GetMapping(), mapping.GetSize());
42
43 ASSERT_NE(mapping.GetMapping(), copied.GetMapping());
44 ASSERT_EQ(mapping.GetSize(), copied.GetSize());
45 ASSERT_EQ(
46 0, memcmp(mapping.GetMapping(), copied.GetMapping(), mapping.GetSize()));
47}
48
50 size_t length = 10;
51 MallocMapping mapping(reinterpret_cast<uint8_t*>(malloc(length)), length);
52 free(const_cast<uint8_t*>(mapping.Release()));
53 ASSERT_EQ(nullptr, mapping.GetMapping());
54 ASSERT_EQ(0u, mapping.GetSize());
55}
56
57TEST(MallocMapping, IsDontNeedSafe) {
58 size_t length = 10;
59 MallocMapping mapping(reinterpret_cast<uint8_t*>(malloc(length)), length);
60 ASSERT_NE(nullptr, mapping.GetMapping());
61 ASSERT_FALSE(mapping.IsDontNeedSafe());
62}
63
64TEST(MallocMapping, CopySizeZero) {
65 char ch = 'a';
66 MallocMapping mapping = MallocMapping::Copy(&ch, &ch);
67 ASSERT_EQ(0u, mapping.GetSize());
68}
69
70} // namespace fml
A Mapping like NonOwnedMapping, but uses Free as its release proc.
Definition: mapping.h:144
bool IsDontNeedSafe() const override
Definition: mapping.cc:151
static MallocMapping Copy(const T *begin, const T *end)
Definition: mapping.h:162
size_t GetSize() const override
Definition: mapping.cc:143
const uint8_t * GetMapping() const override
Definition: mapping.cc:147
uint8_t * Release()
Definition: mapping.cc:155
size_t length
void * malloc(size_t size)
Definition: allocation.cc:19
Definition: ascii_trie.cc:9
TEST(MallocMapping, EmptyContructor)