Software APIs
prng_unittest.cc
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
6 
7 #include <algorithm>
8 #include <array>
9 
10 #include "gmock/gmock.h"
11 #include "gtest/gtest.h"
12 
13 namespace sca_prng_unittest {
14 namespace {
15 
16 /**
17  * First 100 return values of `random.randint(0, 255)` with a seed value of `0`.
18  */
19 constexpr std::array<uint8_t, 100> kExpected = {
20  197, 215, 20, 132, 248, 207, 155, 244, 183, 111, 71, 144, 71, 48, 128,
21  75, 158, 50, 37, 169, 241, 51, 181, 222, 161, 104, 244, 226, 133, 31,
22  7, 47, 204, 0, 252, 170, 124, 166, 32, 97, 113, 122, 72, 229, 46,
23  41, 163, 250, 55, 154, 149, 63, 170, 104, 147, 227, 46, 197, 162, 123,
24  148, 94, 96, 95, 16, 133, 243, 35, 45, 66, 76, 19, 41, 200, 141,
25  120, 110, 214, 140, 230, 252, 182, 42, 166, 59, 249, 171, 97, 124, 8,
26  138, 59, 112, 190, 87, 170, 218, 31, 51, 74};
27 
28 TEST(RandByte, Seed_0) {
29  std::vector<uint8_t> actual(kExpected.size());
30 
31  prng_seed(0);
32  std::generate(actual.begin(), actual.end(), prng_rand_byte);
33  EXPECT_THAT(actual, testing::ElementsAreArray(kExpected));
34 }
35 
36 TEST(RandBytes, Seed_0) {
37  std::vector<uint8_t> actual(kExpected.size());
38 
39  prng_seed(0);
40  prng_rand_bytes(actual.data(), actual.size());
41  EXPECT_THAT(actual, testing::ElementsAreArray(kExpected));
42 }
43 
44 } // namespace
45 } // namespace sca_prng_unittest