Bug 1732219 - Add API for fetching the preview image. r=geckoview-reviewers,agi,mconley
[gecko.git] / mfbt / tests / TestRandomNum.cpp
blobf53c42fc83f896e02fa31c400037d06c74943106
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/RandomNum.h"
8 #include <vector>
12 * We're going to check that random number generation is sane on a basic
13 * level - That is, we want to check that the function returns success
14 * and doesn't just keep returning the same number.
16 * Note that there are many more tests that could be done, but to really test
17 * a PRNG we'd probably need to generate a large set of randoms and
18 * perform statistical analysis on them. Maybe that's worth doing eventually?
20 * For now we should be fine just performing a dumb test of generating 5
21 * numbers and making sure they're all unique. In theory, it is possible for
22 * this test to report a false negative, but with 5 numbers the probability
23 * is less than one-in-a-trillion.
27 #define NUM_RANDOMS_TO_GENERATE 5
29 using mozilla::Maybe;
30 using mozilla::RandomUint64;
32 static uint64_t getRandomUint64OrDie() {
33 Maybe<uint64_t> maybeRandomNum = RandomUint64();
35 MOZ_RELEASE_ASSERT(maybeRandomNum.isSome());
37 return maybeRandomNum.value();
40 static void TestRandomUint64() {
41 // The allocator uses RandomNum.h too, but its initialization path allocates
42 // memory. While the allocator itself handles the situation, we can't, so
43 // we make sure to use an allocation before getting a Random number ourselves.
44 std::vector<uint64_t> randomsList;
45 randomsList.reserve(NUM_RANDOMS_TO_GENERATE);
47 for (uint8_t i = 0; i < NUM_RANDOMS_TO_GENERATE; ++i) {
48 uint64_t randomNum = getRandomUint64OrDie();
50 for (uint64_t num : randomsList) {
51 MOZ_RELEASE_ASSERT(randomNum != num);
54 randomsList.push_back(randomNum);
58 int main() {
59 TestRandomUint64();
60 return 0;