summaryrefslogtreecommitdiff
path: root/test/test.cc
blob: 9cbfe14758f3cd18a8912e5115c34544e1b4032f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
    Copyright (C) 2014 Carl Hetherington <cth@carlh.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE libsub_test
#include <boost/test/unit_test.hpp>
#include <boost/filesystem.hpp>
#include <fstream>
#include <string>
#include <iostream>
#include "iso6937_tables.h"

using std::string;
using std::cerr;
using std::cout;
using std::min;
using std::max;
using std::hex;
using std::ifstream;
using std::getline;
using std::stringstream;

boost::filesystem::path private_test;

struct TestConfig
{
	TestConfig()
	{
		if (boost::unit_test::framework::master_test_suite().argc >= 2) {
			private_test = boost::unit_test::framework::master_test_suite().argv[1];
		} else {
			BOOST_TEST_MESSAGE ("Private data libsub-test-private not found; some tests will not run");
		}

		sub::make_iso6937_tables ();
	}
};

BOOST_GLOBAL_FIXTURE (TestConfig);

void
check_text (boost::filesystem::path a, boost::filesystem::path b)
{
	if (!boost::filesystem::exists (a)) {
		cerr << "File not found: " << a << "\n";
	}

	if (!boost::filesystem::exists (b)) {
		cerr << "File not found: " << b << "\n";
	}

	BOOST_CHECK (boost::filesystem::exists (a));
	BOOST_CHECK (boost::filesystem::exists (b));

	ifstream p (a.c_str ());
	ifstream q (b.c_str ());

	string x;
	string y;
	while (p.good() && q.good()) {
		getline (p, x);
		getline (q, y);
		BOOST_CHECK_EQUAL (x, y);
	}

	BOOST_CHECK (p.good() == false);
	BOOST_CHECK (q.good() == false);
}

void
check_file (boost::filesystem::path ref, boost::filesystem::path check)
{
	uintmax_t N = boost::filesystem::file_size (ref);
	BOOST_CHECK_EQUAL (N, boost::filesystem::file_size (check));
	FILE* ref_file = fopen (ref.string().c_str(), "rb");
	BOOST_CHECK (ref_file);
	FILE* check_file = fopen (check.string().c_str(), "rb");
	BOOST_CHECK (check_file);

	int const buffer_size = 65536;
	uint8_t* ref_buffer = new uint8_t[buffer_size];
	uint8_t* check_buffer = new uint8_t[buffer_size];

	uintmax_t offset = 0;
	while (offset < N) {
		uintmax_t this_time = min (uintmax_t (buffer_size), N - offset);
		size_t r = fread (ref_buffer, 1, this_time, ref_file);
		BOOST_CHECK_EQUAL (r, this_time);
		r = fread (check_buffer, 1, this_time, check_file);
		BOOST_CHECK_EQUAL (r, this_time);

		for (uintmax_t i = 0; i < this_time; ++i) {
			stringstream s;
			s << "Files differ at offset " << (offset + i)
			  << "; reference is " << hex << ((int) ref_buffer[i])
			  << ", check is " << hex << ((int) check_buffer[i]);

			BOOST_CHECK_MESSAGE (ref_buffer[i] == check_buffer[i], s.str ());
		}

		offset += this_time;
	}

	delete[] ref_buffer;
	delete[] check_buffer;

	fclose (ref_file);
	fclose (check_file);
}