LCOV - code coverage report
Current view: top level - tests - rng.cpp (source / functions) Hit Total Coverage
Test: Lunchbox Lines: 29 29 100.0 %
Date: 2016-03-29 17:09:06 Functions: 8 8 100.0 %

          Line data    Source code
       1             : 
       2             : /* Copyright (c) 2007-2012, Stefan Eilemann <eile@equalizergraphics.com>
       3             :  *
       4             :  * This library is free software; you can redistribute it and/or modify it under
       5             :  * the terms of the GNU Lesser General Public License version 2.1 as published
       6             :  * by the Free Software Foundation.
       7             :  *
       8             :  * This library is distributed in the hope that it will be useful, but WITHOUT
       9             :  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
      10             :  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
      11             :  * details.
      12             :  *
      13             :  * You should have received a copy of the GNU Lesser General Public License
      14             :  * along with this library; if not, write to the Free Software Foundation, Inc.,
      15             :  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      16             :  */
      17             : 
      18             : // Tests the functionality of the random number generator
      19             : 
      20             : #include <test.h>
      21             : 
      22             : #include <lunchbox/clock.h>
      23             : #include <lunchbox/init.h>
      24             : #include <lunchbox/rng.h>
      25             : #include <servus/uint128_t.h>
      26             : 
      27             : #define MAXLOOPS 100000
      28             : 
      29             : #define TESTLOOP( type, min, max )                                      \
      30             : {                                                                       \
      31             :     size_t i = MAXLOOPS;                                                \
      32             :     while( --i )                                                        \
      33             :         if( rng.get< type >() <= ( min ))                               \
      34             :             break;                                                      \
      35             :     TESTINFO( i, "Did never get value below " << (min) << " for " << #type ); \
      36             :     i = MAXLOOPS;                                                       \
      37             :     while( --i )                                                        \
      38             :         if( rng.get< type >() >= ( max ))                               \
      39             :             break;                                                      \
      40             :     TESTINFO( i, "Did never get value above " << (max) << " for " << #type ); \
      41             :     {                                                                   \
      42             :         const type value = rng.get< type >();                           \
      43             :         i = MAXLOOPS;                                                   \
      44             :         while( --i )                                                    \
      45             :             if( rng.get< type >() != value )                            \
      46             :                 break;                                                  \
      47             :         TESTINFO( i, "Always get the same value " << value << " for "   \
      48             :                   << #type );                                           \
      49             :     }                                                                   \
      50             : }
      51             : 
      52           5 : template< class T > void testSpeed()
      53             : {
      54           5 :     lunchbox::RNG rng;
      55          10 :     lunchbox::Clock clock;
      56      500005 :     for( size_t i = 0; i < MAXLOOPS; ++i )
      57      500000 :         rng.get< T >();
      58          10 :     std::cout << float( MAXLOOPS ) * sizeof( T ) / clock.getTimef()
      59          10 :               << " byte/ms in " << sizeof( T ) << " byte reads" << std::endl;
      60           5 : }
      61             : 
      62           1 : int main( int argc, char **argv )
      63             : {
      64           1 :     TEST( lunchbox::init( argc, argv ));
      65             : 
      66           1 :     lunchbox::RNG rng;
      67             : 
      68           1 :     TESTLOOP( uint8_t,  0,        255 );
      69           1 :     TESTLOOP( uint16_t, 50,       65000 );
      70           1 :     TESTLOOP( uint32_t, 1<<20,    1u<<12 );
      71           1 :     TESTLOOP( uint64_t, 1ull<<52, 1ull<<12 );
      72             : 
      73           1 :     TESTLOOP( int8_t,  -126,       127 );
      74           1 :     TESTLOOP( int16_t, -32000,     32000 );
      75           1 :     TESTLOOP( int32_t, -(1<<5),      1<<5 );
      76           1 :     TESTLOOP( int64_t, -(1<<10),     1<<10 );
      77             : 
      78           1 :     TESTLOOP( float,  0.1f, 0.9f );
      79           1 :     TESTLOOP( double, 0.1,  0.9 );
      80             : 
      81           1 :     testSpeed< uint8_t >();
      82           1 :     testSpeed< uint16_t >();
      83           1 :     testSpeed< uint32_t >();
      84           1 :     testSpeed< uint64_t >();
      85           1 :     testSpeed< servus::uint128_t >();
      86             : 
      87           1 :     TEST( lunchbox::exit( ));
      88           1 :     return EXIT_SUCCESS;
      89           3 : }

Generated by: LCOV version 1.11