rippled
Slice_test.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github0.com/ripple/rippled
4  Copyright (c) 2012-2016 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 <ripple/basics/Slice.h>
21 #include <ripple/beast/unit_test.h>
22 #include <array>
23 #include <cstdint>
24 
25 namespace ripple {
26 namespace test {
27 
28 struct Slice_test : beast::unit_test::suite
29 {
30  void
31  run() override
32  {
33  std::uint8_t const data[] = {
34  0xa8, 0xa1, 0x38, 0x45, 0x23, 0xec, 0xe4, 0x23, 0x71, 0x6d, 0x2a,
35  0x18, 0xb4, 0x70, 0xcb, 0xf5, 0xac, 0x2d, 0x89, 0x4d, 0x19, 0x9c,
36  0xf0, 0x2c, 0x15, 0xd1, 0xf9, 0x9b, 0x66, 0xd2, 0x30, 0xd3};
37 
38  {
39  testcase("Equality & Inequality");
40 
41  Slice const s0{};
42 
43  BEAST_EXPECT(s0.size() == 0);
44  BEAST_EXPECT(s0.data() == nullptr);
45  BEAST_EXPECT(s0 == s0);
46 
47  // Test slices of equal and unequal size pointing to same data:
48  for (std::size_t i = 0; i != sizeof(data); ++i)
49  {
50  Slice const s1{data, i};
51 
52  BEAST_EXPECT(s1.size() == i);
53  BEAST_EXPECT(s1.data() != nullptr);
54 
55  if (i == 0)
56  BEAST_EXPECT(s1 == s0);
57  else
58  BEAST_EXPECT(s1 != s0);
59 
60  for (std::size_t j = 0; j != sizeof(data); ++j)
61  {
62  Slice const s2{data, j};
63 
64  if (i == j)
65  BEAST_EXPECT(s1 == s2);
66  else
67  BEAST_EXPECT(s1 != s2);
68  }
69  }
70 
71  // Test slices of equal size but pointing to different data:
72  std::array<std::uint8_t, sizeof(data)> a;
73  std::array<std::uint8_t, sizeof(data)> b;
74 
75  for (std::size_t i = 0; i != sizeof(data); ++i)
76  a[i] = b[i] = data[i];
77 
78  BEAST_EXPECT(makeSlice(a) == makeSlice(b));
79  b[7]++;
80  BEAST_EXPECT(makeSlice(a) != makeSlice(b));
81  a[7]++;
82  BEAST_EXPECT(makeSlice(a) == makeSlice(b));
83  }
84 
85  {
86  testcase("Indexing");
87 
88  Slice const s{data, sizeof(data)};
89 
90  for (std::size_t i = 0; i != sizeof(data); ++i)
91  BEAST_EXPECT(s[i] == data[i]);
92  }
93 
94  {
95  testcase("Advancing");
96 
97  for (std::size_t i = 0; i < sizeof(data); ++i)
98  {
99  for (std::size_t j = 0; i + j < sizeof(data); ++j)
100  {
101  Slice s(data + i, sizeof(data) - i);
102  s += j;
103 
104  BEAST_EXPECT(s.data() == data + i + j);
105  BEAST_EXPECT(s.size() == sizeof(data) - i - j);
106  }
107  }
108  }
109  }
110 };
111 
112 BEAST_DEFINE_TESTSUITE(Slice, ripple_basics, ripple);
113 
114 } // namespace test
115 } // namespace ripple
ripple::Slice::size
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition: Slice.h:80
ripple::makeSlice
std::enable_if_t< std::is_same< T, char >::value||std::is_same< T, unsigned char >::value, Slice > makeSlice(std::array< T, N > const &a)
Definition: Slice.h:241
ripple::Slice
An immutable linear range of bytes.
Definition: Slice.h:44
ripple::Slice::data
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition: Slice.h:97
ripple::test::Slice_test::run
void run() override
Definition: Slice_test.cpp:31
ripple::test::Slice_test
Definition: Slice_test.cpp:28
array
cstdint
std::uint8_t
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
std::size_t
ripple::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(DeliverMin, app, ripple)