2
* Copyright (c) 2021 OceanBase
3
* OceanBase CE is licensed under Mulan PubL v2.
4
* You can use this software according to the terms and conditions of the Mulan PubL v2.
5
* You may obtain a copy of Mulan PubL v2 at:
6
* http://license.coscl.org.cn/MulanPubL-2.0
7
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
8
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
9
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
10
* See the Mulan PubL v2 for more details.
13
#include <gtest/gtest.h>
14
#include "ob_log_adapt_string.h" // ObLogAdaptString
16
using namespace oceanbase::common;
22
class TestLogAdaptString : public ::testing::Test
25
TestLogAdaptString() {}
26
~TestLogAdaptString() {}
29
void test_append_str(ObLogAdaptString &str, std::string &std_str, const char *cstr)
31
const char *ret_cstr = NULL;
32
ASSERT_EQ(OB_SUCCESS, str.append(cstr));
35
ASSERT_EQ(OB_SUCCESS, str.cstr(ret_cstr));
36
ASSERT_STREQ(std_str.c_str(), ret_cstr);
39
TEST_F(TestLogAdaptString, smoke_test)
41
ObLogAdaptString str(ObModIds::OB_LOG_TEMP_MEMORY);
43
const char *cstr = "";
45
test_append_str(str, std_str, "");
46
test_append_str(str, std_str, "I am me ");
47
test_append_str(str, std_str, "中华人民共和国 ");
49
EXPECT_EQ(OB_SUCCESS, str.append_int64(100));
50
std_str.append("100");
52
ASSERT_EQ(OB_SUCCESS, str.cstr(cstr));
53
ASSERT_STREQ(std_str.c_str(), cstr);
55
OBLOG_LOG(INFO, "cstr", K(cstr), K(str));
58
TEST_F(TestLogAdaptString, argument_test)
60
ObLogAdaptString str(ObModIds::OB_LOG_TEMP_MEMORY);
63
EXPECT_EQ(OB_INVALID_ARGUMENT, str.append(NULL));
65
EXPECT_EQ(OB_SUCCESS, str.append(""));
67
EXPECT_EQ(OB_SUCCESS, str.append_int64(-1));
70
EXPECT_EQ(OB_SUCCESS, str.append_int64(INT64_MAX));
72
sprintf(int64_max, "%ld", INT64_MAX);
73
std_str.append(int64_max);
76
const char *cstr = "";
77
ASSERT_EQ(OB_SUCCESS, str.cstr(cstr));
78
ASSERT_STREQ(std_str.c_str(), cstr);
80
OBLOG_LOG(INFO, "cstr", K(cstr), K(std_str.c_str()));
83
TEST_F(TestLogAdaptString, all_sort_of_string)
85
ObLogAdaptString str(ObModIds::OB_LOG_TEMP_MEMORY);
87
const char *cstr = "";
88
char buf[1 * _M_ + 1];
90
(void)memset(buf, 'a', sizeof(buf));
92
// Empty strings are also equal
93
EXPECT_EQ(OB_SUCCESS, str.cstr(cstr));
94
EXPECT_STREQ(std_str.c_str(), cstr);
96
for (int i = 0; i < 3; i++) {
98
test_append_str(str, std_str, "");
99
test_append_str(str, std_str, "11111111111111");
100
test_append_str(str, std_str, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
104
test_append_str(str, std_str, buf);
105
test_append_str(str, std_str, buf);
106
test_append_str(str, std_str, buf);
110
buf[16 * _K_] = '\0';
111
test_append_str(str, std_str, buf);
113
buf[32 * _K_] = '\0';
114
test_append_str(str, std_str, buf);
117
test_append_str(str, std_str, buf);
125
int main(int argc, char **argv)
127
oceanbase::common::ObLogger::get_logger().set_log_level("INFO");
128
OB_LOGGER.set_log_level("INFO");
129
OB_LOGGER.set_file_name("test_ob_log_adapt_string.log", true);
130
testing::InitGoogleTest(&argc, argv);
131
return RUN_ALL_TESTS();