Re: [kvm-unit-tests PATCH v3 1/7] lib: Add pseudo random functions

Nina Schoetterl-Glausch
2024-06-25T07:06:22Z

On Tue, 2024-06-25 at 13:08 +1000, Nicholas Piggin wrote:
> On Fri Jun 21, 2024 at 12:16 AM AEST, Nina Schoetterl-Glausch wrote:

[...]

> > I tested the implementation in the following way:
> >
> > cat <<'EOF' > rand.py
> > #!/usr/bin/python3
> >
> > def prng32(seed):
> > from hashlib import sha256
> > state = seed.to_bytes(8, byteorder="big")
> > while True:
> > state = sha256(state).digest()
> > for i in range(8):
> > yield int.from_bytes(state[i*4:(i+1)*4], byteorder="big")
> >
> > r = prng32(0)
> > for i in range(100):
> > print(f"{next(r):08x}")
> >
> > EOF
> >
> > cat <<'EOF' > rand.c
> > #include <stdio.h>
> > #include "rand.h"
> >
> > void main(void)
> > {
> > prng_state state = prng_init(0);
> > for (int i = 0; i < 100; i++) {
> > printf("%08x\n", prng32(&state));
> > }
> > }
> > EOF
> > cat <<'EOF' > libcflat.h
> > #define ARRAY_SIZE(_a) (sizeof(_a)/sizeof((_a)[0]))
> > EOF
> > chmod +x rand.py
> > ln -s lib/rand.c librand.c
> > gcc -Ilib librand.c rand.c
> > diff <(./a.out) <(./rand.py)
>
> Cool... you made a unit test for the unit tests. We could start a
> make check? :)

I wouldn't complain about it, but my test is a bit hacky and I don't
expect the code to get touched much.
>
> Acked-by: Nicholas Piggin <npiggin@gmail.com>
>
> Thanks,
> Nick