diff options
author | randomdan <randomdan@localhost> | 2014-11-06 01:14:42 +0000 |
---|---|---|
committer | randomdan <randomdan@localhost> | 2014-11-06 01:14:42 +0000 |
commit | 318a44757d7be4f4885b502e46f98eb37b53a839 (patch) | |
tree | 263ac67c9858e3683d9619c481f00e3078e7494b /slicer | |
parent | Separate serializer and deserializer class hierarchies (diff) | |
download | slicer-318a44757d7be4f4885b502e46f98eb37b53a839.tar.bz2 slicer-318a44757d7be4f4885b502e46f98eb37b53a839.tar.xz slicer-318a44757d7be4f4885b502e46f98eb37b53a839.zip |
Big refactor to use Boost UTF
Diffstat (limited to 'slicer')
-rw-r--r-- | slicer/test/Jamfile.jam | 41 | ||||
-rw-r--r-- | slicer/test/conversions.cpp | 47 | ||||
-rw-r--r-- | slicer/test/do-slicer.cpp | 89 | ||||
-rw-r--r-- | slicer/test/fileStructure.cpp | 23 | ||||
-rw-r--r-- | slicer/test/fileStructure.h | 25 | ||||
-rw-r--r-- | slicer/test/preprocessor.cpp | 50 | ||||
-rw-r--r-- | slicer/test/run-slicer.cpp | 289 | ||||
-rw-r--r-- | slicer/test/serializers.cpp | 293 |
8 files changed, 461 insertions, 396 deletions
diff --git a/slicer/test/Jamfile.jam b/slicer/test/Jamfile.jam index 4a149a8..ffedb00 100644 --- a/slicer/test/Jamfile.jam +++ b/slicer/test/Jamfile.jam @@ -6,6 +6,7 @@ lib Ice ; lib IceUtil ; lib boost_system ; lib boost_filesystem ; +lib boost_utf : : <name>boost_unit_test_framework ; lib slicer-test : : <name>slicertypes <search>bin/slicer ; lib types : @@ -14,49 +15,53 @@ lib types : <library>pthread <library>Ice <library>IceUtil + : : + <library>pthread + <library>Ice + <library>IceUtil ; lib common : helpers.cpp + conversions.cpp + fileStructure.cpp ../../libmisc/misc.cpp : <library>dl + <library>types <library>boost_system <library>boost_filesystem + <library>boost_utf + <implicit-dependency>types : : + <library>types <library>boost_filesystem <library>boost_system <include>../../libmisc + <define>BOOST_TEST_DYN_LINK + <library>boost_utf + <implicit-dependency>types ; -unit-test do-slicer : - types - common - do-slicer.cpp +unit-test preprocess : + preprocessor.cpp : <linkflags>-rdynamic <library>dl + <library>common <include>.. - <library>pthread - <library>Ice - <library>IceUtil <library>../slicer//slicer - <implicit-dependency>types ; -unit-test run-slicer : - run-slicer.cpp - types - common +unit-test serializers : + serializers.cpp : - <implicit-dependency>types - <dependency>do-slicer + <dependency>preprocess <library>slicer-test + <library>common <include>.. - <library>pthread - <library>Ice - <library>IceUtil <library>../slicer//slicer <library>../xml//slicer-xml <library>../json//slicer-json -; + ; + diff --git a/slicer/test/conversions.cpp b/slicer/test/conversions.cpp new file mode 100644 index 0000000..a6c2c06 --- /dev/null +++ b/slicer/test/conversions.cpp @@ -0,0 +1,47 @@ +#include "types.h" +#include <boost/numeric/conversion/cast.hpp> + +#define SHORT(x) boost::numeric_cast< ::Ice::Short >(x) + +namespace Slicer { + boost::posix_time::ptime + dateTimeToPTime(const ::TestModule::DateTime &) + { + throw std::runtime_error("Not implemented"); + } + + ::TestModule::DateTime + ptimeToDateTime(const boost::posix_time::ptime &) + { + throw std::runtime_error("Not implemented"); + } + + std::string + dateTimeToString(const ::TestModule::DateTime & in) + { + char buf[BUFSIZ]; + struct tm tm({ in.second, in.minute, in.hour, in.day, in.month, in.year, 0, 0, 0 +#ifdef _BSD_SOURCE + , 0, 0 +#endif + }); + mktime(&tm); + auto len = strftime(buf, BUFSIZ, "%Y-%b-%d %H:%M:%S", &tm); + return std::string(buf, len); + } + + ::TestModule::DateTime + stringToDateTime(const std::string & in) + { + struct tm tm; + memset(&tm, 0, sizeof(struct tm)); + auto end = strptime(in.c_str(), "%Y-%b-%d %H:%M:%S", &tm); + if (!end || *end) { + throw std::runtime_error("Invalid date string: " + in); + } + return ::TestModule::DateTime({ + SHORT(tm.tm_year), SHORT(tm.tm_mon), SHORT(tm.tm_mday), + SHORT(tm.tm_hour), SHORT(tm.tm_min), SHORT(tm.tm_sec)}); + } +} + diff --git a/slicer/test/do-slicer.cpp b/slicer/test/do-slicer.cpp deleted file mode 100644 index 7607ff2..0000000 --- a/slicer/test/do-slicer.cpp +++ /dev/null @@ -1,89 +0,0 @@ -#include <slicer/parser.h> -#include <boost/filesystem/convenience.hpp> -#include <boost/filesystem/operations.hpp> -#include <boost/format.hpp> -#include <boost/function.hpp> -#include "../../libmisc/misc.h" -#include <types.h> -#include "helpers.h" - -namespace fs = boost::filesystem; - -namespace Slicer { - // These are the conversion helpers defined used in types.ice, they're not called in this test, but - // need to exist for the dynamic load to complete - boost::posix_time::ptime - dateTimeToPTime(const ::TestModule::DateTime &) - { - throw std::runtime_error("Not implemented"); - } - - ::TestModule::DateTime - ptimeToDateTime(const boost::posix_time::ptime &) - { - throw std::runtime_error("Not implemented"); - } - - std::string - dateTimeToString(const ::TestModule::DateTime &) - { - throw std::runtime_error("Not implemented"); - } - - ::TestModule::DateTime - stringToDateTime(const std::string &) - { - throw std::runtime_error("Not implemented"); - } -} - -int -main(int, char ** argv) -{ - const fs::path me = argv[0]; - const fs::path base = "types"; - const fs::path bjamout = me.parent_path(); - const fs::path root = me.parent_path().parent_path().parent_path().parent_path(); - fprintf(stderr, "root -- %s\n", root.string().c_str()); - const fs::path slice = fs::change_extension(root / base, ".ice"); - fprintf(stderr, "slice - %s\n", slice.string().c_str()); - - // Tmp - const fs::path tmp = root / "bin" / "slicer"; - fprintf(stderr, "tmp --- %s\n", tmp.string().c_str()); - fs::create_directory(tmp); - - // Slicer - const fs::path cpp = fs::change_extension(tmp / base, ".cpp"); - fprintf(stderr, "cpp --- %s\n", cpp.string().c_str()); - fs::remove(cpp); - Slicer::Slicer::Apply(slice, cpp); - - // Compile - const fs::path obj = fs::change_extension(tmp / base, ".o"); - fprintf(stderr, "obj --- %s\n", obj.string().c_str()); - system(stringbf( - "g++ -Os -fPIC -c -std=c++0x -I tmp -I /usr/include/Ice -I /usr/include/IceUtil -I %s -I %s %s -o %s", - bjamout, - root / "..", - cpp, obj)); - - // Link - const fs::path so = fs::change_extension(tmp / ("libslicer" + slice.filename().string()), ".so"); - fprintf(stderr, "so ---- %s\n", so.string().c_str()); - system(stringbf( - "g++ -shared -lIce -lIceUtil %s/lib%s.so %s -o %s", - bjamout, base, - obj, so)); - - // Load - fprintf(stderr, "load -- %s\n", so.string().c_str()); - auto handle = loadlib(so); - - // Unload - fprintf(stderr, "unload %p\n", handle); - closelib(handle); - - return 0; -} - diff --git a/slicer/test/fileStructure.cpp b/slicer/test/fileStructure.cpp new file mode 100644 index 0000000..d586786 --- /dev/null +++ b/slicer/test/fileStructure.cpp @@ -0,0 +1,23 @@ +#include "fileStructure.h" +#include <boost/test/unit_test.hpp> +#include <boost/filesystem/convenience.hpp> + +FileStructure::FileStructure() : + me(boost::filesystem::canonical("/proc/self/exe")), + base("types"), + bjamout(me.parent_path()), + root(me.parent_path().parent_path().parent_path().parent_path()), + slice(fs::change_extension(root / base, ".ice")), + tmp(root / "bin" / "slicer"), + tmpf(tmp / "byFile"), + tmph(tmp / "byHelper") +{ + fs::create_directory(tmp); + fs::create_directory(tmpf); + fs::create_directory(tmph); +} + +FileStructure::~FileStructure() +{ +} + diff --git a/slicer/test/fileStructure.h b/slicer/test/fileStructure.h new file mode 100644 index 0000000..475146c --- /dev/null +++ b/slicer/test/fileStructure.h @@ -0,0 +1,25 @@ +#ifndef SLICER_TEST_FILESTRUCTURE +#define SLICER_TEST_FILESTRUCTURE + +#include <boost/filesystem/path.hpp> + +namespace fs = boost::filesystem; + +class FileStructure { + public: + FileStructure(); + ~FileStructure(); + + protected: + const fs::path me; + const fs::path base; + const fs::path bjamout; + const fs::path root; + const fs::path slice; + const fs::path tmp; + const fs::path tmpf; + const fs::path tmph; +}; + +#endif + diff --git a/slicer/test/preprocessor.cpp b/slicer/test/preprocessor.cpp new file mode 100644 index 0000000..f4c75fc --- /dev/null +++ b/slicer/test/preprocessor.cpp @@ -0,0 +1,50 @@ +#define BOOST_TEST_MODULE preprocess +#include <boost/test/unit_test.hpp> + +#include <slicer/parser.h> +#include <boost/filesystem/convenience.hpp> +#include <boost/filesystem/operations.hpp> +#include <boost/format.hpp> +#include <boost/function.hpp> +#include <misc.h> +#include <types.h> +#include "helpers.h" +#include "fileStructure.h" + +namespace fs = boost::filesystem; + +BOOST_FIXTURE_TEST_SUITE ( preprocessor, FileStructure ); + +BOOST_AUTO_TEST_CASE( slicer_test_ice ) +{ + const fs::path cpp = fs::change_extension(tmp / base, ".cpp"); + BOOST_TEST_CHECKPOINT("cpp: " << cpp); + fs::remove(cpp); + Slicer::Slicer::Apply(slice, cpp); + + const fs::path obj = fs::change_extension(tmp / base, ".o"); + const std::string compile = stringbf( + "g++ -Os -fPIC -c -std=c++0x -I tmp -I /usr/include/Ice -I /usr/include/IceUtil -I %s -I %s %s -o %s", + bjamout, + root / "..", + cpp, obj); + BOOST_TEST_CHECKPOINT("compile: " << compile); + system(compile); + + const fs::path so = fs::change_extension(tmp / ("libslicer" + slice.filename().string()), ".so"); + const std::string link = stringbf( + "g++ -shared -lIce -lIceUtil %s/lib%s.so %s -o %s", + bjamout, base, + obj, so); + BOOST_TEST_CHECKPOINT("link: " << link); + system(link); + + BOOST_TEST_CHECKPOINT("load: " << so); + auto handle = loadlib(so); + + BOOST_TEST_CHECKPOINT("unload: " << handle); + closelib(handle); +} + +BOOST_AUTO_TEST_SUITE_END(); + diff --git a/slicer/test/run-slicer.cpp b/slicer/test/run-slicer.cpp deleted file mode 100644 index 8b56eed..0000000 --- a/slicer/test/run-slicer.cpp +++ /dev/null @@ -1,289 +0,0 @@ -#include <slicer/parser.h> -#include <slicer/slicer.h> -#include <slicer/modelParts.h> -#include <xml/serializer.h> -#include <libxml2/libxml/parser.h> -#include <json/serializer.h> -#include <boost/filesystem/convenience.hpp> -#include <boost/filesystem/operations.hpp> -#include <boost/numeric/conversion/cast.hpp> -#include <boost/format.hpp> -#include <boost/function.hpp> -#include <boost/assert.hpp> -#include <types.h> -#include <misc.h> -#include <fstream> -#include "helpers.h" - -namespace fs = boost::filesystem; -#define SHORT(x) boost::numeric_cast< ::Ice::Short >(x) - -namespace Slicer { - boost::posix_time::ptime - dateTimeToPTime(const ::TestModule::DateTime &) - { - throw std::runtime_error("Not implemented"); - } - - ::TestModule::DateTime - ptimeToDateTime(const boost::posix_time::ptime &) - { - throw std::runtime_error("Not implemented"); - } - - std::string - dateTimeToString(const ::TestModule::DateTime & in) - { - char buf[BUFSIZ]; - struct tm tm({ in.second, in.minute, in.hour, in.day, in.month, in.year, 0, 0, 0 -#ifdef _BSD_SOURCE - , 0, 0 -#endif - }); - mktime(&tm); - auto len = strftime(buf, BUFSIZ, "%Y-%b-%d %H:%M:%S", &tm); - return std::string(buf, len); - } - - ::TestModule::DateTime - stringToDateTime(const std::string & in) - { - struct tm tm; - memset(&tm, 0, sizeof(struct tm)); - auto end = strptime(in.c_str(), "%Y-%b-%d %H:%M:%S", &tm); - if (!end || *end) { - throw std::runtime_error("Invalid date string: " + in); - } - return ::TestModule::DateTime({ - SHORT(tm.tm_year), SHORT(tm.tm_mon), SHORT(tm.tm_mday), - SHORT(tm.tm_hour), SHORT(tm.tm_min), SHORT(tm.tm_sec)}); - } -} - -template<typename T, typename DeserializerIn> -void -verifyByFile(const fs::path & root, const fs::path & tmp, const fs::path & infile, const boost::function<void(const T &)> & check = NULL) -{ - const fs::path input = root / "initial" / infile; - const fs::path output = tmp / infile; - const fs::path outputJson = tmp / fs::change_extension(infile, "json"); - const fs::path outputXml = tmp / fs::change_extension(infile, "xml"); - - fprintf(stderr, "%s : Deserialize\n", input.string().c_str()); - IceInternal::Handle<T> p = Slicer::Deserialize<DeserializerIn, T>(input); - if (check) { - fprintf(stderr, "%s : Check1\n", input.string().c_str()); - check(*p); - } - fprintf(stderr, "%s : Serialize -> %s\n", input.string().c_str(), outputJson.string().c_str()); - Slicer::Serialize<Slicer::JsonFileSerializer>(p, outputJson); - fprintf(stderr, "%s : Serialize -> %s\n", input.string().c_str(), outputXml.string().c_str()); - Slicer::Serialize<Slicer::XmlFileSerializer>(p, outputXml); - if (check) { - fprintf(stderr, "%s : Check2\n", input.string().c_str()); - check(*p); - } - fprintf(stderr, "%s : OK\n", input.string().c_str()); - - system(stringbf("diff -w %s %s", input, output)); -} - -template<typename T, typename Deserializer, typename Serializer, typename Internal> -void -verifyByHelper(const fs::path & root, const fs::path & tmp, const fs::path & infile, - const boost::function<Internal(const fs::path &)> & in, - const boost::function<void(const Internal &, const fs::path &)> & out, - const boost::function<void(Internal &)> & ifree, - const boost::function<void(const T &)> & check = NULL) -{ - const fs::path input = root / "initial" / infile; - const fs::path output = tmp / infile; - - fprintf(stderr, "%s : Read\n", input.string().c_str()); - Internal docRead = in(input); - - fprintf(stderr, "%s : Deserialize\n", input.string().c_str()); - IceInternal::Handle<T> p = Slicer::Deserialize<Deserializer, T>(docRead); - ifree(docRead); - if (check) { - fprintf(stderr, "%s : Check1\n", input.string().c_str()); - check(*p); - } - - fprintf(stderr, "%s : Serialize\n", input.string().c_str()); - Internal docWrite; - Slicer::Serialize<Serializer>(p, docWrite); - if (check) { - fprintf(stderr, "%s : Check2\n", input.string().c_str()); - check(*p); - } - - fprintf(stderr, "%s : Write\n", input.string().c_str()); - out(docWrite, output); - ifree(docWrite); - - fprintf(stderr, "%s : OK\n", input.string().c_str()); - - system(stringbf("diff -w %s %s", input, output)); -} - -void -checkBuiltIns_valuesCorrect(const TestModule::BuiltIns & bt) -{ - BOOST_ASSERT(bt.mbool); - BOOST_ASSERT(bt.mbyte == 4); - BOOST_ASSERT(bt.mshort == 40); - BOOST_ASSERT(bt.mint == 80); - BOOST_ASSERT(bt.mlong == 800); - BOOST_ASSERT(bt.mfloat == 3.125); - BOOST_ASSERT(bt.mdouble == 3.0625); - BOOST_ASSERT(bt.mstring == "Sample text"); -} - -void -checkInherits_types(const TestModule::InheritanceCont & i) -{ - BOOST_ASSERT(i.b); - BOOST_ASSERT(TestModule::D1Ptr::dynamicCast(i.b)); - BOOST_ASSERT(TestModule::D1Ptr::dynamicCast(i.b)->a == 1); - BOOST_ASSERT(TestModule::D1Ptr::dynamicCast(i.b)->b == 2); - BOOST_ASSERT(i.bs.size() == 3); - BOOST_ASSERT(i.bs[0]); - BOOST_ASSERT(TestModule::D2Ptr::dynamicCast(i.bs[0])); - BOOST_ASSERT(TestModule::D2Ptr::dynamicCast(i.bs[0])->a == 1); - BOOST_ASSERT(TestModule::D2Ptr::dynamicCast(i.bs[0])->c == 100); - BOOST_ASSERT(i.bs[1]); - BOOST_ASSERT(TestModule::D3Ptr::dynamicCast(i.bs[1])); - BOOST_ASSERT(TestModule::D3Ptr::dynamicCast(i.bs[1])->a == 2); - BOOST_ASSERT(TestModule::D3Ptr::dynamicCast(i.bs[1])->c == 100); - BOOST_ASSERT(TestModule::D3Ptr::dynamicCast(i.bs[1])->d == 200); - BOOST_ASSERT(i.bs[2]); - BOOST_ASSERT(i.bs[2]->a == 3); - BOOST_ASSERT(!TestModule::D1Ptr::dynamicCast(i.bs[2])); - BOOST_ASSERT(!TestModule::D2Ptr::dynamicCast(i.bs[2])); - BOOST_ASSERT(!TestModule::D3Ptr::dynamicCast(i.bs[2])); - BOOST_ASSERT(i.bm.size() == 3); - BOOST_ASSERT(TestModule::D1Ptr::dynamicCast(i.bm.find(10)->second)); - BOOST_ASSERT(TestModule::D3Ptr::dynamicCast(i.bm.find(12)->second)); - BOOST_ASSERT(!TestModule::D1Ptr::dynamicCast(i.bm.find(14)->second)); - BOOST_ASSERT(!TestModule::D2Ptr::dynamicCast(i.bm.find(14)->second)); - BOOST_ASSERT(!TestModule::D3Ptr::dynamicCast(i.bm.find(14)->second)); -} - -void -checkOptionals_notset(const TestModule::Optionals & opts) -{ - BOOST_ASSERT(!opts.optSimple); - BOOST_ASSERT(!opts.optStruct); - BOOST_ASSERT(!opts.optClass); - BOOST_ASSERT(!opts.optSeq); - BOOST_ASSERT(!opts.optDict); -} - -void -checkOptionals_areset(const TestModule::Optionals & opts) -{ - BOOST_ASSERT(opts.optSimple); - BOOST_ASSERT(opts.optSimple == 4); - BOOST_ASSERT(opts.optStruct); - BOOST_ASSERT(opts.optStruct->a == 1); - BOOST_ASSERT(opts.optStruct->b == 2); - BOOST_ASSERT(opts.optClass); - BOOST_ASSERT((*opts.optClass)->a == 1); - BOOST_ASSERT((*opts.optClass)->b == 2); - BOOST_ASSERT(opts.optSeq->size() == 2); - BOOST_ASSERT((*opts.optSeq)[0]->a == 3); - BOOST_ASSERT((*opts.optSeq)[0]->b == 4); - BOOST_ASSERT((*opts.optSeq)[1]->a == 5); - BOOST_ASSERT((*opts.optSeq)[1]->b == 6); - BOOST_ASSERT(opts.optDict); - BOOST_ASSERT(opts.optDict->size() == 2); - BOOST_ASSERT(opts.optDict->find(1) == opts.optDict->end()); - BOOST_ASSERT(opts.optDict->find(10) != opts.optDict->end()); - BOOST_ASSERT(opts.optDict->find(10)->second->a == 11); - BOOST_ASSERT(opts.optDict->find(10)->second->b == 12); - BOOST_ASSERT(opts.optDict->find(13) != opts.optDict->end()); - BOOST_ASSERT(opts.optDict->find(13)->second->a == 14); - BOOST_ASSERT(opts.optDict->find(13)->second->b == 15); -} - -xmlpp::Document * -readXml(const fs::path & path) -{ - return new xmlpp::Document(xmlParseFile(path.string().c_str())); -} - -void -writeXml(xmlpp::Document * const & doc, const fs::path & path) -{ - doc->write_to_file_formatted(path.string()); -} - -void -freeXml(xmlpp::Document * & doc) -{ - delete doc; -} - -json::Value -readJson(const fs::path & path) -{ - std::ifstream inFile(path.string()); - std::stringstream buffer; - buffer << inFile.rdbuf(); - Glib::ustring doc(buffer.str()); - Glib::ustring::const_iterator itr = doc.begin(); - return json::parseValue(itr); -} - -void -writeJson(const json::Value & value, const fs::path & path) -{ - std::ofstream outFile(path.string()); - json::serializeValue(value, outFile, "utf-8"); -} - -void -freeJson(json::Value &) -{ -} - -int -main(int, char ** argv) -{ - const fs::path me = argv[0]; - const fs::path base = "types"; - const fs::path bjamout = me.parent_path(); - const fs::path root = me.parent_path().parent_path().parent_path().parent_path(); - fprintf(stderr, "root -- %s\n", root.string().c_str()); - const fs::path slice = fs::change_extension(root / base, ".ice"); - fprintf(stderr, "slice - %s\n", slice.string().c_str()); - - // Tmp - const fs::path tmp = root / "bin" / "slicer"; - fprintf(stderr, "tmp --- %s\n", tmp.string().c_str()); - fs::create_directory(tmp); - const fs::path tmpf = tmp / "byFile"; - fs::create_directory(tmpf); - const fs::path tmph = tmp / "byHelper"; - fs::create_directory(tmph); - - // Execute - verifyByFile<TestModule::BuiltIns, Slicer::XmlFileDeserializer>(root, tmpf, "builtins.xml", checkBuiltIns_valuesCorrect); - verifyByFile<TestModule::Optionals, Slicer::XmlFileDeserializer>(root, tmpf, "optionals-notset.xml", checkOptionals_notset); - verifyByFile<TestModule::Optionals, Slicer::XmlFileDeserializer>(root, tmpf, "optionals-areset.xml", checkOptionals_areset); - verifyByFile<TestModule::InheritanceCont, Slicer::XmlFileDeserializer>(root, tmpf, "inherit-a.xml"); - verifyByFile<TestModule::InheritanceCont, Slicer::XmlFileDeserializer>(root, tmpf, "inherit-b.xml", checkInherits_types); - verifyByFile<TestModule::DateTimeContainer, Slicer::XmlFileDeserializer>(root, tmpf, "conv-datetime.xml"); - verifyByFile<TestModule::BuiltIns, Slicer::JsonFileDeserializer>(root, tmpf, "builtins2.json", checkBuiltIns_valuesCorrect); - verifyByFile<TestModule::Optionals, Slicer::JsonFileDeserializer>(root, tmpf, "optionals-areset2.json", checkOptionals_areset); - verifyByFile<TestModule::InheritanceCont, Slicer::JsonFileDeserializer>(root, tmpf, "inherit-c.json", checkInherits_types); - verifyByFile<TestModule::InheritanceCont2, Slicer::JsonFileDeserializer>(root, tmpf, "inherit-d.json"); - verifyByFile<TestModule::InheritanceContMapped, Slicer::JsonFileDeserializer>(root, tmpf, "inherit-mapped.json"); - - verifyByHelper<TestModule::Optionals, Slicer::JsonValueDeserializer, Slicer::JsonValueSerializer, json::Value>(root, tmph, "optionals-areset2.json", readJson, writeJson, freeJson, checkOptionals_areset); - verifyByHelper<TestModule::Optionals, Slicer::XmlDocumentDeserializer, Slicer::XmlDocumentSerializer, xmlpp::Document *>(root, tmph, "optionals-areset.xml", readXml, writeXml, freeXml, checkOptionals_areset); - - return 0; -} - diff --git a/slicer/test/serializers.cpp b/slicer/test/serializers.cpp new file mode 100644 index 0000000..74ecf40 --- /dev/null +++ b/slicer/test/serializers.cpp @@ -0,0 +1,293 @@ +#define BOOST_TEST_MODULE execute_serializers +#include <boost/test/unit_test.hpp> + +#include <slicer/parser.h> +#include <slicer/slicer.h> +#include <slicer/modelParts.h> +#include <xml/serializer.h> +#include <libxml2/libxml/parser.h> +#include <json/serializer.h> +#include <boost/filesystem/operations.hpp> +#include <boost/filesystem/convenience.hpp> +#include <boost/format.hpp> +#include <boost/function.hpp> +#include <types.h> +#include <misc.h> +#include <fstream> +#include "helpers.h" +#include "fileStructure.h" + +namespace fs = boost::filesystem; + +BOOST_TEST_DONT_PRINT_LOG_VALUE ( TestModule::ClassMap::const_iterator ) + +class FileBased : public FileStructure { + public: + template<typename T, typename DeserializerIn> + void + verifyByFile(const fs::path & infile, const boost::function<void(const T &)> & check = NULL) + { + const fs::path input = root / "initial" / infile; + const fs::path output = tmp / infile; + const fs::path outputJson = tmp / fs::change_extension(infile, "json"); + const fs::path outputXml = tmp / fs::change_extension(infile, "xml"); + + BOOST_TEST_CHECKPOINT("Deserialize: " << input); + IceInternal::Handle<T> p = Slicer::Deserialize<DeserializerIn, T>(input); + + if (check) { + BOOST_TEST_CHECKPOINT("Check1: " << input); + check(*p); + } + + BOOST_TEST_CHECKPOINT("Serialize " << input << " -> " << outputJson); + Slicer::Serialize<Slicer::JsonFileSerializer>(p, outputJson); + + BOOST_TEST_CHECKPOINT("Serialize " << input << " -> " << outputXml); + Slicer::Serialize<Slicer::XmlFileSerializer>(p, outputXml); + + if (check) { + BOOST_TEST_CHECKPOINT("Check2: " << input); + check(*p); + } + + BOOST_TEST_CHECKPOINT("Checksum: " << input << " === " << output); + system(stringbf("diff -w %s %s", input, output)); + } + + template<typename T, typename Deserializer, typename Serializer, typename Internal> + void + verifyByHelper(const fs::path & infile, + const boost::function<Internal(const fs::path &)> & in, + const boost::function<void(const Internal &, const fs::path &)> & out, + const boost::function<void(Internal &)> & ifree, + const boost::function<void(const T &)> & check = NULL) + { + const fs::path input = root / "initial" / infile; + const fs::path output = tmp / infile; + + BOOST_TEST_CHECKPOINT("Read: " << input); + Internal docRead = in(input); + + BOOST_TEST_CHECKPOINT("Deserialize: " << input); + IceInternal::Handle<T> p = Slicer::Deserialize<Deserializer, T>(docRead); + ifree(docRead); + + if (check) { + BOOST_TEST_CHECKPOINT("Check1: " << input); + check(*p); + } + + BOOST_TEST_CHECKPOINT("Serialize: " << input); + Internal docWrite; + Slicer::Serialize<Serializer>(p, docWrite); + + if (check) { + BOOST_TEST_CHECKPOINT("Check2: " << input); + check(*p); + } + + BOOST_TEST_CHECKPOINT("Write: " << output); + out(docWrite, output); + ifree(docWrite); + + BOOST_TEST_CHECKPOINT("Checksum: " << input << " === " << output); + system(stringbf("diff -w %s %s", input, output)); + } +}; + +void +checkBuiltIns_valuesCorrect(const TestModule::BuiltIns & bt) +{ + BOOST_REQUIRE_EQUAL(bt.mbool, true); + BOOST_REQUIRE_EQUAL(bt.mbyte, 4); + BOOST_REQUIRE_EQUAL(bt.mshort, 40); + BOOST_REQUIRE_EQUAL(bt.mint, 80); + BOOST_REQUIRE_EQUAL(bt.mlong, 800); + BOOST_REQUIRE_EQUAL(bt.mfloat, 3.125); + BOOST_REQUIRE_EQUAL(bt.mdouble, 3.0625); + BOOST_REQUIRE_EQUAL(bt.mstring, "Sample text"); +} + +void +checkInherits_types(const TestModule::InheritanceCont & i) +{ + BOOST_REQUIRE(i.b); + BOOST_REQUIRE(TestModule::D1Ptr::dynamicCast(i.b)); + BOOST_REQUIRE_EQUAL(TestModule::D1Ptr::dynamicCast(i.b)->a, 1); + BOOST_REQUIRE_EQUAL(TestModule::D1Ptr::dynamicCast(i.b)->b, 2); + BOOST_REQUIRE_EQUAL(i.bs.size(), 3); + BOOST_REQUIRE(i.bs[0]); + BOOST_REQUIRE(TestModule::D2Ptr::dynamicCast(i.bs[0])); + BOOST_REQUIRE_EQUAL(TestModule::D2Ptr::dynamicCast(i.bs[0])->a, 1); + BOOST_REQUIRE_EQUAL(TestModule::D2Ptr::dynamicCast(i.bs[0])->c, 100); + BOOST_REQUIRE(i.bs[1]); + BOOST_REQUIRE(TestModule::D3Ptr::dynamicCast(i.bs[1])); + BOOST_REQUIRE_EQUAL(TestModule::D3Ptr::dynamicCast(i.bs[1])->a, 2); + BOOST_REQUIRE_EQUAL(TestModule::D3Ptr::dynamicCast(i.bs[1])->c, 100); + BOOST_REQUIRE_EQUAL(TestModule::D3Ptr::dynamicCast(i.bs[1])->d, 200); + BOOST_REQUIRE(i.bs[2]); + BOOST_REQUIRE_EQUAL(i.bs[2]->a, 3); + BOOST_REQUIRE(!TestModule::D1Ptr::dynamicCast(i.bs[2])); + BOOST_REQUIRE(!TestModule::D2Ptr::dynamicCast(i.bs[2])); + BOOST_REQUIRE(!TestModule::D3Ptr::dynamicCast(i.bs[2])); + BOOST_REQUIRE_EQUAL(i.bm.size(), 3); + BOOST_REQUIRE(TestModule::D1Ptr::dynamicCast(i.bm.find(10)->second)); + BOOST_REQUIRE(TestModule::D3Ptr::dynamicCast(i.bm.find(12)->second)); + BOOST_REQUIRE(!TestModule::D1Ptr::dynamicCast(i.bm.find(14)->second)); + BOOST_REQUIRE(!TestModule::D2Ptr::dynamicCast(i.bm.find(14)->second)); + BOOST_REQUIRE(!TestModule::D3Ptr::dynamicCast(i.bm.find(14)->second)); +} + +void +checkOptionals_notset(const TestModule::Optionals & opts) +{ + BOOST_REQUIRE(!opts.optSimple); + BOOST_REQUIRE(!opts.optStruct); + BOOST_REQUIRE(!opts.optClass); + BOOST_REQUIRE(!opts.optSeq); + BOOST_REQUIRE(!opts.optDict); +} + +void +checkOptionals_areset(const TestModule::Optionals & opts) +{ + BOOST_REQUIRE(opts.optSimple); + BOOST_REQUIRE_EQUAL(opts.optSimple, 4); + BOOST_REQUIRE(opts.optStruct); + BOOST_REQUIRE_EQUAL(opts.optStruct->a, 1); + BOOST_REQUIRE_EQUAL(opts.optStruct->b, 2); + BOOST_REQUIRE(opts.optClass); + BOOST_REQUIRE_EQUAL((*opts.optClass)->a, 1); + BOOST_REQUIRE_EQUAL((*opts.optClass)->b, 2); + BOOST_REQUIRE_EQUAL(opts.optSeq->size(), 2); + BOOST_REQUIRE_EQUAL((*opts.optSeq)[0]->a, 3); + BOOST_REQUIRE_EQUAL((*opts.optSeq)[0]->b, 4); + BOOST_REQUIRE_EQUAL((*opts.optSeq)[1]->a, 5); + BOOST_REQUIRE_EQUAL((*opts.optSeq)[1]->b, 6); + BOOST_REQUIRE(opts.optDict); + BOOST_REQUIRE_EQUAL(opts.optDict->size(), 2); + BOOST_REQUIRE_EQUAL(opts.optDict->find(1), opts.optDict->end()); + BOOST_REQUIRE(opts.optDict->find(10) != opts.optDict->end()); + BOOST_REQUIRE_EQUAL(opts.optDict->find(10)->second->a, 11); + BOOST_REQUIRE_EQUAL(opts.optDict->find(10)->second->b, 12); + BOOST_REQUIRE(opts.optDict->find(13) != opts.optDict->end()); + BOOST_REQUIRE_EQUAL(opts.optDict->find(13)->second->a, 14); + BOOST_REQUIRE_EQUAL(opts.optDict->find(13)->second->b, 15); +} + +xmlpp::Document * +readXml(const fs::path & path) +{ + return new xmlpp::Document(xmlParseFile(path.string().c_str())); +} + +void +writeXml(xmlpp::Document * const & doc, const fs::path & path) +{ + doc->write_to_file_formatted(path.string()); +} + +void +freeXml(xmlpp::Document * & doc) +{ + delete doc; +} + +json::Value +readJson(const fs::path & path) +{ + std::ifstream inFile(path.string()); + std::stringstream buffer; + buffer << inFile.rdbuf(); + Glib::ustring doc(buffer.str()); + Glib::ustring::const_iterator itr = doc.begin(); + return json::parseValue(itr); +} + +void +writeJson(const json::Value & value, const fs::path & path) +{ + std::ofstream outFile(path.string()); + json::serializeValue(value, outFile, "utf-8"); +} + +void +freeJson(json::Value &) +{ +} + +BOOST_FIXTURE_TEST_SUITE ( byFile, FileBased ); + +BOOST_AUTO_TEST_CASE( builtins_xml ) +{ + verifyByFile<TestModule::BuiltIns, Slicer::XmlFileDeserializer>("builtins.xml", checkBuiltIns_valuesCorrect); +} + +BOOST_AUTO_TEST_CASE( optionals_notset_xml ) +{ + verifyByFile<TestModule::Optionals, Slicer::XmlFileDeserializer>("optionals-notset.xml", checkOptionals_notset); +} + +BOOST_AUTO_TEST_CASE( optionals_areset_xml ) +{ + verifyByFile<TestModule::Optionals, Slicer::XmlFileDeserializer>("optionals-areset.xml", checkOptionals_areset); +} + +BOOST_AUTO_TEST_CASE( inherit_a_xml ) +{ + verifyByFile<TestModule::InheritanceCont, Slicer::XmlFileDeserializer>("inherit-a.xml"); +} + +BOOST_AUTO_TEST_CASE( inherit_b_xml ) +{ + verifyByFile<TestModule::InheritanceCont, Slicer::XmlFileDeserializer>("inherit-b.xml", checkInherits_types); +} + +BOOST_AUTO_TEST_CASE( conv_datetime_xml ) +{ + verifyByFile<TestModule::DateTimeContainer, Slicer::XmlFileDeserializer>("conv-datetime.xml"); +} + +BOOST_AUTO_TEST_CASE( builtins2_json ) +{ + verifyByFile<TestModule::BuiltIns, Slicer::JsonFileDeserializer>("builtins2.json", checkBuiltIns_valuesCorrect); +} + +BOOST_AUTO_TEST_CASE( optionals_areset2_json ) +{ + verifyByFile<TestModule::Optionals, Slicer::JsonFileDeserializer>("optionals-areset2.json", checkOptionals_areset); +} + +BOOST_AUTO_TEST_CASE( inherit_c_json ) +{ + verifyByFile<TestModule::InheritanceCont, Slicer::JsonFileDeserializer>("inherit-c.json", checkInherits_types); +} + +BOOST_AUTO_TEST_CASE( inherit_d_json ) +{ + verifyByFile<TestModule::InheritanceCont2, Slicer::JsonFileDeserializer>("inherit-d.json"); +} + +BOOST_AUTO_TEST_CASE( inherit_mapped_json ) +{ + verifyByFile<TestModule::InheritanceContMapped, Slicer::JsonFileDeserializer>("inherit-mapped.json"); +} + +BOOST_AUTO_TEST_SUITE_END(); + + +BOOST_FIXTURE_TEST_SUITE ( byHandler, FileBased ); + +BOOST_AUTO_TEST_CASE( optionals_areset2_json ) +{ + verifyByHelper<TestModule::Optionals, Slicer::JsonValueDeserializer, Slicer::JsonValueSerializer, json::Value>("optionals-areset2.json", readJson, writeJson, freeJson, checkOptionals_areset); +} + +BOOST_AUTO_TEST_CASE( optionals_areset_xml ) +{ + verifyByHelper<TestModule::Optionals, Slicer::XmlDocumentDeserializer, Slicer::XmlDocumentSerializer, xmlpp::Document *>("optionals-areset.xml", readXml, writeXml, freeXml, checkOptionals_areset); +} + +BOOST_AUTO_TEST_SUITE_END(); + |