-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync_logger_lib_test_orig.cc
74 lines (61 loc) · 2.2 KB
/
async_logger_lib_test_orig.cc
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
#include "async_logger_orig.h"
#include "gtest/gtest.h"
#include <cstdio>
#include <fstream>
#include <iostream>
using namespace std;
// using ParamType = typename std::size_t;
// $HOME/gitsrc/googletest/googletest/docs/advanced.html
// class AsyncLoggerTest : public testing::Test, public
// testing::WithParamInterface<size_t> {
class AsyncLoggerTest : public testing::TestWithParam<string> {
public:
AsyncLoggerTest() : oldCoutStreamBuf(cout.rdbuf()) {
// https://stackoverflow.com/questions/4810516/c-redirecting-stdout
strCout = new ostringstream;
cout.rdbuf(strCout->rdbuf());
logger = new async_logger;
logger->log("main_thread");
test1 = new thread([this]() { logger->log("testing thread 1"); });
}
~AsyncLoggerTest() {
// Restore output to stdout.
cout.rdbuf(oldCoutStreamBuf);
cout << "Output buffer is: " << endl << strCout->str() << endl;
delete logger;
delete test1;
delete strCout;
}
ostringstream *strCout;
async_logger *logger;
thread *test1;
private:
streambuf *oldCoutStreamBuf;
};
TEST_P(AsyncLoggerTest, CheckLogOutput) {
/* Check for ctor log message. */
ASSERT_NE(nullptr, strCout);
EXPECT_NE(string::npos, strCout->str().find("main_thread\n"));
/* Log the "command-line" parameters. */
string inputstr = GetParam();
for (size_t idx = 0; idx < inputstr.length(); ++idx) {
logger->log("Argument " + std::to_string(idx) + " = " + inputstr.at(idx));
}
/* Let thread launched explicitly in ctor catch up. */
test1->join();
ASSERT_NE(nullptr, strCout);
EXPECT_NE(string::npos, strCout->str().find("testing thread 1"));
/* Check output from async_logger thread. */
ASSERT_NE(nullptr, strCout);
for (size_t idx = 0; idx < inputstr.length(); ++idx) {
EXPECT_NE(string::npos,
strCout->str().find("Argument " + std::to_string(idx) + " = " +
inputstr.at(idx)));
}
logger->log("Main ending");
}
/* Arguments are, in order, an arbitrary choice, the test class name, and the
* parameter values. */
INSTANTIATE_TEST_SUITE_P(AsyncLogger, AsyncLoggerTest,
testing::Values("", "a",
"abcdefghijklmnopqrstuvwxyz"));