aboutsummaryrefslogtreecommitdiffstats
path: root/libtests/input_source.cc
blob: 5a69f295751c0356eb1ce6917571eb74f4bdec80 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <qpdf/BufferInputSource.hh>
#include <qpdf/PointerHolder.hh>
#include <qpdf/Buffer.hh>
#include <qpdf/QPDFTokenizer.hh>
#include <iostream>
#include <cstring>

static PointerHolder<Buffer>
get_buffer()
{
    size_t size = 3172;
    PointerHolder<Buffer> b(new Buffer(size));
    unsigned char* p = b->getBuffer();
    for (size_t i = 0; i < size; ++i)
    {
        p[i] = static_cast<unsigned char>(i & 0xff);
    }
    return b;
}

class Finder: public InputSource::Finder
{
  public:
    Finder(PointerHolder<InputSource> is, std::string const& after) :
        is(is),
        after(after)
    {
    }
    virtual ~Finder()
    {
    }
    virtual bool check();

  private:
    PointerHolder<InputSource> is;
    std::string after;
};

bool
Finder::check()
{
    QPDFTokenizer tokenizer;
    QPDFTokenizer::Token t = tokenizer.readToken(is, "finder", true);
    if (t == QPDFTokenizer::Token(QPDFTokenizer::tt_word, "potato"))
    {
        t = tokenizer.readToken(is, "finder", true);
        return (t == QPDFTokenizer::Token(QPDFTokenizer::tt_word, after));
    }
    return false;
}

void check(char const* description, bool expected, bool actual)
{
    std::cout << description << ": "
              << ((actual == expected) ? "PASS" : "FAIL")
              << std::endl;
}

int main()
{
    PointerHolder<Buffer> b1 = get_buffer();
    unsigned char* b = b1->getBuffer();
    // Straddle block boundaries
    memcpy(b + 1022, "potato", 6);
    // Overlap so that the first check() would advance past the start
    // of the next match
    memcpy(b + 2037, "potato potato salad ", 20);
    PointerHolder<InputSource> is =
        new BufferInputSource("test buffer input source", b1.get());
    Finder f1(is, "salad");
    check("find potato salad", true,
          is->findFirst("potato", 0, 0, f1));
    check("barely find potato salad", true,
          is->findFirst("potato", 1100, 945, f1));
    check("barely find potato salad", true,
          is->findFirst("potato", 2000, 45, f1));
    check("potato salad is too late", false,
          is->findFirst("potato", 1100, 944, f1));
    check("potato salad is too late", false,
          is->findFirst("potato", 2000, 44, f1));
    check("potato salad not found", false,
          is->findFirst("potato", 2045, 0, f1));
    check("potato salad not found", false,
          is->findFirst("potato", 0, 1, f1));

    // Put one more right at EOF
    memcpy(b + b1->getSize() - 12, "potato salad", 12);
    check("potato salad at EOF", true,
          is->findFirst("potato", 3000, 0, f1));

    is->findFirst("potato", 0, 0, f1);
    check("findFirst found first", true,
          is->tell() == 2056);
    check("findLast found potato salad", true,
          is->findLast("potato", 0, 0, f1));
    check("findLast found at EOF", true,
          is->tell() == 3172);

    // Make check() bump into EOF
    memcpy(b + b1->getSize() - 6, "potato", 6);
    check("potato but not salad salad at EOF", false,
          is->findFirst("potato", 3000, 0, f1));
    check("findLast found potato salad", true,
          is->findLast("potato", 0, 0, f1));
    check("findLast found first one", true,
          is->tell() == 2056);

    return 0;
}