Supporters update.
[dcpomatic.git] / test / font_id_allocator_test.cc
1 /*
2     Copyright (C) 2023 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 #include "lib/font_id_allocator.h"
23 #include <boost/test/unit_test.hpp>
24
25
26 using std::string;
27 using std::vector;
28
29
30 BOOST_AUTO_TEST_CASE(font_id_allocator_test_without_disambiguation)
31 {
32         FontIDAllocator allocator;
33
34         /* Reel 0 has just one asset with two fonts */
35         allocator.add_font(0, "asset1", "font");
36         allocator.add_font(0, "asset1", "font2");
37
38         /* Reel 1 has two assets each with two more fonts */
39         allocator.add_font(1, "asset2", "font");
40         allocator.add_font(1, "asset2", "font2");
41         allocator.add_font(1, "asset3", "font3");
42         allocator.add_font(1, "asset3", "font4");
43
44         allocator.allocate();
45
46         BOOST_CHECK(allocator.font_id(0, "asset1", "font") == "0_font");
47         BOOST_CHECK(allocator.font_id(0, "asset1", "font2") == "0_font2");
48         BOOST_CHECK(allocator.font_id(1, "asset2", "font") == "1_font");
49         BOOST_CHECK(allocator.font_id(1, "asset2", "font2") == "1_font2");
50         BOOST_CHECK(allocator.font_id(1, "asset3", "font3") == "1_font3");
51         BOOST_CHECK(allocator.font_id(1, "asset3", "font4") == "1_font4");
52 }
53
54
55 BOOST_AUTO_TEST_CASE(font_id_allocator_test_with_disambiguation)
56 {
57         FontIDAllocator allocator;
58
59         /* Reel 0 has two assets each with a font with the same ID (perhaps a subtitle and a ccap).
60          * This would have crashed DCP-o-matic before the FontIDAllocator change (bug #2600)
61          * so it's OK if the second font gets a new index that we didn't see before.
62          */
63         allocator.add_font(0, "asset1", "font");
64         allocator.add_font(0, "asset2", "font");
65
66         /* Reel 1 has one asset with another font */
67         allocator.add_font(1, "asset3", "font1");
68
69         allocator.allocate();
70
71         BOOST_CHECK(allocator.font_id(0, "asset1", "font") == "0_font");
72         /* This should get a prefix that is higher than any reel index */
73         BOOST_CHECK(allocator.font_id(0, "asset2", "font") == "2_font");
74         BOOST_CHECK(allocator.font_id(1, "asset3", "font1") == "1_font1");
75 }
76
77
78 /* Bug #2822: multiple reels, each with subs + closed captions, and each using the same
79  * basic font ID.
80  */
81 BOOST_AUTO_TEST_CASE(font_id_allocator_test_with_disambiguation2)
82 {
83         FontIDAllocator allocator;
84
85         allocator.add_font(0, "asset1", "font");
86         allocator.add_font(0, "asset2", "font");
87
88         allocator.add_font(1, "asset1", "font");
89         allocator.add_font(1, "asset2", "font");
90
91         allocator.allocate();
92         vector<string> ids = {
93                 allocator.font_id(0, "asset1", "font"),
94                 allocator.font_id(0, "asset2", "font"),
95                 allocator.font_id(1, "asset1", "font"),
96                 allocator.font_id(1, "asset2", "font")
97         };
98
99         std::sort(ids.begin(), ids.end());
100         BOOST_CHECK(std::adjacent_find(ids.begin(), ids.end()) == ids.end());
101 }