aboutsummaryrefslogtreecommitdiffstats
path: root/libtests/pl_function.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2022-09-08 22:38:36 +0200
committerJay Berkenbilt <ejb@ql.org>2022-09-08 23:35:27 +0200
commitb0f054e600bd07f540ac0a081dd8b5a8cc561617 (patch)
tree5e539e77e5b2fc3f54e1380eb2b9877662bc43ef /libtests/pl_function.cc
parent5911a348a5ae9065610be5fe5f251cab399a52b8 (diff)
downloadqpdf-b0f054e600bd07f540ac0a081dd8b5a8cc561617.tar.zst
Add ability to initialize Pl_Function with a C-style function
Diffstat (limited to 'libtests/pl_function.cc')
-rw-r--r--libtests/pl_function.cc59
1 files changed, 59 insertions, 0 deletions
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 <qpdf/Pl_String.hh>
#include <iostream>
+namespace
+{
+ struct Count
+ {
+ int count{0};
+ };
+} // namespace
+
+int
+f(unsigned char const* data, size_t len, void* udata)
+{
+ auto c = reinterpret_cast<Count*>(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<Count*>(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;
}