mirror of
https://github.com/XRPLF/rippled.git
synced 2025-12-06 17:27:55 +00:00
Normalize files containing unit test code:
Source files are split to place all unit test code into translation
units ending in .test.cpp with no other business logic in the same file,
and in directories named "test".
A new target is added to the SConstruct, invoked by:
scons count
This prints the total number of source code lines occupied by unit tests,
in rippled specific code and excluding library subtrees.
This commit is contained in:
@@ -1,96 +0,0 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github.com/ripple/rippled
|
||||
Copyright (c) 2012, 2013 Ripple Labs Inc.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <ripple/basics/KeyCache.h>
|
||||
|
||||
#include <beast/unit_test/suite.h>
|
||||
#include <beast/chrono/manual_clock.h>
|
||||
|
||||
namespace ripple {
|
||||
|
||||
class KeyCache_test : public beast::unit_test::suite
|
||||
{
|
||||
public:
|
||||
void run ()
|
||||
{
|
||||
beast::manual_clock <std::chrono::steady_clock> clock;
|
||||
clock.set (0);
|
||||
|
||||
typedef std::string Key;
|
||||
typedef KeyCache <Key> Cache;
|
||||
|
||||
// Insert an item, retrieve it, and age it so it gets purged.
|
||||
{
|
||||
Cache c ("test", clock, 1, 2);
|
||||
|
||||
expect (c.size () == 0);
|
||||
expect (c.insert ("one"));
|
||||
expect (! c.insert ("one"));
|
||||
expect (c.size () == 1);
|
||||
expect (c.exists ("one"));
|
||||
expect (c.touch_if_exists ("one"));
|
||||
++clock;
|
||||
c.sweep ();
|
||||
expect (c.size () == 1);
|
||||
expect (c.exists ("one"));
|
||||
++clock;
|
||||
c.sweep ();
|
||||
expect (c.size () == 0);
|
||||
expect (! c.exists ("one"));
|
||||
expect (! c.touch_if_exists ("one"));
|
||||
}
|
||||
|
||||
// Insert two items, have one expire
|
||||
{
|
||||
Cache c ("test", clock, 2, 2);
|
||||
|
||||
expect (c.insert ("one"));
|
||||
expect (c.size () == 1);
|
||||
expect (c.insert ("two"));
|
||||
expect (c.size () == 2);
|
||||
++clock;
|
||||
c.sweep ();
|
||||
expect (c.size () == 2);
|
||||
expect (c.touch_if_exists ("two"));
|
||||
++clock;
|
||||
c.sweep ();
|
||||
expect (c.size () == 1);
|
||||
expect (c.exists ("two"));
|
||||
}
|
||||
|
||||
// Insert three items (1 over limit), sweep
|
||||
{
|
||||
Cache c ("test", clock, 2, 3);
|
||||
|
||||
expect (c.insert ("one"));
|
||||
++clock;
|
||||
expect (c.insert ("two"));
|
||||
++clock;
|
||||
expect (c.insert ("three"));
|
||||
++clock;
|
||||
expect (c.size () == 3);
|
||||
c.sweep ();
|
||||
expect (c.size () < 3);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(KeyCache,common,ripple);
|
||||
|
||||
}
|
||||
@@ -236,7 +236,7 @@ void RangeSet::simplify ()
|
||||
|
||||
void RangeSet::checkInternalConsistency () const noexcept
|
||||
{
|
||||
#if BEAST_DEBUG
|
||||
#ifndef NDEBUG
|
||||
if (mRanges.size () > 1)
|
||||
{
|
||||
const_iterator const last = std::prev (mRanges.end ());
|
||||
@@ -244,92 +244,17 @@ void RangeSet::checkInternalConsistency () const noexcept
|
||||
for (const_iterator cur = mRanges.begin (); cur != last; ++cur)
|
||||
{
|
||||
const_iterator const next = std::next (cur);
|
||||
|
||||
bassert (cur->first <= cur->second);
|
||||
|
||||
bassert (next->first <= next->second);
|
||||
|
||||
bassert (cur->second + 1 < next->first);
|
||||
assert (cur->first <= cur->second);
|
||||
assert (next->first <= next->second);
|
||||
assert (cur->second + 1 < next->first);
|
||||
}
|
||||
}
|
||||
else if (mRanges.size () == 1)
|
||||
{
|
||||
const_iterator const iter = mRanges.begin ();
|
||||
|
||||
bassert (iter->first <= iter->second);
|
||||
assert (iter->first <= iter->second);
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
class RangeSet_test : public beast::unit_test::suite
|
||||
{
|
||||
public:
|
||||
RangeSet createPredefinedSet ()
|
||||
{
|
||||
RangeSet set;
|
||||
|
||||
// Set will include:
|
||||
// [ 0, 5]
|
||||
// [10,15]
|
||||
// [20,25]
|
||||
// etc...
|
||||
|
||||
for (int i = 0; i < 10; ++i)
|
||||
set.setRange (10 * i, 10 * i + 5);
|
||||
|
||||
return set;
|
||||
}
|
||||
|
||||
void testMembership ()
|
||||
{
|
||||
testcase ("membership");
|
||||
|
||||
RangeSet r1, r2;
|
||||
|
||||
r1.setRange (1, 10);
|
||||
r1.clearValue (5);
|
||||
r1.setRange (11, 20);
|
||||
|
||||
r2.setRange (1, 4);
|
||||
r2.setRange (6, 10);
|
||||
r2.setRange (10, 20);
|
||||
|
||||
expect (!r1.hasValue (5));
|
||||
|
||||
expect (r2.hasValue (9));
|
||||
}
|
||||
|
||||
void testPrevMissing ()
|
||||
{
|
||||
testcase ("prevMissing");
|
||||
|
||||
RangeSet const set = createPredefinedSet ();
|
||||
|
||||
for (int i = 0; i < 100; ++i)
|
||||
{
|
||||
int const oneBelowRange = (10*(i/10))-1;
|
||||
|
||||
int const expectedPrevMissing =
|
||||
((i % 10) > 6) ? (i-1) : oneBelowRange;
|
||||
|
||||
expect (set.prevMissing (i) == expectedPrevMissing);
|
||||
}
|
||||
}
|
||||
|
||||
void run ()
|
||||
{
|
||||
testMembership ();
|
||||
|
||||
testPrevMissing ();
|
||||
|
||||
// TODO: Traverse functions must be tested
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(RangeSet,ripple_basics,ripple);
|
||||
|
||||
} // ripple
|
||||
|
||||
|
||||
@@ -260,112 +260,4 @@ beast::StringPairArray parseDelimitedKeyValueString (beast::String parameters,
|
||||
return keyValues;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
class StringUtilities_test : public beast::unit_test::suite
|
||||
{
|
||||
public:
|
||||
void testUnHexSuccess (std::string strIn, std::string strExpected)
|
||||
{
|
||||
std::string strOut;
|
||||
|
||||
expect (strUnHex (strOut, strIn) == strExpected.length (),
|
||||
"strUnHex: parsing correct input failed");
|
||||
|
||||
expect (strOut == strExpected,
|
||||
"strUnHex: parsing doesn't produce expected result");
|
||||
}
|
||||
|
||||
void testUnHexFailure (std::string strIn)
|
||||
{
|
||||
std::string strOut;
|
||||
|
||||
expect (strUnHex (strOut, strIn) == -1,
|
||||
"strUnHex: parsing incorrect input succeeded");
|
||||
|
||||
expect (strOut.empty (),
|
||||
"strUnHex: parsing incorrect input returned data");
|
||||
}
|
||||
|
||||
void testUnHex ()
|
||||
{
|
||||
testcase ("strUnHex");
|
||||
|
||||
testUnHexSuccess ("526970706c6544", "RippleD");
|
||||
testUnHexSuccess ("A", "\n");
|
||||
testUnHexSuccess ("0A", "\n");
|
||||
testUnHexSuccess ("D0A", "\r\n");
|
||||
testUnHexSuccess ("0D0A", "\r\n");
|
||||
testUnHexSuccess ("200D0A", " \r\n");
|
||||
testUnHexSuccess ("282A2B2C2D2E2F29", "(*+,-./)");
|
||||
|
||||
// Check for things which contain some or only invalid characters
|
||||
testUnHexFailure ("123X");
|
||||
testUnHexFailure ("V");
|
||||
testUnHexFailure ("XRP");
|
||||
}
|
||||
|
||||
void testParseUrl ()
|
||||
{
|
||||
testcase ("parseUrl");
|
||||
|
||||
std::string strScheme;
|
||||
std::string strDomain;
|
||||
int iPort;
|
||||
std::string strPath;
|
||||
|
||||
unexpected (!parseUrl ("lower://domain", strScheme, strDomain, iPort, strPath),
|
||||
"parseUrl: lower://domain failed");
|
||||
|
||||
unexpected (strScheme != "lower",
|
||||
"parseUrl: lower://domain : scheme failed");
|
||||
|
||||
unexpected (strDomain != "domain",
|
||||
"parseUrl: lower://domain : domain failed");
|
||||
|
||||
unexpected (iPort != -1,
|
||||
"parseUrl: lower://domain : port failed");
|
||||
|
||||
unexpected (strPath != "",
|
||||
"parseUrl: lower://domain : path failed");
|
||||
|
||||
unexpected (!parseUrl ("UPPER://domain:234/", strScheme, strDomain, iPort, strPath),
|
||||
"parseUrl: UPPER://domain:234/ failed");
|
||||
|
||||
unexpected (strScheme != "upper",
|
||||
"parseUrl: UPPER://domain:234/ : scheme failed");
|
||||
|
||||
unexpected (iPort != 234,
|
||||
boost::str (boost::format ("parseUrl: UPPER://domain:234/ : port failed: %d") % iPort));
|
||||
|
||||
unexpected (strPath != "/",
|
||||
"parseUrl: UPPER://domain:234/ : path failed");
|
||||
|
||||
unexpected (!parseUrl ("Mixed://domain/path", strScheme, strDomain, iPort, strPath),
|
||||
"parseUrl: Mixed://domain/path failed");
|
||||
|
||||
unexpected (strScheme != "mixed",
|
||||
"parseUrl: Mixed://domain/path tolower failed");
|
||||
|
||||
unexpected (strPath != "/path",
|
||||
"parseUrl: Mixed://domain/path path failed");
|
||||
}
|
||||
|
||||
void testToString ()
|
||||
{
|
||||
testcase ("toString");
|
||||
auto result = to_string("hello");
|
||||
expect(result == "hello", result);
|
||||
}
|
||||
|
||||
void run ()
|
||||
{
|
||||
testParseUrl ();
|
||||
testUnHex ();
|
||||
testToString ();
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(StringUtilities, ripple_basics, ripple);
|
||||
|
||||
} // ripple
|
||||
|
||||
@@ -1,151 +0,0 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github.com/ripple/rippled
|
||||
Copyright (c) 2012, 2013 Ripple Labs Inc.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <ripple/basics/TaggedCache.h>
|
||||
|
||||
#include <beast/unit_test/suite.h>
|
||||
#include <beast/chrono/manual_clock.h>
|
||||
|
||||
namespace ripple {
|
||||
|
||||
/*
|
||||
I guess you can put some items in, make sure they're still there. Let some
|
||||
time pass, make sure they're gone. Keep a strong pointer to one of them, make
|
||||
sure you can still find it even after time passes. Create two objects with
|
||||
the same key, canonicalize them both and make sure you get the same object.
|
||||
Put an object in but keep a strong pointer to it, advance the clock a lot,
|
||||
then canonicalize a new object with the same key, make sure you get the
|
||||
original object.
|
||||
*/
|
||||
|
||||
class TaggedCache_test : public beast::unit_test::suite
|
||||
{
|
||||
public:
|
||||
void run ()
|
||||
{
|
||||
beast::Journal const j;
|
||||
|
||||
beast::manual_clock <std::chrono::steady_clock> clock;
|
||||
clock.set (0);
|
||||
|
||||
typedef int Key;
|
||||
typedef std::string Value;
|
||||
typedef TaggedCache <Key, Value> Cache;
|
||||
|
||||
Cache c ("test", 1, 1, clock, j);
|
||||
|
||||
// Insert an item, retrieve it, and age it so it gets purged.
|
||||
{
|
||||
expect (c.getCacheSize() == 0);
|
||||
expect (c.getTrackSize() == 0);
|
||||
expect (! c.insert (1, "one"));
|
||||
expect (c.getCacheSize() == 1);
|
||||
expect (c.getTrackSize() == 1);
|
||||
|
||||
{
|
||||
std::string s;
|
||||
expect (c.retrieve (1, s));
|
||||
expect (s == "one");
|
||||
}
|
||||
|
||||
++clock;
|
||||
c.sweep ();
|
||||
expect (c.getCacheSize () == 0);
|
||||
expect (c.getTrackSize () == 0);
|
||||
}
|
||||
|
||||
// Insert an item, maintain a strong pointer, age it, and
|
||||
// verify that the entry still exists.
|
||||
{
|
||||
expect (! c.insert (2, "two"));
|
||||
expect (c.getCacheSize() == 1);
|
||||
expect (c.getTrackSize() == 1);
|
||||
|
||||
{
|
||||
Cache::mapped_ptr p (c.fetch (2));
|
||||
expect (p != nullptr);
|
||||
++clock;
|
||||
c.sweep ();
|
||||
expect (c.getCacheSize() == 0);
|
||||
expect (c.getTrackSize() == 1);
|
||||
}
|
||||
|
||||
// Make sure its gone now that our reference is gone
|
||||
++clock;
|
||||
c.sweep ();
|
||||
expect (c.getCacheSize() == 0);
|
||||
expect (c.getTrackSize() == 0);
|
||||
}
|
||||
|
||||
// Insert the same key/value pair and make sure we get the same result
|
||||
{
|
||||
expect (! c.insert (3, "three"));
|
||||
|
||||
{
|
||||
Cache::mapped_ptr const p1 (c.fetch (3));
|
||||
Cache::mapped_ptr p2 (std::make_shared <Value> ("three"));
|
||||
c.canonicalize (3, p2);
|
||||
expect (p1.get() == p2.get());
|
||||
}
|
||||
++clock;
|
||||
c.sweep ();
|
||||
expect (c.getCacheSize() == 0);
|
||||
expect (c.getTrackSize() == 0);
|
||||
}
|
||||
|
||||
// Put an object in but keep a strong pointer to it, advance the clock a lot,
|
||||
// then canonicalize a new object with the same key, make sure you get the
|
||||
// original object.
|
||||
{
|
||||
// Put an object in
|
||||
expect (! c.insert (4, "four"));
|
||||
expect (c.getCacheSize() == 1);
|
||||
expect (c.getTrackSize() == 1);
|
||||
|
||||
{
|
||||
// Keep a strong pointer to it
|
||||
Cache::mapped_ptr p1 (c.fetch (4));
|
||||
expect (p1 != nullptr);
|
||||
expect (c.getCacheSize() == 1);
|
||||
expect (c.getTrackSize() == 1);
|
||||
// Advance the clock a lot
|
||||
++clock;
|
||||
c.sweep ();
|
||||
expect (c.getCacheSize() == 0);
|
||||
expect (c.getTrackSize() == 1);
|
||||
// Canonicalize a new object with the same key
|
||||
Cache::mapped_ptr p2 (std::make_shared <std::string> ("four"));
|
||||
expect (c.canonicalize (4, p2, false));
|
||||
expect (c.getCacheSize() == 1);
|
||||
expect (c.getTrackSize() == 1);
|
||||
// Make sure we get the original object
|
||||
expect (p1.get() == p2.get());
|
||||
}
|
||||
|
||||
++clock;
|
||||
c.sweep ();
|
||||
expect (c.getCacheSize() == 0);
|
||||
expect (c.getTrackSize() == 0);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(TaggedCache,common,ripple);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user