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