rippled
varint_test.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of Beast: https://github.com/vinniefalco/Beast
4  Copyright 2014, Vinnie Falco <vinnie.falco@gmail.com>
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/beast/unit_test.h>
21 #include <ripple/nodestore/impl/varint.h>
22 #include <array>
23 #include <vector>
24 
25 namespace ripple {
26 namespace NodeStore {
27 namespace tests {
28 
29 class varint_test : public beast::unit_test::suite
30 {
31 public:
32  void
34  {
35  testcase("encode, decode");
36  for (auto const v : vv)
37  {
39  auto const n0 = write_varint(vi.data(), v);
40  expect(n0 > 0, "write error");
41  expect(n0 == size_varint(v), "size error");
42  std::size_t v1;
43  auto const n1 = read_varint(vi.data(), n0, v1);
44  expect(n1 == n0, "read error");
45  expect(v == v1, "wrong value");
46  }
47  }
48 
49  void
50  run() override
51  {
53  {0,
54  1,
55  2,
56  126,
57  127,
58  128,
59  253,
60  254,
61  255,
62  16127,
63  16128,
64  16129,
65  0xff,
66  0xffff,
67  0xffffffff,
68  0xffffffffffffUL,
69  0xffffffffffffffffUL});
70  }
71 };
72 
73 BEAST_DEFINE_TESTSUITE(varint, NodeStore, ripple);
74 
75 } // namespace tests
76 } // namespace NodeStore
77 } // namespace ripple
vector
ripple::NodeStore::tests::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(varint, NodeStore, ripple)
ripple::NodeStore::tests::varint_test::run
void run() override
Definition: varint_test.cpp:50
ripple::NodeStore::read_varint
std::size_t read_varint(void const *buf, std::size_t buflen, std::size_t &t)
Definition: varint.h:56
array
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::NodeStore::tests::varint_test
Definition: varint_test.cpp:29
ripple::NodeStore::size_varint
std::size_t size_varint(T v)
Definition: varint.h:89
std::size_t
ripple::NodeStore::tests::varint_test::test_varints
void test_varints(std::vector< std::size_t > vv)
Definition: varint_test.cpp:33
std::array::data
T data(T... args)
ripple::NodeStore::write_varint
std::size_t write_varint(void *p0, std::size_t v)
Definition: varint.h:102