import sys, os.path
sys.path.append(os.path.split(os.path.abspath(__file__))[0] + '/..')

import unittest
import csv
import time
import random
import datetime
import numpy as np

from cdbf import CountdownBloomFilter, ScalableCountdownBloomFilter


class CountdownBloomFilterTests(unittest.TestCase):
    '''
    Tests for CountdownBloomFilter
    '''
    @classmethod
    def setUp(self):
        self.batch_refresh_period = 0.1
        self.bf = CountdownBloomFilter(1000, 0.02, 5)

    def test_empty(self):
        assert len(self.bf) == 0
        assert self.bf.cellarray.nonzero()[0].shape == (0,)

    def test_cellarray(self):
        assert self.bf.cellarray.shape == (8148,)

    def test_add(self):
        existing = self.bf.add('random_uuid')
        assert existing == False
        existing = self.bf.add('random_uuid')
        assert existing == True
        assert (self.bf.cellarray.nonzero()[0] == np.array([ 228, 2104, 3151, 4372, 6496, 7449])).all()

    def test_single_batch_expiration(self):
        existing = self.bf.add('random_uuid')
        assert existing == False
        existing = self.bf.add('random_uuid')
        assert existing == True
        nzi = self.bf.cellarray.nonzero()[0]
        assert (self.bf.cellarray[nzi] == np.array([255, 255, 255, 255, 255, 255], dtype=np.uint8)).all()
        self.bf.batched_expiration_maintenance(self.batch_refresh_period)
        print self.bf.cellarray[nzi]
        assert (self.bf.cellarray[nzi] == np.array([249, 250, 250, 250, 250, 250], dtype=np.uint8)).all()

    def test_expiration(self):
        existing = self.bf.add('random_uuid')
        assert existing == False
        existing = self.bf.add('random_uuid')
        assert existing == True
        nzi = self.bf.cellarray.nonzero()[0]
        # Check membership just before expiration
        for i in range(49):
            self.bf.batched_expiration_maintenance(self.batch_refresh_period)
        existing = 'random_uuid' in self.bf
        print existing
        assert existing == True
        # Check membership right after expiration
        self.bf.batched_expiration_maintenance(self.batch_refresh_period)
        existing = 'random_uuid' in self.bf
        print existing
        assert existing == False

    
class ScalableCountdownBloomFilter(unittest.TestCase):
    '''
    Tests for CountdownBloomFilter
    '''
    @classmethod
    def setUp(self):
        pass


if __name__ == '__main__':
     unittest.main()

@@ -423,16 +423,46 @@
 d = 0.1%0A
+        self.expiration = 5.0%0A
         
@@ -504,17 +504,31 @@
 , 0.02, 
-5
+self.expiration
 )%0A%0A    d
@@ -1001,32 +1001,146 @@
  7449%5D)).all()%0A%0A
+    def test_compute_refresh_time(self):%0A        assert self.bf.compute_refresh_time() == 2.4132205876674775e-06%0A%0A
     def test_sin
@@ -1685,32 +1685,560 @@
 .uint8)).all()%0A%0A
+    def test_expiration_realtime(self):%0A        existing = self.bf.add('random_uuid')%0A        assert existing == False%0A        existing = self.bf.add('random_uuid')%0A        assert existing == True%0A        elapsed = 0%0A        start = time.time()%0A        while existing:%0A            t1 = time.time()%0A            if elapsed:%0A                self.bf.batched_expiration_maintenance(elapsed)%0A            existing = 'random_uuid' in self.bf%0A            t2 = time.time()%0A            elapsed = t2 - t1%0A        print time.time() - start%0A%0A
     def test_exp
@@ -2496,32 +2496,100 @@
 fore expiration%0A
+        nbr_step = int(self.expiration / self.batch_refresh_period)%0A
         for i in
@@ -2595,18 +2595,28 @@
 n range(
-49
+nbr_step - 1
 ):%0A     
@@ -2732,39 +2732,16 @@
 self.bf%0A
-        print existing%0A
         
@@ -2936,31 +2936,8 @@
 .bf%0A
-        print existing%0A
     
@@ -2966,20 +2966,16 @@
  False%0A%0A
-    
 %0Aclass S

Add test_expiration_realtime