rippled
Loading...
Searching...
No Matches
join_test.cpp
1//------------------------------------------------------------------------------
2/*
3This file is part of rippled: https://github.com/ripple/rippled
4Copyright (c) 2022 Ripple Labs Inc.
5
6Permission to use, copy, modify, and/or distribute this software for any
7purpose with or without fee is hereby granted, provided that the above
8copyright notice and this permission notice appear in all copies.
9
10THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <test/jtx/Account.h>
21
22#include <xrpl/basics/join.h>
23#include <xrpl/beast/unit_test.h>
24
25namespace ripple {
26namespace test {
27
29{
30 void
31 run() override
32 {
33 auto test = [this](auto collectionanddelimiter, std::string expected) {
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(
47 "2/-1/5/10");
48 // One item C++ array edge case
49 test(
51 "test");
52 // Empty C++ array edge case
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(
72 "19+25");
73 // vector
74 test(
76 "09942");
77 {
78 // vector with one item edge case
79 using namespace jtx;
80 test(
83 Account::master.human());
84 }
85 // empty vector edge case
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
102BEAST_DEFINE_TESTSUITE(join, basics, ripple);
103
104} // namespace test
105} // namespace ripple
A testsuite class.
Definition suite.h:55
static Account const master
The master account.
Definition Account.h:48
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
Stream & join(Stream &s, Iter iter, Iter end, std::string const &delimiter)
Definition join.h:28
T str(T... args)
void run() override
Runs the suite.
Definition join_test.cpp:31
T to_string(T... args)