Fix the behaviour of FileGroup when seeking too far.
[dcpomatic.git] / test / file_group_test.cc
1 /*
2     Copyright (C) 2013-2014 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 /** @file  test/file_group_test.cc
22  *  @brief Test FileGroup class.
23  *  @ingroup selfcontained
24  */
25
26 #include <stdint.h>
27 #include <cstdio>
28 #include <boost/test/unit_test.hpp>
29 #include <boost/filesystem.hpp>
30 #include "lib/file_group.h"
31
32 using std::vector;
33
34 BOOST_AUTO_TEST_CASE (file_group_test)
35 {
36         /* Random data; must be big enough for all the files */
37         uint8_t data[65536];
38         for (int i = 0; i < 65536; ++i) {
39                 data[i] = rand() & 0xff;
40         }
41
42         int const num_files = 4;
43
44         int length[] = {
45                 99,
46                 18941,
47                 33110,
48                 42
49         };
50
51         int total_length = 0;
52         for (int i = 0; i < num_files; ++i) {
53                 total_length += length[i];
54         }
55
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");
62
63         int base = 0;
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);
67                 fclose (f);
68                 base += length[i];
69         }
70
71         FileGroup fg (name);
72         uint8_t test[65536];
73
74         int pos = 0;
75
76         /* Basic read from 0 */
77         BOOST_CHECK_EQUAL (fg.read (test, 64), 64);
78         BOOST_CHECK_EQUAL (memcmp (data, test, 64), 0);
79         pos += 64;
80
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);
84         pos += 4;
85
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);
89         pos += 128;
90
91         /* Read overlapping B/C/D and over-reading */
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);
94
95         /* Seeking off the end of the file should not give an error */
96         BOOST_CHECK_EQUAL (fg.seek (total_length * 2, SEEK_SET), total_length * 2);
97         /* and attempting to read should return nothing */
98         BOOST_CHECK_EQUAL (fg.read (test, 64), 0);
99         /* but the requested seek should be remembered, so if we now go back (relatively) */
100         BOOST_CHECK_EQUAL (fg.seek (-total_length * 2, SEEK_CUR), 0);
101         /* we should be at the start again */
102         BOOST_CHECK_EQUAL (fg.read (test, 64), 64);
103         BOOST_CHECK_EQUAL (memcmp (data, test, 64), 0);
104
105         /* SEEK_SET */
106         BOOST_CHECK_EQUAL (fg.seek (999, SEEK_SET), 999);
107         BOOST_CHECK_EQUAL (fg.read (test, 64), 64);
108         BOOST_CHECK_EQUAL (memcmp (data + 999, test, 64), 0);
109
110         /* SEEK_CUR */
111         BOOST_CHECK_EQUAL (fg.seek (42, SEEK_CUR), 999 + 64 + 42);
112         BOOST_CHECK_EQUAL (fg.read (test, 64), 64);
113         BOOST_CHECK_EQUAL (memcmp (data + 999 + 64 + 42, test, 64), 0);
114
115         /* SEEK_END */
116         BOOST_CHECK_EQUAL (fg.seek (1077, SEEK_END), total_length - 1077);
117         BOOST_CHECK_EQUAL (fg.read (test, 256), 256);
118         BOOST_CHECK_EQUAL (memcmp (data + total_length - 1077, test, 256), 0);
119 }