Remove libavformat-specific bit from FileGroup.
[dcpomatic.git] / test / file_group_test.cc
1 /*
2     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include <stdint.h>
21 #include <cstdio>
22 #include <boost/test/unit_test.hpp>
23 #include <boost/filesystem.hpp>
24 #include "lib/file_group.h"
25
26 using std::vector;
27
28 BOOST_AUTO_TEST_CASE (file_group_test)
29 {
30         /* Random data; must be big enough for all the files */
31         uint8_t data[65536];
32         for (int i = 0; i < 65536; ++i) {
33                 data[i] = rand() & 0xff;
34         }
35         
36         int const num_files = 4;
37
38         int length[] = {
39                 99,
40                 18941,
41                 33110,
42                 42
43         };
44
45         int total_length = 0;
46         for (int i = 0; i < num_files; ++i) {
47                 total_length += length[i];
48         }
49
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");
56
57         int base = 0;
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);
61                 fclose (f);
62                 base += length[i];
63         }
64
65         FileGroup fg;
66         fg.set_paths (name);
67         uint8_t test[65536];
68
69         int pos = 0;
70
71         /* Basic read from 0 */
72         BOOST_CHECK_EQUAL (fg.read (test, 64), 64);
73         BOOST_CHECK_EQUAL (memcmp (data, test, 64), 0);
74         pos += 64;
75
76         /* Another read following the previous */
77         BOOST_CHECK_EQUAL (fg.read (test, 4), 4);
78         BOOST_CHECK_EQUAL (memcmp (data + pos, test, 4), 0);
79         pos += 4;
80
81         /* Read overlapping A and B */
82         BOOST_CHECK_EQUAL (fg.read (test, 128), 128);
83         BOOST_CHECK_EQUAL (memcmp (data + pos, test, 128), 0);
84         pos += 128;
85
86         /* Read overlapping B/C/D and over-reading */
87         BOOST_CHECK_EQUAL (fg.read (test, total_length * 3), total_length - pos);
88         BOOST_CHECK_EQUAL (memcmp (data + pos, test, total_length - pos), 0);
89
90         /* Bad seek */
91         BOOST_CHECK_EQUAL (fg.seek (total_length * 2, SEEK_SET), -1);
92
93         /* SEEK_SET */
94         BOOST_CHECK_EQUAL (fg.seek (999, SEEK_SET), 999);
95         BOOST_CHECK_EQUAL (fg.read (test, 64), 64);
96         BOOST_CHECK_EQUAL (memcmp (data + 999, test, 64), 0);
97
98         /* SEEK_CUR */
99         BOOST_CHECK_EQUAL (fg.seek (42, SEEK_CUR), 999 + 64 + 42);
100         BOOST_CHECK_EQUAL (fg.read (test, 64), 64);
101         BOOST_CHECK_EQUAL (memcmp (data + 999 + 64 + 42, test, 64), 0);
102
103         /* SEEK_END */
104         BOOST_CHECK_EQUAL (fg.seek (1077, SEEK_END), total_length - 1077);
105         BOOST_CHECK_EQUAL (fg.read (test, 256), 256);
106         BOOST_CHECK_EQUAL (memcmp (data + total_length - 1077, test, 256), 0);
107 }