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/suffix_array_test.cc | 46 ++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 15 deletions(-) (limited to 'extractor/suffix_array_test.cc') diff --git a/extractor/suffix_array_test.cc b/extractor/suffix_array_test.cc index 8431a16e..ba0dbcc3 100644 --- a/extractor/suffix_array_test.cc +++ b/extractor/suffix_array_test.cc @@ -1,13 +1,17 @@ #include +#include + +#include +#include + #include "mocks/mock_data_array.h" #include "phrase_location.h" #include "suffix_array.h" -#include - using namespace std; using namespace ::testing; +namespace ar = boost::archive; namespace extractor { namespace { @@ -20,30 +24,30 @@ class SuffixArrayTest : public Test { EXPECT_CALL(*data_array, GetData()).WillRepeatedly(ReturnRef(data)); EXPECT_CALL(*data_array, GetVocabularySize()).WillRepeatedly(Return(7)); EXPECT_CALL(*data_array, GetSize()).WillRepeatedly(Return(13)); - suffix_array = make_shared(data_array); + suffix_array = SuffixArray(data_array); } vector data; - shared_ptr suffix_array; + SuffixArray suffix_array; shared_ptr data_array; }; TEST_F(SuffixArrayTest, TestData) { - EXPECT_EQ(data_array, suffix_array->GetData()); - EXPECT_EQ(14, suffix_array->GetSize()); + EXPECT_EQ(data_array, suffix_array.GetData()); + EXPECT_EQ(14, suffix_array.GetSize()); } TEST_F(SuffixArrayTest, TestBuildSuffixArray) { vector expected_suffix_array = {13, 11, 2, 12, 3, 6, 10, 1, 4, 7, 5, 9, 0, 8}; for (size_t i = 0; i < expected_suffix_array.size(); ++i) { - EXPECT_EQ(expected_suffix_array[i], suffix_array->GetSuffix(i)); + EXPECT_EQ(expected_suffix_array[i], suffix_array.GetSuffix(i)); } } TEST_F(SuffixArrayTest, TestBuildLCP) { vector expected_lcp = {-1, 0, 2, 0, 1, 0, 0, 3, 1, 1, 0, 0, 4, 1}; - EXPECT_EQ(expected_lcp, suffix_array->BuildLCPArray()); + EXPECT_EQ(expected_lcp, suffix_array.BuildLCPArray()); } TEST_F(SuffixArrayTest, TestLookup) { @@ -53,25 +57,37 @@ TEST_F(SuffixArrayTest, TestLookup) { EXPECT_CALL(*data_array, HasWord("word1")).WillRepeatedly(Return(true)); EXPECT_CALL(*data_array, GetWordId("word1")).WillRepeatedly(Return(6)); - EXPECT_EQ(PhraseLocation(11, 14), suffix_array->Lookup(0, 14, "word1", 0)); + EXPECT_EQ(PhraseLocation(11, 14), suffix_array.Lookup(0, 14, "word1", 0)); EXPECT_CALL(*data_array, HasWord("word2")).WillRepeatedly(Return(false)); - EXPECT_EQ(PhraseLocation(0, 0), suffix_array->Lookup(0, 14, "word2", 0)); + EXPECT_EQ(PhraseLocation(0, 0), suffix_array.Lookup(0, 14, "word2", 0)); EXPECT_CALL(*data_array, HasWord("word3")).WillRepeatedly(Return(true)); EXPECT_CALL(*data_array, GetWordId("word3")).WillRepeatedly(Return(4)); - EXPECT_EQ(PhraseLocation(11, 13), suffix_array->Lookup(11, 14, "word3", 1)); + EXPECT_EQ(PhraseLocation(11, 13), suffix_array.Lookup(11, 14, "word3", 1)); EXPECT_CALL(*data_array, HasWord("word4")).WillRepeatedly(Return(true)); EXPECT_CALL(*data_array, GetWordId("word4")).WillRepeatedly(Return(1)); - EXPECT_EQ(PhraseLocation(11, 13), suffix_array->Lookup(11, 13, "word4", 2)); + EXPECT_EQ(PhraseLocation(11, 13), suffix_array.Lookup(11, 13, "word4", 2)); EXPECT_CALL(*data_array, HasWord("word5")).WillRepeatedly(Return(true)); EXPECT_CALL(*data_array, GetWordId("word5")).WillRepeatedly(Return(2)); - EXPECT_EQ(PhraseLocation(11, 13), suffix_array->Lookup(11, 13, "word5", 3)); + EXPECT_EQ(PhraseLocation(11, 13), suffix_array.Lookup(11, 13, "word5", 3)); + + EXPECT_EQ(PhraseLocation(12, 13), suffix_array.Lookup(11, 13, "word3", 4)); + EXPECT_EQ(PhraseLocation(11, 11), suffix_array.Lookup(11, 13, "word5", 1)); +} + +TEST_F(SuffixArrayTest, TestSerialization) { + stringstream stream(ios_base::binary | ios_base::out | ios_base::in); + ar::binary_oarchive output_stream(stream, ar::no_header); + output_stream << suffix_array; + + SuffixArray suffix_array_copy; + ar::binary_iarchive input_stream(stream, ar::no_header); + input_stream >> suffix_array_copy; - EXPECT_EQ(PhraseLocation(12, 13), suffix_array->Lookup(11, 13, "word3", 4)); - EXPECT_EQ(PhraseLocation(11, 11), suffix_array->Lookup(11, 13, "word5", 1)); + EXPECT_EQ(suffix_array, suffix_array_copy); } } // namespace -- cgit v1.2.3