summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSeth M Morton <seth.m.morton@gmail.com>2014-07-17 19:32:15 -0700
committerSeth M Morton <seth.m.morton@gmail.com>2014-07-19 00:05:04 -0700
commit65778c9054703de6e6e242998c674181fdb6d671 (patch)
tree40d2e3f410d79a43bfc6f6fcada79161f64ca3e9
parente85e785653ee3a61c842febdd7289869c8f09d74 (diff)
downloadnatsort-65778c9054703de6e6e242998c674181fdb6d671.tar.gz
Added a stress tester.
This stress tester generates random strings and tries to sort them.
-rw-r--r--.travis.yml1
-rw-r--r--test_natsort/stress_natsort.py51
2 files changed, 52 insertions, 0 deletions
diff --git a/.travis.yml b/.travis.yml
index 4d09ad0..f8c1cba 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -14,6 +14,7 @@ script:
- python -m pytest --cov natsort
- python -m pytest --doctest-modules natsort
- python -m pytest README.rst
+- python -m pytest test_natsort/stress_natsort.py
after_success:
coveralls
deploy:
diff --git a/test_natsort/stress_natsort.py b/test_natsort/stress_natsort.py
new file mode 100644
index 0000000..b4394e8
--- /dev/null
+++ b/test_natsort/stress_natsort.py
@@ -0,0 +1,51 @@
+# -*- coding: utf-8 -*-
+"""\
+This file contains functions to stress-test natsort.
+"""
+import sys
+import random
+import string
+import copy
+from pytest import fail
+from natsort import natsorted
+from natsort.py23compat import py23_range
+
+
+def test_random():
+ """Try to sort 1,000,000 randomly generated strings without exception."""
+
+ # Repeat test 1,000,000 times
+ for _ in py23_range(1000000):
+ # Made a list of five randomly generated strings
+ lst = [''.join(random.sample(string.printable, random.randint(7, 30))) for __ in py23_range(5)]
+ # Try to sort. If there is an exception, give some detailed info.
+ try:
+ natsorted(lst)
+ except Exception as e:
+ msg = "Ended with exception type '{exc}: {msg}'.\n"
+ msg += "Failed on the input {lst}."
+ fail(msg.format(exc=type(e).__name__, msg=str(e), lst=str(lst)))
+
+
+def test_similar():
+ """Try to sort 1,000,000 randomly generated similar strings without exception."""
+
+ # Repeat test 1,000,000 times
+ for _ in py23_range(1000000):
+ # Create a randomly generated string
+ base = random.sample(string.printable, random.randint(7, 30))
+ # Make a list of strings based on this string, with some randomly generated modifications
+ lst = []
+ for __ in py23_range(5):
+ new_str = copy.copy(base)
+ for ___ in py23_range(random.randint(1,5)):
+ new_str[random.randint(0,len(base)-1)] = random.choice(string.printable)
+ lst.append(''.join(new_str))
+ # Try to sort. If there is an exception, give some detailed info.
+ try:
+ natsorted(lst)
+ except Exception as e:
+ msg = "Ended with exception type '{exc}: {msg}'.\n"
+ msg += "Failed on the input {lst}."
+ fail(msg.format(exc=type(e).__name__, msg=str(e), lst=str(lst)))
+