2022-05-03 12:21:01 +00:00
|
|
|
#include <qpdf/assert_test.h>
|
|
|
|
|
2024-02-17 16:01:48 +00:00
|
|
|
#include <qpdf/QPDF.hh>
|
2023-03-28 09:25:10 +00:00
|
|
|
#include <qpdf/QPDFObjectHandle.hh>
|
2023-03-29 18:52:03 +00:00
|
|
|
#include <qpdf/QPDFObject_private.hh>
|
2023-03-28 09:25:10 +00:00
|
|
|
#include <qpdf/QPDF_Array.hh>
|
2024-02-17 16:01:48 +00:00
|
|
|
|
2019-08-17 22:54:24 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
int
|
|
|
|
main()
|
|
|
|
{
|
2023-03-28 09:25:10 +00:00
|
|
|
auto obj = QPDF_Array::create({}, true);
|
|
|
|
QPDF_Array& a = *obj->as<QPDF_Array>();
|
|
|
|
|
2019-08-17 22:54:24 +00:00
|
|
|
assert(a.size() == 0);
|
|
|
|
|
2023-03-28 09:25:10 +00:00
|
|
|
a.push_back(QPDFObjectHandle::parse("1"));
|
|
|
|
a.push_back(QPDFObjectHandle::parse("(potato)"));
|
|
|
|
a.push_back(QPDFObjectHandle::parse("null"));
|
|
|
|
a.push_back(QPDFObjectHandle::parse("null"));
|
|
|
|
a.push_back(QPDFObjectHandle::parse("/Quack"));
|
2019-08-17 22:54:24 +00:00
|
|
|
assert(a.size() == 5);
|
2024-03-17 17:14:47 +00:00
|
|
|
assert(a.at(0).second.isInteger() && (a.at(0).second.getIntValue() == 1));
|
|
|
|
assert(a.at(1).second.isString() && (a.at(1).second.getStringValue() == "potato"));
|
|
|
|
assert(a.at(2).second.isNull());
|
|
|
|
assert(a.at(3).second.isNull());
|
|
|
|
assert(a.at(4).second.isName() && (a.at(4).second.getName() == "/Quack"));
|
2019-08-17 22:54:24 +00:00
|
|
|
|
|
|
|
a.insert(4, QPDFObjectHandle::parse("/BeforeQuack"));
|
|
|
|
assert(a.size() == 6);
|
2024-03-17 17:14:47 +00:00
|
|
|
assert(a.at(0).second.isInteger() && (a.at(0).second.getIntValue() == 1));
|
|
|
|
assert(a.at(4).second.isName() && (a.at(4).second.getName() == "/BeforeQuack"));
|
|
|
|
assert(a.at(5).second.isName() && (a.at(5).second.getName() == "/Quack"));
|
2019-08-17 22:54:24 +00:00
|
|
|
|
|
|
|
a.insert(2, QPDFObjectHandle::parse("/Third"));
|
|
|
|
assert(a.size() == 7);
|
2024-03-17 17:14:47 +00:00
|
|
|
assert(a.at(1).second.isString() && (a.at(1).second.getStringValue() == "potato"));
|
|
|
|
assert(a.at(2).second.isName() && (a.at(2).second.getName() == "/Third"));
|
|
|
|
assert(a.at(3).second.isNull());
|
|
|
|
assert(a.at(6).second.isName() && (a.at(6).second.getName() == "/Quack"));
|
2019-08-17 22:54:24 +00:00
|
|
|
|
|
|
|
a.insert(0, QPDFObjectHandle::parse("/First"));
|
|
|
|
assert(a.size() == 8);
|
2024-03-17 17:14:47 +00:00
|
|
|
assert(a.at(0).second.isName() && (a.at(0).second.getName() == "/First"));
|
|
|
|
assert(a.at(1).second.isInteger() && (a.at(1).second.getIntValue() == 1));
|
|
|
|
assert(a.at(7).second.isName() && (a.at(7).second.getName() == "/Quack"));
|
2019-08-17 22:54:24 +00:00
|
|
|
|
|
|
|
a.erase(6);
|
|
|
|
assert(a.size() == 7);
|
2024-03-17 17:14:47 +00:00
|
|
|
assert(a.at(0).second.isName() && (a.at(0).second.getName() == "/First"));
|
|
|
|
assert(a.at(1).second.isInteger() && (a.at(1).second.getIntValue() == 1));
|
|
|
|
assert(a.at(5).second.isNull());
|
|
|
|
assert(a.at(6).second.isName() && (a.at(6).second.getName() == "/Quack"));
|
2019-08-17 22:54:24 +00:00
|
|
|
|
|
|
|
a.erase(6);
|
|
|
|
assert(a.size() == 6);
|
2024-03-17 17:14:47 +00:00
|
|
|
assert(a.at(0).second.isName() && (a.at(0).second.getName() == "/First"));
|
|
|
|
assert(a.at(1).second.isInteger() && (a.at(1).second.getIntValue() == 1));
|
|
|
|
assert(a.at(3).second.isName() && (a.at(3).second.getName() == "/Third"));
|
|
|
|
assert(a.at(4).second.isNull());
|
|
|
|
assert(a.at(5).second.isNull());
|
2019-08-17 22:54:24 +00:00
|
|
|
|
|
|
|
a.setAt(4, QPDFObjectHandle::parse("12"));
|
2024-03-17 17:14:47 +00:00
|
|
|
assert(a.at(4).second.isInteger() && (a.at(4).second.getIntValue() == 12));
|
2019-08-17 22:54:24 +00:00
|
|
|
a.setAt(4, QPDFObjectHandle::newNull());
|
2024-03-17 17:14:47 +00:00
|
|
|
assert(a.at(4).second.isNull());
|
2019-08-17 22:54:24 +00:00
|
|
|
|
2023-03-28 09:25:10 +00:00
|
|
|
a.erase(a.size() - 1);
|
2019-08-17 22:54:24 +00:00
|
|
|
assert(a.size() == 5);
|
2024-03-17 17:14:47 +00:00
|
|
|
assert(a.at(0).second.isName() && (a.at(0).second.getName() == "/First"));
|
|
|
|
assert(a.at(1).second.isInteger() && (a.at(1).second.getIntValue() == 1));
|
|
|
|
assert(a.at(3).second.isName() && (a.at(3).second.getName() == "/Third"));
|
|
|
|
assert(a.at(4).second.isNull());
|
2019-08-17 22:54:24 +00:00
|
|
|
|
2023-03-28 09:25:10 +00:00
|
|
|
a.erase(a.size() - 1);
|
2019-08-17 22:54:24 +00:00
|
|
|
assert(a.size() == 4);
|
2024-03-17 17:14:47 +00:00
|
|
|
assert(a.at(0).second.isName() && (a.at(0).second.getName() == "/First"));
|
|
|
|
assert(a.at(1).second.isInteger() && (a.at(1).second.getIntValue() == 1));
|
|
|
|
assert(a.at(3).second.isName() && (a.at(3).second.getName() == "/Third"));
|
2019-08-17 22:54:24 +00:00
|
|
|
|
2023-03-28 09:25:10 +00:00
|
|
|
a.erase(a.size() - 1);
|
2019-08-17 22:54:24 +00:00
|
|
|
assert(a.size() == 3);
|
2024-03-17 17:14:47 +00:00
|
|
|
assert(a.at(0).second.isName() && (a.at(0).second.getName() == "/First"));
|
|
|
|
assert(a.at(1).second.isInteger() && (a.at(1).second.getIntValue() == 1));
|
|
|
|
assert(a.at(2).second.isString() && (a.at(2).second.getStringValue() == "potato"));
|
2019-08-17 22:54:24 +00:00
|
|
|
|
2024-02-17 16:01:48 +00:00
|
|
|
QPDF pdf;
|
|
|
|
pdf.emptyPDF();
|
|
|
|
|
|
|
|
obj = QPDF_Array::create({10, "null"_qpdf.getObj()}, true);
|
|
|
|
QPDF_Array& b = *obj->as<QPDF_Array>();
|
2024-03-08 16:57:56 +00:00
|
|
|
b.setAt(5, pdf.newIndirectNull());
|
2024-02-17 16:01:48 +00:00
|
|
|
b.setAt(7, "[0 1 2 3]"_qpdf);
|
2024-03-17 17:14:47 +00:00
|
|
|
assert(b.at(3).second.isNull());
|
|
|
|
assert(b.at(8).second.isNull());
|
|
|
|
assert(b.at(5).second.isIndirect());
|
2024-03-08 16:57:56 +00:00
|
|
|
assert(b.unparse() == "[ null null null null null 3 0 R null [ 0 1 2 3 ] null null ]");
|
2024-02-17 16:01:48 +00:00
|
|
|
auto c = b.copy(true);
|
|
|
|
auto d = b.copy(false);
|
2024-03-17 17:14:47 +00:00
|
|
|
b.at(7).second.setArrayItem(2, "42"_qpdf);
|
2024-03-08 16:57:56 +00:00
|
|
|
assert(c->unparse() == "[ null null null null null 3 0 R null [ 0 1 42 3 ] null null ]");
|
|
|
|
assert(d->unparse() == "[ null null null null null 3 0 R null [ 0 1 2 3 ] null null ]");
|
2024-02-17 16:01:48 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
b.setAt(3, {});
|
|
|
|
std::cout << "inserted uninitialized object\n";
|
|
|
|
} catch (std::logic_error&) {
|
|
|
|
}
|
|
|
|
QPDF pdf2;
|
|
|
|
pdf2.emptyPDF();
|
|
|
|
try {
|
|
|
|
pdf.makeIndirectObject(obj);
|
|
|
|
b.setAt(3, pdf2.getObject(1, 0));
|
|
|
|
std::cout << "inserted uninitialized object\n";
|
|
|
|
} catch (std::logic_error&) {
|
|
|
|
}
|
|
|
|
|
2019-08-17 22:54:24 +00:00
|
|
|
std::cout << "sparse array tests done" << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|