2 Copyright (C) 2013-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 /** @file test/file_group_test.cc
23 * @brief Test FileGroup class.
24 * @ingroup selfcontained
28 #include "lib/file_group.h"
29 #include <boost/test/unit_test.hpp>
30 #include <boost/filesystem.hpp>
38 BOOST_AUTO_TEST_CASE (file_group_test)
40 /* Random data; must be big enough for all the files */
42 for (int i = 0; i < 65536; ++i) {
43 data[i] = rand() & 0xff;
46 int const num_files = 4;
56 for (int i = 0; i < num_files; ++i) {
57 total_length += length[i];
60 boost::filesystem::create_directories ("build/test/file_group_test");
61 vector<boost::filesystem::path> name = {
62 "build/test/file_group_test/A",
63 "build/test/file_group_test/B",
64 "build/test/file_group_test/C",
65 "build/test/file_group_test/D"
69 for (int i = 0; i < num_files; ++i) {
70 auto f = fopen (name[i].string().c_str(), "wb");
71 fwrite (data + base, 1, length[i], f);
81 /* Basic read from 0 */
82 BOOST_CHECK_EQUAL (fg.read(test, 64), 64);
83 BOOST_CHECK_EQUAL (memcmp(data, test, 64), 0);
86 /* Another read following the previous */
87 BOOST_CHECK_EQUAL (fg.read(test, 4), 4);
88 BOOST_CHECK_EQUAL (memcmp(data + pos, test, 4), 0);
91 /* Read overlapping A and B */
92 BOOST_CHECK_EQUAL (fg.read(test, 128), 128);
93 BOOST_CHECK_EQUAL (memcmp(data + pos, test, 128), 0);
96 /* Read overlapping B/C/D and over-reading by a lot */
97 BOOST_CHECK_EQUAL (fg.read(test, total_length * 3), total_length - pos);
98 BOOST_CHECK_EQUAL (memcmp(data + pos, test, total_length - pos), 0);
100 /* Over-read by a little */
101 BOOST_CHECK_EQUAL (fg.seek(0, SEEK_SET), 0);
102 BOOST_CHECK_EQUAL (fg.read(test, total_length), total_length);
103 BOOST_CHECK_EQUAL (fg.read(test, 1), 0);
105 /* Seeking off the end of the file should not give an error */
106 BOOST_CHECK_EQUAL (fg.seek(total_length * 2, SEEK_SET), total_length * 2);
107 /* and attempting to read should return nothing */
108 BOOST_CHECK_EQUAL (fg.read(test, 64), 0);
109 /* but the requested seek should be remembered, so if we now go back (relatively) */
110 BOOST_CHECK_EQUAL (fg.seek(-total_length * 2, SEEK_CUR), 0);
111 /* we should be at the start again */
112 BOOST_CHECK_EQUAL (fg.read(test, 64), 64);
113 BOOST_CHECK_EQUAL (memcmp(data, test, 64), 0);
116 BOOST_CHECK_EQUAL (fg.seek(999, SEEK_SET), 999);
117 BOOST_CHECK_EQUAL (fg.read(test, 64), 64);
118 BOOST_CHECK_EQUAL (memcmp(data + 999, test, 64), 0);
121 BOOST_CHECK_EQUAL (fg.seek(42, SEEK_CUR), 999 + 64 + 42);
122 BOOST_CHECK_EQUAL (fg.read(test, 64), 64);
123 BOOST_CHECK_EQUAL (memcmp(data + 999 + 64 + 42, test, 64), 0);
126 BOOST_CHECK_EQUAL (fg.seek(1077, SEEK_END), total_length - 1077);
127 BOOST_CHECK_EQUAL (fg.read(test, 256), 256);
128 BOOST_CHECK_EQUAL (memcmp(data + total_length - 1077, test, 256), 0);