summaryrefslogtreecommitdiffstats
path: root/src/test/common/test_split.cc
blob: 285dea752fcc22b9eca6acbffa1edfb1f26c8965 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab ft=cpp

/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2019 Red Hat, Inc.
 *
 * 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 "common/split.h"
#include <algorithm>
#include <gtest/gtest.h>

namespace ceph {

using string_list = std::initializer_list<std::string_view>;

bool operator==(const split& lhs, const string_list& rhs) {
  return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
bool operator==(const string_list& lhs, const split& rhs) {
  return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}

TEST(split, split)
{
  EXPECT_EQ(string_list({}), split(""));
  EXPECT_EQ(string_list({}), split(","));
  EXPECT_EQ(string_list({}), split(",;"));

  EXPECT_EQ(string_list({"a"}), split("a,;"));
  EXPECT_EQ(string_list({"a"}), split(",a;"));
  EXPECT_EQ(string_list({"a"}), split(",;a"));

  EXPECT_EQ(string_list({"a", "b"}), split("a,b;"));
  EXPECT_EQ(string_list({"a", "b"}), split("a,;b"));
  EXPECT_EQ(string_list({"a", "b"}), split(",a;b"));
}

TEST(split, iterator_indirection)
{
  const auto parts = split("a,b");
  auto i = parts.begin();
  ASSERT_NE(i, parts.end());
  EXPECT_EQ("a", *i); // test operator*
}

TEST(split, iterator_dereference)
{
  const auto parts = split("a,b");
  auto i = parts.begin();
  ASSERT_NE(i, parts.end());
  EXPECT_EQ(1, i->size()); // test operator->
}

TEST(split, iterator_pre_increment)
{
  const auto parts = split("a,b");
  auto i = parts.begin();
  ASSERT_NE(i, parts.end());

  ASSERT_EQ("a", *i);
  EXPECT_EQ("b", *++i); // test operator++()
  EXPECT_EQ("b", *i);
}

TEST(split, iterator_post_increment)
{
  const auto parts = split("a,b");
  auto i = parts.begin();
  ASSERT_NE(i, parts.end());

  ASSERT_EQ("a", *i);
  EXPECT_EQ("a", *i++); // test operator++(int)
  ASSERT_NE(parts.end(), i);
  EXPECT_EQ("b", *i);
}

TEST(split, iterator_singular)
{
  const auto parts = split("a,b");
  auto i = parts.begin();

  // test comparions against default-constructed 'singular' iterators
  split::iterator j;
  split::iterator k;
  EXPECT_EQ(j, parts.end()); // singular == end
  EXPECT_EQ(j, k);           // singular == singular
  EXPECT_NE(j, i);           // singular != valid
}

TEST(split, iterator_multipass)
{
  const auto parts = split("a,b");
  auto i = parts.begin();
  ASSERT_NE(i, parts.end());

  // copy the iterator to test LegacyForwardIterator's multipass guarantee
  auto j = i;
  ASSERT_EQ(i, j);

  ASSERT_EQ("a", *i);
  ASSERT_NE(parts.end(), ++i);
  EXPECT_EQ("b", *i);

  ASSERT_EQ("a", *j); // test that ++i left j unmodified
  ASSERT_NE(parts.end(), ++j);
  EXPECT_EQ("b", *j);

  EXPECT_EQ(i, j);
}

} // namespace ceph