add test fixture and image comparison

This commit is contained in:
Konstantin Käfer
2014-07-07 20:30:46 -07:00
parent 501af4a380
commit 49b0f098c2
16 changed files with 134 additions and 20 deletions

View File

@@ -35,6 +35,10 @@ test/%: build/test/Makefile
$(MAKE) -C build/test BUILDTYPE=$(BUILDTYPE) V=$(V) $*
(cd build/$(BUILDTYPE) && exec ./test_$*)
# build Mac OS X project for Xcode
xtest: config.gypi clear_xcode_cache node
deps/run_gyp test/test.gyp --depth=. -Goutput_dir=.. --generator-output=./build -f xcode
open ./build/test/test.xcodeproj
##### Makefile builds ##########################################################

View File

@@ -13,7 +13,7 @@ std::string compress_png(int width, int height, void *rgba, bool flip = false);
class Image {
public:
Image(const std::string &img);
Image(const std::string &img, bool flip = false);
~Image();
inline const char *getData() const { return img; }

View File

@@ -7,7 +7,7 @@
{
'target_name': 'shaders',
'type': 'none',
# 'hard_dependency': 1,
'hard_dependency': 1,
'actions': [
{
'action_name': 'Build Shaders',

View File

@@ -84,7 +84,7 @@ void warningHandler(png_structp, png_const_charp error_msg) {
fprintf(stderr, "PNG: %s\n", error_msg);
}
Image::Image(const std::string &data) {
Image::Image(const std::string &data, bool flip) {
Buffer buffer(data);
if (buffer.length < 8 || !png_check_sig((const png_bytep)buffer.data, 8)) {
@@ -148,7 +148,7 @@ Image::Image(const std::string &data) {
png_bytep *rows = nullptr;
} pointers(height);
for (unsigned i = 0; i < height; ++i) {
pointers.rows[i] = (png_bytep)(surface + (i * rowbytes));
pointers.rows[flip ? height - 1 - i : i] = (png_bytep)(surface + (i * rowbytes));
}
// Read image data

100
test/fixtures/fixture_request.cpp vendored Normal file
View File

@@ -0,0 +1,100 @@
#include <llmr/platform/platform.hpp>
#include <llmr/platform/request.hpp>
#include <llmr/util/uv.hpp>
const std::string basename = []{
std::string basename = __FILE__;
basename.erase(basename.find_last_of("/"));
return basename;
}();
namespace llmr {
std::shared_ptr<platform::Request>
platform::request_http(const std::string &url,
std::function<void(Response *)> callback,
std::shared_ptr<uv::loop> loop) {
uv_loop_t *l = nullptr;
if (loop) {
l = **loop;
} else {
l = uv_default_loop();
}
std::string clean_url = basename + "/" + url;
auto pos = clean_url.find("://");
if (pos != std::string::npos) {
clean_url.replace(pos, 3, "/");
}
std::replace(clean_url.begin(), clean_url.end(), '+', ' ');
std::shared_ptr<Request> req = std::make_shared<Request>(url, callback, loop);
int err;
uv_fs_t open_req;
err = uv_fs_open(l, &open_req, clean_url.c_str(), O_RDONLY, S_IRUSR, nullptr);
uv_fs_req_cleanup(&open_req);
if (err < 0) {
req->res->code = err;
req->res->error_message = uv_strerror(err);
req->complete();
return req;
}
uv_file fd = err;
uv_fs_t stat_req;
uv_fs_fstat(l, &stat_req, fd, nullptr);
uv_fs_req_cleanup(&stat_req);
err = uv_fs_fstat(l, &stat_req, fd, nullptr);
if (err < 0) {
req->res->code = err;
req->res->error_message = uv_strerror(err);
req->complete();
return req;
}
const uint64_t size = static_cast<const uv_stat_t*>(stat_req.ptr)->st_size;
std::string body;
body.resize(size);
uv_buf_t uvbuf = uv_buf_init(const_cast<char *>(body.data()), body.size());
uv_fs_t read_req;
err = uv_fs_read(l, &read_req, fd, &uvbuf, 1, 0, nullptr);
uv_fs_req_cleanup(&read_req);
if (err < 0) {
req->res->code = err;
req->res->error_message = uv_strerror(err);
req->complete();
return req;
}
uv_fs_t close_req;
err = uv_fs_close(l, &close_req, fd, nullptr);
uv_fs_req_cleanup(&close_req);
if (err < 0) {
req->res->code = err;
req->res->error_message = uv_strerror(err);
req->complete();
return req;
}
req->res->body.swap(body);
req->res->code = 200;
req->complete();
return req;
}
void platform::cancel_request_http(const std::shared_ptr<Request> &req) {
if (req) {
req->cancelled = true;
}
}
} // end namespace llmr

File diff suppressed because one or more lines are too long

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

BIN
test/fixtures/result/out.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 267 KiB

View File

@@ -10,9 +10,15 @@
#include <iostream>
#include <fstream>
const std::string basename = []{
std::string basename = __FILE__;
basename.erase(basename.find_last_of("/"));
return basename;
}();
TEST(Headless, initialize) {
const int width = 1024;
const int height = 768;
const unsigned int width = 1024;
const unsigned int height = 768;
llmr::util::timer timer;
@@ -32,12 +38,12 @@ TEST(Headless, initialize) {
map.setStyleJSON(stylejson.str());
timer.report("map style");
map.setLonLatZoom(0, 0, 2);
map.setAngle(0);
map.setDebug(false);
timer.report("map style");
// Run the loop. It will terminate when we don't have any further listeners.
map.run();
@@ -47,17 +53,18 @@ TEST(Headless, initialize) {
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
timer.report("gl readpixels");
std::string result = llmr::util::compress_png(width, height, pixels, true);
timer.report("compress png");
llmr::util::write_file("out.png", result);
timer.report("save file");
std::ifstream imagefile(basename + "/fixtures/result/out.png");
ASSERT_TRUE(imagefile.good());
std::stringstream image;
image << imagefile.rdbuf();
llmr::util::Image reference(image.str(), true);
ASSERT_EQ(reference.getWidth(), width);
ASSERT_EQ(reference.getHeight(), height);
ASSERT_EQ(0, std::memcmp(pixels, reference.getData(), width * height * 4));
delete[] pixels;
timer.report("destruct");
}

View File

@@ -15,6 +15,11 @@
'-framework OpenGL',
],
},
'xcode_settings': {
'OTHER_LDFLAGS': [
'-framework OpenGL',
],
},
}, {
'link_settings': {
'libraries': [
@@ -145,16 +150,13 @@
"./headless.cpp",
"../common/headless_view.hpp",
"../common/headless_view.cpp",
"../common/curl_request.hpp",
"../common/curl_request.cpp",
"./fixtures/fixture_request.cpp",
],
"dependencies": [
"../deps/gtest/gtest.gyp:gtest",
"../llmr.gyp:llmr-x86",
"link_gl",
"link_curl",
'../llmr.gyp:copy_default_stylesheet',
'../llmr.gyp:copy_certificate_bundle',
]
},
{