xref: /trunk/main/sal/qa/rtl/random/rtl_random.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
187d2adbcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
387d2adbcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
487d2adbcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
587d2adbcSAndrew Rist  * distributed with this work for additional information
687d2adbcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
787d2adbcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
887d2adbcSAndrew Rist  * "License"); you may not use this file except in compliance
987d2adbcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
1187d2adbcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
1387d2adbcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
1487d2adbcSAndrew Rist  * software distributed under the License is distributed on an
1587d2adbcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1687d2adbcSAndrew Rist  * KIND, either express or implied.  See the License for the
1787d2adbcSAndrew Rist  * specific language governing permissions and limitations
1887d2adbcSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
2087d2adbcSAndrew Rist  *************************************************************/
2187d2adbcSAndrew Rist 
2287d2adbcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir 
25cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
26cdf0e10cSrcweir #include "precompiled_sal.hxx"
27cdf0e10cSrcweir // autogenerated file with codegen.pl
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <algorithm> // STL
30cdf0e10cSrcweir 
31*36f0c4ccSDamjan Jovanovic #include "gtest/gtest.h"
32cdf0e10cSrcweir #include <rtl/random.h>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir namespace rtl_random
35cdf0e10cSrcweir {
36cdf0e10cSrcweir 
37*36f0c4ccSDamjan Jovanovic class createPool : public ::testing::Test
38cdf0e10cSrcweir {
39cdf0e10cSrcweir public:
40cdf0e10cSrcweir     // initialise your test code values here.
SetUp()41*36f0c4ccSDamjan Jovanovic     void SetUp()
42cdf0e10cSrcweir     {
43cdf0e10cSrcweir     }
44cdf0e10cSrcweir 
TearDown()45*36f0c4ccSDamjan Jovanovic     void TearDown()
46cdf0e10cSrcweir     {
47cdf0e10cSrcweir     }
48*36f0c4ccSDamjan Jovanovic }; // class createPool
49cdf0e10cSrcweir 
TEST_F(createPool,createPool_001)50*36f0c4ccSDamjan Jovanovic TEST_F(createPool, createPool_001)
51cdf0e10cSrcweir {
52cdf0e10cSrcweir     // this is demonstration code
53cdf0e10cSrcweir 
54cdf0e10cSrcweir     rtlRandomPool aPool = rtl_random_createPool();
55cdf0e10cSrcweir 
56cdf0e10cSrcweir     // LLA: seems to be that an other test is not possible for createPool()
57*36f0c4ccSDamjan Jovanovic     ASSERT_TRUE(aPool != NULL) << "create failed";
58cdf0e10cSrcweir     rtl_random_destroyPool(aPool);
59cdf0e10cSrcweir }
60cdf0e10cSrcweir 
61*36f0c4ccSDamjan Jovanovic class destroyPool : public ::testing::Test
62cdf0e10cSrcweir {
63cdf0e10cSrcweir public:
64cdf0e10cSrcweir     // initialise your test code values here.
SetUp()65*36f0c4ccSDamjan Jovanovic     void SetUp()
66cdf0e10cSrcweir     {
67cdf0e10cSrcweir     }
68cdf0e10cSrcweir 
TearDown()69*36f0c4ccSDamjan Jovanovic     void TearDown()
70cdf0e10cSrcweir     {
71cdf0e10cSrcweir     }
72*36f0c4ccSDamjan Jovanovic }; // class destroyPool
73cdf0e10cSrcweir 
TEST_F(destroyPool,destroyPool_000)74*36f0c4ccSDamjan Jovanovic TEST_F(destroyPool, destroyPool_000)
75cdf0e10cSrcweir {
76cdf0e10cSrcweir     // GPF, if failed
77cdf0e10cSrcweir     rtl_random_destroyPool(NULL);
78cdf0e10cSrcweir }
79cdf0e10cSrcweir 
TEST_F(destroyPool,destroyPool_001)80*36f0c4ccSDamjan Jovanovic TEST_F(destroyPool, destroyPool_001)
81cdf0e10cSrcweir {
82cdf0e10cSrcweir     rtlRandomPool aPool = rtl_random_createPool();
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     // LLA: seems to be that an other test is not possible for createPool()
85*36f0c4ccSDamjan Jovanovic     ASSERT_TRUE(aPool != NULL) << "create failed";
86cdf0e10cSrcweir 
87cdf0e10cSrcweir     rtl_random_destroyPool(aPool);
88cdf0e10cSrcweir }
89cdf0e10cSrcweir 
90*36f0c4ccSDamjan Jovanovic class addBytes : public ::testing::Test
91cdf0e10cSrcweir {
92cdf0e10cSrcweir public:
93cdf0e10cSrcweir     // initialise your test code values here.
SetUp()94*36f0c4ccSDamjan Jovanovic     void SetUp()
95cdf0e10cSrcweir     {
96cdf0e10cSrcweir     }
97cdf0e10cSrcweir 
TearDown()98*36f0c4ccSDamjan Jovanovic     void TearDown()
99cdf0e10cSrcweir     {
100cdf0e10cSrcweir     }
101*36f0c4ccSDamjan Jovanovic }; // class addBytes
102cdf0e10cSrcweir 
TEST_F(addBytes,addBytes_000)103*36f0c4ccSDamjan Jovanovic TEST_F(addBytes, addBytes_000)
104cdf0e10cSrcweir {
105cdf0e10cSrcweir     rtlRandomPool aPool = rtl_random_createPool();
106cdf0e10cSrcweir 
107cdf0e10cSrcweir     sal_uInt32  nBufLen = 4;
108cdf0e10cSrcweir     sal_uInt8   *pBuffer = new sal_uInt8[ nBufLen ];
109cdf0e10cSrcweir     memset(pBuffer, 0, nBufLen);
110cdf0e10cSrcweir 
111cdf0e10cSrcweir     rtlRandomError aError = rtl_random_addBytes(NULL, NULL, 0);
112*36f0c4ccSDamjan Jovanovic     ASSERT_TRUE(aError == rtl_Random_E_Argument) << "wrong parameter";
113cdf0e10cSrcweir 
114cdf0e10cSrcweir     /* rtlRandomError */ aError = rtl_random_addBytes(aPool, NULL, 0);
115*36f0c4ccSDamjan Jovanovic     ASSERT_TRUE(aError == rtl_Random_E_Argument) << "wrong parameter";
116cdf0e10cSrcweir 
117cdf0e10cSrcweir     /* rtlRandomError */ aError = rtl_random_addBytes(aPool, pBuffer, nBufLen);
118*36f0c4ccSDamjan Jovanovic     ASSERT_TRUE(aError == rtl_Random_E_None) << "wrong parameter";
119cdf0e10cSrcweir 
120cdf0e10cSrcweir     rtl_random_destroyPool(aPool);
121cdf0e10cSrcweir     delete [] pBuffer;
122cdf0e10cSrcweir 
123cdf0e10cSrcweir }
124cdf0e10cSrcweir 
TEST_F(addBytes,addBytes_001)125*36f0c4ccSDamjan Jovanovic TEST_F(addBytes, addBytes_001)
126cdf0e10cSrcweir {
127cdf0e10cSrcweir     rtlRandomPool aPool = rtl_random_createPool();
128cdf0e10cSrcweir 
129cdf0e10cSrcweir     sal_uInt32  nBufLen = 4;
130cdf0e10cSrcweir     sal_uInt8   *pBuffer = new sal_uInt8[ nBufLen ];
131cdf0e10cSrcweir 
132cdf0e10cSrcweir     memset(pBuffer, 0, nBufLen);
133cdf0e10cSrcweir 
134cdf0e10cSrcweir     rtl_random_addBytes(aPool, pBuffer, nBufLen);
135cdf0e10cSrcweir 
136*36f0c4ccSDamjan Jovanovic     printf("%2x %2x %2x %2x\n", pBuffer[0], pBuffer[1], pBuffer[2], pBuffer[3]);
137cdf0e10cSrcweir 
138cdf0e10cSrcweir     rtl_random_destroyPool(aPool);
139cdf0e10cSrcweir     delete [] pBuffer;
140cdf0e10cSrcweir }
141cdf0e10cSrcweir 
142cdf0e10cSrcweir class Statistics
143cdf0e10cSrcweir {
144*36f0c4ccSDamjan Jovanovic protected:
145cdf0e10cSrcweir     int m_nDispensation[256];
146cdf0e10cSrcweir 
147cdf0e10cSrcweir     int m_nMin;
148cdf0e10cSrcweir     int m_nMax;
149cdf0e10cSrcweir     int m_nAverage;
150cdf0e10cSrcweir     int m_nMinDeviation;
151cdf0e10cSrcweir     int m_nMaxDeviation;
152cdf0e10cSrcweir 
153cdf0e10cSrcweir public:
clearDispensation()154cdf0e10cSrcweir     void clearDispensation()
155cdf0e10cSrcweir         {
156cdf0e10cSrcweir             for (int i = 0;i < 256;i ++)                        // clear array
157cdf0e10cSrcweir             {
158cdf0e10cSrcweir                 m_nDispensation[i] = 0;
159cdf0e10cSrcweir             }
160cdf0e10cSrcweir         }
Statistics()161cdf0e10cSrcweir     Statistics()
162cdf0e10cSrcweir         {
163cdf0e10cSrcweir             clearDispensation();
164cdf0e10cSrcweir         }
~Statistics()165cdf0e10cSrcweir     ~Statistics(){}
166cdf0e10cSrcweir 
addValue(sal_Int16 _nIndex,sal_Int32 _nValue)167cdf0e10cSrcweir     void addValue(sal_Int16 _nIndex, sal_Int32 _nValue)
168cdf0e10cSrcweir         {
169*36f0c4ccSDamjan Jovanovic             ASSERT_TRUE(_nIndex >= 0 && _nIndex < 256);
170cdf0e10cSrcweir             m_nDispensation[_nIndex] += _nValue;
171cdf0e10cSrcweir         }
172cdf0e10cSrcweir 
build(sal_Int32 _nCountMax)173cdf0e10cSrcweir     void build(sal_Int32 _nCountMax)
174cdf0e10cSrcweir         {
175cdf0e10cSrcweir             m_nMin = _nCountMax;
176cdf0e10cSrcweir             m_nMax = 0;
177cdf0e10cSrcweir 
178cdf0e10cSrcweir             m_nAverage = _nCountMax / 256;
179cdf0e10cSrcweir 
180cdf0e10cSrcweir             m_nMinDeviation = _nCountMax;
181cdf0e10cSrcweir             m_nMaxDeviation = 0;
182cdf0e10cSrcweir 
183cdf0e10cSrcweir             for (int i = 0;i < 256;i ++)                        // show dispensation
184cdf0e10cSrcweir             {
185cdf0e10cSrcweir                 m_nMin = std::min(m_nMin, m_nDispensation[i]);
186cdf0e10cSrcweir                 m_nMax = std::max(m_nMax, m_nDispensation[i]);
187cdf0e10cSrcweir 
188cdf0e10cSrcweir                 m_nMinDeviation = std::min(m_nMinDeviation, abs(m_nAverage - m_nDispensation[i]));
189cdf0e10cSrcweir                 m_nMaxDeviation = std::max(m_nMaxDeviation, abs(m_nAverage - m_nDispensation[i]));
190cdf0e10cSrcweir             }
191cdf0e10cSrcweir         }
192cdf0e10cSrcweir 
print()193cdf0e10cSrcweir     void print()
194cdf0e10cSrcweir         {
195cdf0e10cSrcweir             // LLA: these are only info values
196*36f0c4ccSDamjan Jovanovic             printf("\nSome statistics\n");
197*36f0c4ccSDamjan Jovanovic             printf("Min: %d\n", m_nMin);
198*36f0c4ccSDamjan Jovanovic             printf("Max: %d\n", m_nMax);
199*36f0c4ccSDamjan Jovanovic             printf("Average: %d\n", m_nAverage);
200*36f0c4ccSDamjan Jovanovic             printf("Min abs deviation: %d\n", m_nMinDeviation);
201*36f0c4ccSDamjan Jovanovic             printf("Max abs deviation: %d\n", m_nMaxDeviation);
202cdf0e10cSrcweir         }
203cdf0e10cSrcweir 
getAverage()204cdf0e10cSrcweir     sal_Int32 getAverage() {return m_nAverage;}
getMaxDeviation()205cdf0e10cSrcweir     sal_Int32 getMaxDeviation() {return m_nMaxDeviation;}
206cdf0e10cSrcweir 
207cdf0e10cSrcweir };
208cdf0e10cSrcweir 
209*36f0c4ccSDamjan Jovanovic class getBytes : public ::testing::Test
210cdf0e10cSrcweir {
211cdf0e10cSrcweir public:
212cdf0e10cSrcweir     // initialise your test code values here.
SetUp()213*36f0c4ccSDamjan Jovanovic     void SetUp()
214cdf0e10cSrcweir     {
215cdf0e10cSrcweir     }
216cdf0e10cSrcweir 
TearDown()217*36f0c4ccSDamjan Jovanovic     void TearDown()
218cdf0e10cSrcweir     {
219cdf0e10cSrcweir     }
220*36f0c4ccSDamjan Jovanovic }; // class getBytes
221cdf0e10cSrcweir 
TEST_F(getBytes,getBytes_000)222*36f0c4ccSDamjan Jovanovic TEST_F(getBytes, getBytes_000)
223cdf0e10cSrcweir {
224cdf0e10cSrcweir     rtlRandomPool aPool = rtl_random_createPool();
225cdf0e10cSrcweir 
226cdf0e10cSrcweir     sal_uInt32  nBufLen = 4;
227cdf0e10cSrcweir     sal_uInt8   *pBuffer = new sal_uInt8[ nBufLen ];
228cdf0e10cSrcweir     memset(pBuffer, 0, nBufLen);
229cdf0e10cSrcweir 
230cdf0e10cSrcweir     rtlRandomError aError = rtl_random_getBytes(NULL, NULL, 0);
231*36f0c4ccSDamjan Jovanovic     ASSERT_TRUE(aError == rtl_Random_E_Argument) << "wrong parameter";
232cdf0e10cSrcweir 
233cdf0e10cSrcweir     /* rtlRandomError */ aError = rtl_random_getBytes(aPool, NULL, 0);
234*36f0c4ccSDamjan Jovanovic     ASSERT_TRUE(aError == rtl_Random_E_Argument) << "wrong parameter";
235cdf0e10cSrcweir 
236cdf0e10cSrcweir     /* rtlRandomError */ aError = rtl_random_getBytes(aPool, pBuffer, nBufLen);
237*36f0c4ccSDamjan Jovanovic     ASSERT_TRUE(aError == rtl_Random_E_None) << "wrong parameter";
238cdf0e10cSrcweir 
239cdf0e10cSrcweir     rtl_random_destroyPool(aPool);
240cdf0e10cSrcweir     delete [] pBuffer;
241cdf0e10cSrcweir }
242cdf0e10cSrcweir 
TEST_F(getBytes,getBytes_001)243*36f0c4ccSDamjan Jovanovic TEST_F(getBytes, getBytes_001)
244cdf0e10cSrcweir {
245cdf0e10cSrcweir     rtlRandomPool aPool = rtl_random_createPool();
246cdf0e10cSrcweir 
247cdf0e10cSrcweir     sal_uInt32  nBufLen = 4;
248cdf0e10cSrcweir     sal_uInt8   *pBuffer = new sal_uInt8[ nBufLen ];
249cdf0e10cSrcweir     memset(pBuffer, 0, nBufLen);
250cdf0e10cSrcweir 
251cdf0e10cSrcweir     rtlRandomError aError = rtl_random_getBytes(aPool, pBuffer, nBufLen);
252*36f0c4ccSDamjan Jovanovic     ASSERT_TRUE(aError == rtl_Random_E_None) << "wrong parameter";
253cdf0e10cSrcweir 
254*36f0c4ccSDamjan Jovanovic     printf("%2x %2x %2x %2x\n", pBuffer[0], pBuffer[1], pBuffer[2], pBuffer[3]);
255cdf0e10cSrcweir 
256cdf0e10cSrcweir     rtl_random_destroyPool(aPool);
257cdf0e10cSrcweir     delete [] pBuffer;
258cdf0e10cSrcweir }
259cdf0e10cSrcweir 
TEST_F(getBytes,getBytes_002)260*36f0c4ccSDamjan Jovanovic TEST_F(getBytes, getBytes_002)
261cdf0e10cSrcweir {
262cdf0e10cSrcweir     rtlRandomPool aPool = rtl_random_createPool();
263cdf0e10cSrcweir 
264cdf0e10cSrcweir     sal_uInt32  nBufLen = 4;
265cdf0e10cSrcweir     sal_uInt8   *pBuffer = new sal_uInt8[ nBufLen << 1 ];
266cdf0e10cSrcweir     memset(pBuffer, 0, nBufLen << 1);
267cdf0e10cSrcweir 
268*36f0c4ccSDamjan Jovanovic     ASSERT_TRUE(pBuffer[4] == 0 && pBuffer[5] == 0 && pBuffer[6] == 0 && pBuffer[7] == 0) << "memset failed";
269cdf0e10cSrcweir 
270cdf0e10cSrcweir     rtlRandomError aError = rtl_random_getBytes(aPool, pBuffer, nBufLen);
271*36f0c4ccSDamjan Jovanovic     ASSERT_TRUE(aError == rtl_Random_E_None) << "wrong parameter";
272cdf0e10cSrcweir 
273*36f0c4ccSDamjan Jovanovic     printf("%2x %2x %2x %2x %2x %2x %2x %2x\n", pBuffer[0], pBuffer[1], pBuffer[2], pBuffer[3], pBuffer[4], pBuffer[5], pBuffer[6], pBuffer[7]);
274cdf0e10cSrcweir 
275*36f0c4ccSDamjan Jovanovic     ASSERT_TRUE(pBuffer[4] == 0 && pBuffer[5] == 0 && pBuffer[6] == 0 && pBuffer[7] == 0) << "internal memory overwrite";
276cdf0e10cSrcweir 
277cdf0e10cSrcweir     rtl_random_destroyPool(aPool);
278cdf0e10cSrcweir     delete [] pBuffer;
279cdf0e10cSrcweir }
280cdf0e10cSrcweir 
TEST_F(getBytes,getBytes_003)281*36f0c4ccSDamjan Jovanovic TEST_F(getBytes, getBytes_003)
282cdf0e10cSrcweir {
283cdf0e10cSrcweir     rtlRandomPool aPool = rtl_random_createPool();
284cdf0e10cSrcweir 
285cdf0e10cSrcweir     sal_uInt32  nBufLen = 1;
286cdf0e10cSrcweir     sal_uInt8   *pBuffer = new sal_uInt8[ nBufLen ];
287cdf0e10cSrcweir     memset(pBuffer, 0, nBufLen);
288cdf0e10cSrcweir 
289cdf0e10cSrcweir     Statistics aStat;
290cdf0e10cSrcweir 
291*36f0c4ccSDamjan Jovanovic     ASSERT_TRUE(pBuffer[0] == 0) << "memset failed";
292cdf0e10cSrcweir 
293cdf0e10cSrcweir     int nCount = 0;
294cdf0e10cSrcweir 
295cdf0e10cSrcweir     int nCountMax = 1000000;
296cdf0e10cSrcweir     for(nCount = 0;nCount < nCountMax; nCount ++)                  // run 100000000 through getBytes(...)
297cdf0e10cSrcweir     {
298cdf0e10cSrcweir         /* rtlRandomError aError = */ rtl_random_getBytes(aPool, pBuffer, nBufLen);
299*36f0c4ccSDamjan Jovanovic         /* ASSERT_TRUE(aError == rtl_Random_E_None); << "wrong parameter" */
300cdf0e10cSrcweir 
301cdf0e10cSrcweir         aStat.addValue(pBuffer[0], 1);
302cdf0e10cSrcweir     }
303cdf0e10cSrcweir 
304cdf0e10cSrcweir     aStat.build(nCountMax);
305cdf0e10cSrcweir     aStat.print();
306cdf0e10cSrcweir 
307*36f0c4ccSDamjan Jovanovic     ASSERT_TRUE(aStat.getMaxDeviation() < aStat.getAverage()) << "deviation should be less average";
308cdf0e10cSrcweir 
309cdf0e10cSrcweir     rtl_random_destroyPool(aPool);
310cdf0e10cSrcweir     delete [] pBuffer;
311cdf0e10cSrcweir }
312cdf0e10cSrcweir 
TEST_F(getBytes,getBytes_003_1)313*36f0c4ccSDamjan Jovanovic TEST_F(getBytes, getBytes_003_1)
314cdf0e10cSrcweir {
315cdf0e10cSrcweir     rtlRandomPool aPool = rtl_random_createPool();
316cdf0e10cSrcweir 
317cdf0e10cSrcweir     sal_uInt32  nBufLen = 256;
318cdf0e10cSrcweir     sal_uInt8   *pBuffer = new sal_uInt8[ nBufLen ];
319cdf0e10cSrcweir     memset(pBuffer, 0, nBufLen);
320cdf0e10cSrcweir 
321cdf0e10cSrcweir     Statistics aStat;
322cdf0e10cSrcweir 
323*36f0c4ccSDamjan Jovanovic     ASSERT_TRUE(pBuffer[0] == 0) << "memset failed";
324cdf0e10cSrcweir 
325cdf0e10cSrcweir     int nCount = 0;
326cdf0e10cSrcweir 
327cdf0e10cSrcweir     int nCountMax = 10000;
328cdf0e10cSrcweir     for(nCount = 0;nCount < nCountMax; nCount ++)                  // run 100000000 through getBytes(...)
329cdf0e10cSrcweir     {
330cdf0e10cSrcweir         /* rtlRandomError aError = */ rtl_random_getBytes(aPool, pBuffer, nBufLen);
331*36f0c4ccSDamjan Jovanovic         // ASSERT_TRUE(aError == rtl_Random_E_None) << "wrong parameter"";
332cdf0e10cSrcweir 
333cdf0e10cSrcweir         for (sal_uInt32 i=0;i<nBufLen;i++)
334cdf0e10cSrcweir         {
335cdf0e10cSrcweir             aStat.addValue(pBuffer[i], 1);
336cdf0e10cSrcweir         }
337cdf0e10cSrcweir     }
338cdf0e10cSrcweir 
339cdf0e10cSrcweir     aStat.build(nCountMax * nBufLen);
340cdf0e10cSrcweir     aStat.print();
341cdf0e10cSrcweir 
342*36f0c4ccSDamjan Jovanovic     ASSERT_TRUE(aStat.getMaxDeviation() < aStat.getAverage()) << "deviation should be less average";
343cdf0e10cSrcweir 
344cdf0e10cSrcweir     rtl_random_destroyPool(aPool);
345cdf0e10cSrcweir     delete [] pBuffer;
346cdf0e10cSrcweir }
347cdf0e10cSrcweir 
348cdf0e10cSrcweir // -----------------------------------------------------------------------------
349cdf0e10cSrcweir } // namespace rtl_random
350cdf0e10cSrcweir 
main(int argc,char ** argv)351*36f0c4ccSDamjan Jovanovic int main(int argc, char **argv)
352*36f0c4ccSDamjan Jovanovic {
353*36f0c4ccSDamjan Jovanovic     ::testing::InitGoogleTest(&argc, argv);
354*36f0c4ccSDamjan Jovanovic     return RUN_ALL_TESTS();
355*36f0c4ccSDamjan Jovanovic }
356