oceanbase

Форк
0
/
test_ob_log_adapt_string.cpp 
132 строки · 3.5 Кб
1
/**
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.
11
 */
12

13
#include <gtest/gtest.h>
14
#include "ob_log_adapt_string.h"    // ObLogAdaptString
15

16
using namespace oceanbase::common;
17
namespace oceanbase
18
{
19
namespace libobcdc
20
{
21

22
class TestLogAdaptString : public ::testing::Test
23
{
24
public:
25
  TestLogAdaptString() {}
26
  ~TestLogAdaptString() {}
27
};
28

29
void test_append_str(ObLogAdaptString &str, std::string &std_str, const char *cstr)
30
{
31
  const char *ret_cstr = NULL;
32
  ASSERT_EQ(OB_SUCCESS, str.append(cstr));
33
  std_str.append(cstr);
34

35
  ASSERT_EQ(OB_SUCCESS, str.cstr(ret_cstr));
36
  ASSERT_STREQ(std_str.c_str(), ret_cstr);
37
}
38

39
TEST_F(TestLogAdaptString, smoke_test)
40
{
41
  ObLogAdaptString str(ObModIds::OB_LOG_TEMP_MEMORY);
42
  std::string std_str;
43
  const char *cstr = "";
44

45
  test_append_str(str, std_str, "");
46
  test_append_str(str, std_str, "I am me ");
47
  test_append_str(str, std_str, "中华人民共和国 ");
48

49
  EXPECT_EQ(OB_SUCCESS, str.append_int64(100));
50
  std_str.append("100");
51

52
  ASSERT_EQ(OB_SUCCESS, str.cstr(cstr));
53
  ASSERT_STREQ(std_str.c_str(), cstr);
54

55
  OBLOG_LOG(INFO, "cstr", K(cstr), K(str));
56
}
57

58
TEST_F(TestLogAdaptString, argument_test)
59
{
60
  ObLogAdaptString str(ObModIds::OB_LOG_TEMP_MEMORY);
61
  std::string std_str;
62

63
  EXPECT_EQ(OB_INVALID_ARGUMENT, str.append(NULL));
64

65
  EXPECT_EQ(OB_SUCCESS, str.append(""));
66
  std_str.append("");
67
  EXPECT_EQ(OB_SUCCESS, str.append_int64(-1));
68
  std_str.append("-1");
69

70
  EXPECT_EQ(OB_SUCCESS, str.append_int64(INT64_MAX));
71
  char int64_max[100];
72
  sprintf(int64_max, "%ld", INT64_MAX);
73
  std_str.append(int64_max);
74

75

76
  const char *cstr = "";
77
  ASSERT_EQ(OB_SUCCESS, str.cstr(cstr));
78
  ASSERT_STREQ(std_str.c_str(), cstr);
79

80
  OBLOG_LOG(INFO, "cstr", K(cstr), K(std_str.c_str()));
81
}
82

83
TEST_F(TestLogAdaptString, all_sort_of_string)
84
{
85
  ObLogAdaptString str(ObModIds::OB_LOG_TEMP_MEMORY);
86
  std::string std_str;
87
  const char *cstr = "";
88
  char buf[1 * _M_ + 1];
89

90
  (void)memset(buf, 'a', sizeof(buf));
91

92
  // Empty strings are also equal
93
  EXPECT_EQ(OB_SUCCESS, str.cstr(cstr));
94
  EXPECT_STREQ(std_str.c_str(), cstr);
95

96
  for (int i = 0; i < 3; i++) {
97
    // less than 8K
98
    test_append_str(str, std_str, "");
99
    test_append_str(str, std_str, "11111111111111");
100
    test_append_str(str, std_str, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
101

102
    // equals to 8K
103
    buf[8 * _K_] = '\0';
104
    test_append_str(str, std_str, buf);
105
    test_append_str(str, std_str, buf);
106
    test_append_str(str, std_str, buf);
107
    buf[8 * _K_] = 'a';
108

109
    // greater than 8K
110
    buf[16 * _K_] = '\0';
111
    test_append_str(str, std_str, buf);
112
    buf[16 * _K_] = 'a';
113
    buf[32 * _K_] = '\0';
114
    test_append_str(str, std_str, buf);
115
    buf[32 * _K_] = 'a';
116
    buf[1 * _M_] = '\0';
117
    test_append_str(str, std_str, buf);
118
    buf[1 * _M_] = 'a';
119
  }
120
}
121

122
}
123
}
124

125
int main(int argc, char **argv)
126
{
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();
132
}
133

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.