<commit_before># encoding: utf-8
import operator
import sys
import pytest
import doctest

from pytest import approx
from operator import eq, ne
from decimal import Decimal
from fractions import Fraction

inf, nan = float("inf"), float("nan")


class MyDocTestRunner(doctest.DocTestRunner):
    def __init__(self):
        doctest.DocTestRunner.__init__(self)

    def report_failure(self, out, test, example, got):
        raise AssertionError(
            "'{}' evaluates to '{}', not '{}'".format(
                example.source.strip(), got.strip(), example.want.strip()
            )
        )


class TestApprox(object):
    @pytest.fixture
    def plus_minus(self):
        return u"\u00b1" if sys.version_info[0] > 2 else u"+-"

    def test_repr_string(self, plus_minus):
        tol1, tol2, infr = "1.0e-06", "2.0e-06", "inf"
        assert repr(approx(1.0)) == "1.0 {pm} {tol1}".format(pm=plus_minus, tol1=tol1)
        assert repr(
            approx([1.0, 2.0])
        ) == "approx([1.0 {pm} {tol1}, 2.0 {pm} {tol2}])".format(
            pm=plus_minus, tol1=tol1, tol2=tol2
        )
        assert repr(
            approx((1.0, 2.0))
        ) == "approx((1.0 {pm} {tol1}, 2.0 {pm} {tol2}))".format(
            pm=plus_minus, tol1=tol1, tol2=tol2
        )
        assert repr(approx(inf)) == "inf"
        assert repr(approx(1.0, rel=nan)) == "1.0 {pm} ???".format(pm=plus_minus)
        assert repr(approx(1.0, rel=inf)) == "1.0 {pm} {infr}".format(
            pm=plus_minus, infr=infr
        )
        assert repr(approx(1.0j, rel=inf)) == "1j"

        # Dictionaries aren't ordered, so we need to check both orders.
        assert repr(approx({"a": 1.0, "b": 2.0})) in (
            "approx({{'a': 1.0 {pm} {tol1}, 'b': 2.0 {pm} {tol2}}})".format(
                pm=plus_minus, tol1=tol1, tol2=tol2
            ),
            "approx({{'b': 2.0 {pm} {tol2}, 'a': 1.0 {pm} {tol1}}})".format(
                pm=plus_minus, tol1=tol1, tol2=tol2
            ),
        )

    def test_repr_nd_array(self, plus_minus):
        # Make sure that arrays of all different dimensions are repr'd
        # correctly.
        np = pytest.importorskip("numpy")
        examples = [
            (np.array(5.), "approx(5.0 {pm} 5.0e-06)"),
            (np.array([5.]), "approx([5.0 {pm} 5.0e-06])"),
            (np.array([[5.]]), "approx([[5.0 {pm} 5.0e-06]])"),
            (np.array([[5., 6.]]), "approx([[5.0 {pm} 5.0e-06, 6.0 {pm} 6.0e-06]])"),
            (
                np.array([[5.], [6.]]),
                "approx([[5.0 {pm} 5.0e-06], [6.0 {pm} 6.0e-06]])",
            ),
        ]
        for np_array, repr_string in examples:
            assert repr(approx(np_array)) == repr_string.format(pm=plus_minus)

    def test_operator_overloading(self):
        assert 1 == approx(1, rel=1e-6, abs=1e-12)
        assert not (1 != approx(1, rel=1e-6, abs=1e-12))
        assert 10 != approx(1, rel=1e-6, abs=1e-12)
        assert not (10 == approx(1, rel=1e-6, abs=1e-12))

    def test_exactly_equal(self):
        examples = [
            (2.0, 2.0),
            (0.1e200, 0.1e200),
            (1.123e-300, 1.123e-300),<commit_msg>Use parametrize in repr test for nd arrays<commit_after># encoding: utf-8
import operator
import sys
import pytest
import doctest

from pytest import approx
from operator import eq, ne
from decimal import Decimal
from fractions import Fraction

inf, nan = float("inf"), float("nan")


class MyDocTestRunner(doctest.DocTestRunner):
    def __init__(self):
        doctest.DocTestRunner.__init__(self)

    def report_failure(self, out, test, example, got):
        raise AssertionError(
            "'{}' evaluates to '{}', not '{}'".format(
                example.source.strip(), got.strip(), example.want.strip()
            )
        )


class TestApprox(object):
    @pytest.fixture
    def plus_minus(self):
        return u"\u00b1" if sys.version_info[0] > 2 else u"+-"

    def test_repr_string(self, plus_minus):
        tol1, tol2, infr = "1.0e-06", "2.0e-06", "inf"
        assert repr(approx(1.0)) == "1.0 {pm} {tol1}".format(pm=plus_minus, tol1=tol1)
        assert repr(
            approx([1.0, 2.0])
        ) == "approx([1.0 {pm} {tol1}, 2.0 {pm} {tol2}])".format(
            pm=plus_minus, tol1=tol1, tol2=tol2
        )
        assert repr(
            approx((1.0, 2.0))
        ) == "approx((1.0 {pm} {tol1}, 2.0 {pm} {tol2}))".format(
            pm=plus_minus, tol1=tol1, tol2=tol2
        )
        assert repr(approx(inf)) == "inf"
        assert repr(approx(1.0, rel=nan)) == "1.0 {pm} ???".format(pm=plus_minus)
        assert repr(approx(1.0, rel=inf)) == "1.0 {pm} {infr}".format(
            pm=plus_minus, infr=infr
        )
        assert repr(approx(1.0j, rel=inf)) == "1j"

        # Dictionaries aren't ordered, so we need to check both orders.
        assert repr(approx({"a": 1.0, "b": 2.0})) in (
            "approx({{'a': 1.0 {pm} {tol1}, 'b': 2.0 {pm} {tol2}}})".format(
                pm=plus_minus, tol1=tol1, tol2=tol2
            ),
            "approx({{'b': 2.0 {pm} {tol2}, 'a': 1.0 {pm} {tol1}}})".format(
                pm=plus_minus, tol1=tol1, tol2=tol2
            ),
        )

    @pytest.mark.parametrize(
        "value, repr_string",
        [
            (5., "approx(5.0 {pm} 5.0e-06)"),
            ([5.], "approx([5.0 {pm} 5.0e-06])"),
            ([[5.]], "approx([[5.0 {pm} 5.0e-06]])"),
            ([[5., 6.]], "approx([[5.0 {pm} 5.0e-06, 6.0 {pm} 6.0e-06]])"),
            ([[5.], [6.]], "approx([[5.0 {pm} 5.0e-06], [6.0 {pm} 6.0e-06]])"),
        ],
    )
    def test_repr_nd_array(self, plus_minus, value, repr_string):
        """Make sure that arrays of all different dimensions are repr'd correctly."""
        np = pytest.importorskip("numpy")
        np_array = np.array(value)
        assert repr(approx(np_array)) == repr_string.format(pm=plus_minus)

    def test_operator_overloading(self):
        assert 1 == approx(1, rel=1e-6, abs=1e-12)
        assert not (1 != approx(1, rel=1e-6, abs=1e-12))
        assert 10 != approx(1, rel=1e-6, abs=1e-12)
        assert not (10 == approx(1, rel=1e-6, abs=1e-12))

    def test_exactly_equal(self):
        examples = [
            (2.0, 2.0),
            (0.1e200, 0.1e200),
            (1.123e-300, 1.123e-300),
{'fraction_non_alphanumeric': 0.18599110546378653, 'fraction_numerical': 0.06130876747141042, 'mean_word_length': 2.1706948640483383, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5362492', 'n_tokens_mistral': 2527, 'n_tokens_neox': 2374, 'n_words': 507}