Exemple #1
0
def loadHashTable(name):
    ch = CuckooHash(50000)
    with open(name, "r") as f:
        for i in f:

            ch.insert([i])
    return ch
Exemple #2
0
def test_appendData():
    CH = CuckooHash(10000)
    for i in range(1000):
        CH.insert("word", random.randint(1, 100))

    #assert that the length of the data associated with
    #the word that was inserted 1000 times is 1000
    assert len(CH.find("word")[1]) == 1000
def test_allThere():                                 #after inserting elements we should be able to find them all. 
    c = CuckooHash(10)                               #Since the initial size is 10 and we are inserting 50 elements, if it pass this test, __growHash works
    lost = 0
    for i in range(50):                              
        c.insert(str(i), str(i))
    for i in range(50):
        if c.find(str(i)) == None:
            lost +=1
    assert lost == 0
def test_randomInsert():
    c = CuckooHash(10)
    inserted = []
    for i in range(random.randint(0,100)):           #loop upto a 100 times
        x = random.randint(0,100)                    #get a random int upto 100
        if not (x in inserted):                      #this list is to keep track of what is supposed to be inserted (no duplicates)
            inserted.append(x)
        c.insert(str(x), str(x))                     #try to insert x in the CuckooHash(should not insert duplicates)
    assert len(inserted) == len(c)                   #the length of the list and the CuckooHash should be the same
Exemple #5
0
def test_find():
    CH = CuckooHash(10000)
    words = []
    for i in range(1000):
        word = randWord()
        CH.insert(word, random.randint(1, 100))
        words.append(word)
    for word in words:
        assert CH.find(word)
Exemple #6
0
def test__insertDouble():
    c = CuckooHash(100)
    c.insert("DoubleWord", 234)
    c.insert("word1", 1)
    c.insert("word2", 2)
    c.insert("word3", 3)
    assert c.insert("DoubleWord", 234) == False
def test_noDuplicates():                             #Insert 20 times the same key
    c = CuckooHash(20)
    for i in range(20):
        c.insert("A", str(i))
    assert c.find("A") == "0"                        #it should return the data of the first inserted
    c.delete("A")                                    #After deleting("A"), if we try to find it, we shouldn't be able to because we dont have duplicates
    assert c.find("A") == None                       #return None if it wasn't there
Exemple #8
0
def test__insertMany():
    c = CuckooHash(300)
    file = open("wordlist.txt")
    word = file.readline()
    for i in range(580):
        c.insert(word, 2)
        word = file.readline()
    file.close()
    file = open(
        "wordlist.txt"
    )  # asserts that all the data was successfully inserted and the table was properly grown
    word = file.readline()
    for i in range(400):
        assert c.find(word) != None
        word = file.readline()
    file.close()
def test_delete():                                   #Insert numbers from 0 to 19
    c = CuckooHash(1000)
    for i in range(20):
        c.insert(str(i), str(i))
    assert len(c) == 20                              #the length should be 20
    for i in range(10):
        c.delete(str(i))                             #Delete the numbers upto 9.
    assert len(c) == 10                              #the final length should be 10
Exemple #10
0
def test_random():
    CH = CuckooHash(100)
    count = 0

    for i in range(10000):
        choice = random.randint(0, 1)
        word = randWord()

        if choice == 0:
            if not CH.find(word):
                count += 1
            CH.insert(word, word)

        if choice == 1:
            if CH.find(word):
                count -= 1
            CH.delete(word)

    assert count == CH.numKeys()
Exemple #11
0
def test_delete():
    CH = CuckooHash(10000)
    words = []
    for i in range(1000):
        word = randWord()
        CH.insert(word, random.randint(1, 100))
        words.append(word)
    for word in words:
        CH.delete(word)

    #assert that there are zero keys in the cuckoo hash
    #after deleting every key that was inserted
    assert CH.numKeys() == 0

    #assert that attempts to find words in the cuckoo hash fail
    for i in range(100):
        assert not CH.find(randWord())
Exemple #12
0
def test_growHash():
    CH = CuckooHash(10)
    counter = 0
    for i in range(10000):
        word = randWord()
        if CH.find(word):
            counter += 1
        CH.insert(word, random.randint(1, 100))
    counter += CH.numKeys()
    assert counter == 10000
Exemple #13
0
def test__SuccessfulFind():
    c = CuckooHash(400)
    file = open("wordlist.txt")
    word = file.readline()
    for i in range(100):
        c.insert(word, i)
        word = file.readline()
    file.close()
    file = open("wordlist.txt")
    word = file.readline()
    for i in range(100):
        assert c.find(
            word
        ) != None  # Successfully find all the words. If a word returns none then it didn't find it
        assert c.find(word) == i  # returns the proper data for the key
        word = file.readline()
    file.close()
Exemple #14
0
def main():
    ch = CuckooHash()
    name = input("Enter the name of the file containing the required data: ")
    stocks(name, ch)
    display(ch)
Exemple #15
0
def test__findFail():
    c = CuckooHash(100)
    assert c.find("Cuckoo") == None
def test_grow():                                     #After inserting 3 elements the array should grow because the numOfRecords>=numOfBuckets
    c = CuckooHash(6)
    c.insert("A", "a")
    c.insert("B", "b")
    c.insert("C", "c")
    assert c.numOfBuckets() == 12
Exemple #17
0
def test__deleteFalseKey():
    c = CuckooHash(20)
    assert c.delete("Delete") == None
Exemple #18
0
def test__delete():
    c = CuckooHash(100)
    c.insert("toDelete", 44)
    c.delete("toDelete")
    assert c.find("toDelete") == None
def test_empty():                                    #cant find or delete something on an empty CuckooHash
    c = CuckooHash(20)
    assert c.find("A") == None                       
    assert c.delete("A") == False
    assert len(c) ==  0
Exemple #20
0
def test__deleteFromTable():
    c = CuckooHash(400)
    c.insert("a", 1)
    c.insert("b", 2)
    c.insert("c", 3)
    c.insert("d", 4)
    c.insert("e", 5)
    c.insert("f", 6)
    c.insert("g", 7)
    c.insert("h", 8)
    c.insert("i", 9)
    c.delete("e")
    assert c.insert("e", 5) != False
    assert c.find("e") == 5
def test_insertingAll():                             #Insert the numbers from 0 to 99, the final length should be 100
    c = CuckooHash(100)                            
    for i in  range(100):
        c.insert(str(i), str(i))
    assert len(c) == 100
Exemple #22
0
def AutoCorrect(word):
	word=word+"\n"
	filename=input("enter name of the file")
	dic=CuckooHash(130)
	file_obj=open(filename,"r")
	for w in file_obj:
		dic.insert([w])
	dic.print()	
	print("length of tables is now",dic.len,"population is",dic.population)
	result=dic.lookup(word)
	if not(result):
		possible_words=list()
		alphabets=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
		for i in range(len(word)-2,-1,-1):
			#print("i is now",i)
			for letter in alphabets:
				new_word=list(word)
				new_word[i]=letter
				new_word=''.join(new_word)
				#print(new_word)
				isvalid=dic.lookup(new_word)
				if (isvalid):
					possible_words.append(new_word)

				new_word2=list(word)
				#print(new_word2)
				new_word2.append('')
				for j in range(len(word)-1,i,-1):
					new_word2[j+1]=new_word2[j]
				new_word2[i+1]=letter
				new_word3=list(new_word2)
				if i==0:
					new_word3[i+1]=new_word3[i]
					new_word3[i]=letter
					#print(new_word3)
					new_word3=''.join(new_word3)
					isvalid=dic.lookup(new_word3)
					if (isvalid):
						possible_words.append(new_word3)
				new_word2=''.join(new_word2)
				#print(new_word2)
				isvalid=dic.lookup(new_word2)
				if isvalid:
					possible_words.append(new_word2)

			new_word4=list(word)
			for j in range(i,len(word)-1):
				new_word4[j]=new_word4[j+1]
			temp=new_word4.pop()
			new_word4=''.join(new_word4)	
			isvalid=dic.lookup(new_word4)
			if isvalid:
				possible_words.append(new_word4)

		return possible_words
	else:
		#print("word exists")
		return None	
Exemple #23
0
def test__Findtime():

    # Calculate how long it takes to find 100 records in an hash table that only has 100 records in it
    h = CuckooHash(1000)
    file = open("wordlist.txt")
    word = file.readline()

    # insert 100 words from a file
    for i in range(100):
        h.insert(word, 2)
        word = file.readline()
    file.close()
    file = open("wordlist.txt")
    word = file.readline()

    # time how long it takes to find those 100 words
    tm = time.time()
    for i in range(100):
        h.find(word)
    tm = time.time() - tm
    file.close()
    # ----------------------------------------
    # calculate how long it takes to find 100 records in a hashTable with 300 records
    c = CuckooHash(1000)
    file = open("wordlist.txt")
    word = file.readline()

    # insert 300 records from a file
    for i in range(300):
        c.insert(word, 2)
        word = file.readline()
    file.close()

    # Want it to find a different 100 words than the first time,
    # So read some words first
    file = open("wordlist.txt")
    word = file.readline()
    for i in range(150):
        word = file.readline()

    # time how long it takes to find 100 words
    t = time.time()
    for i in range(100):
        c.find(word)
        word = file.readline()
    t = time.time() - t
    file.close()
    # -------------------------------------------------
    # calculate how long it takes to find 100 records in a hashTable with 800 records
    ck = CuckooHash(1000)
    file = open("wordlist.txt")
    word = file.readline()

    # insert 800 words from a file
    for i in range(800):
        ck.insert(word, 2)
        word = file.readline()
    file.close()

    # Want it to find a different 100 words than the other time
    # So read in some words first
    file = open("wordlist.txt")
    word = file.readline()
    for i in range(300):
        word = file.readline()

    # time how long it takes to find 100 words
    s = time.time()
    for i in range(100):
        ck.find(word)
        word = file.readline()
    s = time.time() - s
    file.close()

    # Check if the times for find on the different tables sizes are the same or
    # within a 20 percent acceptable difference
    assert abs(t - s) <= t * 0.2
    assert abs(s - tm) <= s * 0.2
    assert abs(t - tm) <= t * 0.2