summaryrefslogtreecommitdiffstats
path: root/llvm/unittests/XRay/FDRRecordPrinterTest.cpp
blob: f0666ecdd34e7901047f8d001458b308dfc27c35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//===- llvm/unittest/XRay/FDRRecordPrinterTest.cpp --------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/Support/raw_ostream.h"
#include "llvm/XRay/FDRRecords.h"
#include "llvm/XRay/RecordPrinter.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <string>

namespace llvm {
namespace xray {
namespace {

using ::testing::Eq;

template <class RecordType> struct Helper {};

template <> struct Helper<BufferExtents> {
  static std::unique_ptr<Record> construct() {
    return make_unique<BufferExtents>(1);
  }

  static constexpr char Expected[] = "<Buffer: size = 1 bytes>";
};
constexpr char Helper<BufferExtents>::Expected[];

template <> struct Helper<WallclockRecord> {
  static std::unique_ptr<Record> construct() {
    return make_unique<WallclockRecord>(1, 2);
  }

  static constexpr char Expected[] = "<Wall Time: seconds = 1.000002>";
};
constexpr char Helper<WallclockRecord>::Expected[];

template <> struct Helper<NewCPUIDRecord> {
  static std::unique_ptr<Record> construct() {
    return make_unique<NewCPUIDRecord>(1);
  }

  static constexpr char Expected[] = "<CPU ID: 1>";
};
constexpr char Helper<NewCPUIDRecord>::Expected[];

template <> struct Helper<TSCWrapRecord> {
  static std::unique_ptr<Record> construct() {
    return make_unique<TSCWrapRecord>(1);
  }

  static constexpr char Expected[] = "<TSC Wrap: base = 1>";
};
constexpr char Helper<TSCWrapRecord>::Expected[];

template <> struct Helper<CustomEventRecord> {
  static std::unique_ptr<Record> construct() {
    return make_unique<CustomEventRecord>(4, 1, "data");
  }

  static constexpr char Expected[] =
      "<Custom Event: tsc = 1, size = 4, data = 'data'>";
};
constexpr char Helper<CustomEventRecord>::Expected[];

template <> struct Helper<CallArgRecord> {
  static std::unique_ptr<Record> construct() {
    return make_unique<CallArgRecord>(1);
  }

  static constexpr char Expected[] = "<Call Argument: data = 1 (hex = 0x1)>";
};
constexpr char Helper<CallArgRecord>::Expected[];

template <> struct Helper<PIDRecord> {
  static std::unique_ptr<Record> construct() {
    return make_unique<PIDRecord>(1);
  }

  static constexpr char Expected[] = "<PID: 1>";
};
constexpr char Helper<PIDRecord>::Expected[];

template <> struct Helper<NewBufferRecord> {
  static std::unique_ptr<Record> construct() {
    return make_unique<NewBufferRecord>(1);
  }

  static constexpr char Expected[] = "<Thread ID: 1>";
};
constexpr char Helper<NewBufferRecord>::Expected[];

template <> struct Helper<EndBufferRecord> {
  static std::unique_ptr<Record> construct() {
    return make_unique<EndBufferRecord>();
  }

  static constexpr char Expected[] = "<End of Buffer>";
};
constexpr char Helper<EndBufferRecord>::Expected[];

template <class T> class PrinterTest : public ::testing::Test {
protected:
  std::string Data;
  raw_string_ostream OS;
  RecordPrinter P;
  std::unique_ptr<Record> R;

public:
  PrinterTest() : Data(), OS(Data), P(OS), R(Helper<T>::construct()) {}
};

TYPED_TEST_CASE_P(PrinterTest);

TYPED_TEST_P(PrinterTest, PrintsRecord) {
  ASSERT_NE(nullptr, this->R);
  ASSERT_FALSE(errorToBool(this->R->apply(this->P)));
  this->OS.flush();
  EXPECT_THAT(this->Data, Eq(Helper<TypeParam>::Expected));
}

REGISTER_TYPED_TEST_CASE_P(PrinterTest, PrintsRecord);
using FDRRecordTypes =
    ::testing::Types<BufferExtents, NewBufferRecord, EndBufferRecord,
                     NewCPUIDRecord, TSCWrapRecord, WallclockRecord,
                     CustomEventRecord, CallArgRecord, BufferExtents,
                     PIDRecord>;
INSTANTIATE_TYPED_TEST_CASE_P(Records, PrinterTest, FDRRecordTypes);

TEST(FDRRecordPrinterTest, WriteFunctionRecordEnter) {
  std::string Data;
  raw_string_ostream OS(Data);
  RecordPrinter P(OS);
  FunctionRecord R(RecordTypes::ENTER, 1, 2);
  ASSERT_FALSE(errorToBool(R.apply(P)));
  OS.flush();
  EXPECT_THAT(Data, Eq("<Function Enter: #1 delta = +1>"));
}

TEST(FDRRecordPrinterTest, WriteFunctionRecordExit) {
  std::string Data;
  raw_string_ostream OS(Data);
  RecordPrinter P(OS);
  FunctionRecord R(RecordTypes::EXIT, 1, 2);
  ASSERT_FALSE(errorToBool(R.apply(P)));
  OS.flush();
  EXPECT_THAT(Data, Eq("<Function Exit: #1 delta = +1>"));
}

TEST(FDRRecordPrinterTest, WriteFunctionRecordTailExit) {
  std::string Data;
  raw_string_ostream OS(Data);
  RecordPrinter P(OS);
  FunctionRecord R(RecordTypes::TAIL_EXIT, 1, 2);
  ASSERT_FALSE(errorToBool(R.apply(P)));
  OS.flush();
  EXPECT_THAT(Data, Eq("<Function Tail Exit: #1 delta = +1>"));
}

TEST(FDRRecordPrinterTest, WriteFunctionRecordEnterArg) {
  std::string Data;
  raw_string_ostream OS(Data);
  RecordPrinter P(OS);
  FunctionRecord R(RecordTypes::ENTER_ARG, 1, 2);
  ASSERT_FALSE(errorToBool(R.apply(P)));
  OS.flush();
  EXPECT_THAT(Data, Eq("<Function Enter With Arg: #1 delta = +1>"));
}

} // namespace
} // namespace xray
} // namespace llvm
OpenPOWER on IntegriCloud