random_test.cpp (1751B)
1 // This file is part of Blend2D project <https://blend2d.com> 2 // 3 // See blend2d.h or LICENSE.md for license and copyright information 4 // SPDX-License-Identifier: Zlib 5 6 #include "api-build_test_p.h" 7 #if defined(BL_TEST) 8 9 #include "random_p.h" 10 #include "simd/simd_p.h" 11 12 // bl::Random - Tests 13 // ================== 14 15 namespace bl { 16 namespace Tests { 17 18 UNIT(random, BL_TEST_GROUP_CORE_UTILITIES) { 19 // Number of iterations for tests that use loop. 20 static constexpr uint32_t kCount = 1000000; 21 22 #if defined(BL_TARGET_OPT_SSE2) 23 INFO("Testing whether the SIMD implementation matches the C++ one"); 24 { 25 BLRandom a(0); 26 BLRandom b(0); 27 28 SIMD::Vec2xU64 b_lo = RandomInternal::next_uint64AsI128(&b); 29 SIMD::Vec2xU64 b_hi = SIMD::swizzle_u32<2, 3, 0, 1>(b_lo); 30 31 uint64_t a_val = a.next_uint64(); 32 uint64_t b_val = (uint64_t(SIMD::cast_to_i32(b_lo)) ) + 33 (uint64_t(SIMD::cast_to_i32(b_hi)) << 32) ; 34 35 EXPECT_EQ(a_val, b_val); 36 } 37 #endif 38 39 INFO("Testing whether the random 32-bit integer is the HI part of the 64-bit one"); 40 { 41 BLRandom a(0); 42 BLRandom b(0); 43 EXPECT_EQ(uint32_t(a.next_uint64() >> 32), b.next_uint32()); 44 } 45 46 INFO("Test whether returned double precision values satisfy [0..1) condition"); 47 { 48 // Supply a low-entropy seed on purpose. 49 BLRandom rnd(3); 50 51 uint32_t below = 0; 52 uint32_t above = 0; 53 54 for (uint32_t i = 0; i < kCount; i++) { 55 double x = rnd.next_double(); 56 below += x < 0.5; 57 above += x >= 0.5; 58 EXPECT_GE(x, 0.0); 59 EXPECT_LT(x, 1.0); 60 } 61 INFO(" Random numbers at [0.0, 0.5): %u of %u", below, kCount); 62 INFO(" Random numbers at [0.5, 1.0): %u of %u", above, kCount); 63 } 64 } 65 66 } // {Tests} 67 } // {bl} 68 69 #endif // BL_TEST