1 // Copyright 2020 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 "fxjs/gc/container_trace.h"
6
7 #include <stdint.h>
8
9 #include <list>
10 #include <map>
11 #include <set>
12 #include <vector>
13
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "v8/include/cppgc/member.h"
16
17 namespace {
18
19 class Thing : public cppgc::GarbageCollected<Thing> {
20 public:
Trace(cppgc::Visitor * visitor) const21 void Trace(cppgc::Visitor* visitor) const {}
22 };
23
24 class CountingVisitor {
25 public:
26 CountingVisitor() = default;
27
Trace(const void * that)28 void Trace(const void* that) { ++call_count_; }
call_count() const29 int call_count() const { return call_count_; }
30
31 private:
32 int call_count_ = 0;
33 };
34
35 } // namespace
36
TEST(ContainerTrace,ActualListTrace)37 TEST(ContainerTrace, ActualListTrace) {
38 std::list<cppgc::Member<Thing>> thing;
39 thing.emplace_back(nullptr);
40
41 CountingVisitor cv;
42 ContainerTrace(&cv, thing);
43 EXPECT_EQ(1, cv.call_count());
44 }
45
TEST(ContainerTrace,ActualMapTraceFirst)46 TEST(ContainerTrace, ActualMapTraceFirst) {
47 std::map<cppgc::Member<Thing>, int> thing;
48 thing[nullptr] = 42;
49
50 CountingVisitor cv;
51 ContainerTrace(&cv, thing);
52 EXPECT_EQ(1, cv.call_count());
53 }
54
TEST(ContainerTrace,ActualMapTraceSecond)55 TEST(ContainerTrace, ActualMapTraceSecond) {
56 std::map<int, cppgc::Member<Thing>> thing;
57 thing[42] = nullptr;
58
59 CountingVisitor cv;
60 ContainerTrace(&cv, thing);
61 EXPECT_EQ(1, cv.call_count());
62 }
63
TEST(ContainerTrace,ActualMapTraceBoth)64 TEST(ContainerTrace, ActualMapTraceBoth) {
65 std::map<cppgc::Member<Thing>, cppgc::Member<Thing>> thing;
66 thing[nullptr] = nullptr;
67
68 CountingVisitor cv;
69 ContainerTrace(&cv, thing);
70 EXPECT_EQ(2, cv.call_count());
71 }
72
TEST(ContainerTrace,ActualSetTrace)73 TEST(ContainerTrace, ActualSetTrace) {
74 std::set<cppgc::Member<Thing>> thing;
75 thing.insert(nullptr);
76
77 CountingVisitor cv;
78 ContainerTrace(&cv, thing);
79 EXPECT_EQ(1, cv.call_count());
80 }
81
TEST(ContainerTrace,ActualVectorTrace)82 TEST(ContainerTrace, ActualVectorTrace) {
83 std::vector<cppgc::Member<Thing>> thing;
84 thing.emplace_back(nullptr);
85
86 CountingVisitor cv;
87 ContainerTrace(&cv, thing);
88 EXPECT_EQ(1, cv.call_count());
89 }
90