import lz4.frame
import time

def test_frame_decompress_mem_usage():

    try:
        import tracemalloc # Python >= 3.4 only
    except:
        return 0

    tracemalloc.start()

    input_data = 'a' * 1024 * 1024
    compressed = lz4.frame.compress(input_data.encode('utf-8'))
    prev_snapshot = None

    for i in range(5000):
        decompressed = lz4.frame.decompress(compressed)

        if i % 100 == 0:
            snapshot = tracemalloc.take_snapshot()

            if prev_snapshot:
                stats = snapshot.compare_to(prev_snapshot, 'lineno')
                if stats[0].size_diff > 1024 * 4:
                    return 1

            prev_snapshot = snapshot



# TODO: add many more memory usage tests along the lines of this one for other funcs

@@ -22,16 +22,279 @@
 rt time%0A
+import pytest%0Atracemalloc = pytest.importorskip('tracemalloc')%0A%0A%0Atest_data=%5B%0A    (b'a' * 1024 * 1024),%0A%5D%0A%0A@pytest.fixture(%0A    params=test_data,%0A    ids=%5B%0A        'data' + str(i) for i in range(len(test_data))%0A    %5D%0A)%0Adef data(request):%0A    return request.param%0A%0A
 %0Adef tes
@@ -326,102 +326,238 @@
 age(
+data
 ):%0A
-%0A
     tr
-y:%0A        import tracemalloc # Python %3E= 3.4 only%0A    except:%0A        return 0%0A%0A   
+acemalloc.start()%0A%0A    compressed = lz4.frame.compress(data)%0A    prev_snapshot = None%0A%0A    for i in range(1000):%0A        decompressed = lz4.frame.decompress(compressed)%0A%0A        if i %25 100 == 0:%0A            snapshot =
  tra
@@ -569,51 +569,292 @@
 loc.
-s
 ta
-rt()%0A%0A    input_data = 'a' * 1024 * 1024
+ke_snapshot()%0A%0A            if prev_snapshot:%0A                stats = snapshot.compare_to(prev_snapshot, 'lineno')%0A                assert stats%5B0%5D.size_diff %3C (1024 * 4)%0A%0A            prev_snapshot = snapshot%0A%0A%0Adef test_frame_decompress_chunk_mem_usage(data):%0A    tracemalloc.start()%0A
 %0A   
@@ -890,35 +890,14 @@
 ess(
-input_data.encode('utf-8'))
+data)%0A
 %0A   
@@ -942,15 +942,74 @@
 nge(
-5
+1
 000):%0A
+        context = lz4.frame.create_decompression_context()%0A
     
@@ -1039,33 +1039,48 @@
 frame.decompress
-(
+_chunk(context, 
 compressed)%0A%0A   
@@ -1268,18 +1268,22 @@
         
-if
+assert
  stats%5B0
@@ -1298,48 +1298,576 @@
 iff 
-%3E 
+%3C (
 1024 * 
-4:%0A                    return 1
+10)%0A%0A            prev_snapshot = snapshot%0A%0A%0Adef test_frame_open_decompress_mem_usage(data):%0A    tracemalloc.start()%0A%0A    with lz4.frame.open('test.lz4', 'w') as f:%0A        f.write(data)%0A%0A    prev_snapshot = None%0A%0A    for i in range(1000):%0A        with lz4.frame.open('test.lz4', 'r') as f:%0A            decompressed = f.read()%0A%0A        if i %25 100 == 0:%0A            snapshot = tracemalloc.take_snapshot()%0A%0A            if prev_snapshot:%0A                stats = snapshot.compare_to(prev_snapshot, 'lineno')%0A                assert stats%5B0%5D.size_diff %3C (1024 * 10)
 %0A%0A  
@@ -1900,18 +1900,16 @@
 snapshot
-%0A%0A
 %0A%0A# TODO

Add tests for memory leaks during decompression