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