Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
test_seed_sequence.py80 linesDownload Raw Back to tests
1import numpy as np
2from numpy.random import SeedSequence
3from numpy.testing import assert_array_compare, assert_array_equal
4
5
6def test_reference_data():
7    """ Check that SeedSequence generates data the same as the C++ reference.
8
9    https://gist.github.com/imneme/540829265469e673d045
10    """
11    inputs = [
12        [3735928559, 195939070, 229505742, 305419896],
13        [3668361503, 4165561550, 1661411377, 3634257570],
14        [164546577, 4166754639, 1765190214, 1303880213],
15        [446610472, 3941463886, 522937693, 1882353782],
16        [1864922766, 1719732118, 3882010307, 1776744564],
17        [4141682960, 3310988675, 553637289, 902896340],
18        [1134851934, 2352871630, 3699409824, 2648159817],
19        [1240956131, 3107113773, 1283198141, 1924506131],
20        [2669565031, 579818610, 3042504477, 2774880435],
21        [2766103236, 2883057919, 4029656435, 862374500],
22    ]
23    outputs = [
24        [3914649087, 576849849, 3593928901, 2229911004],
25        [2240804226, 3691353228, 1365957195, 2654016646],
26        [3562296087, 3191708229, 1147942216, 3726991905],
27        [1403443605, 3591372999, 1291086759, 441919183],
28        [1086200464, 2191331643, 560336446, 3658716651],
29        [3249937430, 2346751812, 847844327, 2996632307],
30        [2584285912, 4034195531, 3523502488, 169742686],
31        [959045797, 3875435559, 1886309314, 359682705],
32        [3978441347, 432478529, 3223635119, 138903045],
33        [296367413, 4262059219, 13109864, 3283683422],
34    ]
35    outputs64 = [
36        [2477551240072187391, 9577394838764454085],
37        [15854241394484835714, 11398914698975566411],
38        [13708282465491374871, 16007308345579681096],
39        [15424829579845884309, 1898028439751125927],
40        [9411697742461147792, 15714068361935982142],
41        [10079222287618677782, 12870437757549876199],
42        [17326737873898640088, 729039288628699544],
43        [16644868984619524261, 1544825456798124994],
44        [1857481142255628931, 596584038813451439],
45        [18305404959516669237, 14103312907920476776],
46    ]
47    for seed, expected, expected64 in zip(inputs, outputs, outputs64):
48        expected = np.array(expected, dtype=np.uint32)
49        ss = SeedSequence(seed)
50        state = ss.generate_state(len(expected))
51        assert_array_equal(state, expected)
52        state64 = ss.generate_state(len(expected64), dtype=np.uint64)
53        assert_array_equal(state64, expected64)
54
55
56def test_zero_padding():
57    """ Ensure that the implicit zero-padding does not cause problems.
58    """
59    # Ensure that large integers are inserted in little-endian fashion to avoid
60    # trailing 0s.
61    ss0 = SeedSequence(42)
62    ss1 = SeedSequence(42 << 32)
63    assert_array_compare(
64        np.not_equal,
65        ss0.generate_state(4),
66        ss1.generate_state(4))
67
68    # Ensure backwards compatibility with the original 0.17 release for small
69    # integers and no spawn key.
70    expected42 = np.array([3444837047, 2669555309, 2046530742, 3581440988],
71                          dtype=np.uint32)
72    assert_array_equal(SeedSequence(42).generate_state(4), expected42)
73
74    # Regression test for gh-16539 to ensure that the implicit 0s don't
75    # conflict with spawn keys.
76    assert_array_compare(
77        np.not_equal,
78        SeedSequence(42, spawn_key=(0,)).generate_state(4),
79        expected42)
80 
codekingpro/portable-devtools · Team Ai