90 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// UNSUPPORTED: c++0310 11// <random>12 13// template<class _IntType = int>14// class uniform_int_distribution15 16// template<class _URNG> result_type operator()(_URNG& g);17 18 19#include <cassert>20#include <cstdint>21#include <limits>22#include <random>23 24#include "test_macros.h"25 26int main(int, char**) {27 28#ifndef TEST_HAS_NO_INT12829 30 // Test that values outside of the 64-bit range can be produced.31 {32 std::minstd_rand0 e;33 std::uniform_int_distribution<__int128_t> d;34 assert(d.min() == 0 && d.max() == std::numeric_limits<__int128_t>::max());35 bool all_in_64bit_range = true;36 for (int i = 0; i < 100; ++i) {37 __int128_t n = d(e);38 all_in_64bit_range = all_in_64bit_range && (n <= UINT64_MAX);39 }40 assert(!all_in_64bit_range);41 }42 43 // Same test as above with min/max set and outside the 64-bit range.44 {45 __int128_t a = ((__int128_t)INT64_MIN) * 10;46 __int128_t b = ((__int128_t)INT64_MAX) * 10;47 std::minstd_rand0 e;48 std::uniform_int_distribution<__int128_t> d(a, b);49 assert(d.min() == a && d.max() == b);50 bool all_in_64bit_range = true;51 for (int i = 0; i < 100; ++i) {52 __int128_t n = d(e);53 assert(a <= n && n <= b);54 all_in_64bit_range = all_in_64bit_range && (INT64_MIN <= n) && (n <= (INT64_MAX));55 }56 assert(!all_in_64bit_range);57 }58 59 // Same test as above with __uint128_t.60 {61 __uint128_t a = UINT64_MAX / 3;62 __uint128_t b = ((__uint128_t)UINT64_MAX) * 10;63 std::minstd_rand0 e;64 std::uniform_int_distribution<__uint128_t> d(a, b);65 assert(d.min() == a && d.max() == b);66 bool all_in_64bit_range = true;67 for (int i = 0; i < 100; ++i) {68 __uint128_t n = d(e);69 assert(a <= n && n <= b);70 all_in_64bit_range = all_in_64bit_range && (n <= (UINT64_MAX));71 }72 assert(!all_in_64bit_range);73 }74 75 // Regression test for PR#51520:76 {77 std::minstd_rand0 e;78 std::uniform_int_distribution<__int128_t> d(INT64_MIN, INT64_MAX);79 assert(d.min() == INT64_MIN && d.max() == INT64_MAX);80 for (int i = 0; i < 100; ++i) {81 __int128_t n = d(e);82 assert((INT64_MIN <= n) && (n <= INT64_MAX));83 }84 }85 86#endif // TEST_HAS_NO_INT12887 88 return 0;89}90