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 Check that FileGroup works.
27 #include <boost/test/unit_test.hpp>
28 #include <boost/filesystem.hpp>
29 #include "lib/file_group.h"
33 BOOST_AUTO_TEST_CASE (file_group_test)
35 /* Random data; must be big enough for all the files */
37 for (int i = 0; i < 65536; ++i) {
38 data[i] = rand() & 0xff;
41 int const num_files = 4;
51 for (int i = 0; i < num_files; ++i) {
52 total_length += length[i];
55 vector<boost::filesystem::path> name;
56 boost::filesystem::create_directories ("build/test/file_group_test");
57 name.push_back ("build/test/file_group_test/A");
58 name.push_back ("build/test/file_group_test/B");
59 name.push_back ("build/test/file_group_test/C");
60 name.push_back ("build/test/file_group_test/D");
63 for (int i = 0; i < num_files; ++i) {
64 FILE* f = fopen (name[i].string().c_str(), "wb");
65 fwrite (data + base, 1, length[i], f);
75 /* Basic read from 0 */
76 BOOST_CHECK_EQUAL (fg.read (test, 64), 64);
77 BOOST_CHECK_EQUAL (memcmp (data, test, 64), 0);
80 /* Another read following the previous */
81 BOOST_CHECK_EQUAL (fg.read (test, 4), 4);
82 BOOST_CHECK_EQUAL (memcmp (data + pos, test, 4), 0);
85 /* Read overlapping A and B */
86 BOOST_CHECK_EQUAL (fg.read (test, 128), 128);
87 BOOST_CHECK_EQUAL (memcmp (data + pos, test, 128), 0);
90 /* Read overlapping B/C/D and over-reading */
91 BOOST_CHECK_EQUAL (fg.read (test, total_length * 3), total_length - pos);
92 BOOST_CHECK_EQUAL (memcmp (data + pos, test, total_length - pos), 0);
95 BOOST_CHECK_EQUAL (fg.seek (total_length * 2, SEEK_SET), -1);
98 BOOST_CHECK_EQUAL (fg.seek (999, SEEK_SET), 999);
99 BOOST_CHECK_EQUAL (fg.read (test, 64), 64);
100 BOOST_CHECK_EQUAL (memcmp (data + 999, test, 64), 0);
103 BOOST_CHECK_EQUAL (fg.seek (42, SEEK_CUR), 999 + 64 + 42);
104 BOOST_CHECK_EQUAL (fg.read (test, 64), 64);
105 BOOST_CHECK_EQUAL (memcmp (data + 999 + 64 + 42, test, 64), 0);
108 BOOST_CHECK_EQUAL (fg.seek (1077, SEEK_END), total_length - 1077);
109 BOOST_CHECK_EQUAL (fg.read (test, 256), 256);
110 BOOST_CHECK_EQUAL (memcmp (data + total_length - 1077, test, 256), 0);