aboutsummaryrefslogtreecommitdiffstats
path: root/libtests
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
parent5911a348a5ae9065610be5fe5f251cab399a52b8 (diff)
downloadqpdf-b0f054e600bd07f540ac0a081dd8b5a8cc561617.tar.zst
Add ability to initialize Pl_Function with a C-style function
Diffstat (limited to 'libtests')
-rw-r--r--libtests/pl_function.cc59
-rw-r--r--libtests/qtest/pl_function/exp9
2 files changed, 68 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;
}
diff --git a/libtests/qtest/pl_function/exp b/libtests/qtest/pl_function/exp
index fd2ea1bc..930240d9 100644
--- a/libtests/qtest/pl_function/exp
+++ b/libtests/qtest/pl_function/exp
@@ -1,2 +1,11 @@
p1: 6: potato
p2: 5: salad
+got one(3)
+got two(3)
+got three(5)
+three threw c-function function returned code 1
+got four(4)
+signed got potato(6)
+signed got salad(5)
+salad threw c-function function returned code 2
+signed got quack(5)