Skip to content

Instantly share code, notes, and snippets.

@MikeLing
Last active June 14, 2017 11:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MikeLing/665b961fae759a58535ac07b1b93e39a to your computer and use it in GitHub Desktop.
Save MikeLing/665b961fae759a58535ac07b1b93e39a to your computer and use it in GitHub Desktop.
#include <gtest/gtest.h>
#include <shogun/lib/DynamicArray.h>
#include <shogun/mathematics/Math.h>
#include "utils/Utils.h"
#include <unistd.h>
#include <shogun/io/SerializableAsciiFile.h>
#include <shogun/base/class_list.h>
using namespace shogun;
#include <shogun/base/SGObject.h>
class CMockObject : public CSGObject
{
public:
CMockObject() : CSGObject()
{
mock_array.push_back(1);
mock_array.push_back(2);
mock_array.push_back(3);
init_params();
}
const char* get_name() const { return "MockObject"; }
protected:
void init_params()
{
register_param("array",(CSGObject** )&mock_array);
}
private:
CDynamicArray<int32_t> mock_array;
};
TEST(CDynamicArray, serializable_mockobject)
{
std::string class_name("serializable_mockobject");
std::string file_template = "/tmp/shogun-unittest-serialization-ascii-" + class_name + ".XXXXXX";
char* filename = mktemp_cst(const_cast<char*>(file_template.c_str()));
CSGObject* object = new_sgserializable(class_name.c_str(), PT_NOT_GENERIC);
ASSERT_TRUE(object != NULL);
// save object to an ascii file
CSerializableAsciiFile *file=new CSerializableAsciiFile(filename, 'w');
bool save_success = object->save_serializable(file);
file->close();
SG_UNREF(file);
ASSERT_TRUE(save_success);
// load parameter from an ascii file
file=new CSerializableAsciiFile(filename, 'r');
CSGObject* deserializedObject = new_sgserializable(class_name.c_str(), PT_NOT_GENERIC);
ASSERT_TRUE(deserializedObject != NULL);
bool load_success = deserializedObject->load_serializable(file);
file->close();
SG_UNREF(file);
ASSERT_TRUE(load_success);
// check whether they are equal, up to accuracy of lossy ASCII
float64_t accuracy=1e-14;
ASSERT_TRUE(object->equals(deserializedObject, accuracy));
SG_UNREF(object)
SG_UNREF(deserializedObject);
int delete_success = unlink(filename);
ASSERT_EQ(0, delete_success);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment