Merge remote-tracking branch 'origin/main' into v2.17.x
[dcpomatic.git] / test / image_filename_sorter_test.cc
1 /*
2     Copyright (C) 2015-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 /** @file  test/image_filename_sorter_test.cc
23  *  @brief Test ImageFilenameSorter
24  *  @ingroup selfcontained
25  */
26
27
28 #include "lib/image_filename_sorter.h"
29 #include "lib/compose.hpp"
30 #include <boost/test/unit_test.hpp>
31 #include <algorithm>
32 #include <random>
33
34
35 using std::sort;
36 using std::vector;
37
38
39 BOOST_AUTO_TEST_CASE (image_filename_sorter_test1)
40 {
41         ImageFilenameSorter x;
42         BOOST_CHECK (x("abc0000000001", "abc0000000002"));
43         BOOST_CHECK (x("1", "2"));
44         BOOST_CHECK (x("1", "0002"));
45         BOOST_CHECK (x("0001", "2"));
46         BOOST_CHECK (x("1", "999"));
47         BOOST_CHECK (x("00057.tif", "00166.tif"));
48         BOOST_CHECK (x("/my/numeric999/path/00057.tif", "/my/numeric999/path/00166.tif"));
49         BOOST_CHECK (x("1_01.tif", "1_02.tif"));
50         BOOST_CHECK (x("EWS_DCP_092815_000000.j2c", "EWS_DCP_092815_000001.j2c"));
51         BOOST_CHECK (x("ap_trlr_178_uhd_bt1886_txt_e5c1_033115.86352.dpx", "ap_trlr_178_uhd_bt1886_txt_e5c1_033115.86353.dpx"));
52
53         BOOST_CHECK (!x("abc0000000002", "abc0000000001"));
54         BOOST_CHECK (!x("2", "1"));
55         BOOST_CHECK (!x("0002", "1"));
56         BOOST_CHECK (!x("2", "0001"));
57         BOOST_CHECK (!x("999", "1"));
58         BOOST_CHECK (!x("/my/numeric999/path/00166.tif", "/my/numeric999/path/00057.tif"));
59         BOOST_CHECK (!x("1_02.tif", "1_01.tif"));
60         BOOST_CHECK (!x("EWS_DCP_092815_000000.j2c", "EWS_DCP_092815_000000.j2c"));
61         BOOST_CHECK (!x("EWS_DCP_092815_000100.j2c", "EWS_DCP_092815_000000.j2c"));
62         BOOST_CHECK (!x("ap_trlr_178_uhd_bt1886_txt_e5c1_033115.86353.dpx", "ap_trlr_178_uhd_bt1886_txt_e5c1_033115.86352.dpx"));
63 }
64
65
66 /** Test a sort of a lot of paths.  Mostly useful for profiling. */
67 BOOST_AUTO_TEST_CASE (image_filename_sorter_test2)
68 {
69         vector<boost::filesystem::path> paths;
70         for (int i = 0; i < 100000; ++i) {
71                 paths.push_back(String::compose("some.filename.with.%1.number.tiff", i));
72         }
73
74         std::random_device rd;
75         std::mt19937 generator(rd());
76         std::shuffle(paths.begin(), paths.end(), generator);
77
78         sort (paths.begin(), paths.end(), ImageFilenameSorter());
79         for (int i = 0; i < 100000; ++i) {
80                 BOOST_CHECK_EQUAL(paths[i].string(), String::compose("some.filename.with.%1.number.tiff", i));
81         }
82 }