From 34051722d62a66ed2bc3f7501faa3375112255f1 Mon Sep 17 00:00:00 2001 From: Alex Zolotarev Date: Tue, 4 Oct 2011 19:38:22 +0300 Subject: [PATCH] Added random url generator --- base/pseudo_random.hpp | 4 - platform/platform.pro | 2 + platform/platform_tests/platform_tests.pro | 1 + .../platform_tests/url_generator_test.cpp | 41 +++++++++ platform/url_generator.cpp | 86 +++++++++++++++++++ platform/url_generator.hpp | 19 ++++ 6 files changed, 149 insertions(+), 4 deletions(-) create mode 100644 platform/platform_tests/url_generator_test.cpp create mode 100644 platform/url_generator.cpp create mode 100644 platform/url_generator.hpp diff --git a/base/pseudo_random.hpp b/base/pseudo_random.hpp index 8fa183d6c0..61c5e51033 100644 --- a/base/pseudo_random.hpp +++ b/base/pseudo_random.hpp @@ -2,8 +2,6 @@ #include "../base/base.hpp" -#include "../base/start_mem_debug.hpp" - class LCG32 { public: @@ -21,5 +19,3 @@ private: }; typedef LCG32 PseudoRNG32; - -#include "../base/stop_mem_debug.hpp" diff --git a/platform/platform.pro b/platform/platform.pro index 5182c6011e..f42b382208 100644 --- a/platform/platform.pro +++ b/platform/platform.pro @@ -53,6 +53,7 @@ HEADERS += \ settings.hpp \ video_timer.hpp \ languages.hpp \ + url_generator.hpp \ SOURCES += \ location_manager.cpp \ @@ -60,3 +61,4 @@ SOURCES += \ settings.cpp \ video_timer.cpp \ languages.cpp \ + url_generator.cpp \ diff --git a/platform/platform_tests/platform_tests.pro b/platform/platform_tests/platform_tests.pro index 155ebff7d4..0f16e0e608 100644 --- a/platform/platform_tests/platform_tests.pro +++ b/platform/platform_tests/platform_tests.pro @@ -25,3 +25,4 @@ SOURCES += \ jansson_test.cpp \ concurrent_runner_test.cpp \ language_test.cpp \ + url_generator_test.cpp \ diff --git a/platform/platform_tests/url_generator_test.cpp b/platform/platform_tests/url_generator_test.cpp new file mode 100644 index 0000000000..7eb8eb1003 --- /dev/null +++ b/platform/platform_tests/url_generator_test.cpp @@ -0,0 +1,41 @@ +#include "../../testing/testing.hpp" + +#include "../url_generator.hpp" + +#include "../../std/algorithm.hpp" + +void VectorContains(vector & v, string const & s) +{ + vector::iterator found = find(v.begin(), v.end(), s); + std::cout << s << endl; + TEST(found != v.end(), (s, "was not found in", v)); + v.erase(found); +} + +UNIT_TEST(UrlGenerator) +{ + vector first; + first.push_back("A"); + first.push_back("B"); + first.push_back("C"); + first.push_back("D"); + first.push_back("E"); + + vector second; + second.push_back("F"); + second.push_back("G"); + second.push_back("H"); + + UrlGenerator g(first, second); + + { + size_t const count = first.size(); + for (size_t i = 0; i < count; ++i) + VectorContains(first, g.PopNextUrl()); + } + { + size_t const count = second.size(); + for (size_t i = 0; i < count; ++i) + VectorContains(second, g.PopNextUrl()); + } +} diff --git a/platform/url_generator.cpp b/platform/url_generator.cpp new file mode 100644 index 0000000000..5bd30acc57 --- /dev/null +++ b/platform/url_generator.cpp @@ -0,0 +1,86 @@ +#include "url_generator.hpp" + +#include "../base/macros.hpp" + +#include "../std/ctime.hpp" + +static char const * g_defaultFirstGroup[] = { +#ifdef OMIM_PRODUCTION + "http://a0.mapswithme.com/", + "http://a1.mapswithme.com/", + "http://a2.mapswithme.com/", + "http://a3.mapswithme.com/", + "http://a4.mapswithme.com/", + "http://a5.mapswithme.com/", + "http://a6.mapswithme.com/", + "http://a7.mapswithme.com/", + "http://a8.mapswithme.com/", + "http://a9.mapswithme.com/", + "http://a10.mapswithme.com/", + "http://a11.mapswithme.com/" +#else + "http://svobodu404popugajam.mapswithme.com:34568/maps/" +#endif +}; +static char const * g_defaultSecondGroup[] = { +#ifdef OMIM_PRODUCTION + "http://b0.mapswithme.com/", + "http://b1.mapswithme.com/", + "http://b2.mapswithme.com/", + "http://b3.mapswithme.com/", + "http://b4.mapswithme.com/", + "http://b5.mapswithme.com/" +#else + "http://svobodu404popugajam.mapswithme.com:34568/maps/" +#endif +}; + +UrlGenerator::UrlGenerator() + : m_randomGenerator(static_cast(time(NULL))), + m_firstGroup(&g_defaultFirstGroup[0], &g_defaultFirstGroup[0] + ARRAY_SIZE(g_defaultFirstGroup)), + m_secondGroup(&g_defaultSecondGroup[0], &g_defaultSecondGroup[0] + ARRAY_SIZE(g_defaultSecondGroup)) +{ +} + +UrlGenerator::UrlGenerator(vector const & firstGroup, vector const & secondGroup) + : m_randomGenerator(static_cast(time(NULL))), m_firstGroup(firstGroup), m_secondGroup(secondGroup) +{ +} + +string UrlGenerator::PopNextUrl() +{ + string s; + switch (m_firstGroup.size()) + { + case 1: + s = m_firstGroup.front(); + m_firstGroup.pop_back(); + break; + case 0: + switch (m_secondGroup.size()) + { + case 1: + s = m_secondGroup.front(); + m_secondGroup.pop_back(); + break; + case 0: // nothing left to return + break; + default: + { + vector::iterator const it = m_secondGroup.begin() + + static_cast::difference_type>(m_randomGenerator.Generate() % m_secondGroup.size()); + s = *it; + m_secondGroup.erase(it); + } + } + break; + default: + { + vector::iterator const it = m_firstGroup.begin() + + static_cast::difference_type>(m_randomGenerator.Generate() % m_firstGroup.size()); + s = *it; + m_firstGroup.erase(it); + } + } + return s; +} diff --git a/platform/url_generator.hpp b/platform/url_generator.hpp new file mode 100644 index 0000000000..198a1a2bd7 --- /dev/null +++ b/platform/url_generator.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include "../base/pseudo_random.hpp" + +#include "../std/vector.hpp" +#include "../std/string.hpp" + +class UrlGenerator +{ + LCG32 m_randomGenerator; + vector m_firstGroup; + vector m_secondGroup; + +public: + UrlGenerator(); + explicit UrlGenerator(vector const & firstGroup, vector const & secondGroup); + /// @return Always return empty string if all urls were already popped + string PopNextUrl(); +};