1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <random> 11 12 // template <class UIntType, UIntType a, UIntType c, UIntType m> 13 // class linear_congruential_engine; 14 15 // explicit linear_congruential_engine(result_type s = default_seed); 16 17 #include <random> 18 #include <sstream> 19 #include <cassert> 20 21 template <class T> 22 void 23 test1() 24 { 25 // c % m != 0 && s % m != 0 26 { 27 typedef std::linear_congruential_engine<T, 2, 3, 7> E; 28 E e(5); 29 std::ostringstream os; 30 os << e; 31 assert(os.str() == "5"); 32 } 33 { 34 typedef std::linear_congruential_engine<T, 2, 3, 0> E; 35 E e(5); 36 std::ostringstream os; 37 os << e; 38 assert(os.str() == "5"); 39 } 40 { 41 typedef std::linear_congruential_engine<T, 2, 3, 4> E; 42 E e(5); 43 std::ostringstream os; 44 os << e; 45 assert(os.str() == "1"); 46 } 47 } 48 49 template <class T> 50 void 51 test2() 52 { 53 // c % m != 0 && s % m == 0 54 { 55 typedef std::linear_congruential_engine<T, 2, 3, 7> E; 56 E e(7); 57 std::ostringstream os; 58 os << e; 59 assert(os.str() == "0"); 60 } 61 { 62 typedef std::linear_congruential_engine<T, 2, 3, 0> E; 63 E e(0); 64 std::ostringstream os; 65 os << e; 66 assert(os.str() == "0"); 67 } 68 { 69 typedef std::linear_congruential_engine<T, 2, 3, 4> E; 70 E e(4); 71 std::ostringstream os; 72 os << e; 73 assert(os.str() == "0"); 74 } 75 } 76 77 template <class T> 78 void 79 test3() 80 { 81 // c % m == 0 && s % m != 0 82 { 83 typedef std::linear_congruential_engine<T, 2, 0, 7> E; 84 E e(3); 85 std::ostringstream os; 86 os << e; 87 assert(os.str() == "3"); 88 } 89 { 90 typedef std::linear_congruential_engine<T, 2, 0, 0> E; 91 E e(5); 92 std::ostringstream os; 93 os << e; 94 assert(os.str() == "5"); 95 } 96 { 97 typedef std::linear_congruential_engine<T, 2, 0, 4> E; 98 E e(7); 99 std::ostringstream os; 100 os << e; 101 assert(os.str() == "3"); 102 } 103 } 104 105 template <class T> 106 void 107 test4() 108 { 109 // c % m == 0 && s % m == 0 110 { 111 typedef std::linear_congruential_engine<T, 2, 0, 7> E; 112 E e(7); 113 std::ostringstream os; 114 os << e; 115 assert(os.str() == "1"); 116 } 117 { 118 typedef std::linear_congruential_engine<T, 2, 0, 0> E; 119 E e(0); 120 std::ostringstream os; 121 os << e; 122 assert(os.str() == "1"); 123 } 124 { 125 typedef std::linear_congruential_engine<T, 2, 0, 4> E; 126 E e(8); 127 std::ostringstream os; 128 os << e; 129 assert(os.str() == "1"); 130 } 131 } 132 133 int main() 134 { 135 test1<unsigned short>(); 136 test1<unsigned int>(); 137 test1<unsigned long>(); 138 test1<unsigned long long>(); 139 140 test2<unsigned short>(); 141 test2<unsigned int>(); 142 test2<unsigned long>(); 143 test2<unsigned long long>(); 144 145 test3<unsigned short>(); 146 test3<unsigned int>(); 147 test3<unsigned long>(); 148 test3<unsigned long long>(); 149 150 test4<unsigned short>(); 151 test4<unsigned int>(); 152 test4<unsigned long>(); 153 test4<unsigned long long>(); 154 } 155