Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / rgw / test_rgw_string.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  * Copyright (C) 2017 Red Hat
7  *
8  * This is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License version 2.1, as published by the Free Software
11  * Foundation. See file COPYING.
12  *
13  */
14
15 #include "rgw/rgw_string.h"
16 #include <gtest/gtest.h>
17
18 const std::string abc{"abc"};
19 const char *def{"def"}; // const char*
20 char ghi_arr[] = {'g', 'h', 'i', '\0'};
21 char *ghi{ghi_arr}; // char*
22 constexpr boost::string_view jkl{"jkl", 3};
23 #define mno "mno" // string literal (char[4])
24 char pqr[] = {'p', 'q', 'r', '\0'};
25
26 TEST(string_size, types)
27 {
28   ASSERT_EQ(3u, string_size(abc));
29   ASSERT_EQ(3u, string_size(def));
30   ASSERT_EQ(3u, string_size(ghi));
31   ASSERT_EQ(3u, string_size(jkl));
32   ASSERT_EQ(3u, string_size(mno));
33   ASSERT_EQ(3u, string_size(pqr));
34
35   constexpr auto compile_time_string_view_size = string_size(jkl);
36   ASSERT_EQ(3u, compile_time_string_view_size);
37   constexpr auto compile_time_string_literal_size = string_size(mno);
38   ASSERT_EQ(3u, compile_time_string_literal_size);
39
40   char arr[] = {'a', 'b', 'c'}; // not null-terminated
41   ASSERT_THROW(string_size(arr), std::invalid_argument);
42 }
43
44 TEST(string_cat_reserve, types)
45 {
46   ASSERT_EQ("abcdefghijklmnopqr",
47             string_cat_reserve(abc, def, ghi, jkl, mno, pqr));
48 }
49
50 TEST(string_cat_reserve, count)
51 {
52   ASSERT_EQ("", string_cat_reserve());
53   ASSERT_EQ("abc", string_cat_reserve(abc));
54   ASSERT_EQ("abcdef", string_cat_reserve(abc, def));
55 }
56
57 TEST(string_join_reserve, types)
58 {
59   ASSERT_EQ("abc, def, ghi, jkl, mno, pqr",
60             string_join_reserve(", ", abc, def, ghi, jkl, mno, pqr));
61 }
62
63 TEST(string_join_reserve, count)
64 {
65   ASSERT_EQ("", string_join_reserve(", "));
66   ASSERT_EQ("abc", string_join_reserve(", ", abc));
67   ASSERT_EQ("abc, def", string_join_reserve(", ", abc, def));
68 }
69
70 TEST(string_join_reserve, delim)
71 {
72   ASSERT_EQ("abcdef", string_join_reserve("", abc, def));
73   ASSERT_EQ("abc def", string_join_reserve(' ', abc, def));
74   ASSERT_EQ("abc\ndef", string_join_reserve('\n', abc, def));
75   ASSERT_EQ("abcfoodef", string_join_reserve(std::string{"foo"}, abc, def));
76 }