/*
 * 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/SerializableXmlFile.h>
#include <unistd.h>
#include <gtest/gtest.h>

using namespace shogun;

#ifdef HAVE_XML

{% set ignores = ['ScalarResult', 'StoreScalarAggregator'] %}

{% for class in classes %}
{% if class in ignores or class.startswith('GUI') %}
TEST(SerializationXML, DISABLED_{{class}})
{% else %}
TEST(SerializationXML, {{class}})
{% endif %}
{
	std::string class_name("{{class}}");
	std::string file_template = "/tmp/shogun-unittest-serialization-xml-" + 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
	CSerializableXmlFile *file=new CSerializableXmlFile(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 CSerializableXmlFile(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 a certain accuracy, since XML is lossy
	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);
}
{% endfor %}

{% for class in template_classes %}
{% for type in types %}
{% if class in ignores %}
TEST(SerializationXML,DISABLED_{{class}}_{{type}})
{% else %}
TEST(SerializationXML,{{class}}_{{type}})
{% endif %}
{
	std::string class_name("{{class}}");
	std::string file_template = "/tmp/shogun-unittest-serialization-xml-" + 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
	CSerializableXmlFile *file=new CSerializableXmlFile(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 CSerializableXmlFile(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 a certain accuracy, since XML is lossy
	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);
}
{% endfor %}
{% endfor %}

#endif /* HAVE_XML */
