// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2015 Gael Guennebaud // // This Source Code Form is subject to the terms of the Mozilla // Public License v. 2.0. If a copy of the MPL was not distributed // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. #include "main.h" typedef long long int64; template Scalar check_in_range(Scalar x, Scalar y) { Scalar r = internal::random(x, y); VERIFY(r >= x); if (y >= x) { VERIFY(r <= y); } return r; } template void check_all_in_range(Scalar x, Scalar y) { Array mask(y - x + 1); mask.fill(0); long n = (y - x + 1) * 32; for (long k = 0; k < n; ++k) { mask(check_in_range(x, y) - x)++; } for (Index i = 0; i < mask.size(); ++i) if (mask(i) == 0) std::cout << "WARNING: value " << x + i << " not reached." << std::endl; VERIFY((mask > 0).all()); } template void check_histogram(Scalar x, Scalar y, int bins) { Array hist(bins); hist.fill(0); int f = 100000; int n = bins * f; int64 range = int64(y) - int64(x); int divisor = int((range + 1) / bins); assert(((range + 1) % bins) == 0); for (int k = 0; k < n; ++k) { Scalar r = check_in_range(x, y); hist(int((int64(r) - int64(x)) / divisor))++; } VERIFY((((hist.cast() / double(f)) - 1.0).abs() < 0.03).all()); } EIGEN_DECLARE_TEST(rand) { long long_ref = NumTraits::highest() / 10; // the minimum guarantees that these conversions are safe auto char_offset = static_cast((std::min)(g_repeat, 64)); auto short_offset = static_cast((std::min)(g_repeat, 8000)); for (int i = 0; i < g_repeat * 10000; i++) { CALL_SUBTEST(check_in_range(10, 11)); CALL_SUBTEST(check_in_range(1.24234523f, 1.24234523f)); CALL_SUBTEST(check_in_range(-1, 1)); CALL_SUBTEST(check_in_range(-1432.2352f, -1432.2352f)); CALL_SUBTEST(check_in_range(10, 11)); CALL_SUBTEST(check_in_range(1.24234523, 1.24234523)); CALL_SUBTEST(check_in_range(-1, 1)); CALL_SUBTEST(check_in_range(-1432.2352, -1432.2352)); CALL_SUBTEST(check_in_range(0, -1)); CALL_SUBTEST(check_in_range(0, -1)); CALL_SUBTEST(check_in_range(0, -1)); CALL_SUBTEST(check_in_range(-673456, 673456)); CALL_SUBTEST(check_in_range(-RAND_MAX + 10, RAND_MAX - 10)); CALL_SUBTEST(check_in_range(-24345, 24345)); CALL_SUBTEST(check_in_range(-long_ref, long_ref)); } CALL_SUBTEST(check_all_in_range(11, 11)); CALL_SUBTEST(check_all_in_range(11, 11 + char_offset)); CALL_SUBTEST(check_all_in_range(-5, 5)); CALL_SUBTEST(check_all_in_range(-11 - char_offset, -11)); CALL_SUBTEST(check_all_in_range(-126, -126 + char_offset)); CALL_SUBTEST(check_all_in_range(126 - char_offset, 126)); CALL_SUBTEST(check_all_in_range(-126, 126)); CALL_SUBTEST(check_all_in_range(11, 11)); CALL_SUBTEST(check_all_in_range(11, 11 + short_offset)); CALL_SUBTEST(check_all_in_range(-5, 5)); CALL_SUBTEST(check_all_in_range(-11 - short_offset, -11)); CALL_SUBTEST(check_all_in_range(-24345, -24345 + short_offset)); CALL_SUBTEST(check_all_in_range(24345, 24345 + short_offset)); CALL_SUBTEST(check_all_in_range(11, 11)); CALL_SUBTEST(check_all_in_range(11, 11 + g_repeat)); CALL_SUBTEST(check_all_in_range(-5, 5)); CALL_SUBTEST(check_all_in_range(-11 - g_repeat, -11)); CALL_SUBTEST(check_all_in_range(-673456, -673456 + g_repeat)); CALL_SUBTEST(check_all_in_range(673456, 673456 + g_repeat)); CALL_SUBTEST(check_all_in_range(11, 11)); CALL_SUBTEST(check_all_in_range(11, 11 + g_repeat)); CALL_SUBTEST(check_all_in_range(-5, 5)); CALL_SUBTEST(check_all_in_range(-11 - g_repeat, -11)); CALL_SUBTEST(check_all_in_range(-long_ref, -long_ref + g_repeat)); CALL_SUBTEST(check_all_in_range(long_ref, long_ref + g_repeat)); CALL_SUBTEST(check_histogram(-5, 5, 11)); int bins = 100; CALL_SUBTEST(check_histogram(-3333, -3333 + bins * (3333 / bins) - 1, bins)); bins = 1000; CALL_SUBTEST(check_histogram(-RAND_MAX + 10, -RAND_MAX + 10 + bins * (RAND_MAX / bins) - 1, bins)); CALL_SUBTEST( check_histogram(-RAND_MAX + 10, -int64(RAND_MAX) + 10 + bins * (2 * int64(RAND_MAX) / bins) - 1, bins)); }