Skip to content

Commit

Permalink
add serializable test for DynamicArray
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeLing committed Jun 19, 2017
1 parent 3b35df3 commit 71b3e57
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
38 changes: 33 additions & 5 deletions tests/unit/lib/DynamicArray_unittest.cc
@@ -1,7 +1,10 @@
#include <gtest/gtest.h>
#include <shogun/io/SerializableAsciiFile.h>
#include <shogun/lib/DynamicArray.h>
#include <shogun/mathematics/Math.h>

#include "utils/Utils.h"

using namespace shogun;

template <typename T>
Expand Down Expand Up @@ -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<TypeParam>* new_array = new CDynamicArray<TypeParam>();
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);
}
23 changes: 19 additions & 4 deletions tests/unit/utils/Utils.cpp
Expand Up @@ -32,12 +32,14 @@
* Written (W) 2017 Giovanni De Toni
*
*/
#include <string>
#include "Utils.h"
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include "Utils.h"
#include <shogun/io/SGIO.h>
#include <string>

using namespace shogun;

char * mktemp_cst(char * __template)
{
Expand Down Expand Up @@ -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
}
6 changes: 6 additions & 0 deletions tests/unit/utils/Utils.h
Expand Up @@ -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

0 comments on commit 71b3e57

Please sign in to comment.