1 //===-- test_helpers.h ----------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of XRay, a function call tracing system.
10 //
11 //===----------------------------------------------------------------------===//
12 #ifndef COMPILER_RT_LIB_XRAY_TESTS_TEST_HELPERS_H_
13 #define COMPILER_RT_LIB_XRAY_TESTS_TEST_HELPERS_H_
14 
15 #include "xray_buffer_queue.h"
16 #include "xray_segmented_array.h"
17 #include "llvm/XRay/Trace.h"
18 #include "llvm/XRay/XRayRecord.h"
19 #include "gmock/gmock.h"
20 
21 // TODO: Move these to llvm/include/Testing/XRay/...
22 namespace llvm {
23 namespace xray {
24 
25 std::string RecordTypeAsString(RecordTypes T);
26 void PrintTo(RecordTypes T, std::ostream *OS);
27 void PrintTo(const XRayRecord &R, std::ostream *OS);
28 void PrintTo(const Trace &T, std::ostream *OS);
29 
30 namespace testing {
31 
32 MATCHER_P(FuncId, F, "") {
33   *result_listener << "where the function id is " << F;
34   return arg.FuncId == F;
35 }
36 
37 MATCHER_P(RecordType, T, "") {
38   *result_listener << "where the record type is " << RecordTypeAsString(T);
39   return arg.Type == T;
40 }
41 
42 MATCHER_P(HasArg, A, "") {
43   *result_listener << "where args contains " << A;
44   return !arg.CallArgs.empty() &&
45          std::any_of(arg.CallArgs.begin(), arg.CallArgs.end(),
46                      [this](decltype(A) V) { return V == A; });
47 }
48 
49 MATCHER_P(TSCIs, M, std::string("TSC is ") + ::testing::PrintToString(M)) {
50   return ::testing::Matcher<decltype(arg.TSC)>(M).MatchAndExplain(
51       arg.TSC, result_listener);
52 }
53 
54 } // namespace testing
55 } // namespace xray
56 } // namespace llvm
57 
58 namespace __xray {
59 
60 std::string serialize(BufferQueue &Buffers, int32_t Version);
61 
PrintTo(const Array<T> & A,std::ostream * OS)62 template <class T> void PrintTo(const Array<T> &A, std::ostream *OS) {
63   *OS << "[";
64   bool first = true;
65   for (const auto &E : A) {
66     if (!first) {
67       *OS << ", ";
68     }
69     PrintTo(E, OS);
70     first = false;
71   }
72   *OS << "]";
73 }
74 
75 } // namespace __xray
76 
77 #endif // COMPILER_RT_LIB_XRAY_TESTS_TEST_HELPERS_H_
78