Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Copyright (C) 2019 Wildfire Games.
- * This file is part of 0 A.D.
- *
- * 0 A.D. is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 2 of the License, or
- * (at your option) any later version.
- *
- * 0 A.D. is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
- */
- #include "lib/self_test.h"
- #include "lib/rand.h"
- #include <iostream>
- #include <random>
- #include <chrono>
- #include <boost/random/mersenne_twister.hpp>
- #include <boost/random/uniform_real_distribution.hpp>
- class TestSoundGroup : public CxxTest::TestSuite
- {
- public:
- void test_generation()
- {
- unsigned int samples = 1000000;
- float min = 0.0f;
- float max = 1000.0f;
- float value = 0.f;
- for (unsigned int i = 0; i < samples; ++i)
- {
- value = RandFloat(min, max);
- TS_ASSERT(value >= min && value <= max);
- m_BoostRng.seed(i);
- value = RandFloatBoost(min, max);
- TS_ASSERT(value >= min && value <= max);
- m_StlRng.seed(i);
- value = RandFloatStl(min, max);
- TS_ASSERT(value >= min && value <= max);
- }
- }
- void test_speed()
- {
- unsigned int samples = 10000000;
- float min = 0.0f;
- float max = 1.0f;
- float value = 0.f;
- std::chrono::high_resolution_clock::time_point begin = std::chrono::high_resolution_clock::now();
- for (unsigned int i = 0; i < samples; ++i)
- {
- value = RandFloat(min, max);
- }
- std::chrono::high_resolution_clock::time_point end = std::chrono::high_resolution_clock::now();
- TS_ASSERT(value >= min && value <= max);
- std::cout << std::endl;
- float defaultSeconds = (std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin).count() / 1000000000.f);
- std::cout << "RandFloat(float min, float max): " << defaultSeconds << " seconds" << std::endl;
- begin = std::chrono::high_resolution_clock::now();
- for (unsigned int i = 0; i < samples; ++i)
- {
- m_BoostRng.seed(i);
- value = RandFloatBoost(min, max);
- }
- end = std::chrono::high_resolution_clock::now();
- float boostSeconds = (std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin).count() / 1000000000.f);
- std::cout << "RandFloatBoost(float min, float max): " << boostSeconds << " seconds" << std::endl;
- TS_ASSERT(value >= min && value <= max);
- begin = std::chrono::high_resolution_clock::now();
- for (unsigned int i = 0; i < samples; ++i)
- {
- m_StlRng.seed(i);
- value = RandFloatStl(min, max);
- }
- end = std::chrono::high_resolution_clock::now();
- float stlSeconds = (std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin).count() / 1000000000.f);
- std::cout << "RandFloatStl(float min, float max): " << (std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin).count() / 1000000000.f) << " seconds" << std::endl;
- TS_ASSERT(value >= min && value <= max);
- TS_ASSERT(stlSeconds < boostSeconds && boostSeconds > defaultSeconds && stlSeconds > defaultSeconds);
- }
- private:
- static boost::random::mt19937 m_BoostRng;
- static std::mt19937 m_StlRng;
- static float RandFloat(float min, float max)
- {
- return rand(static_cast<size_t>(min * 100.f), static_cast<size_t>(max * 100.f)) / 100.f;
- }
- static float RandFloatBoost(float min, float max)
- {
- return boost::random::uniform_real_distribution<float>(min, max)(m_BoostRng);
- }
- static float RandFloatStl(float min, float max)
- {
- return std::uniform_real_distribution<float>(min, max)(m_StlRng);
- }
- };
- boost::random::mt19937 TestSoundGroup::m_BoostRng;
- std::mt19937 TestSoundGroup::m_StlRng;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement