Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add serializable test for DynamicArray #3852

Merged
merged 1 commit into from Jun 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmm, I get Bus error if I change it with char* filename = (char*)"serialization-asciiCDynamicArray.XXXXXX"; And it will happened in mkstemp. So I guess it's because I haven't assign a length of memory to that pointer. Anyway, I think we don't need to worry about it should be a pointer or not :)

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MikeLing could you add this on the very end:
unlink(filename);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it

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