aboutsummaryrefslogtreecommitdiff
path: root/src/test/util_tests.cpp
diff options
context:
space:
mode:
authorSebastian Falbesoner <sebastian.falbesoner@gmail.com>2019-09-25 09:10:28 +0200
committerPieter Wuille <pieter.wuille@gmail.com>2019-10-09 18:11:14 -0700
commitbb36372b8f2bd675313ae8553ceb61f28c2c1afd (patch)
tree497baeb35a5451bd5954e711413d0e8e0ecde721 /src/test/util_tests.cpp
parent5e69aeec3f2a0fafd5e591b7222716f00145761d (diff)
test: add unit tests for Span-parsing helpers
tests the following four functions: - Const() [parse constant] - Func() [parse function] - Expr() [parse expression] - Split() [split up a string]
Diffstat (limited to 'src/test/util_tests.cpp')
-rw-r--r--src/test/util_tests.cpp124
1 files changed, 124 insertions, 0 deletions
diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp
index d0cd4b0a03..31a66b6fa9 100644
--- a/src/test/util_tests.cpp
+++ b/src/test/util_tests.cpp
@@ -12,6 +12,7 @@
#include <util/strencodings.h>
#include <util/string.h>
#include <util/time.h>
+#include <util/spanparsing.h>
#include <stdint.h>
#include <thread>
@@ -1572,4 +1573,127 @@ BOOST_AUTO_TEST_CASE(test_Capitalize)
BOOST_CHECK_EQUAL(Capitalize("\x00\xfe\xff"), "\x00\xfe\xff");
}
+static std::string SpanToStr(Span<const char>& span)
+{
+ return std::string(span.begin(), span.end());
+}
+
+BOOST_AUTO_TEST_CASE(test_spanparsing)
+{
+ using namespace spanparsing;
+ std::string input;
+ Span<const char> sp;
+ bool success;
+
+ // Const(...): parse a constant, update span to skip it if successful
+ input = "MilkToastHoney";
+ sp = MakeSpan(input);
+ success = Const("", sp); // empty
+ BOOST_CHECK(success);
+ BOOST_CHECK_EQUAL(SpanToStr(sp), "MilkToastHoney");
+
+ success = Const("Milk", sp);
+ BOOST_CHECK(success);
+ BOOST_CHECK_EQUAL(SpanToStr(sp), "ToastHoney");
+
+ success = Const("Bread", sp);
+ BOOST_CHECK(!success);
+
+ success = Const("Toast", sp);
+ BOOST_CHECK(success);
+ BOOST_CHECK_EQUAL(SpanToStr(sp), "Honey");
+
+ success = Const("Honeybadger", sp);
+ BOOST_CHECK(!success);
+
+ success = Const("Honey", sp);
+ BOOST_CHECK(success);
+ BOOST_CHECK_EQUAL(SpanToStr(sp), "");
+
+ // Func(...): parse a function call, update span to argument if successful
+ input = "Foo(Bar(xy,z()))";
+ sp = MakeSpan(input);
+
+ success = Func("FooBar", sp);
+ BOOST_CHECK(!success);
+
+ success = Func("Foo(", sp);
+ BOOST_CHECK(!success);
+
+ success = Func("Foo", sp);
+ BOOST_CHECK(success);
+ BOOST_CHECK_EQUAL(SpanToStr(sp), "Bar(xy,z())");
+
+ success = Func("Bar", sp);
+ BOOST_CHECK(success);
+ BOOST_CHECK_EQUAL(SpanToStr(sp), "xy,z()");
+
+ success = Func("xy", sp);
+ BOOST_CHECK(!success);
+
+ // Expr(...): return expression that span begins with, update span to skip it
+ Span<const char> result;
+
+ input = "(n*(n-1))/2";
+ sp = MakeSpan(input);
+ result = Expr(sp);
+ BOOST_CHECK_EQUAL(SpanToStr(result), "(n*(n-1))/2");
+ BOOST_CHECK_EQUAL(SpanToStr(sp), "");
+
+ input = "foo,bar";
+ sp = MakeSpan(input);
+ result = Expr(sp);
+ BOOST_CHECK_EQUAL(SpanToStr(result), "foo");
+ BOOST_CHECK_EQUAL(SpanToStr(sp), ",bar");
+
+ input = "(aaaaa,bbbbb()),c";
+ sp = MakeSpan(input);
+ result = Expr(sp);
+ BOOST_CHECK_EQUAL(SpanToStr(result), "(aaaaa,bbbbb())");
+ BOOST_CHECK_EQUAL(SpanToStr(sp), ",c");
+
+ input = "xyz)foo";
+ sp = MakeSpan(input);
+ result = Expr(sp);
+ BOOST_CHECK_EQUAL(SpanToStr(result), "xyz");
+ BOOST_CHECK_EQUAL(SpanToStr(sp), ")foo");
+
+ input = "((a),(b),(c)),xxx";
+ sp = MakeSpan(input);
+ result = Expr(sp);
+ BOOST_CHECK_EQUAL(SpanToStr(result), "((a),(b),(c))");
+ BOOST_CHECK_EQUAL(SpanToStr(sp), ",xxx");
+
+ // Split(...): split a string on every instance of sep, return vector
+ std::vector<Span<const char>> results;
+
+ input = "xxx";
+ results = Split(MakeSpan(input), 'x');
+ BOOST_CHECK_EQUAL(results.size(), 4);
+ BOOST_CHECK_EQUAL(SpanToStr(results[0]), "");
+ BOOST_CHECK_EQUAL(SpanToStr(results[1]), "");
+ BOOST_CHECK_EQUAL(SpanToStr(results[2]), "");
+ BOOST_CHECK_EQUAL(SpanToStr(results[3]), "");
+
+ input = "one#two#three";
+ results = Split(MakeSpan(input), '-');
+ BOOST_CHECK_EQUAL(results.size(), 1);
+ BOOST_CHECK_EQUAL(SpanToStr(results[0]), "one#two#three");
+
+ input = "one#two#three";
+ results = Split(MakeSpan(input), '#');
+ BOOST_CHECK_EQUAL(results.size(), 3);
+ BOOST_CHECK_EQUAL(SpanToStr(results[0]), "one");
+ BOOST_CHECK_EQUAL(SpanToStr(results[1]), "two");
+ BOOST_CHECK_EQUAL(SpanToStr(results[2]), "three");
+
+ input = "*foo*bar*";
+ results = Split(MakeSpan(input), '*');
+ BOOST_CHECK_EQUAL(results.size(), 4);
+ BOOST_CHECK_EQUAL(SpanToStr(results[0]), "");
+ BOOST_CHECK_EQUAL(SpanToStr(results[1]), "foo");
+ BOOST_CHECK_EQUAL(SpanToStr(results[2]), "bar");
+ BOOST_CHECK_EQUAL(SpanToStr(results[3]), "");
+}
+
BOOST_AUTO_TEST_SUITE_END()