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