File: reader_test.cpp

package info (click to toggle)
sight 25.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,252 kB
  • sloc: cpp: 310,629; xml: 17,622; ansic: 9,960; python: 1,379; sh: 144; makefile: 33
file content (149 lines) | stat: -rw-r--r-- 4,741 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/************************************************************************
 *
 * Copyright (C) 2023 IRCAD France
 *
 * This file is part of Sight.
 *
 * Sight 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.
 *
 * Sight 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 Sight. If not, see <https://www.gnu.org/licenses/>.
 *
 ***********************************************************************/

#include "reader_test.hpp"

#include <core/tools/system.hpp>
#include <core/tools/uuid.hpp>

#include <data/image.hpp>

#include <io/__/service/reader.hpp>
#include <io/bitmap/backend.hpp>
#include <io/bitmap/reader.hpp>

#include <service/op.hpp>

#include <utest_data/data.hpp>
#include <utest_data/generator/image.hpp>

// cspell:ignore nvjpeg sreader

// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION(sight::module::io::bitmap::ut::reader_test);

namespace sight::module::io::bitmap::ut
{

//------------------------------------------------------------------------------

inline static void runreader(
    const boost::property_tree::ptree& _config,
    sight::data::image::sptr _image,
    bool _should_fail = false
)
{
    service::base::sptr sreader = service::add("sight::module::io::bitmap::reader");
    CPPUNIT_ASSERT_MESSAGE(std::string("Failed to create service 'sight::module::io::bitmap::reader'"), sreader);
    sreader->set_inout(_image, "data");

    CPPUNIT_ASSERT_NO_THROW(sreader->set_config(_config));
    CPPUNIT_ASSERT_NO_THROW(sreader->configure());
    CPPUNIT_ASSERT_NO_THROW(sreader->start().get());
    CPPUNIT_ASSERT_NO_THROW(sreader->update().get());
    CPPUNIT_ASSERT_NO_THROW(sreader->stop().get());
    service::remove(sreader);

    // Check the result...
    CPPUNIT_ASSERT_EQUAL(
        _should_fail,
        std::dynamic_pointer_cast<sight::io::service::reader>(sreader)->has_failed()
    );
}

//------------------------------------------------------------------------------

inline static void test_enable(data::image::sptr _actual_image, bool _gpu_required)
{
    const std::array<std::string, 4> extensions = {".jp2", ".jpg", ".png", ".tiff"};

    for(const auto& ext : extensions)
    {
        const auto filename = "wild" + ext;
        const auto filepath = utest_data::dir() / "sight" / "image" / "bitmap" / filename;

        // Add file
        service::config_t config;
        config.add("file", filepath.string());
        config.add("gpu_required", _gpu_required);

        // Run the service
        runreader(config, _actual_image);

        // Only test if the image exists. Conformance tests are already done in the reader
        CPPUNIT_ASSERT(_actual_image);

        const auto& sizes = _actual_image->size();
        CPPUNIT_ASSERT(sizes[0] > 0 && sizes[1] > 0 && sizes[2] == 0);
    }
}

//------------------------------------------------------------------------------

void reader_test::setUp()
{
}

//------------------------------------------------------------------------------

void reader_test::tearDown()
{
}

//------------------------------------------------------------------------------

void reader_test::basic_test()
{
    const auto& filename = "wild" + sight::io::bitmap::extensions(sight::io::bitmap::backend::libtiff).front();
    const auto& filepath = utest_data::dir() / "sight" / "image" / "bitmap" / filename;

    service::config_t config;
    config.add("file", filepath.string());

    auto actual_image = std::make_shared<sight::data::image>();
    runreader(config, actual_image);

    // Only test if the image exists. Conformance tests are already done in the reader
    CPPUNIT_ASSERT(actual_image);

    const auto& sizes = actual_image->size();
    CPPUNIT_ASSERT(sizes[0] > 0 && sizes[1] > 0 && sizes[2] == 0);
}

//------------------------------------------------------------------------------

void reader_test::config_test()
{
    {
        auto actual_image = std::make_shared<sight::data::image>();
        test_enable(actual_image, false);
    }

    if(sight::io::bitmap::nv_jpeg())
    {
        auto actual_image = std::make_shared<sight::data::image>();
        test_enable(actual_image, true);
    }
}

//------------------------------------------------------------------------------

} // namespace sight::module::io::bitmap::ut