<commit_before># -*- coding: utf-8 -*-
"""
    Aho-Corasick string search algorithm.

    Author    : <NAME>, <EMAIL>
    WWW       : http://0x80.pl
    License   : public domain
"""

import os
import sys
import unittest

from pathlib import Path

import ahocorasick

from pytestingutils import conv


on_linux = str(sys.platform).lower().startswith('linux')


def get_memory_usage():
    # Linux only
    pid = os.getpid()

    lines = []
    try:
        with open('/proc/%d/status' % pid, 'rt') as f:
            lines = f.readlines()
    except:
        pass

    for line in lines:
        if line.startswith('VmSize'):
            return float(line.split()[1])

    return 0


def use_memory():
    here = Path(__file__).parent
    with open(here.parent.parent / 'README.rst') as f:
        data = f.read()
        data = conv(data)
        data = data.split()

    ac = ahocorasick.Automaton()
    for i, word in enumerate(data):
        ac.add_word(word, i)

    ac.make_automaton()

    for i in range(1024):
        s = list(ac.keys())


class TestMemory(unittest.TestCase):

    @unittest.skipIf(not on_linux, "Works only on linux")
    @unittest.expectedFailure
    def test_does_not_leak_memory(self):
        before = get_memory_usage()
        use_memory()
        after = get_memory_usage()
        assert before == after<commit_msg>Improve memory leak test<commit_after># -*- coding: utf-8 -*-
"""
    Aho-Corasick string search algorithm.

    Author    : <NAME>, <EMAIL>
    WWW       : http://0x80.pl
    License   : public domain
"""

import os
import sys
from pathlib import Path

import pytest

import ahocorasick

from pytestingutils import conv


on_linux = str(sys.platform).lower().startswith('linux')


def get_memory_usage():
    # Linux only
    pid = os.getpid()

    lines = []
    try:
        with open('/proc/%d/status' % pid, 'rt') as f:
            lines = f.readlines()
    except:
        pass

    for line in lines:
        if line.startswith('VmSize'):
            return float(line.split()[1])

    return 0


def build_automaton():
    here = Path(__file__).parent
    with open(here.parent.parent / 'README.rst') as f:
        data = f.read()
        data = conv(data)
        data = data.split()

    ac = ahocorasick.Automaton()
    for i, word in enumerate(data):
        ac.add_word(word, i)

    ac.make_automaton()
    return ac


@pytest.mark.skipif(not on_linux, reason="Works only on linux")
def test_does_not_leak_memory():
    ac = build_automaton()
    before = get_memory_usage()

    for i in range(1024):
        s = list(ac.keys())

    after = get_memory_usage()
    assert before == after
{'fraction_non_alphanumeric': 0.12334967936627687, 'fraction_numerical': 0.007544322897019992, 'mean_word_length': 2.4087403598971724, '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': '1065145', 'n_tokens_mistral': 903, 'n_tokens_neox': 863, 'n_words': 224}