2 Copyright (C) 2013-2014 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /** @file test/file_group_test.cc
21 * @brief Check that FileGroup works.
26 #include <boost/test/unit_test.hpp>
27 #include <boost/filesystem.hpp>
28 #include "lib/file_group.h"
32 BOOST_AUTO_TEST_CASE (file_group_test)
34 /* Random data; must be big enough for all the files */
36 for (int i = 0; i < 65536; ++i) {
37 data[i] = rand() & 0xff;
40 int const num_files = 4;
50 for (int i = 0; i < num_files; ++i) {
51 total_length += length[i];
54 vector<boost::filesystem::path> name;
55 boost::filesystem::create_directories ("build/test/file_group_test");
56 name.push_back ("build/test/file_group_test/A");
57 name.push_back ("build/test/file_group_test/B");
58 name.push_back ("build/test/file_group_test/C");
59 name.push_back ("build/test/file_group_test/D");
62 for (int i = 0; i < num_files; ++i) {
63 FILE* f = fopen (name[i].string().c_str(), "wb");
64 fwrite (data + base, 1, length[i], f);
74 /* Basic read from 0 */
75 BOOST_CHECK_EQUAL (fg.read (test, 64), 64);
76 BOOST_CHECK_EQUAL (memcmp (data, test, 64), 0);
79 /* Another read following the previous */
80 BOOST_CHECK_EQUAL (fg.read (test, 4), 4);
81 BOOST_CHECK_EQUAL (memcmp (data + pos, test, 4), 0);
84 /* Read overlapping A and B */
85 BOOST_CHECK_EQUAL (fg.read (test, 128), 128);
86 BOOST_CHECK_EQUAL (memcmp (data + pos, test, 128), 0);
89 /* Read overlapping B/C/D and over-reading */
90 BOOST_CHECK_EQUAL (fg.read (test, total_length * 3), total_length - pos);
91 BOOST_CHECK_EQUAL (memcmp (data + pos, test, total_length - pos), 0);
94 BOOST_CHECK_EQUAL (fg.seek (total_length * 2, SEEK_SET), -1);
97 BOOST_CHECK_EQUAL (fg.seek (999, SEEK_SET), 999);
98 BOOST_CHECK_EQUAL (fg.read (test, 64), 64);
99 BOOST_CHECK_EQUAL (memcmp (data + 999, test, 64), 0);
102 BOOST_CHECK_EQUAL (fg.seek (42, SEEK_CUR), 999 + 64 + 42);
103 BOOST_CHECK_EQUAL (fg.read (test, 64), 64);
104 BOOST_CHECK_EQUAL (memcmp (data + 999 + 64 + 42, test, 64), 0);
107 BOOST_CHECK_EQUAL (fg.seek (1077, SEEK_END), total_length - 1077);
108 BOOST_CHECK_EQUAL (fg.read (test, 256), 256);
109 BOOST_CHECK_EQUAL (memcmp (data + total_length - 1077, test, 256), 0);