Skip to content
Permalink
Browse files

Add test for monad return with serializer (reproducing Gil Tensorflow…

… issue).
  • Loading branch information...
viferga committed Jun 16, 2019
1 parent ce19b5d commit f06c3e2baec63d8b3542ca44b9b5ca6082ff19cd
@@ -219,6 +219,8 @@ void rapid_json_serial_impl_serialize_value(value v, RapidJSONSerialValue * json
{
RapidJSONSerialValue & json_map = json_v->SetObject();

RapidJSONSerialValue json_array(rapidjson::kArrayType);

void * buffer = value_to_buffer(v);

size_t size = value_type_size(v);
@@ -227,22 +229,32 @@ void rapid_json_serial_impl_serialize_value(value v, RapidJSONSerialValue * json
{
const char * data = (const char *)(((uintptr_t)buffer) + iterator);

RapidJSONSerialValue json_member, json_inner_value;

json_member.SetUint64((uint64_t)iterator);
RapidJSONSerialValue json_inner_value;

json_inner_value.SetUint((unsigned int)*data);

json_map.AddMember(json_member, json_inner_value, allocator);
json_array.PushBack(json_inner_value, allocator);
}

RapidJSONSerialValue json_member, json_inner_value;
// Set data
{
RapidJSONSerialValue json_member;

json_member.SetString("length");
json_member.SetString("data");

json_inner_value.SetUint64((uint64_t)size);
json_map.AddMember(json_member, json_array, allocator);
}

json_map.AddMember(json_member, json_inner_value, allocator);
// Set length
{
RapidJSONSerialValue json_member, json_inner_value;

json_member.SetString("length");

json_inner_value.SetUint64((uint64_t)size);

json_map.AddMember(json_member, json_inner_value, allocator);
}
}
else if (id == TYPE_ARRAY)
{
@@ -132,3 +132,4 @@ add_subdirectory(metacall_initialize_test)
add_subdirectory(metacall_initialize_ex_test)
add_subdirectory(metacall_reinitialize_test)
add_subdirectory(metacall_fork_test)
add_subdirectory(metacall_return_monad_test)
@@ -0,0 +1,147 @@
#
# Executable name and options
#

# Target name
set(target metacall-return-monad-test)
message(STATUS "Test ${target}")

#
# Compiler warnings
#

include(Warnings)

#
# Compiler security
#

include(SecurityFlags)

#
# Sources
#

set(include_path "${CMAKE_CURRENT_SOURCE_DIR}/include/${target}")
set(source_path "${CMAKE_CURRENT_SOURCE_DIR}/source")

set(sources
${source_path}/main.cpp
${source_path}/metacall_return_monad_test.cpp
)

# Group source files
set(header_group "Header Files (API)")
set(source_group "Source Files")
source_group_by_path(${include_path} "\\\\.h$|\\\\.hpp$"
${header_group} ${headers})
source_group_by_path(${source_path} "\\\\.cpp$|\\\\.c$|\\\\.h$|\\\\.hpp$"
${source_group} ${sources})

#
# Create executable
#

# Build executable
add_executable(${target}
${sources}
)

# Create namespaced alias
add_executable(${META_PROJECT_NAME}::${target} ALIAS ${target})

#
# Project options
#

set_target_properties(${target}
PROPERTIES
${DEFAULT_PROJECT_OPTIONS}
FOLDER "${IDE_FOLDER}"
)

#
# Include directories
#

target_include_directories(${target}
PRIVATE
${DEFAULT_INCLUDE_DIRECTORIES}
${PROJECT_BINARY_DIR}/source/include
)

#
# Libraries
#

target_link_libraries(${target}
PRIVATE
${DEFAULT_LIBRARIES}

GTest

${META_PROJECT_NAME}::version
${META_PROJECT_NAME}::preprocessor
${META_PROJECT_NAME}::environment
${META_PROJECT_NAME}::format
${META_PROJECT_NAME}::log
${META_PROJECT_NAME}::memory
${META_PROJECT_NAME}::adt
${META_PROJECT_NAME}::reflect
${META_PROJECT_NAME}::dynlink
${META_PROJECT_NAME}::detour
${META_PROJECT_NAME}::serial
${META_PROJECT_NAME}::configuration
${META_PROJECT_NAME}::loader
${META_PROJECT_NAME}::metacall
)

#
# Compile definitions
#

target_compile_definitions(${target}
PRIVATE
${DEFAULT_COMPILE_DEFINITIONS}
)

#
# Compile options
#

target_compile_options(${target}
PRIVATE
${DEFAULT_COMPILE_OPTIONS}
)

#
# Linker options
#

target_link_libraries(${target}
PRIVATE
${DEFAULT_LINKER_OPTIONS}
)

#
# Define test
#

add_test(NAME ${target}
COMMAND $<TARGET_FILE:${target}>
)

#
# Define test properties
#

set_property(TEST ${target}
PROPERTY LABELS ${target} MEMCHECK_IGNORE
)

include(TestEnvironmentVariables)

test_environment_variables(${target}
""
${TESTS_ENVIRONMENT_VARIABLES}
)
@@ -0,0 +1,28 @@
/*
* MetaCall Library by Parra Studios
* A library for providing a foreign function interface calls.
*
* Copyright (C) 2016 - 2019 Vicente Eduardo Ferrer Garcia <vic798@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include <gmock/gmock.h>

int main(int argc, char * argv[])
{
::testing::InitGoogleMock(&argc, argv);

return RUN_ALL_TESTS();
}
@@ -0,0 +1,133 @@
/*
* MetaCall Library by Parra Studios
* A library for providing a foreign function interface calls.
*
* Copyright (C) 2016 - 2019 Vicente Eduardo Ferrer Garcia <vic798@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include <gmock/gmock.h>

#include <metacall/metacall.h>
#include <metacall/metacall_value.h>
#include <metacall/metacall_loaders.h>

void * c_function(void * args[])
{
printf("%s\n", (char*)args[0]);

return metacall_value_create_int(1);
}

class metacall_return_monad_test : public testing::Test
{
public:
};

TEST_F(metacall_return_monad_test, DefaultConstructor)
{
metacall_print_info();

metacall_log_stdio_type log_stdio = { stdout };

ASSERT_EQ((int) 0, (int) metacall_log(METACALL_LOG_STDIO, (void *)&log_stdio));

ASSERT_EQ((int) 0, (int) metacall_initialize());

/* Python */
#if defined(OPTION_BUILD_LOADERS_PY)
{
static const char buffer[] =
"#!/usr/bin/python3.5\n"
"def monad(value: int):\n"
"\tresult = 'asd';\n"
"\tif value > 0:\n"
"\t\treturn result;\n"
"\telse:\n"
"\t\treturn bytes(result, 'utf-8');\n";

static const char tag[] = "py";

struct metacall_allocator_std_type std_ctx = { &std::malloc, &std::realloc, &std::free };

void * allocator = metacall_allocator_create(METACALL_ALLOCATOR_STD, (void *)&std_ctx);

void * ret = NULL;

size_t size = 0;

char * value_str = NULL;

ASSERT_EQ((int) 0, (int) metacall_load_from_memory(tag, buffer, sizeof(buffer), NULL));

ret = metacall("monad", 1);

EXPECT_NE((void *) NULL, (void *) ret);

EXPECT_EQ((enum metacall_value_id) METACALL_STRING, (enum metacall_value_id) metacall_value_id(ret));

EXPECT_EQ((int) 0, (int) strcmp("asd", metacall_value_to_string(ret)));

value_str = metacall_serialize(metacall_serial(), ret, &size, allocator);

std::cout << value_str << std::endl;

metacall_allocator_free(allocator, value_str);

metacall_value_destroy(ret);

ret = metacall("monad", 0);

EXPECT_NE((void *) NULL, (void *) ret);

EXPECT_EQ((enum metacall_value_id) METACALL_BUFFER, (enum metacall_value_id) metacall_value_id(ret));

EXPECT_EQ((int) 0, (int) memcmp(metacall_value_to_buffer(ret), "asd", metacall_value_size(ret)));

value_str = metacall_serialize(metacall_serial(), ret, &size, allocator);

std::cout << value_str << std::endl;

metacall_allocator_free(allocator, value_str);

metacall_value_destroy(ret);

metacall_allocator_destroy(allocator);
}
#endif /* OPTION_BUILD_LOADERS_PY */

/* Print inspect information */
{
size_t size = 0;

struct metacall_allocator_std_type std_ctx = { &std::malloc, &std::realloc, &std::free };

void * allocator = metacall_allocator_create(METACALL_ALLOCATOR_STD, (void *)&std_ctx);

char * inspect_str = metacall_inspect(&size, allocator);

EXPECT_NE((char *) NULL, (char *) inspect_str);

EXPECT_GT((size_t) size, (size_t) 0);

std::cout << inspect_str << std::endl;

metacall_allocator_free(allocator, inspect_str);

metacall_allocator_destroy(allocator);
}

EXPECT_EQ((int) 0, (int) metacall_destroy());
}

0 comments on commit f06c3e2

Please sign in to comment.
You can’t perform that action at this time.