1//========================================================================
2//
3// check_cidfontswidthsbuilder.cpp
4//
5// This file is licensed under the GPLv2 or later
6//
7// Copyright 2023 g10 Code GmbH, Author: Sune Stolborg Vuorela <sune@vuorela.dk>
8//========================================================================
9
10#include "CIDFontsWidthsBuilder.h"
11
12#include <QtTest/QTest>
13
14class TestCIDFontsWidthsBuilder : public QObject
15{
16 Q_OBJECT
17public:
18 using QObject::QObject;
19private Q_SLOTS:
20 void testEmpty();
21 void testSingle();
22 void testSimpleSequence();
23};
24
25void TestCIDFontsWidthsBuilder::testEmpty()
26{
27 CIDFontsWidthsBuilder b;
28 auto segments = b.takeSegments();
29 QCOMPARE(segments.size(), 0);
30}
31
32static bool compare(const CIDFontsWidthsBuilder::Segment &segment1, const CIDFontsWidthsBuilder::Segment &segment2)
33{
34 return std::visit(
35 visitor: [](const auto &s1, const auto &s2) {
36 using T1 = std::decay_t<decltype(s1)>;
37 using T2 = std::decay_t<decltype(s2)>;
38 if constexpr (!std::is_same_v<T1, T2>) {
39 return false;
40 } else if constexpr (std::is_same_v<T1, CIDFontsWidthsBuilder::ListSegment>) {
41 return s1.first == s2.first && s1.widths == s2.widths;
42 } else if constexpr (std::is_same_v<T1, CIDFontsWidthsBuilder::RangeSegment>) {
43 return s1.first == s2.first && s1.last == s2.last && s1.width == s2.width;
44 } else {
45 return false;
46 }
47 },
48 variants: segment1, variants: segment2);
49}
50
51void TestCIDFontsWidthsBuilder::testSingle()
52{
53 CIDFontsWidthsBuilder b;
54 b.addWidth(index: 0, width: 10);
55 auto segments = b.takeSegments();
56 QCOMPARE(segments.size(), 1);
57 auto segment0 = CIDFontsWidthsBuilder::ListSegment { .first: 0, .widths: { 10 } };
58 QVERIFY(compare(segments[0], segment0));
59}
60
61void TestCIDFontsWidthsBuilder::testSimpleSequence()
62{
63 CIDFontsWidthsBuilder b;
64 for (int i = 0; i < 2; i++) { // repeat to verify that takeSegments resets
65 b.addWidth(index: 0, width: 10);
66 b.addWidth(index: 1, width: 10);
67 b.addWidth(index: 2, width: 10);
68 b.addWidth(index: 3, width: 10);
69 b.addWidth(index: 4, width: 10);
70 b.addWidth(index: 5, width: 20);
71 b.addWidth(index: 6, width: 21);
72 b.addWidth(index: 7, width: 21);
73 b.addWidth(index: 8, width: 20);
74 b.addWidth(index: 9, width: 10);
75 b.addWidth(index: 10, width: 10);
76 b.addWidth(index: 11, width: 10);
77 b.addWidth(index: 12, width: 10);
78 b.addWidth(index: 13, width: 10);
79 b.addWidth(index: 14, width: 20);
80 b.addWidth(index: 15, width: 21);
81 b.addWidth(index: 16, width: 21);
82 b.addWidth(index: 17, width: 20);
83 b.addWidth(index: 19, width: 20);
84 auto segments = b.takeSegments();
85 QCOMPARE(segments.size(), 5);
86 auto segment0 = CIDFontsWidthsBuilder::RangeSegment { .first: 0, .last: 4, .width: 10 };
87 QVERIFY(compare(segments[0], segment0));
88 auto segment1 = CIDFontsWidthsBuilder::ListSegment { .first: 5, .widths: { 20, 21, 21, 20 } };
89 QVERIFY(compare(segments[1], segment1));
90 auto segment2 = CIDFontsWidthsBuilder::RangeSegment { .first: 9, .last: 13, .width: 10 };
91 QVERIFY(compare(segments[2], segment2));
92 auto segment3 = CIDFontsWidthsBuilder::ListSegment { .first: 14, .widths: { 20, 21, 21, 20 } };
93 QVERIFY(compare(segments[3], segment3));
94 auto segment4 = CIDFontsWidthsBuilder::ListSegment { .first: 19, .widths: { 20 } };
95 QVERIFY(compare(segments[4], segment4));
96 }
97}
98
99QTEST_GUILESS_MAIN(TestCIDFontsWidthsBuilder);
100#include "check_cidfontswidthsbuilder.moc"
101

source code of poppler/qt6/tests/check_cidfontswidthsbuilder.cpp