/*
 * THIS IS A GENERATED FILE!  DO NOT CHANGE THIS FILE!  CHANGE THE
 * CORRESPONDING TEMPLATE FILE, PLEASE!
 */

#include <shogun/base/SGObject.h>
#include <shogun/base/class_list.h>
#include <shogun/io/SerializableJsonFile.h>
#include <unistd.h>
#include <gtest/gtest.h>

using namespace shogun;

#ifdef HAVE_JSON
{% set ignores = ['RationalApproximationIndividualJob',
	'CCSOSVM',
	'TwoStateModel'] %}

{% for class in classes %}
{% if class in ignores or class.startswith('GUI') %}
TEST(SerializationJSON, DISABLED_{{class}})
{% else %}
TEST(SerializationJSON, {{class}})
{% endif %}
{
	std::string class_name("{{class}}");
	std::string file_template = "/tmp/shogun-unittest-serialization-json-" + class_name + ".XXXXXX";
	char* filename = mktemp(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
	CSerializableJsonFile *file=new CSerializableJsonFile(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 CSerializableJsonFile(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 since json is lossy
	float64_t accuracy=1e-6;
	ASSERT(object->equals(deserializedObject, accuracy, true));

	SG_UNREF(object)
	SG_UNREF(deserializedObject);

	int delete_success = unlink(filename);
	ASSERT_EQ(0, delete_success);
}
{% endfor %}

{% for class in template_classes %}
{% for type in types %}
{% if class in ignores %}
TEST(SerializationJSON,DISABLED_{{class}}_{{type}})
{% else %}
TEST(SerializationJSON,{{class}}_{{type}})
{% endif %}
{
	std::string class_name("{{class}}");
	std::string file_template = "/tmp/shogun-unittest-serialization-json-" + class_name + "_{{type}}" + ".XXXXXX";
	char* filename = mktemp(const_cast<char*>(file_template.c_str()));
	CSGObject* object = new_sgserializable(class_name.c_str(), {{type}});
	ASSERT_TRUE(object != NULL);

	// save object to an ascii file
	CSerializableJsonFile *file=new CSerializableJsonFile(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 CSerializableJsonFile(filename, 'r');
	CSGObject* deserializedObject = new_sgserializable(class_name.c_str(), {{type}});
	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 since json is lossy
	float64_t accuracy=1e-6;
	ASSERT(object->equals(deserializedObject, accuracy, true));

	SG_UNREF(object)
	SG_UNREF(deserializedObject);

	int delete_success = unlink(filename);
	ASSERT_EQ(0, delete_success);
}
{% endfor %}
{% endfor %}

#endif /* HAVE_JSON */
