e0f83f6315ad877248f6311dd617e46d6956139e
[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 Check that FileGroup works.
23  */
24
25 #include <stdint.h>
26 #include <cstdio>
27 #include <boost/test/unit_test.hpp>
28 #include <boost/filesystem.hpp>
29 #include "lib/file_group.h"
30
31 using std::vector;
32
33 BOOST_AUTO_TEST_CASE (file_group_test)
34 {
35         /* Random data; must be big enough for all the files */
36         uint8_t data[65536];
37         for (int i = 0; i < 65536; ++i) {
38                 data[i] = rand() & 0xff;
39         }
40
41         int const num_files = 4;
42
43         int length[] = {
44                 99,
45                 18941,
46                 33110,
47                 42
48         };
49
50         int total_length = 0;
51         for (int i = 0; i < num_files; ++i) {
52                 total_length += length[i];
53         }
54
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");
61
62         int base = 0;
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);
66                 fclose (f);
67                 base += length[i];
68         }
69
70         FileGroup fg (name);
71         uint8_t test[65536];
72
73         int pos = 0;
74
75         /* Basic read from 0 */
76         BOOST_CHECK_EQUAL (fg.read (test, 64), 64);
77         BOOST_CHECK_EQUAL (memcmp (data, test, 64), 0);
78         pos += 64;
79
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);
83         pos += 4;
84
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);
88         pos += 128;
89
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);
93
94         /* Bad seek */
95         BOOST_CHECK_EQUAL (fg.seek (total_length * 2, SEEK_SET), -1);
96
97         /* SEEK_SET */
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);
101
102         /* SEEK_CUR */
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);
106
107         /* SEEK_END */
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);
111 }