From 71b3e5799aa3b515c0fd5e6a49209935ad1bc7e0 Mon Sep 17 00:00:00 2001 From: MikeLing Date: Mon, 19 Jun 2017 15:49:22 +0800 Subject: [PATCH] add serializable test for DynamicArray --- tests/unit/lib/DynamicArray_unittest.cc | 38 +++++++++++++++++++++---- tests/unit/utils/Utils.cpp | 23 ++++++++++++--- tests/unit/utils/Utils.h | 6 ++++ 3 files changed, 58 insertions(+), 9 deletions(-) diff --git a/tests/unit/lib/DynamicArray_unittest.cc b/tests/unit/lib/DynamicArray_unittest.cc index 411ce2ee13a..b43aff9a16e 100644 --- a/tests/unit/lib/DynamicArray_unittest.cc +++ b/tests/unit/lib/DynamicArray_unittest.cc @@ -1,7 +1,10 @@ #include +#include #include #include +#include "utils/Utils.h" + using namespace shogun; template @@ -251,10 +254,35 @@ TEST_F(CDynamicArrayFixture, append_array_bool) EXPECT_EQ(wrapper_array_b->get_element(2), true); } -TEST_F(CDynamicArrayFixture, serializable_array) +TYPED_TEST(CDynamicArrayTest, save_serializable) { - wrapper_array_i->save_serializable_pre(); - wrapper_array_i->load_serializable_pre(); - wrapper_array_b->save_serializable_pre(); - wrapper_array_b->load_serializable_pre(); + for (int32_t i = 0; i < 5; i++) + { + this->custom_array->push_back((TypeParam)CMath::random(0, 1)); + } + + /* generate file name */ + char filename[] = "serialization-asciiCDynamicArray.XXXXXX"; + generate_temp_filename(filename); + + CSerializableAsciiFile* file = new CSerializableAsciiFile(filename, 'w'); + this->custom_array->save_serializable(file); + file->close(); + SG_UNREF(file); + + file = new CSerializableAsciiFile(filename, 'r'); + CDynamicArray* new_array = new CDynamicArray(); + new_array->load_serializable(file); + file->close(); + SG_UNREF(file); + + ASSERT(this->custom_array->get_num_elements() == 5) + for (int32_t i = 0; i < this->custom_array->get_num_elements(); i++) + { + EXPECT_EQ( + this->custom_array->get_element(i), new_array->get_element(i)); + } + + SG_UNREF(new_array); + unlink(filename); } diff --git a/tests/unit/utils/Utils.cpp b/tests/unit/utils/Utils.cpp index 9db4147ea79..0c5b46f6512 100644 --- a/tests/unit/utils/Utils.cpp +++ b/tests/unit/utils/Utils.cpp @@ -32,12 +32,14 @@ * Written (W) 2017 Giovanni De Toni * */ -#include +#include "Utils.h" +#include #include #include -#include -#include -#include "Utils.h" +#include +#include + +using namespace shogun; char * mktemp_cst(char * __template) { @@ -68,3 +70,16 @@ char * mktemp_cst(char * __template) return __template; } + +void generate_temp_filename(char* file_name) +{ +#ifdef _WIN32 + int err = _mktemp_s(file_name, strlen(file_name)); + ASSERT(err == 0); +#else + int fd = mkstemp(file_name); + ASSERT(fd != -1); + int retval = close(fd); + ASSERT(retval != -1); +#endif +} diff --git a/tests/unit/utils/Utils.h b/tests/unit/utils/Utils.h index f6f15fa2cd8..aa52ceaae47 100644 --- a/tests/unit/utils/Utils.h +++ b/tests/unit/utils/Utils.h @@ -46,4 +46,10 @@ */ char * mktemp_cst(char * __template); +/** Generate file name for serialization test + * + * @param file_name template of file name + */ +void generate_temp_filename(char* file_name); + #endif