Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 2d02b81

Browse files
committed
added unit tests for MallocMapping
1 parent de4beb6 commit 2d02b81

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ FILE: ../../../flutter/fml/macros.h
170170
FILE: ../../../flutter/fml/make_copyable.h
171171
FILE: ../../../flutter/fml/mapping.cc
172172
FILE: ../../../flutter/fml/mapping.h
173+
FILE: ../../../flutter/fml/mapping_unittests.cc
173174
FILE: ../../../flutter/fml/memory/ref_counted.h
174175
FILE: ../../../flutter/fml/memory/ref_counted_internal.h
175176
FILE: ../../../flutter/fml/memory/ref_counted_unittest.cc

fml/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ if (enable_unittests) {
252252
"file_unittest.cc",
253253
"hash_combine_unittests.cc",
254254
"logging_unittests.cc",
255+
"mapping_unittests.cc",
255256
"memory/ref_counted_unittest.cc",
256257
"memory/task_runner_checker_unittest.cc",
257258
"memory/weak_ptr_unittest.cc",

fml/mapping_unittests.cc

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
8+
namespace fml {
9+
10+
TEST(MallocMapping, EmptyContructor) {
11+
MallocMapping mapping;
12+
ASSERT_EQ(nullptr, mapping.GetMapping());
13+
ASSERT_EQ(0u, mapping.GetSize());
14+
}
15+
16+
TEST(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+
23+
TEST(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, mapping.GetMapping());
29+
ASSERT_EQ(0u, mapping.GetSize());
30+
ASSERT_NE(nullptr, moved.GetMapping());
31+
ASSERT_EQ(length, moved.GetSize());
32+
}
33+
34+
TEST(MallocMapping, Copy) {
35+
size_t length = 10;
36+
MallocMapping mapping(reinterpret_cast<uint8_t*>(malloc(length)), length);
37+
memset(const_cast<uint8_t*>(mapping.GetMapping()), 0xac, mapping.GetSize());
38+
MallocMapping copied =
39+
MallocMapping::Copy(mapping.GetMapping(), mapping.GetSize());
40+
41+
ASSERT_NE(mapping.GetMapping(), copied.GetMapping());
42+
ASSERT_EQ(mapping.GetSize(), copied.GetSize());
43+
ASSERT_EQ(
44+
0, memcmp(mapping.GetMapping(), copied.GetMapping(), mapping.GetSize()));
45+
}
46+
47+
TEST(MallocMapping, Release) {
48+
size_t length = 10;
49+
MallocMapping mapping(reinterpret_cast<uint8_t*>(malloc(length)), length);
50+
free(const_cast<uint8_t*>(mapping.Release()));
51+
ASSERT_EQ(nullptr, mapping.GetMapping());
52+
ASSERT_EQ(0u, mapping.GetSize());
53+
}
54+
55+
} // namespace fml

0 commit comments

Comments
 (0)