96 lines · cpp
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-exception6//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>16using namespace llvm;17 18#ifndef NDEBUG19TEST(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 36TEST(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 54TEST(DebugTest, DebugWithType) {55 llvm::DebugFlag = true;56 57 // Check if the DEBUG_WITH_TYPE macro is enabled for the given type.58 auto CheckDebugWithType = [](const char *Type) {59 bool Visited = false;60 DEBUG_WITH_TYPE(Type, { Visited = true; });61 return Visited;62 };63 64 {65 static const char *DT[] = {"A", "B"};66 setCurrentDebugTypes(DT, sizeof(DT) / sizeof(DT[0]));67 EXPECT_TRUE(CheckDebugWithType("A"));68 EXPECT_TRUE(CheckDebugWithType("B"));69 EXPECT_FALSE(CheckDebugWithType("C"));70 }71 {72 static const char *DT[] = {"A:"};73 setCurrentDebugTypes(DT, sizeof(DT) / sizeof(DT[0]));74 EXPECT_FALSE(CheckDebugWithType("A"));75 EXPECT_TRUE(CheckDebugWithType("B"));76 EXPECT_TRUE(CheckDebugWithType("C"));77 }78 {79 static const char *DT[] = {"A:", "B"};80 setCurrentDebugTypes(DT, sizeof(DT) / sizeof(DT[0]));81 EXPECT_FALSE(CheckDebugWithType("A"));82 EXPECT_TRUE(CheckDebugWithType("B"));83 EXPECT_FALSE(CheckDebugWithType("C"));84 }85 {86 static const char *DT[] = {"A:3", "B:", "C"};87 setCurrentDebugTypes(DT, sizeof(DT) / sizeof(DT[0]));88 EXPECT_TRUE(CheckDebugWithType("A"));89 EXPECT_FALSE(isCurrentDebugType("A", 4));90 EXPECT_FALSE(CheckDebugWithType("B"));91 EXPECT_TRUE(isCurrentDebugType("C", 10));92 EXPECT_FALSE(CheckDebugWithType("D"));93 }94}95#endif96