Change the last queries conatiner to the list.

This commit is contained in:
Lev Dragunov 2015-08-21 13:00:55 +03:00 committed by Alex Zolotarev
parent bd101de1a0
commit c9b9ebc256
3 changed files with 26 additions and 20 deletions

View file

@ -16,7 +16,6 @@ namespace search
{
QuerySaver::QuerySaver()
{
m_topQueries.reserve(kMaxSuggestCount);
Load();
}
@ -30,10 +29,10 @@ void QuerySaver::SaveNewQuery(string const & query)
if (it != m_topQueries.end())
m_topQueries.erase(it);
else if (m_topQueries.size() >= kMaxSuggestCount)
m_topQueries.erase(m_topQueries.begin());
m_topQueries.pop_back();
// Add new query and save it to drive.
m_topQueries.push_back(query);
m_topQueries.push_front(query);
Save();
}

View file

@ -1,3 +1,4 @@
#include "std/list.hpp"
#include "std/string.hpp"
#include "std/vector.hpp"
@ -9,8 +10,9 @@ class QuerySaver
public:
QuerySaver();
void SaveNewQuery(string const & query);
/// Returns last save query from oldest to newest queries.
vector<string> const & GetTopQueries() const { return m_topQueries; }
/// Returns several last saved queries from newest to oldest query.
/// @see kMaxSuggestCount in implementation file.
list<string> const & GetTopQueries() const { return m_topQueries; }
/// Clear last queries storage. All data will be lost.
void Clear();
@ -22,6 +24,6 @@ private:
void Save();
void Load();
vector<string> m_topQueries;
list<string> m_topQueries;
};
} // namespace seatch

View file

@ -16,16 +16,18 @@ namespace search
UNIT_TEST(QuerySaverFogTest)
{
QuerySaver saver;
saver.Clear();
saver.SaveNewQuery(record1);
vector<string> const & result = saver.GetTopQueries();
list<string> const & result = saver.GetTopQueries();
TEST_EQUAL(result.size(), 1, ());
TEST_EQUAL(result[0], record1, ());
TEST_EQUAL(result.front(), record1, ());
saver.Clear();
}
UNIT_TEST(QuerySaverClearTest)
{
QuerySaver saver;
saver.Clear();
saver.SaveNewQuery(record1);
TEST_GREATER(saver.GetTopQueries().size(), 0, ());
saver.Clear();
@ -35,20 +37,21 @@ UNIT_TEST(QuerySaverClearTest)
UNIT_TEST(QuerySaverOrderingTest)
{
QuerySaver saver;
saver.Clear();
saver.SaveNewQuery(record1);
saver.SaveNewQuery(record2);
{
vector<string> const & result = saver.GetTopQueries();
list<string> const & result = saver.GetTopQueries();
TEST_EQUAL(result.size(), 2, ());
TEST_EQUAL(result[0], record1, ());
TEST_EQUAL(result[1], record2, ());
TEST_EQUAL(result.back(), record1, ());
TEST_EQUAL(result.front(), record2, ());
}
saver.SaveNewQuery(record1);
{
vector<string> const & result = saver.GetTopQueries();
list<string> const & result = saver.GetTopQueries();
TEST_EQUAL(result.size(), 2, ());
TEST_EQUAL(result[1], record1, ());
TEST_EQUAL(result[0], record2, ());
TEST_EQUAL(result.front(), record1, ());
TEST_EQUAL(result.back(), record2, ());
}
saver.Clear();
}
@ -56,6 +59,7 @@ UNIT_TEST(QuerySaverOrderingTest)
UNIT_TEST(QuerySaverSerializerTest)
{
QuerySaver saver;
saver.Clear();
saver.SaveNewQuery(record1);
saver.SaveNewQuery(record2);
vector<char> data;
@ -65,25 +69,26 @@ UNIT_TEST(QuerySaverSerializerTest)
TEST_EQUAL(saver.GetTopQueries().size(), 0, ());
saver.Deserialize(string(data.begin(), data.end()));
vector<string> const & result = saver.GetTopQueries();
list<string> const & result = saver.GetTopQueries();
TEST_EQUAL(result.size(), 2, ());
TEST_EQUAL(result[0], record1, ());
TEST_EQUAL(result[1], record2, ());
TEST_EQUAL(result.back(), record1, ());
TEST_EQUAL(result.front(), record2, ());
}
UNIT_TEST(QuerySaverPersistanceStore)
{
{
QuerySaver saver;
saver.Clear();
saver.SaveNewQuery(record1);
saver.SaveNewQuery(record2);
}
{
QuerySaver saver;
vector<string> const & result = saver.GetTopQueries();
list<string> const & result = saver.GetTopQueries();
TEST_EQUAL(result.size(), 2, ());
TEST_EQUAL(result[0], record1, ());
TEST_EQUAL(result[1], record2, ());
TEST_EQUAL(result.back(), record1, ());
TEST_EQUAL(result.front(), record2, ());
saver.Clear();
}
}