Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions engine/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -290,27 +290,6 @@ clean: mostlyclean
-@echo [$(MODULE)] Cleaning target files
@$(REMOVE) $(MODULE) sc_http$(MODULE_EXT)

# Unit Tests
sc_http$(MODULE_EXT): interfaces$(PATHSEP)sc_http.cpp util$(PATHSEP)sc_io.cpp sc_thread.cpp sc_util.cpp
-@echo [$@] Linking
$(CXX) $(CPP_FLAGS) -std=c++0x -DUNIT_TEST $(OPTS_INTERNAL) $(OPTS) $(LINK_FLAGS) $^ -o $@ $(LINK_LIBS)

rng$(MODULE_EXT): util$(PATHSEP)rng.cpp lib$(PATHSEP)fmt$(PATHSEP)format.cpp util$(PATHSEP)chrono.cpp
-@echo [$@] Linking
$(CXX) $(CPP_FLAGS) -DUNIT_TEST $(OPTS_INTERNAL) $(OPTS) $(LINK_FLAGS) $^ -o $@ $(LINK_LIBS)

timeline$(MODULE_EXT): util$(PATHSEP)timeline.hpp util$(PATHSEP)timeline.cpp
-@echo [$@] Linking
$(CXX) $(CPP_FLAGS) -std=c++0x -DUNIT_TEST $(OPTS_INTERNAL) $(OPTS) $(LINK_FLAGS) $^ -o $@ $(LINK_LIBS)

sample_data$(MODULE_EXT): util$(PATHSEP)sample_data.hpp util$(PATHSEP)sample_data.cpp
-@echo [$@] Linking
$(CXX) $(CPP_FLAGS) -std=c++0x -DUNIT_TEST $(OPTS_INTERNAL) $(OPTS) $(LINK_FLAGS) $^ -o $@ $(LINK_LIBS)

sc_expressions$(MODULE_EXT): sim$(PATHSEP)sc_expressions.cpp sc_util.cpp
-@echo [$@] Linking
$(CXX) $(CPP_FLAGS) -DUNIT_TEST $(OPTS_INTERNAL) $(OPTS) $(LINK_FLAGS) $^ -o $@ $(LINK_LIBS)

# Deprecated targets

unix windows mac:
Expand Down
53 changes: 0 additions & 53 deletions engine/interfaces/sc_http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,56 +406,3 @@ std::tuple<std::string, std::string> http::normalize_header( util::string_view h


#endif /* NO_SC_NETWORKING */

#ifdef UNIT_TEST

uint32_t dbc::get_school_mask( school_e ) { return 0; }
void sim_t::errorf( const char*, ... ) { }

int main( int argc, char* argv[] )
{
if ( argc > 1 )
{
for ( int i = 1; i < argc; ++i )
{
if ( !strcmp( argv[ i ], "--dump" ) )
{
url_db.clear();
const char* const url_cache_file = "simc_cache.dat";
http::cache_load( url_cache_file );

for ( auto& i : url_db )
{
std::cout << "URL: \"" << i.first << "\" (" << i.second.last_modified_header << ")\n"
<< i.second.result << '\n';
}
}
else
{
std::string result;
if ( http::get( result, argv[ i ], cache::CURRENT ) )
std::cout << result << '\n';
else
std::cout << "Unable to download \"" << argv[ i ] << "\".\n";
}
}
}
else
{
std::string result;

if ( http::get( result, "http://us.battle.net/wow/en/character/llane/pagezero/advanced", cache::CURRENT ) )
std::cout << result << '\n';
else
std::cout << "Unable to download armory data.\n";

if ( http::get( result, "http://www.wowhead.com/list=1564664", cache::CURRENT ) )
std::cout << result << '\n';
else
std::cout << "Unable to download wowhead data.\n";
}

return 0;
}

#endif /* UNIT_TEST */
91 changes: 0 additions & 91 deletions engine/sim/expressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1593,94 +1593,3 @@ player_t* target_wrapper_expr_t::target() const
{
return action.get_expression_target();
}

#ifdef UNIT_TEST

uint32_t dbc::get_school_mask( school_e )
{
return 0;
}

namespace
{
expr_t* parse_expression( const char* arg )
{
std::vector<expr_token_t> tokens = expression_t::parse_tokens( 0, arg );
expression_t::print_tokens( tokens, 0 );

if ( expression_t::convert_to_rpn( tokens ) )
{
puts( "rpn:" );
expression_t::print_tokens( tokens, 0 );

return build_expression_tree( 0, tokens, false );
}

return 0;
}

void time_test( expr_t* expr, uint64_t n )
{
double value = 0;
const int64_t start = util::milliseconds();
for ( uint64_t i = 0; i < n; ++i )
value = expr->eval();
const int64_t stop = util::milliseconds();
printf( "evaluate: %f in %.4f seconds\n", value, ( stop - start ) / 1000.0 );
}
}

void sim_t::cancel()
{
}

int sim_t::errorf( const char* format, ... )
{
va_list ap;
va_start( ap, format );
int result = vfprintf( stderr, format, ap );
va_end( ap );
return result;
}

void sim_t::output( sim_t*, const char* format, ... )
{
va_list ap;
va_start( ap, format );
vfprintf( stdout, format, ap );
va_end( ap );
}

int main( int argc, char** argv )
{
uint64_t n_evals = 1;

for ( int i = 1; i < argc; i++ )
{
if ( util::str_compare_ci( argv[ i ], "-n" ) )
{
++i;
assert( i < argc );
std::istringstream is( argv[ i ] );
is >> n_evals;
assert( n_evals > 0 );
continue;
}

expr_t* expr = parse_expression( argv[ i ] );
if ( expr )
{
if ( n_evals == 1 )
{
puts( "evaluate:" );
printf( "%f\n", expr->eval() );
}
else
time_test( expr, n_evals );
}
}

return 0;
}

#endif
204 changes: 0 additions & 204 deletions engine/util/rng.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,207 +363,3 @@ void truncated_gauss_t::calculate_cdf()
}

} // rng
#ifdef UNIT_TEST
// Code to test functionality and performance of our RNG implementations

#include <random>
#include <tuple>

#include "lib/fmt/format.h"
#include "util/generic.hpp"
#include "util/chrono.hpp"

using test_clock = chrono::cpu_clock;

template <typename Engine>
static void test_one( rng::basic_rng_t<Engine>& rng, uint64_t n )
{
auto start_time = test_clock::now();

double average = 0;
for ( uint64_t i = 0; i < n; ++i )
{
double d = rng.real();
//std::cout << d << "\n";
average += d;
}

average /= n;
auto elapsed_cpu = chrono::elapsed_fp_seconds(start_time);

fmt::print( "{} calls to rng::{}::real(), average = {:.8f}, time = {}s, numbers/sec = {}\n\n",
n, rng.name(), average, elapsed_cpu, static_cast<uint64_t>( n * 1000.0 / elapsed_cpu ) );
}

template <typename Engine>
static void test_seed( rng::basic_rng_t<Engine>& rng, uint64_t n )
{
auto start_time = test_clock::now();

for ( uint64_t i = 0; i < n; ++i )
{
rng.seed( uint64_t( m_pi ) * n );
}

auto elapsed_cpu = chrono::elapsed_fp_seconds(start_time);

fmt::print( "{} calls to rng::{}::seed(), time = {}s, numbers/sec = {}\n\n",
n, rng.name(), elapsed_cpu, static_cast<uint64_t>( n * 1000.0 / elapsed_cpu ) );
}

// Monte-Carlo PI calculation.
template <typename Engine>
static void monte_carlo( rng::basic_rng_t<Engine>& rng, uint64_t n )
{
auto start_time = test_clock::now();

uint64_t count_in_sphere = 0;
for ( uint64_t i = 0; i < n; ++i )
{
double x1 = rng.real();
double x2 = rng.real();
if ( x1 * x1 + x2 * x2 < 1.0 )
++count_in_sphere;
}
auto elapsed_cpu = chrono::elapsed_fp_seconds(start_time);

fmt::print( "{} runs for pi-calculation with {}. count in sphere = {}, pi = {:.21f}, time = {}s, numbers/sec = {}\n\n",
n, rng.name(), count_in_sphere, static_cast<double>( count_in_sphere ) * 4 / n,
elapsed_cpu, static_cast<uint64_t>( n * 1000.0 / elapsed_cpu ) );
}

template <typename Engine>
static void test_uniform_int( rng::basic_rng_t<Engine>& rng, uint64_t n, unsigned num_buckets )
{
auto start_time = test_clock::now();

std::vector<unsigned> histogram(num_buckets);

for ( uint64_t i = 0; i < n; ++i )
{
auto result = rng.range(histogram.size());
histogram[ result ] += 1;
}

auto elapsed_cpu = chrono::elapsed_fp_seconds(start_time);

double expected_bucket_size = static_cast<double>(n) / histogram.size();

fmt::print("{} call to rng::{}::range(size_t({}u))\n", n, rng.name(), histogram.size());
for (unsigned i = 0; i < histogram.size(); ++i)
{
double pct = static_cast<double>(histogram[ i ]) / n;
double diff = static_cast<double>(histogram[ i ]) / expected_bucket_size - 1.0;
fmt::print(" bucket {:2n}: {:5.2f}% ({}) difference to expected: {:9.6f}%\n", i, pct, histogram[ i ], diff);
}
fmt::print("time = {} s\n\n", elapsed_cpu);
}

namespace detail {
template <typename Tuple, typename F, std::size_t... I>
void for_each_impl(Tuple&& t, F&& f, std::index_sequence<I...>)
{
int _[] = { ( std::invoke( std::forward<F>( f ), std::get<I>( std::forward<Tuple>(t) ) ), 0 )... };
(void)_;
}
}

template <typename Tuple, typename F>
void for_each(Tuple&& t, F&& f)
{
return detail::for_each_impl( std::forward<Tuple>(t), std::forward<F>(f),
std::make_index_sequence<std::tuple_size<std::remove_reference_t<Tuple>>::value>{});
}

int main( int /*argc*/, char** /*argv*/ )
{
auto generators = std::make_tuple(
rng::basic_rng_t<rng::xoshiro256plus_t>{},
rng::basic_rng_t<rng::xorshift128_t>{},
rng::basic_rng_t<rng::xorshift1024_t>{}
);

std::random_device rd;
uint64_t seed = uint64_t(rd()) | (uint64_t(rd()) << 32);
fmt::print( "Seed: {}\n\n", seed );

for_each( generators, [seed]( auto& g ) { g.seed( seed ); } );

for_each( generators, []( auto& g ) { test_one( g, 1'000'000'000 ); } );

for_each( generators, []( auto& g ) { monte_carlo( g, 1'000'000'000 ); } );

for_each( generators, []( auto& g ) { test_seed( g, 10'000'000 ); } );

for_each( generators, []( auto& g ) { test_uniform_int( g, 10'000, 10 ); } );

fmt::print( "random device: min={} max={}\n\n", rd.min(), rd.max() );

auto& rng = std::get<0>( generators );
fmt::print( "Testing {}\n\n", rng.name() );

const uint64_t n = 100'000'000;

// double gauss
{
auto start_time = test_clock::now();

double average = 0;
for ( uint64_t i = 0; i < n; i++ )
average += rng.gauss( 0, 1 );
average /= n;

fmt::print( "{} calls to gauss(0, 1): average = {:.8f}, time = {}s\n\n",
n, average, chrono::elapsed_fp_seconds(start_time) );
}

// double exgauss
{
auto start_time = test_clock::now();

double average = 0;
for ( uint64_t i = 0; i < n; i++ )
average += 0.1 + rng.exgauss( 0.3, 0.06, 0.25 );
average /= n;

fmt::print( "{} calls to 0.1 + rng.exgauss( 0.3,0.06,0.25 );: average = {:.8f}, time = {}s\n\n",
n, average, chrono::elapsed_fp_seconds(start_time) );
}

// exponential
{
auto start_time = test_clock::now();

double nu = 0.25;
double average = 0;
for ( uint64_t i = 0; i < n; i++ )
{
double result = rng.exponential( nu );
average += result;
}
average /= n;

fmt::print( "{} calls exp nu=0.25: average = {}, time = {}s\n\n",
n, average, chrono::elapsed_fp_seconds(start_time) );
}

fmt::print( "\nreal:\n" );
for ( unsigned i = 1; i <= 100; i++ )
fmt::print( " {:>11.8f}{}", rng.real(), i % 10 == 0 ? "\n" : "" );

fmt::print( "\ngauss mean=0, std_dev=1.0:\n" );
for ( unsigned i = 1; i <= 100; i++ )
fmt::print( " {:>11.8f}{}", rng.gauss( 0.0, 1.0 ), i % 10 == 0 ? "\n" : "" );

fmt::print( "\nroll 30%:\n" );
for ( unsigned i = 1; i <= 100; i++ )
fmt::print( " {:d}{}", rng.roll( 0.30 ), i % 10 == 0 ? "\n" : "" );

fmt::print( "\n" );
fmt::print( "m_pi={}\n", m_pi );
fmt::print( "calls to rng::stdnormal_inv( double x )\n" );
fmt::print( "x=0.975: {:.7} should be equal to 1.959964\n", rng::stdnormal_inv( 0.975 ) );
fmt::print( "x=0.995: {:.8} should be equal to 2.5758293\n", rng::stdnormal_inv( 0.995 ) );
}

#endif // UNIT_TEST