blob: d7638be134258b2e5c22c6987ad04a3ee239b2b4 [file] [log] [blame]
Artem Senicheve8837d52020-06-07 11:59:04 +03001// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2020 YADRO
3
4#include "log_buffer.hpp"
5
6#include <gtest/gtest.h>
7
8TEST(LogBufferTest, Append)
9{
10 const std::string msg = "Test message";
11
12 LogBuffer buf(0, 0);
13
14 buf.append(msg.data(), msg.length());
15 ASSERT_EQ(std::distance(buf.begin(), buf.end()), 1);
16 EXPECT_EQ(buf.begin()->text, msg);
17 EXPECT_NE(buf.begin()->timeStamp, 0);
18
19 // must be merged with previous message
20 const std::string append = "Append";
21 buf.append(append.data(), append.length());
22 ASSERT_EQ(std::distance(buf.begin(), buf.end()), 1);
23 EXPECT_EQ(buf.begin()->text, msg + append);
24
25 // end of line, we still have 1 message
26 buf.append("\n", 1);
27 ASSERT_EQ(std::distance(buf.begin(), buf.end()), 1);
28
29 // second message
30 buf.append(append.data(), append.length());
31 ASSERT_EQ(std::distance(buf.begin(), buf.end()), 2);
32 EXPECT_EQ((++buf.begin())->text, append);
33}
34
35TEST(LogBufferTest, AppendEol)
36{
37 LogBuffer buf(0, 0);
38
39 buf.append("\r\r\r\r", 4);
40 EXPECT_EQ(std::distance(buf.begin(), buf.end()), 4);
41
42 buf.clear();
43 buf.append("\n\n\n\n", 4);
44 EXPECT_EQ(std::distance(buf.begin(), buf.end()), 4);
45
46 buf.clear();
47 buf.append("\r\n\r\n", 4);
48 EXPECT_EQ(std::distance(buf.begin(), buf.end()), 2);
49
50 buf.clear();
51 buf.append("\n\r\n\r", 4);
52 EXPECT_EQ(std::distance(buf.begin(), buf.end()), 2);
53
54 buf.clear();
55 buf.append("\r\r\r\n\n\n", 6);
56 EXPECT_EQ(std::distance(buf.begin(), buf.end()), 5);
57}
58
59TEST(LogBufferTest, Clear)
60{
61 const std::string msg = "Test message";
62
63 LogBuffer buf(0, 0);
64 buf.append(msg.data(), msg.length());
65 EXPECT_FALSE(buf.empty());
66 buf.clear();
67 EXPECT_TRUE(buf.empty());
68}
69
70TEST(LogBufferTest, SizeLimit)
71{
72 const size_t limit = 5;
73 const std::string msg = "Test message\n";
74
75 LogBuffer buf(limit, 0);
76 for (size_t i = 0; i < limit + 3; ++i)
77 {
78 buf.append(msg.data(), msg.length());
79 }
80 EXPECT_EQ(std::distance(buf.begin(), buf.end()), limit);
81}
82
83TEST(LogBufferTest, FullHandler)
84{
85 const size_t limit = 5;
86 const std::string msg = "Test message\n";
87
88 size_t count = 0;
89
90 LogBuffer buf(limit, 0);
91 buf.setFullHandler([&count, &buf]() {
92 ++count;
93 buf.clear();
94 });
95 for (size_t i = 0; i < limit + 3; ++i)
96 {
97 buf.append(msg.data(), msg.length());
98 }
99 EXPECT_EQ(count, 1);
100 EXPECT_EQ(std::distance(buf.begin(), buf.end()), 2);
101}