File: check-macros.cpp

package info (click to toggle)
easyloggingpp 9.97.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 2,840 kB
  • sloc: cpp: 11,415; python: 2,336; sh: 337; makefile: 29
file content (73 lines) | stat: -rw-r--r-- 2,739 bytes parent folder | download | duplicates (5)
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
 //
 // This file is part of Easylogging++ samples
 //
 // Check macros (incl. debug versions i.e, DCHECK etc) + PCHECK and DCHECK
 // We add logging flag `DisableApplicationAbortOnFatalLog` that prevents application abort because CHECK macros always log FATAL
 //
 // Revision 1.1
 // @author mkhan3189
 //

#include "easylogging++.h"

INITIALIZE_EASYLOGGINGPP

int main(void) {
    el::Loggers::addFlag(el::LoggingFlag::DisableApplicationAbortOnFatalLog);
    
    // These checks should fail
    LOG(INFO) << "----- DONT WORRY ABOUT FOLLOWING CHECKS FAILING - THEY ARE EXPECTED";
    CHECK(1 > 2) << "1 is not greater than 2";
    CHECK_EQ(1, 2) << "1 is not equal to 2";
    CHECK_NE(1, 1) << "Wow, I did not know 1 == 1";
    CHECK_STREQ("abc", "def") << " :)";
    CHECK_STRNE("abc", "abc") << " :(";
    CHECK_STRCASEEQ("abc", "ABCD") << " :p";
    CHECK_STRCASENE("abc", "ABC") << " B)";
    int* f = new int;
    CHECK_NOTNULL(f);

    delete f;
    f = nullptr;
    // These checks should pass 
    LOG(WARNING) << "----- START WORRYING ABOUT CHECKS NOW";
    CHECK(1 < 2) << " snap -- lib has bug!";
    CHECK_EQ(1, 1) << " snap -- lib has bug!";
    CHECK_NE(1, 2) << " snap -- lib has bug!";
    CHECK_STREQ("abc", "abc") << " snap -- lib has bug!";
    CHECK_STRNE("abc", "abe") << " snap -- lib has bug!";
    CHECK_STRCASEEQ("abc", "ABC") << " snap -- lib has bug!";
    CHECK_STRCASENE("abc", "ABE") << " snap -- lib has bug!";
    LOG(INFO) << "----- HOPEFULLY NO CHECK FAILED SINCE YOU STARTED WORRYING!";

    // DCHECKs
    DCHECK(1 > 2) << "1 is not greater than 2";
    DCHECK_EQ(1, 2) << "1 is not equal to 2";
    DCHECK_NE(1, 1) << "Wow, I did not know 1 == 1";
    DCHECK_STREQ("abc", "def") << " :)";
    DCHECK_STRNE("abc", "abc") << " :(";
    DCHECK_STRCASEEQ("abc", "ABCD") << " :p";
    DCHECK_STRCASENE("abc", "ABC") << " B)";
    
    // PCHECKs
    std::fstream fstr("a/file/that/does/not/exist", std::fstream::in);
    PCHECK(fstr.is_open());
    DPCHECK(fstr.is_open());

    int min = 1;
    int max = 5;
    CHECK_BOUNDS(1, min, max) << "Index out of bounds";
    CHECK_BOUNDS(2, min, max) << "Index out of bounds";
    CHECK_BOUNDS(3, min, max) << "Index out of bounds";
    CHECK_BOUNDS(4, min, max) << "Index out of bounds";
    CHECK_BOUNDS(5, min, max) << "Index out of bounds";
    CHECK_BOUNDS(6, min, max) << "Index out of bounds";
    DCHECK_BOUNDS(1, min, max) << "Index out of bounds";
    DCHECK_BOUNDS(2, min, max) << "Index out of bounds";
    DCHECK_BOUNDS(3, min, max) << "Index out of bounds";
    DCHECK_BOUNDS(4, min, max) << "Index out of bounds";
    DCHECK_BOUNDS(5, min, max) << "Index out of bounds";
    DCHECK_BOUNDS(6, min, max) << "Index out of bounds";

    return 0;
}