/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* 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/. */
// Note that because we always provide FastBernoulliTrial with a fixed // pseudorandom seed in these tests, the results here are completely // deterministic. // // A non-optimized version of this test runs in .009s on my laptop. Using larger // sample sizes lets us meet tighter bounds on the counts.
// Establish a very high skip count.
bernoulli.setProbability(0.0);
// This should re-establish a zero skip count.
bernoulli.setProbability(1.0);
// So this should return true.
MOZ_RELEASE_ASSERT(bernoulli.trial());
}
staticvoid TestCuspProbabilities() { /* * FastBernoulliTrial takes care to avoid screwing up on edge cases. The * checks here all look pretty dumb, but they exercise paths in the code that * could exhibit undefined behavior if coded naïvely.
*/
/* * This should not be perceptibly different from 1; for 64-bit doubles, this * is a one in ten trillion chance of the trial not succeeding. Overflows * converting doubles to size_t skip counts may change this, though.
*/
mozilla::FastBernoulliTrial bernoulli(nextafter(1, 0), 0x67ff17e25d855942ULL,
0x74f298193fe1c5b1ULL);
for (size_t i = 0; i < 1000; i++) MOZ_RELEASE_ASSERT(bernoulli.trial());
/* * This should not be perceptibly different from 0; for 64-bit doubles, * the FastBernoulliTrial will actually treat this as exactly zero.
*/
bernoulli.setProbability(nextafter(0, 1)); for (size_t i = 0; i < 1000; i++) MOZ_RELEASE_ASSERT(!bernoulli.trial());
/* * This should be a vanishingly low probability which FastBernoulliTrial does * *not* treat as exactly zero.
*/
bernoulli.setProbability(1 - nextafter(1, 0)); for (size_t i = 0; i < 1000; i++) MOZ_RELEASE_ASSERT(!bernoulli.trial());
}
int main() {
TestProportions();
TestHarmonics();
TestTrialN();
TestChangeProbability();
TestCuspProbabilities();
return 0;
}
¤ Dauer der Verarbeitung: 0.34 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.