Skip to content

Commit

Permalink
feat: implement additional tests for coverage
Browse files Browse the repository at this point in the history
note: the added test is not yet working
  • Loading branch information
lokitoth committed Feb 6, 2024
1 parent dfef8c9 commit 98ce35d
Show file tree
Hide file tree
Showing 11 changed files with 911 additions and 4 deletions.
20 changes: 17 additions & 3 deletions vowpalwabbit/fb_parser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,22 @@ target_include_directories(vw_fb_parser PUBLIC ${FLATBUFFERS_INCLUDE_DIR})
# DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
# endif()

set(vw_fb_parser_test_sources
tests/prototype_example.cc
tests/prototype_example.h
tests/prototype_example_root.cc
tests/prototype_example_root.h
tests/prototype_label.cc
tests/prototype_label.h
tests/prototype_namespace.cc
tests/prototype_namespace.h
tests/flatbuffer_parser_test.cc
)

message(STATUS "vw_fb_parser_test_sources: ${vw_fb_parser_test_sources}")

vw_add_test_executable(
FOR_LIB "fb_parser"
SOURCES "tests/flatbuffer_parser_test.cc"
EXTRA_DEPS vw_core vw_test_common
FOR_LIB "fb_parser"
EXTRA_DEPS vw_core vw_test_common
SOURCES ${vw_fb_parser_test_sources}
)
2 changes: 1 addition & 1 deletion vowpalwabbit/fb_parser/src/parse_example_flatbuffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ int parser::parse(io_buf& buf, uint8_t* buffer_pointer, VW::experimental::api_st
if (len == 0)
{
// nothing to read
RETURN_ERROR(status, nothing_to_parse);
return VW::experimental::error_code::nothing_to_parse;
}
else
{
Expand Down
94 changes: 94 additions & 0 deletions vowpalwabbit/fb_parser/tests/flatbuffer_parser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,33 @@
#include "vw/core/constant.h"
#include "vw/core/feature_group.h"
#include "vw/core/vw.h"
#include "vw/core/example.h"
#include "vw/core/learner.h"
#include "vw/fb_parser/parse_example_flatbuffer.h"
#include "vw/test_common/test_common.h"
#include "vw/common/string_view.h"
#include "vw/common/future_compat.h"

#include "vw/core/error_constants.h"

#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <string>
#include <vector>

#include "prototype_label.h"
#include "prototype_namespace.h"
#include "prototype_example.h"
#include "prototype_example_root.h"

USE_PROTOTYPE_MNEMONICS


namespace fb = VW::parsers::flatbuffer;
using namespace flatbuffers;
using namespace vwtest;

flatbuffers::Offset<void> get_label(flatbuffers::FlatBufferBuilder& builder, VW::parsers::flatbuffer::Label label_type)
{
flatbuffers::Offset<void> label;
Expand Down Expand Up @@ -257,3 +275,79 @@ TEST(flatbuffer_parser_tests, test_flatbuffer_standalone_example_error_code)

VW::finish_example(*all, *examples[0]);
}

template <typename root_prototype_t, bool test_audit_strings = true>
void run_parse_and_verify_test(VW::workspace& w, const root_prototype_t& root_obj)
{
flatbuffers::FlatBufferBuilder builder;
auto root = vwtest::create_example_root<test_audit_strings>(builder, w, root_obj);
builder.FinishSizePrefixed(root);

VW::io_buf buf;

span<uint8_t> fb_span = builder.GetBufferSpan();
buf.add_file(VW::io::create_buffer_view((const char*)fb_span.data(), fb_span.size()));

VW::multi_ex examples;

bool done = false;
while (!done && !w.parser_runtime.example_parser->done)
{
VW::multi_ex dispatch_examples;
dispatch_examples.push_back(&VW::get_unused_example(&w));

VW::experimental::api_status status;
int result = w.parser_runtime.flat_converter->parse_examples(&w, buf, dispatch_examples, nullptr, &status);

switch (result)
{
case VW::experimental::error_code::success:
if (!w.l->is_multiline() || !dispatch_examples[0]->is_newline)
{
examples.push_back(dispatch_examples[0]);
}

break;
case VW::experimental::error_code::nothing_to_parse:
done = true;
break;
default:
throw std::runtime_error(status.get_error_msg());
}
}

vwtest::verify_example_root<test_audit_strings>(w, w.parser_runtime.flat_converter->data(), root_obj);
vwtest::verify_example_root<test_audit_strings>(w, examples, root_obj);

VW::finish_example(w, examples);
}

TEST(FlatbufferParser, MultiExample)
{
auto all = VW::initialize(vwtest::make_args("--no_stdin", "--quiet", "--flatbuffer", "--cb_explore_adf"));

flatbuffers::FlatBufferBuilder builder;

multiex prototype = {
{
{
{
{ "U_a", { { "a", 1.f }, { "b", 2.f } } },
{ "U_b", { { "a", 3.f }, { "b", 4.f } } },
},
vwtest::cb_label_shared(),
"tag1"
},
{
{
{ "T_a", { { "a", 5.f }, { "b", 6.f } } },
{ "T_b", { { "a", 7.f }, { "b", 8.f } } },
},
vwtest::cb_label({ { 1, 1, 0.5f } }),
},
}
};

run_parse_and_verify_test(*all, prototype);
}

43 changes: 43 additions & 0 deletions vowpalwabbit/fb_parser/tests/prototype_example.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) by respective owners including Yahoo!, Microsoft, and
// individual contributors. All rights reserved. Released under a BSD (revised)
// license as described in the file LICENSE.


#include "prototype_example.h"

namespace vwtest
{



// namespace details
// {
// using example_type_t = std::underlying_type<VW::parsers::flatbuffer::ExampleType>::type;
// constexpr example_type_t bad_example_type = static_cast<example_type_t>(VW::parsers::flatbuffer::ExampleType_MAX) + 1;

// template <typename T>
// struct example_root_type : public std::integral_constant<example_type_t, bad_example_type>
// {
// };

// template <>
// struct example_root_type<prototype_example>
// : public std::integral_constant<example_type_t, VW::parsers::flatbuffer::ExampleType_Example>
// {
// };

// template <>
// struct example_root_type<prototype_multiexample>
// : public std::integral_constant<example_type_t, VW::parsers::flatbuffer::ExampleType_MultiExample>
// {
// };

// template <>
// struct example_root_type<prototype_example_collection>
// : public std::integral_constant<example_type_t, VW::parsers::flatbuffer::ExampleType_ExampleCollection>
// {
// };
// }


}
Loading

0 comments on commit 98ce35d

Please sign in to comment.