xref: /llvm-project/llvm/unittests/Support/DebugTest.cpp (revision 8e6634444876a59c1352a4d62704e93b23b7c5ae)
1 //===- llvm/unittest/Support/DebugTest.cpp --------------------------------===//
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 #include "llvm/Support/Debug.h"
10 #include "llvm/ADT/MapVector.h"
11 #include "llvm/Support/MathExtras.h"
12 #include "llvm/Support/raw_ostream.h"
13 #include "gtest/gtest.h"
14 
15 #include <string>
16 using namespace llvm;
17 
18 #ifndef NDEBUG
19 TEST(DebugTest, Basic) {
20   std::string s1, s2;
21   raw_string_ostream os1(s1), os2(s2);
22   static const char *DT[] = {"A", "B"};
23 
24   llvm::DebugFlag = true;
25   setCurrentDebugTypes(DT, 2);
26   DEBUG_WITH_TYPE("A", os1 << "A");
27   DEBUG_WITH_TYPE("B", os1 << "B");
28   EXPECT_EQ("AB", os1.str());
29 
30   setCurrentDebugType("A");
31   DEBUG_WITH_TYPE("A", os2 << "A");
32   DEBUG_WITH_TYPE("B", os2 << "B");
33   EXPECT_EQ("A", os2.str());
34 }
35 
36 TEST(DebugTest, CommaInDebugBlock) {
37   std::string s1, s2;
38   raw_string_ostream os1(s1), os2(s2);
39   static const char *DT[] = {"A", "B"};
40   static const char Letters[] = {'X', 'Y', 'Z'};
41 
42   llvm::DebugFlag = true;
43   setCurrentDebugTypes(DT, 2);
44   DEBUG_WITH_TYPE("A", {
45     SmallMapVector<int, char, 4> map;
46     for (int i = 0; i < 3; i++)
47       map[i] = Letters[i];
48     for (int i = 2; i >= 0; i--)
49       os1 << map[i];
50   });
51   EXPECT_EQ("ZYX", os1.str());
52 }
53 #endif
54