Skip to content

Commit f6ad40c

Browse files
authored
More unit tests for util (#192)
* Unit test collect_image_files function Signed-off-by: Aleksandr Motsjonov <[email protected]> * Unit tests for database_paths function Signed-off-by: Aleksandr Motsjonov <[email protected]> * Unit test for fix_metadata Signed-off-by: Aleksandr Motsjonov <[email protected]> * fix win32 env tests Signed-off-by: Aleksandr Motsjonov <[email protected]> * fix win32 Signed-off-by: Aleksandr Motsjonov <[email protected]> * debugging to find where win32 fails Signed-off-by: Aleksandr Motsjonov <[email protected]> * trying fix win32 Signed-off-by: Aleksandr Motsjonov <[email protected]> * Fix win32 Signed-off-by: Aleksandr Motsjonov <[email protected]> * format Signed-off-by: Aleksandr Motsjonov <[email protected]> * Fix win32 fix Signed-off-by: Aleksandr Motsjonov <[email protected]> * formatting Signed-off-by: Aleksandr Motsjonov <[email protected]> * formatting and such Signed-off-by: Aleksandr Motsjonov <[email protected]> * Move outside loop from main.cpp into collect_image_files Signed-off-by: Aleksandr Motsjonov <[email protected]> * cleanup Signed-off-by: Aleksandr Motsjonov <[email protected]> * fix up float type validation Signed-off-by: Aleksandr Motsjonov <[email protected]> * Bring back empty batches Signed-off-by: Aleksandr Motsjonov <[email protected]> --------- Signed-off-by: Aleksandr Motsjonov <[email protected]>
1 parent bced9df commit f6ad40c

File tree

6 files changed

+643
-48
lines changed

6 files changed

+643
-48
lines changed

include/rawtoaces/image_converter.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ namespace rta
1111
namespace util
1212
{
1313

14-
/// Collect all files from a given `path` into batchs. If the `path` is a
15-
/// directory, create an entry in `batches` and fill it with the file names
16-
/// from that directory. If the `path` is a file, add its name to the first
17-
/// entry in `batches`.
18-
/// @param path path to a file or directory to process.
19-
/// @param batches the collection of batches to fill in.
20-
/// @result `false` if the file or directory requested in `path` does not
21-
/// exist.
22-
bool collect_image_files(
23-
const std::string &path, std::vector<std::vector<std::string>> &batches );
14+
/// Collect all files from given `paths` into batches.
15+
/// For each path that is a directory, entries are created in the returned batches
16+
/// and fill it with the file names. Invalid paths are skipped with an error message.
17+
/// First batch is reserved for all paths that are files. If no such paths are provided,
18+
/// first batch will be empty.
19+
///
20+
/// @param paths vector of paths to files or directories to process.
21+
/// @return vector of batches, where each batch contains files from one input path.
22+
std::vector<std::vector<std::string>>
23+
collect_image_files( const std::vector<std::string> &paths );
2424

2525
class ImageConverter
2626
{

src/rawtoaces/main.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,9 @@ int main( int argc, const char *argv[] )
3636
return 1;
3737
}
3838

39-
// Create a separate batch for each input directory.
40-
// Reserve the first batch for the individual input files.
41-
std::vector<std::vector<std::string>> batches( 1 );
42-
4339
// Gather all the raw images from arg list
44-
for ( const auto &path: files )
45-
{
46-
if ( !rta::util::collect_image_files( path, batches ) )
47-
{
48-
std::cerr << "File or directory not found: " << path << std::endl;
49-
return 1;
50-
}
51-
}
40+
std::vector<std::vector<std::string>> batches =
41+
rta::util::collect_image_files( files );
5242

5343
// Process raw files ...
5444
bool empty = true;

src/rawtoaces_util/image_converter.cpp

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,60 +50,65 @@ struct CameraIdentifier
5050
* @return true if the file was processed (either added to batch or filtered out),
5151
* false if the file should be ignored
5252
*/
53-
bool check_and_add_file(
53+
void check_and_add_file(
5454
const std::filesystem::path &path, std::vector<std::string> &batch )
5555
{
5656
bool is_regular_file = std::filesystem::is_regular_file( path ) ||
5757
std::filesystem::is_symlink( path );
5858
if ( !is_regular_file )
5959
{
6060
std::cerr << "Not a regular file: " << path << std::endl;
61-
return false;
61+
return;
6262
}
6363

6464
static const std::set<std::string> ignore_filenames = { ".DS_Store" };
6565
std::string filename = path.filename().string();
6666
if ( ignore_filenames.count( filename ) > 0 )
67-
return false;
67+
return;
6868

6969
static const std::set<std::string> ignore_extensions = { ".exr",
7070
".jpg",
7171
".jpeg" };
7272
std::string extension = OIIO::Strutil::lower( path.extension().string() );
7373
if ( ignore_extensions.count( extension ) > 0 )
74-
return false;
74+
return;
7575

7676
batch.push_back( path.string() );
77-
78-
return true;
77+
return;
7978
}
8079

81-
bool collect_image_files(
82-
const std::string &path, std::vector<std::vector<std::string>> &batches )
80+
std::vector<std::vector<std::string>>
81+
collect_image_files( const std::vector<std::string> &paths )
8382
{
84-
if ( !std::filesystem::exists( path ) )
83+
std::vector<std::vector<std::string>> batches( 1 );
84+
85+
for ( const auto &path: paths )
8586
{
86-
return false;
87-
}
87+
if ( !std::filesystem::exists( path ) )
88+
{
89+
std::cerr << "File or directory not found: " << path << std::endl;
90+
continue;
91+
}
8892

89-
auto canonical_filename = std::filesystem::canonical( path );
93+
auto canonical_filename = std::filesystem::canonical( path );
9094

91-
if ( std::filesystem::is_directory( path ) )
92-
{
93-
std::vector<std::string> &curr_batch = batches.emplace_back();
94-
auto it = std::filesystem::directory_iterator( path );
95+
if ( std::filesystem::is_directory( path ) )
96+
{
97+
std::vector<std::string> &curr_batch = batches.emplace_back();
98+
auto it = std::filesystem::directory_iterator( path );
9599

96-
for ( auto filename: it )
100+
for ( auto filename: it )
101+
{
102+
check_and_add_file( filename, curr_batch );
103+
}
104+
}
105+
else
97106
{
98-
check_and_add_file( filename, curr_batch );
107+
check_and_add_file( path, batches[0] );
99108
}
100109
}
101-
else
102-
{
103-
check_and_add_file( path, batches[0] );
104-
}
105110

106-
return true;
111+
return batches;
107112
}
108113

109114
/// Gets the list of database paths for rawtoaces data files.
@@ -1165,6 +1170,8 @@ std::vector<std::string> ImageConverter::supported_cameras()
11651170
/// Normalise the metadata in the cases where the OIIO attribute name
11661171
/// doesn't match the standard OpenEXR and/or ACES Container attribute name.
11671172
/// We only check the attribute names which are set by the raw input plugin.
1173+
///
1174+
/// @param spec ImageSpec to modify
11681175
void fix_metadata( OIIO::ImageSpec &spec )
11691176
{
11701177
const std::map<std::string, std::string> standard_mapping = {
@@ -1186,8 +1193,6 @@ void fix_metadata( OIIO::ImageSpec &spec )
11861193
{
11871194
if ( type.basetype == OIIO::TypeDesc::STRING )
11881195
spec[dst_name] = src_attribute->get_string();
1189-
else if ( type.basetype == OIIO::TypeDesc::FLOAT )
1190-
spec[dst_name] = src_attribute->get_float();
11911196
}
11921197
spec.erase_attribute( src_name );
11931198
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// Copyright Contributors to the rawtoaces Project.
3+
4+
#pragma once
5+
6+
#include <filesystem>
7+
#include <string>
8+
#include <vector>
9+
#include <OpenImageIO/imageio.h>
10+
11+
// Contains the declarations of the private functions,
12+
// exposed here for unit-testing.
13+
14+
namespace rta
15+
{
16+
namespace util
17+
{
18+
19+
std::vector<std::string> database_paths();
20+
21+
void fix_metadata( OIIO::ImageSpec &spec );
22+
23+
} // namespace util
24+
} // namespace rta

tests/CMakeLists.txt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,22 @@ target_link_libraries(
183183
setup_test_coverage(Test_UsageTimer)
184184
add_test ( NAME Test_UsageTimer COMMAND Test_UsageTimer )
185185

186+
################################################################################
186187

188+
add_executable (
189+
Test_ImageConverter
190+
test_image_converter.cpp
191+
)
192+
193+
target_link_libraries(
194+
Test_ImageConverter
195+
PUBLIC
196+
${RAWTOACES_UTIL_LIB}
197+
OpenImageIO::OpenImageIO
198+
)
199+
200+
setup_test_coverage(Test_ImageConverter)
201+
add_test ( NAME Test_ImageConverter COMMAND Test_ImageConverter )
187202

188203
################################################################################
189204
# Coverage report generation
@@ -193,4 +208,4 @@ if( ENABLE_COVERAGE AND COVERAGE_SUPPORTED )
193208
add_subdirectory(config_tests/util)
194209

195210
generate_coverage_report()
196-
endif()
211+
endif()

0 commit comments

Comments
 (0)