File: read_csv_test.cpp

package info (click to toggle)
sight 25.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,180 kB
  • sloc: cpp: 289,476; xml: 17,257; ansic: 9,878; python: 1,379; sh: 144; makefile: 33
file content (188 lines) | stat: -rw-r--r-- 7,191 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
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
/************************************************************************
 *
 * Copyright (C) 2009-2023 IRCAD France
 * Copyright (C) 2012-2019 IHU Strasbourg
 *
 * 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 "read_csv_test.hpp"

#include "io/__/reader/csv_reader.hpp"

#include <core/exception.hpp>

#include <boost/dll/runtime_symbol_info.hpp>
#include <boost/property_tree/ptree.hpp>

#include <filesystem>
#include <fstream>
#include <iostream>

// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION(sight::io::ut::read_csv_test);

namespace sight::io::ut
{

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

void read_csv_test::setUp()
{
    // Set up context before running a test.
    const auto current_path           = boost::dll::program_location().parent_path().parent_path();
    const auto valid_crlf_file        = current_path / "share/sight/io/valid_crlf_file.csv";
    const auto valid_lf_file          = current_path / "share/sight/io/valid_lf_file.csv";
    const auto invalid_csv_file       = current_path / "share/sight/io/invalid_file.csv";
    const auto invalid_file_directory = current_path / "share/sight/io/invalid_file_path.csv";

    // Open an existing file
    m_valid_crlf_csv_directory_path = valid_crlf_file.string();
    m_valid_lf_csv_directory_path   = valid_lf_file.string();
    m_invalid_csv_directory_path    = invalid_csv_file.string();
    m_wrong_filepath_directory      = invalid_file_directory.string();
}

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

void read_csv_test::tearDown()
{
}

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

void read_csv_test::test_inexistence_csv()
{
    CPPUNIT_ASSERT_NO_THROW(io::reader::csv_reader csv_reader(m_valid_crlf_csv_directory_path));
    CPPUNIT_ASSERT_NO_THROW(io::reader::csv_reader csv_reader(m_valid_lf_csv_directory_path));
    CPPUNIT_ASSERT_NO_THROW(io::reader::csv_reader csv_reader(m_invalid_csv_directory_path));
    CPPUNIT_ASSERT_THROW(io::reader::csv_reader csv_reader(m_wrong_filepath_directory), core::exception);
}

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

void read_csv_test::test_valid_crlf_csv()
{
    // cspell: disable
    io::reader::csv_reader csv_reader(m_valid_crlf_csv_directory_path);
    io::reader::csv_reader::token_container_t token_vec;

    token_vec = csv_reader.get_line();
    CPPUNIT_ASSERT("1" == token_vec[0]);
    CPPUNIT_ASSERT("Eldon Base" == token_vec[1]);
    CPPUNIT_ASSERT("platinum" == token_vec[2]);
    CPPUNIT_ASSERT("Muhammed" == token_vec[3]);
    CPPUNIT_ASSERT("3" == token_vec[4]);
    CPPUNIT_ASSERT("-213.25" == token_vec[5]);
    CPPUNIT_ASSERT("38.94" == token_vec[6]);
    CPPUNIT_ASSERT("35" == token_vec[7]);
    CPPUNIT_ASSERT("Name" == token_vec[8]);
    CPPUNIT_ASSERT("Storage" == token_vec[9]);
    CPPUNIT_ASSERT("0.8" == token_vec[10]);
    CPPUNIT_ASSERT(token_vec[11].empty());
    CPPUNIT_ASSERT(12 == token_vec.size());

    token_vec = csv_reader.get_line();
    CPPUNIT_ASSERT("2" == token_vec[0]);
    CPPUNIT_ASSERT("Eldon Base for stackable storage" == token_vec[1]);
    CPPUNIT_ASSERT("plati" == token_vec[2]);
    CPPUNIT_ASSERT("Muhammed and MacIntyre" == token_vec[3]);
    CPPUNIT_ASSERT("12" == token_vec[4]);
    CPPUNIT_ASSERT("-213.25" == token_vec[5]);
    CPPUNIT_ASSERT("32.94" == token_vec[6]);
    CPPUNIT_ASSERT("35" == token_vec[7]);
    CPPUNIT_ASSERT("Organization" == token_vec[8]);
    CPPUNIT_ASSERT("0.89" == token_vec[9]);
    CPPUNIT_ASSERT(token_vec[10].empty());
    CPPUNIT_ASSERT(11 == token_vec.size());

    token_vec = csv_reader.get_line();
    CPPUNIT_ASSERT("3" == token_vec[0]);
    CPPUNIT_ASSERT("Eldon" == token_vec[1]);
    CPPUNIT_ASSERT("plati" == token_vec[2]);
    CPPUNIT_ASSERT("MacIntyre" == token_vec[3]);
    CPPUNIT_ASSERT("9" == token_vec[4]);
    CPPUNIT_ASSERT("0.56" == token_vec[5]);
    CPPUNIT_ASSERT(token_vec[6].empty());
    CPPUNIT_ASSERT(7 == token_vec.size());

    //cspell: enable
}

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

void read_csv_test::test_valid_lf_csv()
{
    // cspell: disable
    io::reader::csv_reader csv_reader(m_valid_lf_csv_directory_path);
    io::reader::csv_reader::token_container_t token_vec;

    token_vec = csv_reader.get_line();
    CPPUNIT_ASSERT("1" == token_vec[0]);
    CPPUNIT_ASSERT("Eldon Base" == token_vec[1]);
    CPPUNIT_ASSERT("platinum" == token_vec[2]);
    CPPUNIT_ASSERT("Muhammed" == token_vec[3]);
    CPPUNIT_ASSERT("3" == token_vec[4]);
    CPPUNIT_ASSERT("-213.25" == token_vec[5]);
    CPPUNIT_ASSERT("38.94" == token_vec[6]);
    CPPUNIT_ASSERT("35" == token_vec[7]);
    CPPUNIT_ASSERT("Name" == token_vec[8]);
    CPPUNIT_ASSERT("Storage" == token_vec[9]);
    CPPUNIT_ASSERT("0.8" == token_vec[10]);
    CPPUNIT_ASSERT(token_vec[11].empty());
    CPPUNIT_ASSERT(12 == token_vec.size());

    token_vec = csv_reader.get_line();
    CPPUNIT_ASSERT("2" == token_vec[0]);
    CPPUNIT_ASSERT("Eldon Base for stackable storage" == token_vec[1]);
    CPPUNIT_ASSERT("plati" == token_vec[2]);
    CPPUNIT_ASSERT("Muhammed and MacIntyre" == token_vec[3]);
    CPPUNIT_ASSERT("12" == token_vec[4]);
    CPPUNIT_ASSERT("-213.25" == token_vec[5]);
    CPPUNIT_ASSERT("32.94" == token_vec[6]);
    CPPUNIT_ASSERT("35" == token_vec[7]);
    CPPUNIT_ASSERT("Organization" == token_vec[8]);
    CPPUNIT_ASSERT("0.89" == token_vec[9]);
    CPPUNIT_ASSERT(token_vec[10].empty());
    CPPUNIT_ASSERT(11 == token_vec.size());

    token_vec = csv_reader.get_line();
    CPPUNIT_ASSERT("3" == token_vec[0]);
    CPPUNIT_ASSERT("Eldon" == token_vec[1]);
    CPPUNIT_ASSERT("plati" == token_vec[2]);
    CPPUNIT_ASSERT("MacIntyre" == token_vec[3]);
    CPPUNIT_ASSERT("9" == token_vec[4]);
    CPPUNIT_ASSERT("0.56" == token_vec[5]);
    CPPUNIT_ASSERT(token_vec[6].empty());
    CPPUNIT_ASSERT(7 == token_vec.size());

    // cspell: enable
}

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

void read_csv_test::test_invalid_csv()
{
    io::reader::csv_reader csv_reader(m_invalid_csv_directory_path);
    io::reader::csv_reader::token_container_t tag_vec = csv_reader.get_line();

    CPPUNIT_ASSERT(std::filesystem::exists(m_invalid_csv_directory_path));
    const std::size_t size = tag_vec.size();
    CPPUNIT_ASSERT(size == 0);
}

} // namespace sight::io::ut