summaryrefslogtreecommitdiff
path: root/lib/xray/tests/unit/test_helpers.h
blob: ff0311e9bd30fb19b4cfc06a775cfb1d205eb0a5 (plain) (blame)
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
//===-- test_helpers.h ----------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file is a part of XRay, a function call tracing system.
//
//===----------------------------------------------------------------------===//
#ifndef COMPILER_RT_LIB_XRAY_TESTS_TEST_HELPERS_H_
#define COMPILER_RT_LIB_XRAY_TESTS_TEST_HELPERS_H_

#include "xray_buffer_queue.h"
#include "xray_segmented_array.h"
#include "llvm/XRay/Trace.h"
#include "llvm/XRay/XRayRecord.h"
#include "gmock/gmock.h"

// TODO: Move these to llvm/include/Testing/XRay/...
namespace llvm {
namespace xray {

std::string RecordTypeAsString(RecordTypes T);
void PrintTo(RecordTypes T, std::ostream *OS);
void PrintTo(const XRayRecord &R, std::ostream *OS);
void PrintTo(const Trace &T, std::ostream *OS);

namespace testing {

MATCHER_P(FuncId, F, "") {
  *result_listener << "where the function id is " << F;
  return arg.FuncId == F;
}

MATCHER_P(RecordType, T, "") {
  *result_listener << "where the record type is " << RecordTypeAsString(T);
  return arg.Type == T;
}

MATCHER_P(HasArg, A, "") {
  *result_listener << "where args contains " << A;
  return !arg.CallArgs.empty() &&
         std::any_of(arg.CallArgs.begin(), arg.CallArgs.end(),
                     [this](decltype(A) V) { return V == A; });
}

MATCHER_P(TSCIs, M, std::string("TSC is ") + ::testing::PrintToString(M)) {
  return ::testing::Matcher<decltype(arg.TSC)>(M).MatchAndExplain(
      arg.TSC, result_listener);
}

} // namespace testing
} // namespace xray
} // namespace llvm

namespace __xray {

std::string serialize(BufferQueue &Buffers, int32_t Version);

template <class T> void PrintTo(const Array<T> &A, std::ostream *OS) {
  *OS << "[";
  bool first = true;
  for (const auto &E : A) {
    if (!first) {
      *OS << ", ";
    }
    PrintTo(E, OS);
    first = false;
  }
  *OS << "]";
}

} // namespace __xray

#endif // COMPILER_RT_LIB_XRAY_TESTS_TEST_HELPERS_H_