summaryrefslogtreecommitdiff
path: root/slicer/test/helpers.cpp
blob: 58f47bc3cb1c7aa9ffcc3a157ea7088d56bfe004 (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
#include "helpers.h"
#include <stdexcept>
#include <stdlib.h>
#include <dlfcn.h>
#include <fstream>
#include <boost/test/test_tools.hpp>

void
system(const std::string & cmd)
{
	if (system(cmd.c_str())) {
		fprintf(stderr, "Failed to execute:\n\t%s\n", cmd.c_str());
		throw std::runtime_error(cmd);
	}
}

void
diff(const boost::filesystem::path & left, const boost::filesystem::path & right)
{
	std::ifstream fl(left.string());
	std::ifstream fr(right.string());

	std::string l, r;
	std::copy_if(std::istreambuf_iterator<char>(fl), std::istreambuf_iterator<char>(), back_inserter(l), [](char x){ return !isspace(x); });
	std::copy_if(std::istreambuf_iterator<char>(fr), std::istreambuf_iterator<char>(), back_inserter(r), [](char x){ return !isspace(x); });

	BOOST_REQUIRE_EQUAL(l, r);
}

void *
loadlib(const boost::filesystem::path & so)
{
	auto handle = dlopen(so.string().c_str(), RTLD_NOW | RTLD_GLOBAL);
	if (!handle) {
		throw std::runtime_error(dlerror());
	}
	return handle;
}

void
closelib(void * handle)
{
	if (dlclose(handle)) {
		throw std::runtime_error(dlerror());
	}
}