File: writer_test.cpp

package info (click to toggle)
sight 25.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 42,184 kB
  • sloc: cpp: 289,476; xml: 17,257; ansic: 9,878; python: 1,379; sh: 144; makefile: 33
file content (202 lines) | stat: -rw-r--r-- 8,459 bytes parent folder | download | duplicates (3)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/************************************************************************
 *
 * 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 "writer_test.hpp"

#include <core/com/slots.hpp>
#include <core/com/slots.hxx>
#include <core/os/temp_path.hpp>
#include <core/runtime/profile.hpp>
#include <core/runtime/runtime.hpp>
#include <core/tools/uuid.hpp>

#include <data/matrix_tl.hpp>

#include <io/__/service/writer.hpp>

#include <service/op.hpp>

#include <ui/__/preferences.hpp>

// cspell:ignore nvjpeg

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

namespace sight::module::io::matrix::ut
{

static const std::string EXPECTED =
    "1;1.0000000;0.0000000;0.0000000;0.0000000;0.0000000;1.0000000;0.0000000;0.0000000;0.0000000;0.0000000;1.0000000;0.0000000;0.0000000;0.0000000;0.0000000;1.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;\n"
    "2;2.0000000;0.0000000;0.0000000;0.0000000;0.0000000;1.0000000;0.0000000;0.0000000;0.0000000;0.0000000;1.0000000;0.0000000;0.0000000;0.0000000;0.0000000;1.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;\n"
    "3;3.0000000;0.0000000;0.0000000;0.0000000;0.0000000;1.0000000;0.0000000;0.0000000;0.0000000;0.0000000;1.0000000;0.0000000;0.0000000;0.0000000;0.0000000;1.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;0.0000000;\n";

static const std::shared_ptr<const sight::data::matrix_tl> SOURCE_TL =
    []
    {
        auto matrix_tl = std::make_shared<sight::data::matrix_tl>();
        matrix_tl->init_pool_size(4);

        const auto fill_tl =
            [&matrix_tl](const core::clock::type _timestamp)
            {
                auto data = matrix_tl->create_buffer(_timestamp);

                std::array<float, 16> matrix {
                    1., 0., 0., 0.,
                    0., 1., 0., 0.,
                    0., 0., 1., 0.,
                    0., 0., 0., 1.
                };

                matrix[0] = float(_timestamp);
                data->set_element(matrix, 0);
                matrix_tl->push_object(data);
            };

        fill_tl(1);
        fill_tl(2);
        fill_tl(3);

        return matrix_tl;
    }();

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

void writer_test::setUp()
{
    ui::preferences::set_enabled(true);
    ui::preferences::set_password_policy(core::crypto::password_keeper::password_policy::never);

    // Set the profile name
    const std::string& profile_name = core::tools::uuid::generate();
    core::runtime::get_current_profile()->set_name(profile_name);
}

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

void writer_test::tearDown()
{
}

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

void writer_test::basic_test()
{
    // Create a temporary directory
    core::os::temp_dir tmp_dir;

    // Create the service
    auto matrix_writer = service::add("sight::module::io::matrix::matrix_writer");
    CPPUNIT_ASSERT_MESSAGE("Failed to create service 'sight::module::io::matrix::matrix_writer'", matrix_writer);
    matrix_writer->set_input(SOURCE_TL, "data");

    // Create the service configuration
    service::config_t config;
    config.add("file", "matrices.csv");

    boost::property_tree::ptree config_child;
    config_child.put("<xmlattr>.interactive", false);
    config.add_child("config", config_child);

    // Start the service
    CPPUNIT_ASSERT_NO_THROW(matrix_writer->set_config(config));
    CPPUNIT_ASSERT_NO_THROW(matrix_writer->configure());
    CPPUNIT_ASSERT_NO_THROW(matrix_writer->start().wait());

    // Test writing in the temporary directory
    matrix_writer->slot("set_base_folder")->run(tmp_dir.string());

    matrix_writer->slot("start_record")->run();
    matrix_writer->slot("write")->run(core::clock::type(1));
    matrix_writer->slot("write")->run(core::clock::type(2));
    matrix_writer->slot("write")->run(core::clock::type(3));
    matrix_writer->slot("stop_record")->run();

    // Stop the service
    CPPUNIT_ASSERT_NO_THROW(matrix_writer->stop().wait());
    service::remove(matrix_writer);

    // Check the result...
    CPPUNIT_ASSERT_EQUAL(
        false,
        std::dynamic_pointer_cast<sight::io::service::writer>(matrix_writer)->has_failed()
    );

    std::ifstream file(tmp_dir / "matrices.csv");
    std::string actual((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());

    CPPUNIT_ASSERT_EQUAL(EXPECTED, actual);
}

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

void writer_test::base_folder_test()
{
    // Create a temporary directory
    core::os::temp_dir tmp_dir;

    // Create the service
    auto matrix_writer = service::add("sight::module::io::matrix::matrix_writer");
    CPPUNIT_ASSERT_MESSAGE("Failed to create service 'sight::module::io::matrix::matrix_writer'", matrix_writer);
    matrix_writer->set_input(SOURCE_TL, "data");

    // Create the service configuration
    service::config_t config;
    config.add("file", "matrices.csv");

    boost::property_tree::ptree config_child;
    config_child.put("<xmlattr>.interactive", false);
    config.add_child("config", config_child);

    // Start the service
    CPPUNIT_ASSERT_NO_THROW(matrix_writer->set_config(config));
    CPPUNIT_ASSERT_NO_THROW(matrix_writer->configure());
    CPPUNIT_ASSERT_NO_THROW(matrix_writer->start().wait());

    // Start recording immediately (no dialog)
    matrix_writer->slot("start_record")->run();

    // Set the base folder after
    matrix_writer->slot("set_base_folder")->run(tmp_dir.string());

    // Test writing
    matrix_writer->slot("write")->run(core::clock::type(1));
    matrix_writer->slot("write")->run(core::clock::type(2));
    matrix_writer->slot("write")->run(core::clock::type(3));

    // Stop the service (the recording should be also stopped)
    CPPUNIT_ASSERT_NO_THROW(matrix_writer->stop().wait());
    service::remove(matrix_writer);

    // Check the result...
    CPPUNIT_ASSERT_EQUAL(
        false,
        std::dynamic_pointer_cast<sight::io::service::writer>(matrix_writer)->has_failed()
    );

    std::ifstream file(tmp_dir / "matrices.csv");
    std::string actual((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());

    CPPUNIT_ASSERT_EQUAL(EXPECTED, actual);
}

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