Path: blob/main/unittest/src/utils/common/RandHelperTest.cpp
169684 views
/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file RandHelperTest.cpp14/// @author Michael Behrisch15/// @date Oct 201016///17// Tests the class RandHelper18/****************************************************************************/192021// ===========================================================================22// included modules23// ===========================================================================24#include <config.h>2526#include <gtest/gtest.h>27#include <utils/common/StdDefs.h>28#include <utils/common/StopWatch.h>29#include <utils/common/RandHelper.h>303132// ===========================================================================33// test definitions34// ===========================================================================35/* Test the method 'rand' without parameters.*/36TEST(RandHelper, test_rand_range) {37for (int i = 0; i < 1000; i++) {38const double rand = RandHelper::rand();39EXPECT_LT(rand, double(1));40EXPECT_LE(0., rand);41}42}4344/* Test the method 'rand' with an upper float limit.*/45TEST(RandHelper, test_rand_range_float) {46for (int i = 0; i < 1000; i++) {47const double rand = RandHelper::rand(double(10));48EXPECT_LT(rand, double(10));49EXPECT_LE(0., rand);50}51}5253/* Test the method 'rand' with an upper int limit.*/54TEST(RandHelper, test_rand_range_int) {55for (int i = 0; i < 1000; i++) {56const double rand = RandHelper::rand(100);57EXPECT_LT(rand, 100);58EXPECT_LE(0, rand);59}60}6162/* Test the method 'rand' with two float limits.*/63TEST(RandHelper, test_rand_range_two_float) {64for (int i = 0; i < 1000; i++) {65const double rand = RandHelper::rand(double(0.1), double(0.5));66EXPECT_LT(rand, double(0.5));67EXPECT_LE(double(0.1), rand);68}69}7071/* Test the method 'rand' with two int limits.*/72TEST(RandHelper, test_rand_range_two_int) {73for (int i = 0; i < 1000; i++) {74const double rand = RandHelper::rand(50, 100);75EXPECT_LT(rand, 100);76EXPECT_LE(50, rand);77}78}7980/* Test whether the 'rand' distribution is more or less uniform.*/81TEST(RandHelper, test_uniform) {82int count[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};83for (int i = 0; i < 1000; i++) {84const double rand = RandHelper::rand(0., double(10));85count[(int)rand]++;86}87for (int i = 0; i < 10; i++) {88EXPECT_LE(50, count[i]) << "Testing interval " << i;89EXPECT_LT(count[i], 150) << "Testing interval " << i;90}91}9293/* Test whether the 'randNorm' distribution is more or less gaussian.*/94TEST(RandHelper, test_norm) {95int count[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};96for (int i = 0; i < 1000; i++) {97const double rand = RandHelper::randNorm(double(5), double(2));98count[MIN2(MAX2((int)rand, 0), 9)]++;99}100EXPECT_LE(0, count[0]);101EXPECT_LT(count[0], 100);102EXPECT_LE(0, count[1]);103EXPECT_LT(count[1], 100);104EXPECT_LE(50, count[2]);105EXPECT_LT(count[2], 150);106EXPECT_LE(100, count[3]);107EXPECT_LT(count[3], 200);108EXPECT_LE(100, count[4]);109EXPECT_LT(count[4], 250);110EXPECT_LE(100, count[5]);111EXPECT_LT(count[5], 250);112EXPECT_LE(100, count[6]);113EXPECT_LT(count[6], 200);114EXPECT_LE(0, count[7]);115EXPECT_LT(count[7], 100);116EXPECT_LE(0, count[8]);117EXPECT_LT(count[8], 100);118EXPECT_LE(0, count[9]);119EXPECT_LT(count[9], 100);120}121122/* Test whether the 'rand' sequence is always the same.*/123TEST(RandHelper, test_sequence) {124RandHelper::initRand();125int expect[] = { 46, 17, 19, 75, 11, 42, 28, 22, 77, 11 };126for (int i = 0; i < 10; i++) {127EXPECT_EQ(expect[i], RandHelper::rand(100));128}129}130131/* Test the performance of the method 'rand' without parameters.*/132// TEST(RandHelper, test_perf) {133// StopWatch sw;134// sw.start();135// for (int i = 0; i < 100000000; i++) {136// const double rand = RandHelper::rand();137// }138// std::cout << sw.stop();139// }140141142