rand_test.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import itertools
  2. import unittest
  3. from terroroftinytown.services.base import BaseService
  4. from terroroftinytown.services.rand import HashRandMixin
  5. import collections
  6. class MockService(HashRandMixin, BaseService):
  7. def __init__(self, *args, **kwargs):
  8. self.shortcode_width = kwargs.pop('shortcode_width', 4)
  9. BaseService.__init__(self, *args, **kwargs)
  10. def get_shortcode_width(self):
  11. return self.shortcode_width
  12. class TestRand(unittest.TestCase):
  13. def test_hash_rand_mixin(self):
  14. params = {
  15. 'alphabet': 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',
  16. }
  17. service = MockService(params)
  18. self.assertEqual('TXMa', service.transform_sequence_num(0))
  19. self.assertEqual('lv0q', service.transform_sequence_num(1))
  20. if __name__ == '__main__':
  21. for width in (1, 2, 3, 4, 5, 6, 7, 8):
  22. service = MockService(
  23. {
  24. 'alphabet': 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',
  25. },
  26. shortcode_width=width
  27. )
  28. distributions = [collections.Counter() for dummy in range(width)]
  29. for num in itertools.count():
  30. shortcode = service.transform_sequence_num(num)
  31. for index, char in enumerate(shortcode):
  32. distributions[index][char] += 1
  33. if num > 100000:
  34. break
  35. print(width)
  36. print(distributions)