mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-21 11:35:53 +00:00
New unit_test framework:
* Header-only! * No external dependencies or other beast modules * Compilation options allow for: - Stand-alone application to run a single test suite - Stand-alone application to run a set of test suites - Global suite of tests inline with the host application - Disable test suite generation completely * Existing tests reworked to use the new classes
This commit is contained in:
@@ -21,7 +21,9 @@
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <stdarg.h>
|
||||
#if BEAST_INCLUDE_BEASTCONFIG
|
||||
#include "../../BeastConfig.h"
|
||||
#endif
|
||||
|
||||
#include "../String.h"
|
||||
#include "../NewLine.h"
|
||||
@@ -32,7 +34,7 @@
|
||||
#include "../../Arithmetic.h"
|
||||
#include "../../HeapBlock.h"
|
||||
|
||||
#include "../../../modules/beast_core/beast_core.h" // for UnitTest
|
||||
#include <stdarg.h>
|
||||
|
||||
namespace beast {
|
||||
|
||||
@@ -1976,364 +1978,4 @@ String String::fromUTF8 (const char* const buffer, int bufferSizeBytes)
|
||||
#pragma warning (pop)
|
||||
#endif
|
||||
|
||||
//==============================================================================
|
||||
//==============================================================================
|
||||
|
||||
#ifdef BEAST_UNITTEST_H_INCLUDED
|
||||
|
||||
class StringTests : public UnitTest
|
||||
{
|
||||
public:
|
||||
StringTests() : UnitTest ("String", "beast")
|
||||
{
|
||||
}
|
||||
|
||||
template <class CharPointerType>
|
||||
struct TestUTFConversion
|
||||
{
|
||||
static void test (UnitTest& test)
|
||||
{
|
||||
String s (createRandomWideCharString());
|
||||
|
||||
typename CharPointerType::CharType buffer [300];
|
||||
|
||||
memset (buffer, 0xff, sizeof (buffer));
|
||||
CharPointerType (buffer).writeAll (s.toUTF32());
|
||||
test.expectEquals (String (CharPointerType (buffer)), s);
|
||||
|
||||
memset (buffer, 0xff, sizeof (buffer));
|
||||
CharPointerType (buffer).writeAll (s.toUTF16());
|
||||
test.expectEquals (String (CharPointerType (buffer)), s);
|
||||
|
||||
memset (buffer, 0xff, sizeof (buffer));
|
||||
CharPointerType (buffer).writeAll (s.toUTF8());
|
||||
test.expectEquals (String (CharPointerType (buffer)), s);
|
||||
|
||||
test.expect (CharPointerType::isValidString (buffer, (int) strlen ((const char*) buffer)));
|
||||
}
|
||||
};
|
||||
|
||||
static String createRandomWideCharString()
|
||||
{
|
||||
beast_wchar buffer[50] = { 0 };
|
||||
Random r;
|
||||
|
||||
for (int i = 0; i < numElementsInArray (buffer) - 1; ++i)
|
||||
{
|
||||
if (r.nextBool())
|
||||
{
|
||||
do
|
||||
{
|
||||
buffer[i] = (beast_wchar) (1 + r.nextInt (0x10ffff - 1));
|
||||
}
|
||||
while (! CharPointer_UTF16::canRepresent (buffer[i]));
|
||||
}
|
||||
else
|
||||
buffer[i] = (beast_wchar) (1 + r.nextInt (0xff));
|
||||
}
|
||||
|
||||
return CharPointer_UTF32 (buffer);
|
||||
}
|
||||
|
||||
void runTest()
|
||||
{
|
||||
{
|
||||
beginTestCase ("Basics");
|
||||
|
||||
expect (String().length() == 0);
|
||||
expect (String() == String::empty);
|
||||
String s1, s2 ("abcd");
|
||||
expect (s1.isEmpty() && ! s1.isNotEmpty());
|
||||
expect (s2.isNotEmpty() && ! s2.isEmpty());
|
||||
expect (s2.length() == 4);
|
||||
s1 = "abcd";
|
||||
expect (s2 == s1 && s1 == s2);
|
||||
expect (s1 == "abcd" && s1 == L"abcd");
|
||||
expect (String ("abcd") == String (L"abcd"));
|
||||
expect (String ("abcdefg", 4) == L"abcd");
|
||||
expect (String ("abcdefg", 4) == String (L"abcdefg", 4));
|
||||
expect (String::charToString ('x') == "x");
|
||||
expect (String::charToString (0) == String::empty);
|
||||
expect (s2 + "e" == "abcde" && s2 + 'e' == "abcde");
|
||||
expect (s2 + L'e' == "abcde" && s2 + L"e" == "abcde");
|
||||
expect (s1.equalsIgnoreCase ("abcD") && s1 < "abce" && s1 > "abbb");
|
||||
expect (s1.startsWith ("ab") && s1.startsWith ("abcd") && ! s1.startsWith ("abcde"));
|
||||
expect (s1.startsWithIgnoreCase ("aB") && s1.endsWithIgnoreCase ("CD"));
|
||||
expect (s1.endsWith ("bcd") && ! s1.endsWith ("aabcd"));
|
||||
expectEquals (s1.indexOf (String::empty), 0);
|
||||
expectEquals (s1.indexOfIgnoreCase (String::empty), 0);
|
||||
expect (s1.startsWith (String::empty) && s1.endsWith (String::empty) && s1.contains (String::empty));
|
||||
expect (s1.contains ("cd") && s1.contains ("ab") && s1.contains ("abcd"));
|
||||
expect (s1.containsChar ('a'));
|
||||
expect (! s1.containsChar ('x'));
|
||||
expect (! s1.containsChar (0));
|
||||
expect (String ("abc foo bar").containsWholeWord ("abc") && String ("abc foo bar").containsWholeWord ("abc"));
|
||||
}
|
||||
|
||||
{
|
||||
beginTestCase ("Operations");
|
||||
|
||||
String s ("012345678");
|
||||
expect (s.hashCode() != 0);
|
||||
expect (s.hashCode64() != 0);
|
||||
expect (s.hashCode() != (s + s).hashCode());
|
||||
expect (s.hashCode64() != (s + s).hashCode64());
|
||||
expect (s.compare (String ("012345678")) == 0);
|
||||
expect (s.compare (String ("012345679")) < 0);
|
||||
expect (s.compare (String ("012345676")) > 0);
|
||||
expect (s.substring (2, 3) == String::charToString (s[2]));
|
||||
expect (s.substring (0, 1) == String::charToString (s[0]));
|
||||
expect (s.getLastCharacter() == s [s.length() - 1]);
|
||||
expect (String::charToString (s.getLastCharacter()) == s.getLastCharacters (1));
|
||||
expect (s.substring (0, 3) == L"012");
|
||||
expect (s.substring (0, 100) == s);
|
||||
expect (s.substring (-1, 100) == s);
|
||||
expect (s.substring (3) == "345678");
|
||||
expect (s.indexOf (L"45") == 4);
|
||||
expect (String ("444445").indexOf ("45") == 4);
|
||||
expect (String ("444445").lastIndexOfChar ('4') == 4);
|
||||
expect (String ("45454545x").lastIndexOf (L"45") == 6);
|
||||
expect (String ("45454545x").lastIndexOfAnyOf ("456") == 7);
|
||||
expect (String ("45454545x").lastIndexOfAnyOf (L"456x") == 8);
|
||||
expect (String ("abABaBaBa").lastIndexOfIgnoreCase ("aB") == 6);
|
||||
expect (s.indexOfChar (L'4') == 4);
|
||||
expect (s + s == "012345678012345678");
|
||||
expect (s.startsWith (s));
|
||||
expect (s.startsWith (s.substring (0, 4)));
|
||||
expect (s.startsWith (s.dropLastCharacters (4)));
|
||||
expect (s.endsWith (s.substring (5)));
|
||||
expect (s.endsWith (s));
|
||||
expect (s.contains (s.substring (3, 6)));
|
||||
expect (s.contains (s.substring (3)));
|
||||
expect (s.startsWithChar (s[0]));
|
||||
expect (s.endsWithChar (s.getLastCharacter()));
|
||||
expect (s [s.length()] == 0);
|
||||
expect (String ("abcdEFGH").toLowerCase() == String ("abcdefgh"));
|
||||
expect (String ("abcdEFGH").toUpperCase() == String ("ABCDEFGH"));
|
||||
|
||||
String s2 ("123");
|
||||
s2 << ((int) 4) << ((short) 5) << "678" << L"9" << '0';
|
||||
s2 += "xyz";
|
||||
expect (s2 == "1234567890xyz");
|
||||
|
||||
beginTestCase ("Numeric conversions");
|
||||
expect (String::empty.getIntValue() == 0);
|
||||
expect (String::empty.getDoubleValue() == 0.0);
|
||||
expect (String::empty.getFloatValue() == 0.0f);
|
||||
expect (s.getIntValue() == 12345678);
|
||||
expect (s.getLargeIntValue() == (int64) 12345678);
|
||||
expect (s.getDoubleValue() == 12345678.0);
|
||||
expect (s.getFloatValue() == 12345678.0f);
|
||||
expect (String (-1234).getIntValue() == -1234);
|
||||
expect (String ((int64) -1234).getLargeIntValue() == -1234);
|
||||
expect (String (-1234.56).getDoubleValue() == -1234.56);
|
||||
expect (String (-1234.56f).getFloatValue() == -1234.56f);
|
||||
expect (String (std::numeric_limits<int>::max()).getIntValue() == std::numeric_limits<int>::max());
|
||||
expect (String (std::numeric_limits<int>::min()).getIntValue() == std::numeric_limits<int>::min());
|
||||
expect (String (std::numeric_limits<int64>::max()).getLargeIntValue() == std::numeric_limits<int64>::max());
|
||||
expect (String (std::numeric_limits<int64>::min()).getLargeIntValue() == std::numeric_limits<int64>::min());
|
||||
expect (("xyz" + s).getTrailingIntValue() == s.getIntValue());
|
||||
expect (s.getHexValue32() == 0x12345678);
|
||||
expect (s.getHexValue64() == (int64) 0x12345678);
|
||||
expect (String::toHexString (0x1234abcd).equalsIgnoreCase ("1234abcd"));
|
||||
expect (String::toHexString ((int64) 0x1234abcd).equalsIgnoreCase ("1234abcd"));
|
||||
expect (String::toHexString ((short) 0x12ab).equalsIgnoreCase ("12ab"));
|
||||
|
||||
expectEquals (String (int (0)), "0");
|
||||
expectEquals (String (short (0)), "0");
|
||||
expectEquals (String (int64 (0)), "0");
|
||||
expectEquals (String ((unsigned int) 0), "0");
|
||||
expectEquals (String ((unsigned short) 0), "0");
|
||||
expectEquals (String (uint64 (0)), "0");
|
||||
|
||||
expectEquals (String (int (-1)), "-1");
|
||||
expectEquals (String (short (-1)), "-1");
|
||||
expectEquals (String (int64 (-1)), "-1");
|
||||
|
||||
expectEquals (String (int (1)), "1");
|
||||
expectEquals (String (short (1)), "1");
|
||||
expectEquals (String (int64 (1)), "1");
|
||||
expectEquals (String ((unsigned int) 1), "1");
|
||||
expectEquals (String ((unsigned short) 1), "1");
|
||||
expectEquals (String (uint64 (1)), "1");
|
||||
|
||||
unsigned char data[] = { 1, 2, 3, 4, 0xa, 0xb, 0xc, 0xd };
|
||||
expect (String::toHexString (data, 8, 0).equalsIgnoreCase ("010203040a0b0c0d"));
|
||||
expect (String::toHexString (data, 8, 1).equalsIgnoreCase ("01 02 03 04 0a 0b 0c 0d"));
|
||||
expect (String::toHexString (data, 8, 2).equalsIgnoreCase ("0102 0304 0a0b 0c0d"));
|
||||
|
||||
beginTestCase ("Subsections");
|
||||
String s3;
|
||||
s3 = "abcdeFGHIJ";
|
||||
expect (s3.equalsIgnoreCase ("ABCdeFGhiJ"));
|
||||
expect (s3.compareIgnoreCase (L"ABCdeFGhiJ") == 0);
|
||||
expect (s3.containsIgnoreCase (s3.substring (3)));
|
||||
expect (s3.indexOfAnyOf ("xyzf", 2, true) == 5);
|
||||
expect (s3.indexOfAnyOf (L"xyzf", 2, false) == -1);
|
||||
expect (s3.indexOfAnyOf ("xyzF", 2, false) == 5);
|
||||
expect (s3.containsAnyOf (L"zzzFs"));
|
||||
expect (s3.startsWith ("abcd"));
|
||||
expect (s3.startsWithIgnoreCase (L"abCD"));
|
||||
expect (s3.startsWith (String::empty));
|
||||
expect (s3.startsWithChar ('a'));
|
||||
expect (s3.endsWith (String ("HIJ")));
|
||||
expect (s3.endsWithIgnoreCase (L"Hij"));
|
||||
expect (s3.endsWith (String::empty));
|
||||
expect (s3.endsWithChar (L'J'));
|
||||
expect (s3.indexOf ("HIJ") == 7);
|
||||
expect (s3.indexOf (L"HIJK") == -1);
|
||||
expect (s3.indexOfIgnoreCase ("hij") == 7);
|
||||
expect (s3.indexOfIgnoreCase (L"hijk") == -1);
|
||||
expect (s3.toStdString() == s3.toRawUTF8());
|
||||
|
||||
String s4 (s3);
|
||||
s4.append (String ("xyz123"), 3);
|
||||
expect (s4 == s3 + "xyz");
|
||||
|
||||
expect (String (1234) < String (1235));
|
||||
expect (String (1235) > String (1234));
|
||||
expect (String (1234) >= String (1234));
|
||||
expect (String (1234) <= String (1234));
|
||||
expect (String (1235) >= String (1234));
|
||||
expect (String (1234) <= String (1235));
|
||||
|
||||
String s5 ("word word2 word3");
|
||||
expect (s5.containsWholeWord (String ("word2")));
|
||||
expect (s5.indexOfWholeWord ("word2") == 5);
|
||||
expect (s5.containsWholeWord (L"word"));
|
||||
expect (s5.containsWholeWord ("word3"));
|
||||
expect (s5.containsWholeWord (s5));
|
||||
expect (s5.containsWholeWordIgnoreCase (L"Word2"));
|
||||
expect (s5.indexOfWholeWordIgnoreCase ("Word2") == 5);
|
||||
expect (s5.containsWholeWordIgnoreCase (L"Word"));
|
||||
expect (s5.containsWholeWordIgnoreCase ("Word3"));
|
||||
expect (! s5.containsWholeWordIgnoreCase (L"Wordx"));
|
||||
expect (! s5.containsWholeWordIgnoreCase ("xWord2"));
|
||||
expect (s5.containsNonWhitespaceChars());
|
||||
expect (s5.containsOnly ("ordw23 "));
|
||||
expect (! String (" \n\r\t").containsNonWhitespaceChars());
|
||||
|
||||
expect (s5.matchesWildcard (L"wor*", false));
|
||||
expect (s5.matchesWildcard ("wOr*", true));
|
||||
expect (s5.matchesWildcard (L"*word3", true));
|
||||
expect (s5.matchesWildcard ("*word?", true));
|
||||
expect (s5.matchesWildcard (L"Word*3", true));
|
||||
expect (! s5.matchesWildcard (L"*34", true));
|
||||
expect (String ("xx**y").matchesWildcard ("*y", true));
|
||||
expect (String ("xx**y").matchesWildcard ("x*y", true));
|
||||
expect (String ("xx**y").matchesWildcard ("xx*y", true));
|
||||
expect (String ("xx**y").matchesWildcard ("xx*", true));
|
||||
expect (String ("xx?y").matchesWildcard ("x??y", true));
|
||||
expect (String ("xx?y").matchesWildcard ("xx?y", true));
|
||||
expect (! String ("xx?y").matchesWildcard ("xx?y?", true));
|
||||
expect (String ("xx?y").matchesWildcard ("xx??", true));
|
||||
|
||||
expectEquals (s5.fromFirstOccurrenceOf (String::empty, true, false), s5);
|
||||
expectEquals (s5.fromFirstOccurrenceOf ("xword2", true, false), s5.substring (100));
|
||||
expectEquals (s5.fromFirstOccurrenceOf (L"word2", true, false), s5.substring (5));
|
||||
expectEquals (s5.fromFirstOccurrenceOf ("Word2", true, true), s5.substring (5));
|
||||
expectEquals (s5.fromFirstOccurrenceOf ("word2", false, false), s5.getLastCharacters (6));
|
||||
expectEquals (s5.fromFirstOccurrenceOf (L"Word2", false, true), s5.getLastCharacters (6));
|
||||
|
||||
expectEquals (s5.fromLastOccurrenceOf (String::empty, true, false), s5);
|
||||
expectEquals (s5.fromLastOccurrenceOf (L"wordx", true, false), s5);
|
||||
expectEquals (s5.fromLastOccurrenceOf ("word", true, false), s5.getLastCharacters (5));
|
||||
expectEquals (s5.fromLastOccurrenceOf (L"worD", true, true), s5.getLastCharacters (5));
|
||||
expectEquals (s5.fromLastOccurrenceOf ("word", false, false), s5.getLastCharacters (1));
|
||||
expectEquals (s5.fromLastOccurrenceOf (L"worD", false, true), s5.getLastCharacters (1));
|
||||
|
||||
expect (s5.upToFirstOccurrenceOf (String::empty, true, false).isEmpty());
|
||||
expectEquals (s5.upToFirstOccurrenceOf ("word4", true, false), s5);
|
||||
expectEquals (s5.upToFirstOccurrenceOf (L"word2", true, false), s5.substring (0, 10));
|
||||
expectEquals (s5.upToFirstOccurrenceOf ("Word2", true, true), s5.substring (0, 10));
|
||||
expectEquals (s5.upToFirstOccurrenceOf (L"word2", false, false), s5.substring (0, 5));
|
||||
expectEquals (s5.upToFirstOccurrenceOf ("Word2", false, true), s5.substring (0, 5));
|
||||
|
||||
expectEquals (s5.upToLastOccurrenceOf (String::empty, true, false), s5);
|
||||
expectEquals (s5.upToLastOccurrenceOf ("zword", true, false), s5);
|
||||
expectEquals (s5.upToLastOccurrenceOf ("word", true, false), s5.dropLastCharacters (1));
|
||||
expectEquals (s5.dropLastCharacters(1).upToLastOccurrenceOf ("word", true, false), s5.dropLastCharacters (1));
|
||||
expectEquals (s5.upToLastOccurrenceOf ("Word", true, true), s5.dropLastCharacters (1));
|
||||
expectEquals (s5.upToLastOccurrenceOf ("word", false, false), s5.dropLastCharacters (5));
|
||||
expectEquals (s5.upToLastOccurrenceOf ("Word", false, true), s5.dropLastCharacters (5));
|
||||
|
||||
expectEquals (s5.replace ("word", L"xyz", false), String ("xyz xyz2 xyz3"));
|
||||
expect (s5.replace (L"Word", "xyz", true) == "xyz xyz2 xyz3");
|
||||
expect (s5.dropLastCharacters (1).replace ("Word", String ("xyz"), true) == L"xyz xyz2 xyz");
|
||||
expect (s5.replace ("Word", "", true) == " 2 3");
|
||||
expectEquals (s5.replace ("Word2", L"xyz", true), String ("word xyz word3"));
|
||||
expect (s5.replaceCharacter (L'w', 'x') != s5);
|
||||
expectEquals (s5.replaceCharacter ('w', L'x').replaceCharacter ('x', 'w'), s5);
|
||||
expect (s5.replaceCharacters ("wo", "xy") != s5);
|
||||
expectEquals (s5.replaceCharacters ("wo", "xy").replaceCharacters ("xy", L"wo"), s5);
|
||||
expectEquals (s5.retainCharacters ("1wordxya"), String ("wordwordword"));
|
||||
expect (s5.retainCharacters (String::empty).isEmpty());
|
||||
expect (s5.removeCharacters ("1wordxya") == " 2 3");
|
||||
expectEquals (s5.removeCharacters (String::empty), s5);
|
||||
expect (s5.initialSectionContainingOnly ("word") == L"word");
|
||||
expect (String ("word").initialSectionContainingOnly ("word") == L"word");
|
||||
expectEquals (s5.initialSectionNotContaining (String ("xyz ")), String ("word"));
|
||||
expectEquals (s5.initialSectionNotContaining (String (";[:'/")), s5);
|
||||
expect (! s5.isQuotedString());
|
||||
expect (s5.quoted().isQuotedString());
|
||||
expect (! s5.quoted().unquoted().isQuotedString());
|
||||
expect (! String ("x'").isQuotedString());
|
||||
expect (String ("'x").isQuotedString());
|
||||
|
||||
String s6 (" \t xyz \t\r\n");
|
||||
expectEquals (s6.trim(), String ("xyz"));
|
||||
expect (s6.trim().trim() == "xyz");
|
||||
expectEquals (s5.trim(), s5);
|
||||
expectEquals (s6.trimStart().trimEnd(), s6.trim());
|
||||
expectEquals (s6.trimStart().trimEnd(), s6.trimEnd().trimStart());
|
||||
expectEquals (s6.trimStart().trimStart().trimEnd().trimEnd(), s6.trimEnd().trimStart());
|
||||
expect (s6.trimStart() != s6.trimEnd());
|
||||
expectEquals (("\t\r\n " + s6 + "\t\n \r").trim(), s6.trim());
|
||||
expect (String::repeatedString ("xyz", 3) == L"xyzxyzxyz");
|
||||
}
|
||||
|
||||
{
|
||||
beginTestCase ("UTF conversions");
|
||||
|
||||
TestUTFConversion <CharPointer_UTF32>::test (*this);
|
||||
TestUTFConversion <CharPointer_UTF8>::test (*this);
|
||||
TestUTFConversion <CharPointer_UTF16>::test (*this);
|
||||
}
|
||||
|
||||
{
|
||||
beginTestCase ("StringArray");
|
||||
|
||||
StringArray s;
|
||||
s.addTokens ("4,3,2,1,0", ";,", "x");
|
||||
expectEquals (s.size(), 5);
|
||||
|
||||
expectEquals (s.joinIntoString ("-"), String ("4-3-2-1-0"));
|
||||
s.remove (2);
|
||||
expectEquals (s.joinIntoString ("--"), String ("4--3--1--0"));
|
||||
expectEquals (s.joinIntoString (String::empty), String ("4310"));
|
||||
s.clear();
|
||||
expectEquals (s.joinIntoString ("x"), String::empty);
|
||||
|
||||
StringArray toks;
|
||||
toks.addTokens ("x,,", ";,", "");
|
||||
expectEquals (toks.size(), 3);
|
||||
expectEquals (toks.joinIntoString ("-"), String ("x--"));
|
||||
toks.clear();
|
||||
|
||||
toks.addTokens (",x,", ";,", "");
|
||||
expectEquals (toks.size(), 3);
|
||||
expectEquals (toks.joinIntoString ("-"), String ("-x-"));
|
||||
toks.clear();
|
||||
|
||||
toks.addTokens ("x,'y,z',", ";,", "'");
|
||||
expectEquals (toks.size(), 3);
|
||||
expectEquals (toks.joinIntoString ("-"), String ("x-'y,z'-"));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static StringTests stringUnitTests;
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user