summaryrefslogtreecommitdiffstats
path: root/src/test/rgw/test_rgw_string.cc
blob: 90a0b00c8c0f0947c65ba0074c299938d94800b4 (plain)
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
75
76
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2017 Red Hat
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation. See file COPYING.
 *
 */

#include "rgw_string.h"
#include <gtest/gtest.h>

const std::string abc{"abc"};
const char *def{"def"}; // const char*
char ghi_arr[] = {'g', 'h', 'i', '\0'};
char *ghi{ghi_arr}; // char*
constexpr std::string_view jkl{"jkl", 3};
#define mno "mno" // string literal (char[4])
char pqr[] = {'p', 'q', 'r', '\0'};

TEST(string_size, types)
{
  ASSERT_EQ(3u, string_size(abc));
  ASSERT_EQ(3u, string_size(def));
  ASSERT_EQ(3u, string_size(ghi));
  ASSERT_EQ(3u, string_size(jkl));
  ASSERT_EQ(3u, string_size(mno));
  ASSERT_EQ(3u, string_size(pqr));

  constexpr auto compile_time_string_view_size = string_size(jkl);
  ASSERT_EQ(3u, compile_time_string_view_size);
  constexpr auto compile_time_string_literal_size = string_size(mno);
  ASSERT_EQ(3u, compile_time_string_literal_size);

  char arr[] = {'a', 'b', 'c'}; // not null-terminated
  ASSERT_THROW(string_size(arr), std::invalid_argument);
}

TEST(string_cat_reserve, types)
{
  ASSERT_EQ("abcdefghijklmnopqr",
            string_cat_reserve(abc, def, ghi, jkl, mno, pqr));
}

TEST(string_cat_reserve, count)
{
  ASSERT_EQ("", string_cat_reserve());
  ASSERT_EQ("abc", string_cat_reserve(abc));
  ASSERT_EQ("abcdef", string_cat_reserve(abc, def));
}

TEST(string_join_reserve, types)
{
  ASSERT_EQ("abc, def, ghi, jkl, mno, pqr",
            string_join_reserve(", ", abc, def, ghi, jkl, mno, pqr));
}

TEST(string_join_reserve, count)
{
  ASSERT_EQ("", string_join_reserve(", "));
  ASSERT_EQ("abc", string_join_reserve(", ", abc));
  ASSERT_EQ("abc, def", string_join_reserve(", ", abc, def));
}

TEST(string_join_reserve, delim)
{
  ASSERT_EQ("abcdef", string_join_reserve("", abc, def));
  ASSERT_EQ("abc def", string_join_reserve(' ', abc, def));
  ASSERT_EQ("abc\ndef", string_join_reserve('\n', abc, def));
  ASSERT_EQ("abcfoodef", string_join_reserve(std::string{"foo"}, abc, def));
}