Various playlist editor developments and fixes.
[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         /* Bad seek */
96         BOOST_CHECK_EQUAL (fg.seek (total_length * 2, SEEK_SET), -1);
97
98         /* SEEK_SET */
99         BOOST_CHECK_EQUAL (fg.seek (999, SEEK_SET), 999);
100         BOOST_CHECK_EQUAL (fg.read (test, 64), 64);
101         BOOST_CHECK_EQUAL (memcmp (data + 999, test, 64), 0);
102
103         /* SEEK_CUR */
104         BOOST_CHECK_EQUAL (fg.seek (42, SEEK_CUR), 999 + 64 + 42);
105         BOOST_CHECK_EQUAL (fg.read (test, 64), 64);
106         BOOST_CHECK_EQUAL (memcmp (data + 999 + 64 + 42, test, 64), 0);
107
108         /* SEEK_END */
109         BOOST_CHECK_EQUAL (fg.seek (1077, SEEK_END), total_length - 1077);
110         BOOST_CHECK_EQUAL (fg.read (test, 256), 256);
111         BOOST_CHECK_EQUAL (memcmp (data + total_length - 1077, test, 256), 0);
112 }