| 1 | #include "test.h" |
| 2 | |
| 3 | #include <game/prng.h> |
| 4 | |
| 5 | #include <gtest/gtest.h> |
| 6 | |
| 7 | // https://www.pcg-random.org/using-pcg-c-basic.html, retrieved 2020-05-25 |
| 8 | // suggests to use `pcg-32-global.demo.c`: |
| 9 | // |
| 10 | // ``` |
| 11 | // unix% ./pcg32-demo |
| 12 | // pcg32_random_r: |
| 13 | // - result: 32-bit unsigned int (uint32_t) |
| 14 | // - period: 2^64 (* 2^63 streams) |
| 15 | // - state type: pcg32_random_t (16 bytes) |
| 16 | // - output func: XSH-RR |
| 17 | // |
| 18 | // Round 1: |
| 19 | // 32bit: 0xa15c02b7 0x7b47f409 0xba1d3330 0x83d2f293 0xbfa4784b 0xcbed606e |
| 20 | // Coins: HHTTTHTHHHTHTTTHHHHHTTTHHHTHTHTHTTHTTTHHHHHHTTTTHHTTTTTHTTTTTTTHT |
| 21 | // Rolls: 3 4 1 1 2 2 3 2 4 3 2 4 3 3 5 2 3 1 3 1 5 1 4 1 5 6 4 6 6 2 6 3 3 |
| 22 | // Cards: Qd Ks 6d 3s 3d 4c 3h Td Kc 5c Jh Kd Jd As 4s 4h Ad Th Ac Jc 7s Qs |
| 23 | // 2s 7h Kh 2d 6c Ah 4d Qh 9h 6s 5s 2c 9c Ts 8d 9s 3c 8c Js 5d 2h 6h |
| 24 | // 7d 8s 9d 5h 8h Qc 7c Tc |
| 25 | // ⋮ |
| 26 | // ⋮ |
| 27 | // Round 5: |
| 28 | // 32bit: 0xfcef7cd6 0x1b488b5a 0xd0daf7ea 0x1d9a70f7 0x241a37cf 0x9a3857b7 |
| 29 | // Coins: HHHHTHHTTHTTHHHTTTHHTHTHTTTTHTTHTHTTTHHHTHTHTTHTTHTHHTHTHHHTHTHTT |
| 30 | // Rolls: 5 4 1 2 6 1 3 1 5 6 3 6 2 1 4 4 5 2 1 5 6 5 6 4 4 4 5 2 6 4 3 5 6 |
| 31 | // Cards: 4d 9s Qc 9h As Qs 7s 4c Kd 6h 6s 2c 8c 5d 7h 5h Jc 3s 7c Jh Js Ks |
| 32 | // Tc Jd Kc Th 3h Ts Qh Ad Td 3c Ah 2d 3d 5c Ac 8s 5s 9c 2h 6c 6d Kh |
| 33 | // Qd 8d 7d 2s 8h 4h 9d 4s |
| 34 | // ``` |
| 35 | // |
| 36 | // Numbers can also be seen at |
| 37 | // https://github.com/imneme/pcg-c/blob/83252d9c23df9c82ecb42210afed61a7b42402d7/test-high/expected/check-pcg32.out. |
| 38 | // |
| 39 | // It's also the output of `pcg32-global-demo.c` from |
| 40 | // https://github.com/imneme/pcg-c-basic/tree/bc39cd76ac3d541e618606bcc6e1e5ba5e5e6aa3. |
| 41 | // |
| 42 | // Only the first 6 numbers are taken from the generator directly, after this, |
| 43 | // something more complicated is done. |
| 44 | |
| 45 | static const unsigned int PCG32_GLOBAL_DEMO[] = { |
| 46 | 0xa15c02b7, |
| 47 | 0x7b47f409, |
| 48 | 0xba1d3330, |
| 49 | 0x83d2f293, |
| 50 | 0xbfa4784b, |
| 51 | 0xcbed606e, |
| 52 | }; |
| 53 | |
| 54 | TEST(Prng, EqualsPcg32GlobalDemo) |
| 55 | { |
| 56 | uint64_t aSeed[2] = {42, 54}; |
| 57 | |
| 58 | CPrng Prng; |
| 59 | Prng.Seed(aSeed); |
| 60 | for(auto Expected : PCG32_GLOBAL_DEMO) |
| 61 | { |
| 62 | EXPECT_EQ(Prng.RandomBits(), Expected); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | TEST(Prng, Description) |
| 67 | { |
| 68 | CPrng Prng; |
| 69 | EXPECT_STREQ(Prng.Description(), "pcg-xsh-rr:unseeded" ); |
| 70 | |
| 71 | uint64_t aSeed0[2] = {0xfedbca9876543210, 0x0123456789abcdef}; |
| 72 | uint64_t aSeed1[2] = {0x0123456789abcdef, 0xfedcba9876543210}; |
| 73 | uint64_t aSeed2[2] = {0x0000000000000000, 0x0000000000000000}; |
| 74 | |
| 75 | Prng.Seed(aSeed: aSeed0); |
| 76 | EXPECT_STREQ(Prng.Description(), "pcg-xsh-rr:fedbca9876543210:0123456789abcdef" ); |
| 77 | Prng.Seed(aSeed: aSeed1); |
| 78 | EXPECT_STREQ(Prng.Description(), "pcg-xsh-rr:0123456789abcdef:fedcba9876543210" ); |
| 79 | Prng.Seed(aSeed: aSeed2); |
| 80 | EXPECT_STREQ(Prng.Description(), "pcg-xsh-rr:0000000000000000:0000000000000000" ); |
| 81 | } |
| 82 | |