File: teststream.cpp

package info (click to toggle)
libopenraw 0.1.2-0.4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,484 kB
  • sloc: cpp: 13,907; sh: 4,090; ansic: 1,922; xml: 769; makefile: 676; perl: 42
file content (125 lines) | stat: -rw-r--r-- 2,886 bytes parent folder | download | duplicates (2)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * libopenraw - teststream.cpp
 *
 * Copyright (C) 2006-2016 Hubert Figuiere
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

/** @brief test the IO::Stream class */

#include <string.h>

#include <string>
#include <iostream>
#include <cstdlib>

#include <boost/test/minimal.hpp>

#include "stream.hpp"
#include "file.hpp"
#include "streamclone.hpp"

using namespace OpenRaw;

std::string g_testfile;

int test_main (int argc, char * argv[])
{
    if (argc == 1) {
        // no argument, lets run like we are in "check"
        const char * srcdir = getenv("srcdir");

        BOOST_ASSERT(srcdir != NULL);
        g_testfile = std::string(srcdir);
        g_testfile += "/io/testfile.tmp";
    }
    else {
        g_testfile = argv[1];
    }
    auto file = IO::Stream::Ptr(new IO::File(g_testfile.c_str()));
    char buf1[128];
    int ret = file->open();
    BOOST_CHECK(ret == 0);

    size_t r = file->read(buf1, 6);
    BOOST_CHECK(r == 6);

    BOOST_CHECK(memcmp(buf1, "abcdef", 6) == 0);

    off_t file_size = file->filesize();
    BOOST_CHECK(file_size == 63);

    const off_t clone_offset = 2;

    auto clone = new IO::StreamClone(file, clone_offset);

    ret = clone->open();
    BOOST_CHECK(ret == 0);

    BOOST_CHECK(clone->filesize() == (file_size - clone_offset));

    char buf2[128];
    r = clone->read(buf2, 4);
    BOOST_CHECK(r == 4);

    BOOST_CHECK(strncmp(buf1 + clone_offset, buf2, 4) == 0);

    uint8_t c = file->readByte();

    BOOST_CHECK(c == 'g');

    // seek

    int new_pos = clone->seek(0, SEEK_CUR);
    BOOST_CHECK(new_pos == 5);

    new_pos = clone->seek(1, SEEK_CUR);
    BOOST_CHECK(new_pos == 6);

    new_pos = clone->seek(2, SEEK_SET);
    BOOST_CHECK(new_pos == 2);

    c = file->readByte();
    BOOST_CHECK(c == 'e');

    new_pos = clone->seek(0, SEEK_CUR);
    BOOST_CHECK(new_pos == 3);

    c = file->readByte();
    BOOST_CHECK(c == 'f');

    new_pos = clone->seek(-2, SEEK_END);
    BOOST_CHECK(new_pos == 59);

    c = file->readByte();
    BOOST_CHECK(c == 'Z');


    clone->close();
    delete clone;
    file->close();
    return 0;
}

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0))
  indent-tabs-mode:nil
  fill-column:80
  End:
*/