126 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// <random>10 11// template <class UIntType, UIntType a, UIntType c, UIntType m>12// class linear_congruential_engine13// {14// public:15// engine characteristics16// static constexpr result_type multiplier = a;17// static constexpr result_type increment = c;18// static constexpr result_type modulus = m;19// static constexpr result_type min() { return c == 0u ? 1u: 0u;}20// static constexpr result_type max() { return m - 1u;}21// static constexpr result_type default_seed = 1u;22 23#include <random>24#include <cassert>25#include <climits>26#include <type_traits>27 28#include "test_macros.h"29 30template <class T>31void where(const T &) {}32 33template <class T, T a, T c, T m>34void35test1()36{37 typedef std::linear_congruential_engine<T, a, c, m> LCE;38 typedef typename LCE::result_type result_type;39 static_assert((LCE::multiplier == a), "");40 static_assert((LCE::increment == c), "");41 static_assert((LCE::modulus == m), "");42#if TEST_STD_VER >= 1143 static_assert((LCE::min() == (c == 0u ? 1u: 0u)), "");44#else45 assert((LCE::min() == (c == 0u ? 1u: 0u)));46#endif47 48 TEST_DIAGNOSTIC_PUSH49 TEST_MSVC_DIAGNOSTIC_IGNORED(4310) // cast truncates constant value50 51#if TEST_STD_VER >= 1152 static_assert((LCE::max() == result_type(m - 1u)), "");53#else54 assert((LCE::max() == result_type(m - 1u)));55#endif56 57 TEST_DIAGNOSTIC_POP58 59 static_assert((LCE::default_seed == 1), "");60 where(LCE::multiplier);61 where(LCE::increment);62 where(LCE::modulus);63 where(LCE::default_seed);64}65 66template <class T>67void68test()69{70 const int W = sizeof(T) * CHAR_BIT;71 const T M(static_cast<T>(-1));72 const T A(static_cast<T>((static_cast<T>(1) << (W / 2)) - 1));73 74 // Cases where m = 075 test1<T, 0, 0, 0>();76 test1<T, A, 0, 0>();77 test1<T, 0, 1, 0>();78 test1<T, A, 1, 0>();79 80 // Cases where m = 2^n for n < w81 test1<T, 0, 0, 256>();82 test1<T, 5, 0, 256>();83 test1<T, 0, 1, 256>();84 test1<T, 5, 1, 256>();85 86 // Cases where m is odd and a = 087 test1<T, 0, 0, M>();88 test1<T, 0, M - 2, M>();89 test1<T, 0, M - 1, M>();90 91 // Cases where m is odd and m % a <= m / a (Schrage)92 test1<T, A, 0, M>();93 test1<T, A, M - 2, M>();94 test1<T, A, M - 1, M>();95}96 97template <class T>98void test_ext() {99 const T M(static_cast<T>(-1));100 101 // Cases where m is odd and m % a > m / a102 test1<T, M - 2, 0, M>();103 test1<T, M - 2, M - 2, M>();104 test1<T, M - 2, M - 1, M>();105 test1<T, M - 1, 0, M>();106 test1<T, M - 1, M - 2, M>();107 test1<T, M - 1, M - 1, M>();108}109 110int main(int, char**)111{112 test<unsigned short>();113 test_ext<unsigned short>();114 test<unsigned int>();115 test_ext<unsigned int>();116 test<unsigned long>();117 test_ext<unsigned long>();118 test<unsigned long long>();119 // This isn't implemented on platforms without __int128120#ifndef TEST_HAS_NO_INT128121 test_ext<unsigned long long>();122#endif123 124 return 0;125}126