Merge branch '1.0' into 1.0-vob
[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 (name);
66         uint8_t test[65536];
67
68         int pos = 0;
69
70         /* Basic read from 0 */
71         BOOST_CHECK_EQUAL (fg.read (test, 64), 64);
72         BOOST_CHECK_EQUAL (memcmp (data, test, 64), 0);
73         pos += 64;
74
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);
78         pos += 4;
79
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);
83         pos += 128;
84
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);
88
89         /* Bad seek */
90         BOOST_CHECK_EQUAL (fg.seek (total_length * 2, SEEK_SET), -1);
91
92         /* SEEK_SET */
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);
96
97         /* SEEK_CUR */
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);
101
102         /* SEEK_END */
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);
106 }