Missing static build libraries.
[libsub.git] / test / test.cc
index feeba36351895d236d3ef8e3feb9d19bd7082a89..bc2acda81e4e8a786cc0be1db2b29c0f8117b8c5 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2014-2019 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
 
 #define BOOST_TEST_DYN_LINK
 #define BOOST_TEST_MODULE libsub_test
+#include "iso6937_tables.h"
+#include "compose.hpp"
 #include <boost/test/unit_test.hpp>
+#include <boost/filesystem.hpp>
 #include <fstream>
 #include <string>
+#include <iostream>
 
 using std::string;
 using std::cerr;
+using std::cout;
+using std::min;
+using std::max;
+using std::hex;
 using std::ifstream;
 using std::getline;
 
-string private_test;
+boost::filesystem::path private_test;
 
 struct TestConfig
 {
@@ -39,24 +47,27 @@ struct TestConfig
                } 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 (string a, string b)
+check_text (boost::filesystem::path a, boost::filesystem::path b)
 {
-       if (access (a.c_str(), F_OK) == -1) {
+       if (!boost::filesystem::exists (a)) {
                cerr << "File not found: " << a << "\n";
        }
 
-       if (access (b.c_str(), F_OK) == -1) {
+       if (!boost::filesystem::exists (b)) {
                cerr << "File not found: " << b << "\n";
        }
-       
-       BOOST_CHECK_EQUAL (access (a.c_str(), F_OK), 0);
-       
+
+       BOOST_CHECK (boost::filesystem::exists (a));
+       BOOST_CHECK (boost::filesystem::exists (b));
+
        ifstream p (a.c_str ());
        ifstream q (b.c_str ());
 
@@ -71,3 +82,42 @@ check_text (string a, string b)
        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) {
+                       string const s = String::compose (
+                               "Files differ at offset %1; reference is %2, check is %3", (offset + i), ((int) ref_buffer[i]), ((int) check_buffer[i])
+                               );
+                       BOOST_CHECK_MESSAGE (ref_buffer[i] == check_buffer[i], s);
+               }
+
+               offset += this_time;
+       }
+
+       delete[] ref_buffer;
+       delete[] check_buffer;
+
+       fclose (ref_file);
+       fclose (check_file);
+}