rippled
test
basics
join_test.cpp
1
//------------------------------------------------------------------------------
2
/*
3
This file is part of rippled: https://github.com/ripple/rippled
4
Copyright (c) 2022 Ripple Labs Inc.
5
6
Permission to use, copy, modify, and/or distribute this software for any
7
purpose with or without fee is hereby granted, provided that the above
8
copyright notice and this permission notice appear in all copies.
9
10
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
*/
18
//==============================================================================
19
20
#include <test/jtx/Account.h>
21
22
#include <ripple/basics/join.h>
23
#include <ripple/beast/unit_test.h>
24
25
namespace
ripple
{
26
namespace
test {
27
28
struct
join_test
: beast::unit_test::suite
29
{
30
void
31
run
()
override
32
{
33
auto
test = [
this
](
auto
collectionanddelimiter,
std::string
expected) {
34
std::stringstream
ss;
35
// Put something else in the buffer before and after to ensure that
36
// the << operator returns the stream correctly.
37
ss <<
"("
<< collectionanddelimiter <<
")"
;
38
auto
const
str = ss.
str
();
39
BEAST_EXPECT(str.substr(1, str.length() - 2) == expected);
40
BEAST_EXPECT(str.front() ==
'('
);
41
BEAST_EXPECT(str.back() ==
')'
);
42
};
43
44
// C++ array
45
test(
46
CollectionAndDelimiter
(
std::array<int, 4>
{2, -1, 5, 10},
"/"
),
47
"2/-1/5/10"
);
48
// One item C++ array edge case
49
test(
50
CollectionAndDelimiter
(
std::array<std::string, 1>
{
"test"
},
" & "
),
51
"test"
);
52
// Empty C++ array edge case
53
test(
CollectionAndDelimiter
(
std::array<int, 0>
{},
","
),
""
);
54
{
55
// C-style array
56
char
letters[4]{
'w'
,
'a'
,
's'
,
'd'
};
57
test(
CollectionAndDelimiter
(letters,
std::to_string
(0)),
"w0a0s0d"
);
58
}
59
{
60
// Auto sized C-style array
61
std::string
words[]{
"one"
,
"two"
,
"three"
,
"four"
};
62
test(
CollectionAndDelimiter
(words,
"\n"
),
"one\ntwo\nthree\nfour"
);
63
}
64
{
65
// One item C-style array edge case
66
std::string
words[]{
"thing"
};
67
test(
CollectionAndDelimiter
(words,
"\n"
),
"thing"
);
68
}
69
// Initializer list
70
test(
71
CollectionAndDelimiter
(
std::initializer_list<size_t>
{19, 25},
"+"
),
72
"19+25"
);
73
// vector
74
test(
75
CollectionAndDelimiter
(
std::vector<int>
{0, 42},
std::to_string
(99)),
76
"09942"
);
77
{
78
// vector with one item edge case
79
using namespace
jtx;
80
test(
81
CollectionAndDelimiter
(
82
std::vector<Account>
{
Account::master
},
"xxx"
),
83
Account::master
.human());
84
}
85
// empty vector edge case
86
test(
CollectionAndDelimiter
(
std::vector<uint256>
{},
","
),
""
);
87
// C-style string
88
test(
CollectionAndDelimiter
(
"string"
,
" "
),
"s t r i n g"
);
89
// Empty C-style string edge case
90
test(
CollectionAndDelimiter
(
""
,
"*"
),
""
);
91
// Single char C-style string edge case
92
test(
CollectionAndDelimiter
(
"x"
,
"*"
),
"x"
);
93
// std::string
94
test(
CollectionAndDelimiter
(
std::string
{
"string"
},
"-"
),
"s-t-r-i-n-g"
);
95
// Empty std::string edge case
96
test(
CollectionAndDelimiter
(
std::string
{
""
},
"*"
),
""
);
97
// Single char std::string edge case
98
test(
CollectionAndDelimiter
(
std::string
{
"y"
},
"*"
),
"y"
);
99
}
100
};
// namespace test
101
102
BEAST_DEFINE_TESTSUITE
(
join
, ripple_basics,
ripple
);
103
104
}
// namespace test
105
}
// namespace ripple
std::string
STL class.
ripple::test::join_test::run
void run() override
Definition:
join_test.cpp:31
ripple::test::join_test
Definition:
join_test.cpp:28
ripple::join
Stream & join(Stream &s, Iter iter, Iter end, std::string const &delimiter)
Definition:
join.h:28
std::vector< int >
std::stringstream
STL class.
ripple::CollectionAndDelimiter
Definition:
join.h:39
std::to_string
T to_string(T... args)
std::array
STL class.
ripple::test::jtx::Account::master
static const Account master
The master account.
Definition:
Account.h:47
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition:
RCLCensorshipDetector.h:29
std::stringstream::str
T str(T... args)
std::initializer_list
ripple::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(DeliverMin, app, ripple)
Generated by
1.8.17