"""Tests `random.randint` python2 and python3 compatibility.
"""
from subprocess import Popen
from subprocess import PIPE
import os

MAIN_FILE = os.path.dirname(os.path.realpath(__file__)).replace("test", "__main__")


def test_randint_compability():
    """Tests if initegers generated by python 2 and python 3 randint are the same.

    Generate several random integers for several seeds and compares them.
    """
    n_int = 100
    os.environ["PYTHONHASHSEED"] = "0"

    for seed_init in ["1a.500", "2e.305", "1e.500"]:
        for low in [0, 10, 100]:
            for width in [5, 10, 100]:
                high = low + width
                args = [
                    MAIN_FILE,
                    seed_init,
                    "-n",
                    str(n_int),
                    "--low",
                    str(low),
                    "--high",
                    str(high),
                ]
                print(*args)
                p = Popen(["python2"] + args, stdin=PIPE, stdout=PIPE, stderr=PIPE)
                py2_rands, _ = p.communicate()
                p = Popen(["python3"] + args, stdin=PIPE, stdout=PIPE, stderr=PIPE)
                py3_rands, _ = p.communicate()

                assert py2_rands == py3_rands
                print(py2_rands)
