<s> import unittest <EOL> import struct <EOL> import pickle <EOL> from hashlib import sha1 <EOL> import numpy as np <EOL> from datasketch import minhash <EOL> from datasketch . b_bit_minhash import bBitMinHash <EOL> class FakeHash ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , h ) : <EOL> '''<STR_LIT>''' <EOL> self . h = h <EOL> def digest ( self ) : <EOL> '''<STR_LIT>''' <EOL> return struct . pack ( '<STR_LIT>' , self . h ) <EOL> class TestMinHash ( unittest . TestCase ) : <EOL> def test_init ( self ) : <EOL> m1 = minhash . MinHash ( <NUM_LIT:4> , <NUM_LIT:1> , hashobj = FakeHash ) <EOL> m2 = minhash . MinHash ( <NUM_LIT:4> , <NUM_LIT:1> , hashobj = FakeHash ) <EOL> self . assertTrue ( np . array_equal ( m1 . hashvalues , m2 . hashvalues ) ) <EOL> self . assertTrue ( np . array_equal ( m1 . permutations , m2 . permutations ) ) <EOL> def test_is_empty ( self ) : <EOL> m = minhash . MinHash ( ) <EOL> self . assertTrue ( m . is_empty ( ) ) <EOL> def test_update ( self ) : <EOL> m1 = minhash . MinHash ( <NUM_LIT:4> , <NUM_LIT:1> , hashobj = FakeHash ) <EOL> m2 = minhash . MinHash ( <NUM_LIT:4> , <NUM_LIT:1> , hashobj = FakeHash ) <EOL> m1 . update ( <NUM_LIT:12> ) <EOL> for i in range ( <NUM_LIT:4> ) : <EOL> self . assertTrue ( m1 . hashvalues [ i ] < m2 . hashvalues [ i ] ) <EOL> def test_jaccard ( self ) : <EOL> m1 = minhash . MinHash ( <NUM_LIT:4> , <NUM_LIT:1> , hashobj = FakeHash ) <EOL> m2 = minhash . MinHash ( <NUM_LIT:4> , <NUM_LIT:1> , hashobj = FakeHash ) <EOL> self . assertTrue ( m1 . jaccard ( m2 ) == <NUM_LIT:1.0> ) <EOL> m2 . update ( <NUM_LIT:12> ) <EOL> self . assertTrue ( m1 . jaccard ( m2 ) == <NUM_LIT:0.0> ) <EOL> m1 . update ( <NUM_LIT> ) <EOL> self . assertTrue ( m1 . jaccard ( m2 ) < <NUM_LIT:1.0> ) <EOL> def test_merge ( self ) : <EOL> m1 = minhash . MinHash ( <NUM_LIT:4> , <NUM_LIT:1> , hashobj = FakeHash ) <EOL> m2 = minhash . MinHash ( <NUM_LIT:4> , <NUM_LIT:1> , hashobj = FakeHash ) <EOL> m2 . update ( <NUM_LIT:12> ) <EOL> m1 . merge ( m2 ) <EOL> self . assertTrue ( m1 . jaccard ( m2 ) == <NUM_LIT:1.0> ) <EOL> def test_union ( self ) : <EOL> m1 = minhash . MinHash ( <NUM_LIT:4> , <NUM_LIT:1> , hashobj = FakeHash ) <EOL> m2 = minhash . MinHash ( <NUM_LIT:4> , <NUM_LIT:1> , hashobj = FakeHash ) <EOL> m2 . update ( <NUM_LIT:12> ) <EOL> u = minhash . MinHash . union ( m1 , m2 ) <EOL> self . assertTrue ( u . jaccard ( m2 ) == <NUM_LIT:1.0> ) <EOL> def test_bytesize ( self ) : <EOL> m1 = minhash . MinHash ( <NUM_LIT:4> , <NUM_LIT:1> , hashobj = FakeHash ) <EOL> self . assertTrue ( m1 . bytesize ( ) == ( <NUM_LIT:4> * <NUM_LIT:4> ) + <NUM_LIT:4> + <NUM_LIT:8> ) <EOL> def test_serialize ( self ) : <EOL> m1 = minhash . MinHash ( <NUM_LIT:2> , <NUM_LIT:1> , hashobj = FakeHash ) <EOL> buf = bytearray ( m1 . bytesize ( ) ) <EOL> m1 . serialize ( buf ) <EOL> def test_deserialize ( self ) : <EOL> m1 = minhash . MinHash ( <NUM_LIT:10> , <NUM_LIT:1> , hashobj = FakeHash ) <EOL> m1 . update ( <NUM_LIT> ) <EOL> buf = bytearray ( m1 . bytesize ( ) ) <EOL> m1 . serialize ( buf ) <EOL> m1d = minhash . MinHash . deserialize ( buf ) <EOL> m1d . hashobj = FakeHash <EOL> self . assertEqual ( m1d . seed , m1 . seed ) <EOL> self . assertEqual ( len ( m1d . hashvalues ) , len ( m1 . hashvalues ) ) <EOL> self . assertTrue ( all ( hvd == hv for hv , hvd in zip ( m1 . hashvalues , <EOL> m1d . hashvalues ) ) ) <EOL> m1 . update ( <NUM_LIT> ) <EOL> m1d . update ( <NUM_LIT> ) <EOL> self . assertTrue ( all ( hvd == hv for hv , hvd in zip ( m1 . hashvalues , <EOL> m1d . hashvalues ) ) ) <EOL> def test_pickle ( self ) : <EOL> m = minhash . MinHash ( <NUM_LIT:4> , <NUM_LIT:1> , hashobj = FakeHash ) <EOL> m . update ( <NUM_LIT> ) <EOL> m . update ( <NUM_LIT> ) <EOL> p = pickle . loads ( pickle . dumps ( m ) ) <EOL> self . assertEqual ( p . seed , m . seed ) <EOL> self . assertTrue ( np . array_equal ( p . hashvalues , m . hashvalues ) ) <EOL> self . assertTrue ( np . array_equal ( p . permutations , m . permutations ) ) <EOL> def test_eq ( self ) : <EOL> m1 = minhash . MinHash ( <NUM_LIT:4> , <NUM_LIT:1> , hashobj = FakeHash ) <EOL> m2 = minhash . MinHash ( <NUM_LIT:4> , <NUM_LIT:1> , hashobj = FakeHash ) <EOL> m3 = minhash . MinHash ( <NUM_LIT:4> , <NUM_LIT:2> , hashobj = FakeHash ) <EOL> m4 = minhash . MinHash ( <NUM_LIT:8> , <NUM_LIT:1> , hashobj = FakeHash ) <EOL> m5 = minhash . MinHash ( <NUM_LIT:4> , <NUM_LIT:1> , hashobj = FakeHash ) <EOL> m1 . update ( <NUM_LIT:11> ) <EOL> m2 . update ( <NUM_LIT:12> ) <EOL> m3 . update ( <NUM_LIT:11> ) <EOL> m4 . update ( <NUM_LIT:11> ) <EOL> m5 . update ( <NUM_LIT:11> ) <EOL> self . assertNotEqual ( m1 , m2 ) <EOL> self . assertNotEqual ( m1 , m3 ) <EOL> self . assertNotEqual ( m1 , m4 ) <EOL> self . assertEqual ( m1 , m5 ) <EOL> m1 . update ( <NUM_LIT:12> ) <EOL> m2 . update ( <NUM_LIT:11> ) <EOL> self . assertEqual ( m1 , m2 ) <EOL> def test_count ( self ) : <EOL> m = minhash . MinHash ( hashobj = FakeHash ) <EOL> m . update ( <NUM_LIT:11> ) <EOL> m . update ( <NUM_LIT> ) <EOL> m . update ( <NUM_LIT> ) <EOL> m . update ( <NUM_LIT> ) <EOL> m . update ( <NUM_LIT> ) <EOL> m . update ( <NUM_LIT:32> ) <EOL> c = m . count ( ) <EOL> self . assertGreaterEqual ( c , <NUM_LIT:0> ) <EOL> class TestbBitMinHash ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . m = minhash . MinHash ( hashobj = FakeHash ) <EOL> self . m . update ( <NUM_LIT:11> ) <EOL> self . m . update ( <NUM_LIT> ) <EOL> self . m . update ( <NUM_LIT> ) <EOL> self . m . update ( <NUM_LIT> ) <EOL> self . m . update ( <NUM_LIT> ) <EOL> self . m . update ( <NUM_LIT:32> ) <EOL> def test_init ( self ) : <EOL> bm = bBitMinHash ( self . m , <NUM_LIT:1> ) <EOL> bm = bBitMinHash ( self . m , <NUM_LIT:2> ) <EOL> bm = bBitMinHash ( self . m , <NUM_LIT:3> ) <EOL> bm = bBitMinHash ( self . m , <NUM_LIT:4> ) <EOL> bm = bBitMinHash ( self . m , <NUM_LIT:5> ) <EOL> bm = bBitMinHash ( self . m , <NUM_LIT:8> ) <EOL> bm = bBitMinHash ( self . m , <NUM_LIT:12> ) <EOL> bm = bBitMinHash ( self . m , <NUM_LIT:16> ) <EOL> bm = bBitMinHash ( self . m , <NUM_LIT> ) <EOL> bm = bBitMinHash ( self . m , <NUM_LIT:32> ) <EOL> def test_eq ( self ) : <EOL> m1 = minhash . MinHash ( <NUM_LIT:4> , <NUM_LIT:1> , hashobj = FakeHash ) <EOL> m2 = minhash . MinHash ( <NUM_LIT:4> , <NUM_LIT:1> , hashobj = FakeHash ) <EOL> m3 = minhash . MinHash ( <NUM_LIT:4> , <NUM_LIT:2> , hashobj = FakeHash ) <EOL> m4 = minhash . MinHash ( <NUM_LIT:8> , <NUM_LIT:1> , hashobj = FakeHash ) <EOL> m5 = minhash . MinHash ( <NUM_LIT:4> , <NUM_LIT:1> , hashobj = FakeHash ) <EOL> m1 . update ( <NUM_LIT:11> ) <EOL> m2 . update ( <NUM_LIT:12> ) <EOL> m3 . update ( <NUM_LIT:11> ) <EOL> m4 . update ( <NUM_LIT:11> ) <EOL> m5 . update ( <NUM_LIT:11> ) <EOL> m1 = bBitMinHash ( m1 ) <EOL> m2 = bBitMinHash ( m2 ) <EOL> m3 = bBitMinHash ( m3 ) <EOL> m4 = bBitMinHash ( m4 ) <EOL> m5 = bBitMinHash ( m5 ) <EOL> self . assertNotEqual ( m1 , m2 ) <EOL> self . assertNotEqual ( m1 , m3 ) <EOL> self . assertNotEqual ( m1 , m4 ) <EOL> self . assertEqual ( m1 , m5 ) <EOL> def test_jaccard ( self ) : <EOL> m1 = minhash . MinHash ( <NUM_LIT:4> , <NUM_LIT:1> , hashobj = FakeHash ) <EOL> m2 = minhash . MinHash ( <NUM_LIT:4> , <NUM_LIT:1> , hashobj = FakeHash ) <EOL> bm1 = bBitMinHash ( m1 ) <EOL> bm2 = bBitMinHash ( m2 ) <EOL> self . assertTrue ( bm1 . jaccard ( bm2 ) == <NUM_LIT:1.0> ) <EOL> m2 . update ( <NUM_LIT:12> ) <EOL> bm2 = bBitMinHash ( m2 ) <EOL> self . assertTrue ( bm1 . jaccard ( bm2 ) < <NUM_LIT:1.0> ) <EOL> m1 . update ( <NUM_LIT> ) <EOL> bm1 = bBitMinHash ( m1 ) <EOL> self . assertTrue ( bm1 . jaccard ( bm2 ) < <NUM_LIT:1.0> ) <EOL> def test_bytesize ( self ) : <EOL> s = bBitMinHash ( self . m ) . bytesize ( ) <EOL> self . assertGreaterEqual ( s , <NUM_LIT:8> * <NUM_LIT:2> + <NUM_LIT:4> + <NUM_LIT:1> + self . m . hashvalues . size / <NUM_LIT:64> ) <EOL> def test_pickle ( self ) : <EOL> for num_perm in [ <NUM_LIT:1> << i for i in range ( <NUM_LIT:4> , <NUM_LIT:10> ) ] : <EOL> m = minhash . MinHash ( num_perm = num_perm , hashobj = FakeHash ) <EOL> m . update ( <NUM_LIT:11> ) <EOL> m . update ( <NUM_LIT> ) <EOL> m . update ( <NUM_LIT> ) <EOL> m . update ( <NUM_LIT> ) <EOL> m . update ( <NUM_LIT> ) <EOL> m . update ( <NUM_LIT:32> ) <EOL> for b in [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:9> , <NUM_LIT> , <NUM_LIT:32> ] : <EOL> bm = bBitMinHash ( m , b ) <EOL> bm2 = pickle . loads ( pickle . dumps ( bm ) ) <EOL> self . assertEqual ( bm , bm2 ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
