From b0f054e600bd07f540ac0a081dd8b5a8cc561617 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Thu, 8 Sep 2022 16:38:36 -0400 Subject: Add ability to initialize Pl_Function with a C-style function --- libtests/pl_function.cc | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) (limited to 'libtests/pl_function.cc') diff --git a/libtests/pl_function.cc b/libtests/pl_function.cc index 101ec02e..2829671b 100644 --- a/libtests/pl_function.cc +++ b/libtests/pl_function.cc @@ -5,6 +5,38 @@ #include #include +namespace +{ + struct Count + { + int count{0}; + }; +} // namespace + +int +f(unsigned char const* data, size_t len, void* udata) +{ + auto c = reinterpret_cast(udata); + ++c->count; + std::cout << "got " << data << "(" << len << ")" << std::endl; + if (c->count == 3) { + return 1; + } + return 0; +} + +int +g(char const* data, size_t len, void* udata) +{ + auto c = reinterpret_cast(udata); + ++c->count; + std::cout << "signed got " << data << "(" << len << ")" << std::endl; + if (c->count == 2) { + return 2; + } + return 0; +} + int main(int argc, char* argv[]) { @@ -23,5 +55,32 @@ main(int argc, char* argv[]) p2.finish(); assert(s == "c2FsYWQ="); + Count c; + Pl_Function p3("c-function", nullptr, f, &c); + p3 << "one"; + p3 << "two"; + try { + p3 << "three"; + assert(false); + } catch (std::runtime_error& e) { + std::cout << "three threw " << e.what() << std::endl; + } + p3 << "four"; + p3.finish(); + assert(c.count == 4); + + c.count = 0; + Pl_Function p4("c-function", nullptr, g, &c); + p4 << "potato"; + try { + p4 << "salad"; + assert(false); + } catch (std::runtime_error& e) { + std::cout << "salad threw " << e.what() << std::endl; + } + p4 << "quack"; + p4.finish(); + assert(c.count == 3); + return 0; } -- cgit v1.2.3-54-g00ecf