2 Copyright (C) 2013 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.
22 #include <boost/test/unit_test.hpp>
23 #include <boost/filesystem.hpp>
24 #include "lib/file_group.h"
28 BOOST_AUTO_TEST_CASE (file_group_test)
30 /* Random data; must be big enough for all the files */
32 for (int i = 0; i < 65536; ++i) {
33 data[i] = rand() & 0xff;
36 int const num_files = 4;
46 for (int i = 0; i < num_files; ++i) {
47 total_length += length[i];
50 vector<boost::filesystem::path> name;
51 boost::filesystem::create_directories ("build/test/file_group_test");
52 name.push_back ("build/test/file_group_test/A");
53 name.push_back ("build/test/file_group_test/B");
54 name.push_back ("build/test/file_group_test/C");
55 name.push_back ("build/test/file_group_test/D");
58 for (int i = 0; i < num_files; ++i) {
59 FILE* f = fopen (name[i].string().c_str(), "wb");
60 fwrite (data + base, 1, length[i], f);
70 /* Basic read from 0 */
71 BOOST_CHECK_EQUAL (fg.read (test, 64), 64);
72 BOOST_CHECK_EQUAL (memcmp (data, test, 64), 0);
75 /* Another read following the previous */
76 BOOST_CHECK_EQUAL (fg.read (test, 4), 4);
77 BOOST_CHECK_EQUAL (memcmp (data + pos, test, 4), 0);
80 /* Read overlapping A and B */
81 BOOST_CHECK_EQUAL (fg.read (test, 128), 128);
82 BOOST_CHECK_EQUAL (memcmp (data + pos, test, 128), 0);
85 /* Read overlapping B/C/D and over-reading */
86 BOOST_CHECK_EQUAL (fg.read (test, total_length * 3), total_length - pos);
87 BOOST_CHECK_EQUAL (memcmp (data + pos, test, total_length - pos), 0);
90 BOOST_CHECK_EQUAL (fg.seek (total_length * 2, SEEK_SET), -1);
93 BOOST_CHECK_EQUAL (fg.seek (999, SEEK_SET), 999);
94 BOOST_CHECK_EQUAL (fg.read (test, 64), 64);
95 BOOST_CHECK_EQUAL (memcmp (data + 999, test, 64), 0);
98 BOOST_CHECK_EQUAL (fg.seek (42, SEEK_CUR), 999 + 64 + 42);
99 BOOST_CHECK_EQUAL (fg.read (test, 64), 64);
100 BOOST_CHECK_EQUAL (memcmp (data + 999 + 64 + 42, test, 64), 0);
103 BOOST_CHECK_EQUAL (fg.seek (1077, SEEK_END), total_length - 1077);
104 BOOST_CHECK_EQUAL (fg.read (test, 256), 256);
105 BOOST_CHECK_EQUAL (memcmp (data + total_length - 1077, test, 256), 0);