<commit_before>import unittest

from pikos.monitors.function_memory_monitor import FunctionMemoryMonitor
from pikos.recorders.list_recorder import ListRecorder
from pikos.tests.test_assistant import TestAssistant
from pikos.tests.compat import TestCase


class TestFunctionMemoryMonitor(TestCase, TestAssistant):

    def test_function(self):
        recorder = ListRecorder()
        logger = FunctionMemoryMonitor(recorder)

        @logger.attach
        def gcd(x, y):
            while x > 0:
                x, y = y % x, x
            return y

        def boo():
            pass

        boo()
        result = gcd(12, 3)
        boo()
        self.assertEqual(result, 3)
        records = recorder.records
        # check that the records make sense
        self.assertFieldValueExist(records, ('function', 'type'),
                                   ('gcd', 'call'), times=1)
        self.assertFieldValueExist(records, ('function', 'type'),
                                   ('gcd', 'return'), times=1)
        self.assertFieldValueNotExist(records, ('function',), ('boo',))
        # The wrapper of the function should not be logged
        self.assertFieldValueNotExist(records, ('function',), ('wrapper',))

    def test_recursive(self):
        recorder = ListRecorder()
        logger = FunctionMemoryMonitor(recorder)

        @logger.attach
        def gcd(x, y):
            return x if y == 0 else gcd(y, (x % y))

        def boo():
            return gcd(7, 12)

        result = boo()
        self.assertEqual(result, 1)
        records = recorder.records
        # check that the records make sense
        # The function should be called 6 times
        self.assertFieldValueExist(records, ('function', 'type'),
                                   ('gcd', 'call'), times=6)
        self.assertFieldValueExist(records, ('function', 'type'),
                                   ('gcd', 'return'), times=6)
        self.assertFieldValueNotExist(records, ('function',), ('boo',))
        # In recursive calls the wrapper of the function is logged
        # self.assertFieldValueNotExist(records, ('function',), ('wrapper',))

    def test_generator(self):
        recorder = ListRecorder()
        logger = FunctionMemoryMonitor(recorder)
        output = (0, 1, 1, 2, 3, 5, 8, 13, 21, 34)
<commit_msg>add a fixme to remove recording the wrappers<commit_after>import unittest

from pikos.monitors.function_memory_monitor import FunctionMemoryMonitor
from pikos.recorders.list_recorder import ListRecorder
from pikos.tests.test_assistant import TestAssistant
from pikos.tests.compat import TestCase


class TestFunctionMemoryMonitor(TestCase, TestAssistant):

    def test_function(self):
        recorder = ListRecorder()
        logger = FunctionMemoryMonitor(recorder)

        @logger.attach
        def gcd(x, y):
            while x > 0:
                x, y = y % x, x
            return y

        def boo():
            pass

        boo()
        result = gcd(12, 3)
        boo()
        self.assertEqual(result, 3)
        records = recorder.records
        # check that the records make sense
        self.assertFieldValueExist(records, ('function', 'type'),
                                   ('gcd', 'call'), times=1)
        self.assertFieldValueExist(records, ('function', 'type'),
                                   ('gcd', 'return'), times=1)
        self.assertFieldValueNotExist(records, ('function',), ('boo',))
        # The wrapper of the function should not be logged
        self.assertFieldValueNotExist(records, ('function',), ('wrapper',))

    def test_recursive(self):
        recorder = ListRecorder()
        logger = FunctionMemoryMonitor(recorder)

        @logger.attach
        def gcd(x, y):
            return x if y == 0 else gcd(y, (x % y))

        def boo():
            return gcd(7, 12)

        result = boo()

        self.assertEqual(result, 1)
        records = recorder.records
        # check that the records make sense
        # The function should be called 6 times
        self.assertFieldValueExist(records, ('function', 'type'),
                                   ('gcd', 'call'), times=6)
        self.assertFieldValueExist(records, ('function', 'type'),
                                   ('gcd', 'return'), times=6)
        self.assertFieldValueNotExist(records, ('function',), ('boo',))
        # FIXME: In recursive case calls the wrapper of the function is
        #        logged self.assertFieldValueNotExist(records, ('function',),
        #        ('wrapper',))

    def test_generator(self):
        recorder = ListRecorder()
        logger = FunctionMemoryMonitor(recorder)
        output = (0, 1, 1, 2, 3, 5, 8, 13, 21, 34)

{'fraction_non_alphanumeric': 0.11158252015273654, 'fraction_numerical': 0.011879507848960543, 'mean_word_length': 2.1559571619812585, 'pattern_counts': {'":': 0, '<': 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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '242121', 'n_tokens_mistral': 1361, 'n_tokens_neox': 1275, 'n_words': 357}