xref: /aosp_15_r20/external/deqp/framework/delibs/decpp/deRandom.cpp (revision 35238bce31c2a825756842865a792f8cf7f89930)
1 /*-------------------------------------------------------------------------
2  * drawElements C++ Base Library
3  * -----------------------------
4  *
5  * Copyright 2014 The Android Open Source Project
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  *//*!
20  * \file
21  * \brief Random number generator utilities.
22  *//*--------------------------------------------------------------------*/
23 
24 #include "deRandom.hpp"
25 
operator ==(const deRandom & a,const deRandom & b)26 inline bool operator==(const deRandom &a, const deRandom &b)
27 {
28     return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w;
29 }
30 
operator !=(const deRandom & a,const deRandom & b)31 inline bool operator!=(const deRandom &a, const deRandom &b)
32 {
33     return a.x != b.x || a.y != b.y || a.z != b.z || a.w != b.w;
34 }
35 
36 namespace de
37 {
38 
operator ==(const Random & other) const39 bool Random::operator==(const Random &other) const
40 {
41     return m_rnd == other.m_rnd;
42 }
43 
operator !=(const Random & other) const44 bool Random::operator!=(const Random &other) const
45 {
46     return m_rnd != other.m_rnd;
47 }
48 
Random_selfTest(void)49 void Random_selfTest(void)
50 {
51     // getBool()
52 
53     {
54         static const bool expected[] = {true,  false, false, false, true, true,  false, false, false, false,
55                                         false, false, true,  false, true, false, false, false, false, true};
56         Random rnd(4789);
57         for (int i = 0; i < DE_LENGTH_OF_ARRAY(expected); i++)
58             DE_TEST_ASSERT(expected[i] == rnd.getBool());
59     }
60 
61     // getInt(a, b)
62 
63     {
64         static const int expected[] = {-6628, -6483, 802, -7758, -8463, 3165, 9216, 3107, 1851, 8707};
65         Random rnd(4789);
66         for (int i = 0; i < DE_LENGTH_OF_ARRAY(expected); i++)
67             DE_TEST_ASSERT(expected[i] == rnd.getInt(-10000, 10000));
68     }
69 
70     // getUint32()
71 
72     {
73         static const uint32_t expected[] = {3694588092u, 3135240271u, 882874943u,  2108407657u, 376640368u,
74                                             1395362929u, 2611849801u, 3151830690u, 901476922u,  989608184u};
75         Random rnd(4789);
76         for (int i = 0; i < DE_LENGTH_OF_ARRAY(expected); i++)
77             DE_TEST_ASSERT(expected[i] == rnd.getUint32());
78     }
79 
80     // getUint64()
81 
82     {
83         static const uint64_t expected[] = {15868135030466279503ull, 3791919008751271785ull,  1617658064308767857ull,
84                                             11217809480510938786ull, 3871813899078351096ull,  14768747990643252542ull,
85                                             8163484985646009214ull,  14928018127607458387ull, 432108271545246292ull,
86                                             7318152987070448462ull};
87         Random rnd(4789);
88         for (int i = 0; i < DE_LENGTH_OF_ARRAY(expected); i++)
89             DE_TEST_ASSERT(expected[i] == rnd.getUint64());
90     }
91 
92     // getFloat()
93 
94     {
95         static const float expected[] = {0.763413f, 0.679680f, 0.288965f, 0.854431f, 0.403095f,
96                                          0.198132f, 0.729899f, 0.741484f, 0.358263f, 0.686578f};
97         const float epsilon           = 0.01f;
98         Random rnd(4789);
99         for (int i = 0; i < DE_LENGTH_OF_ARRAY(expected); i++)
100             DE_TEST_ASSERT(de::abs(expected[i] - rnd.getFloat()) < epsilon);
101     }
102 
103     // getFloat(a, b)
104 
105     {
106         static const float expected[] = {824.996643f,  675.039185f, -24.691774f, 987.999756f, 179.702286f,
107                                          -187.365463f, 764.975647f, 785.724182f, 99.413582f,  687.392151f};
108         const float epsilon           = 0.01f;
109         Random rnd(4789);
110         for (int i = 0; i < DE_LENGTH_OF_ARRAY(expected); i++)
111             DE_TEST_ASSERT(de::abs(expected[i] - rnd.getFloat(-542.2f, 1248.7f)) < epsilon);
112     }
113 
114     // choose(first, last, resultOut, num)
115 
116     {
117         static const int items[]                    = {3, 42, 45, 123, 654, -123, -90, 0, 43};
118         const int numItemsPicked                    = 5;
119         static const int expected[][numItemsPicked] = {
120             {-123, 42, -90, 123, 43}, {43, 0, -90, 123, -123}, {3, 42, 45, 123, 0},    {3, 42, 45, -123, 654},
121             {3, 43, 45, -90, -123},   {-90, 0, 45, -123, 654}, {3, 42, 43, 123, -123}, {-90, 43, 45, 123, 654},
122             {0, 42, 45, 123, 654},    {0, -90, 45, -123, 654}};
123         Random rnd(4789);
124 
125         for (int i = 0; i < DE_LENGTH_OF_ARRAY(expected); i++)
126         {
127             int itemsDst[numItemsPicked];
128             rnd.choose(DE_ARRAY_BEGIN(items), DE_ARRAY_END(items), &itemsDst[0], numItemsPicked);
129             for (int j = 0; j < numItemsPicked; j++)
130                 DE_TEST_ASSERT(expected[i][j] == itemsDst[j]);
131         }
132     }
133 
134     // choose(first, last)
135 
136     {
137         static const int items[]    = {3, 42, 45, 123, 654, -123, -90, 0, 43};
138         static const int expected[] = {43, 123, -90, -90, 0, 3, 43, 0, 654, 43};
139         Random rnd(4789);
140 
141         for (int i = 0; i < DE_LENGTH_OF_ARRAY(expected); i++)
142             DE_TEST_ASSERT(expected[i] == rnd.choose<int>(DE_ARRAY_BEGIN(items), DE_ARRAY_END(items)));
143     }
144 
145     // chooseWeighted(first, last, weights)
146 
147     {
148         static const int items[]     = {3, 42, 45, 123, 654, -123, -90, 0};
149         static const float weights[] = {0.4f, 0.6f, 1.5f, 0.5f, 1.2f, 0.3f, 0.2f, 1.4f};
150         DE_STATIC_ASSERT(DE_LENGTH_OF_ARRAY(items) == DE_LENGTH_OF_ARRAY(weights));
151         static const int expected[] = {-90, 654, 45, 0, 45, 45, -123, -90, 45, 654};
152         Random rnd(4789);
153 
154         for (int i = 0; i < DE_LENGTH_OF_ARRAY(expected); i++)
155             DE_TEST_ASSERT(expected[i] ==
156                            rnd.chooseWeighted<int>(DE_ARRAY_BEGIN(items), DE_ARRAY_END(items), &weights[0]));
157     }
158 
159     // suffle()
160 
161     {
162         int items[]                                            = {3, 42, 45, 123, 654, -123, -90, 0, 43};
163         static const int expected[][DE_LENGTH_OF_ARRAY(items)] = {
164             {45, 43, 654, -123, 123, 42, -90, 0, 3}, {0, 43, 3, 42, -123, -90, 654, 45, 123},
165             {42, 43, 654, 3, 0, 123, -90, -123, 45}, {3, 45, 43, 42, 123, 654, 0, -90, -123},
166             {42, 45, -123, 0, -90, 654, 3, 123, 43}, {654, -123, 3, 42, 43, 0, -90, 123, 45},
167             {0, 3, 654, 42, -90, 45, -123, 123, 43}, {654, 3, 45, 42, -123, -90, 123, 43, 0},
168             {-90, 123, 43, 654, 0, 42, 45, 3, -123}, {0, -123, 45, 42, 43, 123, 3, -90, 654}};
169         Random rnd(4789);
170 
171         for (int i = 0; i < DE_LENGTH_OF_ARRAY(expected); i++)
172         {
173             rnd.shuffle(DE_ARRAY_BEGIN(items), DE_ARRAY_END(items));
174             for (int j = 0; j < DE_LENGTH_OF_ARRAY(items); j++)
175                 DE_TEST_ASSERT(expected[i][j] == items[j]);
176         }
177     }
178 }
179 
180 } // namespace de
181