rippled
mulDiv_test.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.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/mulDiv.h>
21 #include <ripple/beast/unit_test.h>
22 
23 namespace ripple {
24 namespace test {
25 
26 struct mulDiv_test : beast::unit_test::suite
27 {
28  void
29  run() override
30  {
31  const auto max = std::numeric_limits<std::uint64_t>::max();
33 
34  auto result = mulDiv(85, 20, 5);
35  BEAST_EXPECT(result.first && result.second == 340);
36  result = mulDiv(20, 85, 5);
37  BEAST_EXPECT(result.first && result.second == 340);
38 
39  result = mulDiv(0, max - 1, max - 3);
40  BEAST_EXPECT(result.first && result.second == 0);
41  result = mulDiv(max - 1, 0, max - 3);
42  BEAST_EXPECT(result.first && result.second == 0);
43 
44  result = mulDiv(max, 2, max / 2);
45  BEAST_EXPECT(result.first && result.second == 4);
46  result = mulDiv(max, 1000, max / 1000);
47  BEAST_EXPECT(result.first && result.second == 1000000);
48  result = mulDiv(max, 1000, max / 1001);
49  BEAST_EXPECT(result.first && result.second == 1001000);
50  result = mulDiv(max32 + 1, max32 + 1, 5);
51  BEAST_EXPECT(result.first && result.second == 3689348814741910323);
52 
53  // Overflow
54  result = mulDiv(max - 1, max - 2, 5);
55  BEAST_EXPECT(!result.first && result.second == max);
56  }
57 };
58 
59 BEAST_DEFINE_TESTSUITE(mulDiv, ripple_basics, ripple);
60 
61 } // namespace test
62 } // namespace ripple
ripple::test::mulDiv_test
Definition: mulDiv_test.cpp:26
std::uint64_t
ripple::test::mulDiv_test::run
void run() override
Definition: mulDiv_test.cpp:29
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::mulDiv
std::pair< bool, Dest > mulDiv(Source1 value, Dest mul, Source2 div)
Definition: FeeUnits.h:468
std::numeric_limits::max
T max(T... args)
ripple::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(DeliverMin, app, ripple)