联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp

您当前位置:首页 >> Python编程Python编程

日期:2018-08-09 01:47

import unittest

import sys

from utils import word_list_from_file

from stats import StatCounter

from classes_2 import CounterLinkedList

from word_counter_hash_table import word_counter_hash, hash_table_as_str



def hash_table_slots_list(counts, slots):

   slot_list = []

   for i in range(slots):

       slot_list.append((str(i) + ": " + str(counts[i])))

   return slot_list




DATA_FOLDER = './data/'

real_comparisons = StatCounter.get_comparisons




class BaseTester(unittest.TestCase):


   def setUp(self):

       """This runs before each test case"""

       StatCounter.reset_comparisons()



class TestHashTrivial(BaseTester):


   def test_01_two_word_list_2_slots(self):

       slots = 2

       words = ["first", "test"]

       counts, comparisons = word_counter_hash(words, slots)

       expected_comp_slots = ["0: ['test': 1]", "1: ['first': 1]"]

       comp_slot_list = hash_table_slots_list(counts, slots)

       self.assertListEqual(comp_slot_list, expected_comp_slots)


   def test_02_two_word_count_2_slots(self):

       slots = 2

       words = ["first", "test"]

       counts, comparisons = word_counter_hash(words, slots)

       self.assertEqual(comparisons, 0)



class TestHashSmall(BaseTester):


   def test_03_two_word_list_10_slots(self):

       slots = 10

       words = ["second", "test"]

       counts, comparisons = word_counter_hash(words, slots)

       expected_comp_slots = ["0: None",

                              "1: None",

                              "2: ['test': 1]",

                              "3: None",

                              "4: ['second': 1]",

                              "5: None",

                              "6: None",

                              "7: None",

                              "8: None",

                              "9: None"]

       comp_slot_list = hash_table_slots_list(counts, slots)

       self.assertListEqual(comp_slot_list, expected_comp_slots)


   def test_04_two_word_count_10_slots(self):

       slots = 10

       words = ["second", "test"]

       counts, comparisons = word_counter_hash(words, slots)

       self.assertEqual(comparisons, 0)


   def test_05_no_repeats_list(self):

       slots = 3

       words = ["list", "with", "no", "repeat", "words"]

       counts, comparisons = word_counter_hash(words, slots)

       expected_comp_slots = ["0: ['words': 1 -> 'no': 1 -> 'list': 1]",

                              "1: ['repeat': 1]",

                              "2: ['with': 1]"]

       comp_slot_list = hash_table_slots_list(counts, slots)

       self.assertListEqual(comp_slot_list, expected_comp_slots)


   def test_06_no_repeats_count(self):

       slots = 3

       words = ["list", "with", "no", "repeat", "words"]

       counts, comparisons = word_counter_hash(words, slots)

       self.assertEqual(comparisons, 3)



class TestHashLonger(BaseTester):


   def test_07_longer_list(self):

       slots = 50

       words = ['in', 'mathematics', 'and', 'computer', 'science', 'an',

                'algorithm', 'is', 'a', 'step', 'by', 'step', 'procedure',

                'for', 'calculations', 'computer', 'science', 'is', 'the',

                'scientific', 'and', 'practical', 'approach', 'to', 'computation',

                'and', 'its', 'applications', 'in', 'computer', 'science', 'a',

                'data', 'structure', 'is', 'a', 'particular', 'way', 'of',

                'organizing', 'data', 'in', 'a', 'computer', 'so', 'that', 'it',

                'can', 'be', 'used', 'efficiently']

       counts, comparisons = word_counter_hash(words, slots)

       expected_comp_slots = ["0: None",

                              "1: None",

                              "2: ['a': 4 -> 'algorithm': 1]",

                              "3: None",

                              "4: None",

                              "5: ['of': 1]",

                              "6: ['way': 1]",

                              "7: ['used': 1 -> 'its': 1]",

                              "8: ['practical': 1]",

                              "9: None",

                              "10: ['the': 1 -> 'is': 3]",

                              "11: ['can': 1]",

                              "12: None",

                              "13: ['efficiently': 1 -> 'it': 1]",

                              "14: ['so': 1]",

                              "15: ['science': 3]",

                               "16: None",

                               "17: ['mathematics': 1]",

                               "18: None",

                               "19: ['scientific': 1 -> 'an': 1]",

                               "20: None",

                               "21: ['computer': 4]",

                               "22: None",

                               "23: ['in': 3]",

                               "24: None",

                               "25: ['by': 1]",

                               "26: ['computation': 1 -> 'step': 2]",

                               "27: None",

                               "28: ['procedure': 1 -> 'and': 3]",

                               "29: None",

                               "30: ['calculations': 1]",

                               "31: None",

                               "32: None",

                               "33: ['particular': 1]",

                               "34: None",

                               "35: None",

                               "36: None",

                               "37: None",

                               "38: ['approach': 1 -> 'for': 1]",

                               "39: None",

                               "40: ['structure': 1 -> 'data': 2]",

                               "41: None",

                               "42: None",

                               "43: ['that': 1 -> 'applications': 1]",

                               "44: None",

                               "45: None",

                               "46: ['organizing': 1]",

                               "47: ['be': 1 -> 'to': 1]",

                               "48: None",

                               "49: None"]

       comp_slot_list = hash_table_slots_list(counts, slots)

       self.assertListEqual(comp_slot_list, expected_comp_slots)


   def test_08_longer_count(self):

       slots = 50

       words = ['in', 'mathematics', 'and', 'computer', 'science', 'an',

                'algorithm', 'is', 'a', 'step', 'by', 'step', 'procedure',

                'for', 'calculations', 'computer', 'science', 'is', 'the',

                'scientific', 'and', 'practical', 'approach', 'to', 'computation',

                'and', 'its', 'applications', 'in', 'computer', 'science', 'a',

                'data', 'structure', 'is', 'a', 'particular', 'way', 'of',

                'organizing', 'data', 'in', 'a', 'computer', 'so', 'that', 'it',

                'can', 'be', 'used', 'efficiently']

       counts, comparisons = word_counter_hash(words, slots)

       self.assertEqual(comparisons, 31)



class TestHashWonderLand(BaseTester):


   def test_09_wonderland_table(self):

       table_size = 1009

       filename = 'wonderland.txt'

       filename_of_expected = DATA_FOLDER + 'Expected_hash_' + filename

       words = word_list_from_file(DATA_FOLDER + filename)

       result_table, comparisons = word_counter_hash(words, table_size)

       result_str = hash_table_as_str(result_table)

       with open(filename_of_expected, encoding='utf8') as expected_words_file:

           expected_str = expected_words_file.read()

       self.assertEqual(result_str, expected_str)


   def test_10_wonderland_counts(self):

       table_size = 1009

       filename = 'wonderland.txt'

       filename_of_expected = DATA_FOLDER + 'Expected_hash_' + filename

       words = word_list_from_file(DATA_FOLDER + filename)

       result_table, comparisons = word_counter_hash(words, table_size)

       self.assertEqual(comparisons, 60171)

       self.assertEqual(comparisons, real_comparisons())




def all_tests_suite():

   suite = unittest.TestSuite()

   suite.addTest(unittest.makeSuite(TestHashTrivial))

   suite.addTest(unittest.makeSuite(TestHashSmall))

   suite.addTest(unittest.makeSuite(TestHashLonger))

   suite.addTest(unittest.makeSuite(TestHashWonderLand))

   return suite



def main():

   test_runner = unittest.TextTestRunner(verbosity=2)

   test_runner.run(all_tests_suite())



if __name__ == '__main__':

   main()



版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp