From a3243017d6b8c46cc3e41f4243311dc3dbc80ab4 Mon Sep 17 00:00:00 2001 From: Paul Baltescu Date: Tue, 4 Jun 2013 23:17:57 +0100 Subject: Serialize data structures. --- extractor/translation_table_test.cc | 149 +++++++++++++++++++++--------------- 1 file changed, 86 insertions(+), 63 deletions(-) (limited to 'extractor/translation_table_test.cc') diff --git a/extractor/translation_table_test.cc b/extractor/translation_table_test.cc index d14f2f89..606777bd 100644 --- a/extractor/translation_table_test.cc +++ b/extractor/translation_table_test.cc @@ -1,83 +1,106 @@ #include #include +#include #include #include +#include +#include + #include "mocks/mock_alignment.h" #include "mocks/mock_data_array.h" #include "translation_table.h" using namespace std; using namespace ::testing; +namespace ar = boost::archive; namespace extractor { namespace { -TEST(TranslationTableTest, TestScores) { - vector words = {"a", "b", "c"}; - - vector source_data = {2, 3, 2, 3, 4, 0, 2, 3, 6, 0, 2, 3, 6, 0}; - vector source_sentence_start = {0, 6, 10, 14}; - shared_ptr source_data_array = make_shared(); - EXPECT_CALL(*source_data_array, GetData()) - .WillRepeatedly(ReturnRef(source_data)); - EXPECT_CALL(*source_data_array, GetNumSentences()) - .WillRepeatedly(Return(3)); - for (size_t i = 0; i < source_sentence_start.size(); ++i) { - EXPECT_CALL(*source_data_array, GetSentenceStart(i)) - .WillRepeatedly(Return(source_sentence_start[i])); - } - for (size_t i = 0; i < words.size(); ++i) { - EXPECT_CALL(*source_data_array, HasWord(words[i])) - .WillRepeatedly(Return(true)); - EXPECT_CALL(*source_data_array, GetWordId(words[i])) - .WillRepeatedly(Return(i + 2)); - } - EXPECT_CALL(*source_data_array, HasWord("d")) - .WillRepeatedly(Return(false)); - - vector target_data = {2, 3, 2, 3, 4, 5, 0, 3, 6, 0, 2, 7, 0}; - vector target_sentence_start = {0, 7, 10, 13}; - shared_ptr target_data_array = make_shared(); - EXPECT_CALL(*target_data_array, GetData()) - .WillRepeatedly(ReturnRef(target_data)); - for (size_t i = 0; i < target_sentence_start.size(); ++i) { - EXPECT_CALL(*target_data_array, GetSentenceStart(i)) - .WillRepeatedly(Return(target_sentence_start[i])); - } - for (size_t i = 0; i < words.size(); ++i) { - EXPECT_CALL(*target_data_array, HasWord(words[i])) - .WillRepeatedly(Return(true)); - EXPECT_CALL(*target_data_array, GetWordId(words[i])) - .WillRepeatedly(Return(i + 2)); +class TranslationTableTest : public Test { + protected: + virtual void SetUp() { + vector words = {"a", "b", "c"}; + + vector source_data = {2, 3, 2, 3, 4, 0, 2, 3, 6, 0, 2, 3, 6, 0}; + vector source_sentence_start = {0, 6, 10, 14}; + shared_ptr source_data_array = make_shared(); + EXPECT_CALL(*source_data_array, GetData()) + .WillRepeatedly(ReturnRef(source_data)); + EXPECT_CALL(*source_data_array, GetNumSentences()) + .WillRepeatedly(Return(3)); + for (size_t i = 0; i < source_sentence_start.size(); ++i) { + EXPECT_CALL(*source_data_array, GetSentenceStart(i)) + .WillRepeatedly(Return(source_sentence_start[i])); + } + for (size_t i = 0; i < words.size(); ++i) { + EXPECT_CALL(*source_data_array, HasWord(words[i])) + .WillRepeatedly(Return(true)); + EXPECT_CALL(*source_data_array, GetWordId(words[i])) + .WillRepeatedly(Return(i + 2)); + } + EXPECT_CALL(*source_data_array, HasWord("d")) + .WillRepeatedly(Return(false)); + + vector target_data = {2, 3, 2, 3, 4, 5, 0, 3, 6, 0, 2, 7, 0}; + vector target_sentence_start = {0, 7, 10, 13}; + shared_ptr target_data_array = make_shared(); + EXPECT_CALL(*target_data_array, GetData()) + .WillRepeatedly(ReturnRef(target_data)); + for (size_t i = 0; i < target_sentence_start.size(); ++i) { + EXPECT_CALL(*target_data_array, GetSentenceStart(i)) + .WillRepeatedly(Return(target_sentence_start[i])); + } + for (size_t i = 0; i < words.size(); ++i) { + EXPECT_CALL(*target_data_array, HasWord(words[i])) + .WillRepeatedly(Return(true)); + EXPECT_CALL(*target_data_array, GetWordId(words[i])) + .WillRepeatedly(Return(i + 2)); + } + EXPECT_CALL(*target_data_array, HasWord("d")) + .WillRepeatedly(Return(false)); + + vector> links1 = { + make_pair(0, 0), make_pair(1, 1), make_pair(2, 2), make_pair(3, 3), + make_pair(4, 4), make_pair(4, 5) + }; + vector> links2 = {make_pair(1, 0), make_pair(2, 1)}; + vector> links3 = {make_pair(0, 0), make_pair(2, 1)}; + shared_ptr alignment = make_shared(); + EXPECT_CALL(*alignment, GetLinks(0)).WillRepeatedly(Return(links1)); + EXPECT_CALL(*alignment, GetLinks(1)).WillRepeatedly(Return(links2)); + EXPECT_CALL(*alignment, GetLinks(2)).WillRepeatedly(Return(links3)); + + table = TranslationTable(source_data_array, target_data_array, alignment); } - EXPECT_CALL(*target_data_array, HasWord("d")) - .WillRepeatedly(Return(false)); - - vector> links1 = { - make_pair(0, 0), make_pair(1, 1), make_pair(2, 2), make_pair(3, 3), - make_pair(4, 4), make_pair(4, 5) - }; - vector> links2 = {make_pair(1, 0), make_pair(2, 1)}; - vector> links3 = {make_pair(0, 0), make_pair(2, 1)}; - shared_ptr alignment = make_shared(); - EXPECT_CALL(*alignment, GetLinks(0)).WillRepeatedly(Return(links1)); - EXPECT_CALL(*alignment, GetLinks(1)).WillRepeatedly(Return(links2)); - EXPECT_CALL(*alignment, GetLinks(2)).WillRepeatedly(Return(links3)); - - shared_ptr table = make_shared( - source_data_array, target_data_array, alignment); - - EXPECT_EQ(0.75, table->GetTargetGivenSourceScore("a", "a")); - EXPECT_EQ(0, table->GetTargetGivenSourceScore("a", "b")); - EXPECT_EQ(0.5, table->GetTargetGivenSourceScore("c", "c")); - EXPECT_EQ(-1, table->GetTargetGivenSourceScore("c", "d")); - - EXPECT_EQ(1, table->GetSourceGivenTargetScore("a", "a")); - EXPECT_EQ(0, table->GetSourceGivenTargetScore("a", "b")); - EXPECT_EQ(1, table->GetSourceGivenTargetScore("c", "c")); - EXPECT_EQ(-1, table->GetSourceGivenTargetScore("c", "d")); + + TranslationTable table; +}; + +TEST_F(TranslationTableTest, TestScores) { + EXPECT_EQ(0.75, table.GetTargetGivenSourceScore("a", "a")); + EXPECT_EQ(0, table.GetTargetGivenSourceScore("a", "b")); + EXPECT_EQ(0.5, table.GetTargetGivenSourceScore("c", "c")); + EXPECT_EQ(-1, table.GetTargetGivenSourceScore("c", "d")); + + EXPECT_EQ(1, table.GetSourceGivenTargetScore("a", "a")); + EXPECT_EQ(0, table.GetSourceGivenTargetScore("a", "b")); + EXPECT_EQ(1, table.GetSourceGivenTargetScore("c", "c")); + EXPECT_EQ(-1, table.GetSourceGivenTargetScore("c", "d")); +} + +TEST_F(TranslationTableTest, TestSerialization) { + stringstream stream(ios_base::binary | ios_base::out | ios_base::in); + ar::binary_oarchive output_stream(stream, ar::no_header); + output_stream << table; + + TranslationTable table_copy; + ar::binary_iarchive input_stream(stream, ar::no_header); + input_stream >> table_copy; + + EXPECT_EQ(table, table_copy); } } // namespace -- cgit v1.2.3