From 61719634161bd4e5d9eb118c44ab528190693bd6 Mon Sep 17 00:00:00 2001 From: Manuel Gunther <siebenkopf@googlemail.com> Date: Tue, 13 Sep 2016 18:30:34 -0600 Subject: [PATCH] Implemented C++ test case (still working) using HDF5 interface --- bob/io/base/test.cpp | 49 ++++++++++++++++++++++++++++++++--------- bob/io/base/test_cpp.py | 13 ++++++++--- 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/bob/io/base/test.cpp b/bob/io/base/test.cpp index 607a383..2b0ff8a 100644 --- a/bob/io/base/test.cpp +++ b/bob/io/base/test.cpp @@ -8,26 +8,55 @@ #include <bob.io.base/api.h> #include <bob.extension/documentation.h> +#include <boost/format.hpp> +#include <boost/filesystem.hpp> + static auto s_test_api = bob::extension::FunctionDoc( "_test_api", "Some tests for API functions" ) -.add_prototype(""); - -static PyObject* _test_api(PyObject*){ +.add_prototype("tempdir") +.add_parameter("tempdir", "str", "A temporary directory to write data to") +; +static PyObject* _test_api(PyObject*, PyObject *args, PyObject* kwds){ BOB_TRY + static char** kwlist = s_test_api.kwlist(); + + const char* tempdir; + if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &tempdir)) return 0; + + blitz::Array<uint8_t, 1> test_data(5); + for (int i = 0; i < 5; ++i){ + test_data(i) = i+1; + } + + auto h5file = bob::io::base::CodecRegistry::instance()->findByExtension(".hdf5"); + + boost::filesystem::path hdf5(tempdir); hdf5 /= std::string("test.h5"); + + auto output = h5file(hdf5.string().c_str(), 'w'); + output->write(test_data); + output.reset(); + + auto input = h5file(hdf5.string().c_str(), 'r'); + blitz::Array<uint8_t,1> read_data = input->read<uint8_t,1>(0); + input.reset(); + + if (blitz::any(test_data - read_data)) + throw std::runtime_error("The CSV IO test did not succeed"); + Py_RETURN_NONE; BOB_CATCH_FUNCTION("_test_api", 0) } static PyMethodDef module_methods[] = { - { - s_test_api.name(), - (PyCFunction)_test_api, - METH_NOARGS, - s_test_api.doc(), - }, - {0} /* Sentinel */ + { + s_test_api.name(), + (PyCFunction)_test_api, + METH_VARARGS|METH_KEYWORDS, + s_test_api.doc(), + }, + {0} /* Sentinel */ }; PyDoc_STRVAR(module_docstr, "Tests for bob::io::base"); diff --git a/bob/io/base/test_cpp.py b/bob/io/base/test_cpp.py index 987a75e..a9f4e06 100644 --- a/bob/io/base/test_cpp.py +++ b/bob/io/base/test_cpp.py @@ -1,4 +1,11 @@ -from ._test import _test_api +from bob.io.base._test import _test_api -def test_cpp(): - _test_api() +import tempfile +import shutil + +def test_api(): + temp_dir = tempfile.mkdtemp() + try: + _test_api(temp_dir) + finally: + shutil.rmtree(temp_dir) -- GitLab