1 // Copyright 2019 The PDFium Authors
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 "core/fxcrt/fx_memory_wrappers.h"
6
7 #include <memory>
8 #include <sstream>
9 #include <string>
10 #include <vector>
11
12 #include "build/build_config.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace {
16
17 struct OnlyNumbers {
18 int x;
19 int y;
20 };
21
22 } // namespace
23
24 FX_DATA_PARTITION_EXCEPTION(OnlyNumbers);
25
TEST(fxcrt,FxFreeDeleter)26 TEST(fxcrt, FxFreeDeleter) {
27 std::unique_ptr<int, FxFreeDeleter> empty(nullptr);
28 std::unique_ptr<int, FxFreeDeleter> thing(FX_Alloc(int, 1));
29 std::unique_ptr<int, FxFreeDeleter> several(FX_Alloc(int, 100));
30 EXPECT_FALSE(empty);
31 EXPECT_TRUE(thing);
32 EXPECT_TRUE(several);
33 }
34
TEST(fxcrt,FxAllocAllocator)35 TEST(fxcrt, FxAllocAllocator) {
36 // Let ASAN sanity check some simple operations.
37 std::vector<int, FxAllocAllocator<int>> vec;
38 vec.push_back(42);
39 vec.reserve(100);
40 vec.resize(20);
41 vec[11] = 42;
42
43 std::vector<int, FxAllocAllocator<int>> vec2 = vec;
44 vec = std::move(vec2);
45 vec2.resize(0);
46 vec2.push_back(42);
47 }
48
TEST(fxcrt,FxStringAllocator)49 TEST(fxcrt, FxStringAllocator) {
50 // Let ASAN sanity check some simple operations.
51 std::basic_ostringstream<char, std::char_traits<char>,
52 FxStringAllocator<char>>
53 str;
54 str << 'B';
55 str << "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
56 str << "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
57 str << "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
58 str << 42.0f;
59 }
60
TEST(fxcrt,FxAllocAllocatorStructException)61 TEST(fxcrt, FxAllocAllocatorStructException) {
62 std::vector<OnlyNumbers, FxAllocAllocator<OnlyNumbers>> vec;
63 vec.push_back({42, 73});
64 EXPECT_EQ(vec.back().x, 42);
65 EXPECT_EQ(vec.back().y, 73);
66 }
67