<commit_before>import unittest

from pyperf.basic import BasicPerfTest


class TestBasicPerfTest(unittest.TestCase):

    def test_externalize_runs_somewhere_else(self):

        success = True

        def toggle():

            global success
            success = False

        test = BasicPerfTest('', '')
        test._externalize(toggle)
        self.assertTrue(success)

    def test_externalize_reacts_to_exceptions(self):
        """If unhandled, exceptions in externalize hang forever. Catch them."""

        def fail():

            raise Exception()

        test = BasicPerfTest('','')
        with self.assertRaises(Exception):

            test._externalize(fail)

    def test_time_behaves_reasonably(self):
        """Check that obviously longer code is measured as longer running."""

        test1 = BasicPerfTest('for x in range(10): pass')
        test2 = BasicPerfTest('for x in range(10000): pass')

        time1 = test1.time(samples=100)
        time2 = test2.time(samples=100)

        self.assertTrue(time1.runtime < time2.runtime)

    def test_memory_behaves_resonably(self):
        """Check that obviously more consuming code is measured as bigger."""

        test1 = BasicPerfTest('for x in list(range(10)): pass')
        test2 = BasicPerfTest('for x in list(range(100000)): pass')

        mem1 = test1.memory()
        mem2 = test2.memory()

        self.assertTrue(mem1.max < mem2.max)

    def test_profile_runs_both(self):

        test = BasicPerfTest('for x in list(range(10)): pass')
        results = test()

        self.assertTrue(results.runtime.runtime > 0)
        self.assertTrue(results.memory.max > 0)


if __name__ == '__main__':

    unittest.main()
<commit_msg>Add: Tests for PerfTestSet integration<commit_after>import unittest

from pyperf.basic import BasicPerfTest
from pyperf.interfaces import PerfTestSet


class TestBasicPerfTest(unittest.TestCase):

    def test_externalize_runs_somewhere_else(self):

        success = True

        def toggle():

            global success
            success = False

        test = BasicPerfTest('', '')
        test._externalize(toggle)
        self.assertTrue(success)

    def test_externalize_reacts_to_exceptions(self):
        """If unhandled, exceptions in externalize hang forever. Catch them."""

        def fail():

            raise Exception()

        test = BasicPerfTest('','')
        with self.assertRaises(Exception):

            test._externalize(fail)

    def test_time_behaves_reasonably(self):
        """Check that obviously longer code is measured as longer running."""

        test1 = BasicPerfTest('for x in range(10): pass')
        test2 = BasicPerfTest('for x in range(10000): pass')

        time1 = test1.time(samples=100)
        time2 = test2.time(samples=100)

        self.assertTrue(time1.runtime < time2.runtime)

    def test_memory_behaves_resonably(self):
        """Check that obviously more consuming code is measured as bigger."""

        test1 = BasicPerfTest('for x in list(range(1)): pass')
        test2 = BasicPerfTest('for x in list(range(10000)): pass')

        mem1 = test1.memory()
        mem2 = test2.memory()

        self.assertTrue(mem1.max < mem2.max)

    def test_profile_runs_both(self):

        test = BasicPerfTest('for x in list(range(10)): pass')
        results = test(samples=100)

        self.assertTrue(results.runtime.runtime > 0)
        self.assertTrue(results.memory.max > 0)

    def test_integrates_with_perf_test_set(self):

        test = PerfTestSet(
            tests=(
                'for x in list(range(10)): pass',
                'for x in list(range(100)): pass',
                'for x in list(range(10000)): pass',
            ),
            perf_class=BasicPerfTest,
        )

        test.time(samples=100)


if __name__ == '__main__':

    unittest.main()

{'fraction_non_alphanumeric': 0.10650427571909821, 'fraction_numerical': 0.024876911116869654, 'mean_word_length': 2.564173591874423, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6788119', 'n_tokens_mistral': 1204, 'n_tokens_neox': 1104, 'n_words': 270}