# Path: nesoni/storage.py
# LOCAL = threading.local()
#     C = ffi.dlopen(None)
#     def get_mmap(fileno, size):
#         def close():
#     def get_mmap(fileno, size):
#     def __new__(cls, offset, shape, dtype):
#     def __reduce__(self):
#     def __init__(self, filename, clear=False, preallocate=0, expand=1.5):
#     def close(self):
#     def data(self):
#     def data(self, value):
#     def save(self):
#     def _require(self, size):
#     def _load(self):
#     def _in_context(self, func, *args, **kwargs):
#     def _allocate(self, size):
#     def empty(self, shape, dtype):
#     def zeros(self, shape, dtype):
#     def ones(self, shape, dtype):
#     def array(self, data):
# class Disk_array(np.ndarray):
# class _Store(object): 
# class _Value(object): 
# class Storage(object):


import os, random
import numpy as np
from nesoni import storage
s = storage.Storage('output/test.store', clear=True)

a = [ ]
b = [ ]
s.data = a

for i in xrange(10000):
    j = random.choice([0] + ([1] if a else []))
    if j == 0:
        size = random.randrange(1,1000)
        def op(alist, allocator):
            alist.append( allocator.zeros(size, 'int32') )
    elif j == 1:
        k = random.randrange(len(a))
        l = random.randrange(len(a[k]))
        m = random.randrange(l,len(a[k]))
        def op(alist, allocator):
            alist[k][l:m] += 1
    
    #print a
    #print b  
    op(a,s)
    op(b,np)
    for aa,bb in zip(a,b):
        assert np.all(aa == bb)
        
    if i % 1000 == 0:
        print 'reload'
        s.save()
        s.close()

# Path: nesoni/storage.py
# LOCAL = threading.local()
#     C = ffi.dlopen(None)
#     def get_mmap(fileno, size):
#         def close():
#     def get_mmap(fileno, size):
#     def __new__(cls, offset, shape, dtype):
#     def __reduce__(self):
#     def __init__(self, filename, clear=False, preallocate=0, expand=1.5):
#     def close(self):
#     def data(self):
#     def data(self, value):
#     def save(self):
#     def _require(self, size):
#     def _load(self):
#     def _in_context(self, func, *args, **kwargs):
#     def _allocate(self, size):
#     def empty(self, shape, dtype):
#     def zeros(self, shape, dtype):
#     def ones(self, shape, dtype):
#     def array(self, data):
# class Disk_array(np.ndarray):
# class _Store(object): 
# class _Value(object): 
# class Storage(object):


# Path: test/test_storage.py
import os, random
import numpy as np
from nesoni import storage

s = storage.Storage('output/test.store', clear=True)

a = [ ]
b = [ ]
s.data = a

for i in xrange(10000):
    j = random.choice([0] + ([1] if a else []))
    if j == 0:
        size = random.randrange(1,1000)
        def op(alist, allocator):
            alist.append( allocator.zeros(size, 'int32') )
    elif j == 1:
        k = random.randrange(len(a))
        l = random.randrange(len(a[k]))
        m = random.randrange(l,len(a[k]))
        def op(alist, allocator):
            alist[k][l:m] += 1
    
    #print a
    #print b  
    op(a,s)
    op(b,np)
    for aa,bb in zip(a,b):
        assert np.all(aa == bb)
        
    if i % 1000 == 0:
        print 'reload'
        s.save()
        s.close()

        s = storage.Storage('output/test.store')