diff options
author | hut <hut@lavabit.com> | 2010-05-14 17:27:04 +0200 |
---|---|---|
committer | hut <hut@lavabit.com> | 2010-05-14 17:27:04 +0200 |
commit | 420d3d1af8d2991ae6a8cbe061adaab878c4b3ff (patch) | |
tree | ad1cdc33cc12984c141618439b646424d8a76c25 /test | |
parent | 792d6b23711c428a4a2b98537f44e2fbaed97fdd (diff) | |
parent | 2f3326a41430364cb34a227d311691cada605327 (diff) | |
download | ranger-420d3d1af8d2991ae6a8cbe061adaab878c4b3ff.tar.gz |
Merge branch 'master' into cp
Conflicts: ranger/__main__.py
Diffstat (limited to 'test')
-rw-r--r-- | test/__init__.py | 10 | ||||
-rw-r--r-- | test/bm_loader.py | 167 | ||||
-rw-r--r-- | test/tc_directory.py | 7 | ||||
-rw-r--r-- | test/tc_displayable.py | 3 | ||||
-rw-r--r-- | test/tc_loader.py | 138 | ||||
-rw-r--r-- | test/tc_newkeys.py | 17 | ||||
-rw-r--r-- | test/test.py | 4 |
7 files changed, 213 insertions, 133 deletions
diff --git a/test/__init__.py b/test/__init__.py index c2f9bde2..d87d1fc2 100644 --- a/test/__init__.py +++ b/test/__init__.py @@ -17,7 +17,15 @@ import os, sys __all__ = [ x[0:x.index('.')] \ for x in os.listdir(os.path.dirname(__file__)) \ - if x.startswith('tc_') ] + if x.startswith('tc_') or x.startswith('bm_')] + +def TODO(fnc): + def result(*arg, **kw): + try: + fnc(*arg, **kw) + except: + pass # failure expected + return result def init(): sys.path.append(os.path.abspath(os.path.join(sys.path[0], '..'))) diff --git a/test/bm_loader.py b/test/bm_loader.py new file mode 100644 index 00000000..4bfc2db9 --- /dev/null +++ b/test/bm_loader.py @@ -0,0 +1,167 @@ +# Copyright (C) 2009, 2010 Roman Zimbelmann <romanz@lavabit.com> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +from ranger.core.loader import Loader +from ranger.fsobject import Directory, File +from ranger.ext.openstruct import OpenStruct +import os.path +from ranger.shared import FileManagerAware, SettingsAware +from test import Fake +from os.path import realpath, join, dirname +from subprocess import Popen, PIPE +TESTDIR = realpath(join(dirname(__file__), '/usr/include')) + +def skip(x): + return + +def raw_load_content(self): + """ + The method which is used in a Directory object to load stuff. + Keep this up to date! + """ + + from os.path import join, isdir, basename + from os import listdir + import ranger.ext.mount_path + + self.loading = True + self.load_if_outdated() + + try: + if self.exists and self.runnable: + # 0.003s: + self.mount_path = ranger.ext.mount_path.mount_path(self.path) + + # 0.1s: + filenames = [] + for fname in listdir(self.path): + if not self.settings.show_hidden: + hfilter = self.settings.hidden_filter + if hfilter: + if isinstance(hfilter, str) and hfilter in fname: + continue + if hasattr(hfilter, 'search') and \ + hfilter.search(fname): + continue + if isinstance(self.filter, str) and self.filter \ + and self.filter not in fname: + continue + filenames.append(join(self.path, fname)) + # --- + + self.load_content_mtime = os.stat(self.path).st_mtime + + marked_paths = [obj.path for obj in self.marked_items] + + # 2.85s: + files = [] + for name in filenames: + if isdir(name): + try: + item = self.fm.env.get_directory(name) + except: + item = Directory(name) + else: + item = File(name) + item.load_if_outdated() + files.append(item) + + # 0.2s + self.disk_usage = sum(f.size for f in files if f.is_file) + + self.scroll_offset = 0 + self.filenames = filenames + self.files = files + + self._clear_marked_items() + for item in self.files: + if item.path in marked_paths: + self.mark_item(item, True) + else: + self.mark_item(item, False) + + self.sort() + + if len(self.files) > 0: + if self.pointed_obj is not None: + self.sync_index() + else: + self.move(to=0) + else: + self.filenames = None + self.files = None + + self.cycle_list = None + self.content_loaded = True + self.determine_infostring() + self.correct_pointer() + + finally: + self.loading = False + + +class benchmark_load(object): + def __init__(self): + self.loader = Loader() + fm = OpenStruct(loader=self.loader) + SettingsAware.settings = Fake() + FileManagerAware.fm = fm + self.dir = Directory(TESTDIR) + + def bm_run(self, n): + for _ in range(n): + self.dir.load_content(schedule=True) + while self.loader.has_work(): + self.loader.work() + + +class benchmark_raw_load(object): + def __init__(self): + SettingsAware.settings = Fake() + self.dir = Directory(TESTDIR) + + def bm_run(self, n): + generator = self.dir.load_bit_by_bit() + for _ in range(n): + raw_load_content(self.dir) + +def bm_loader(n): + """Do some random calculation""" + tloader = benchmark_load(N) + traw = benchmark_raw_load(N) + +class benchmark_load_varieties(object): + def bm_ls(self, n): + for _ in range(n): + Popen(["ls", '-l', TESTDIR], stdout=open(os.devnull, 'w')).wait() + + def bm_os_listdir_stat(self, n): + for _ in range(n): + for f in os.listdir(TESTDIR): + path = os.path.join(TESTDIR, f) + os.stat(path) + + def bm_os_listdir(self, n): + for _ in range(n): + for f in os.listdir(TESTDIR): + path = os.path.join(TESTDIR, f) + + def bm_os_listdir_stat_listdir(self, n): + for _ in range(n): + for f in os.listdir(TESTDIR): + path = os.path.join(TESTDIR, f) + os.stat(path) + if os.path.isdir(path): + os.listdir(path) diff --git a/test/tc_directory.py b/test/tc_directory.py index f1b204c3..024ebc9d 100644 --- a/test/tc_directory.py +++ b/test/tc_directory.py @@ -15,6 +15,7 @@ if __name__ == '__main__': from __init__ import init; init() +import sys from os.path import realpath, join, dirname from ranger import fsobject @@ -38,7 +39,8 @@ class Test1(unittest.TestCase): self.assertFalse(dir.content_loaded) self.assertEqual(dir.filenames, None) self.assertEqual(dir.files, None) - self.assertRaises(fsobject.NotLoadedYet, len, dir) + if not sys.flags.optimize: # asserts are ignored with python -O + self.assertRaises(AssertionError, len, dir) def test_after_content_loaded(self): import os @@ -79,7 +81,8 @@ class Test1(unittest.TestCase): self.assertFalse(dir.exists) self.assertFalse(dir.accessible) self.assertEqual(dir.filenames, None) - self.assertRaises(fsobject.NotLoadedYet, len, dir) + if not sys.flags.optimize: # asserts are ignored with python -O + self.assertRaises(AssertionError, len, dir) def test_load_if_outdated(self): import os diff --git a/test/tc_displayable.py b/test/tc_displayable.py index 558a20ff..50f37845 100644 --- a/test/tc_displayable.py +++ b/test/tc_displayable.py @@ -20,7 +20,7 @@ import curses from random import randint from ranger.gui.displayable import Displayable, DisplayableContainer -from test import Fake, OK, raise_ok +from test import Fake, OK, raise_ok, TODO class TestWithFakeCurses(unittest.TestCase): def setUp(self): @@ -104,6 +104,7 @@ class TestDisplayableWithCurses(unittest.TestCase): curses.echo() curses.endwin() + @TODO def test_boundaries(self): disp = self.disp hei, wid = self.env.termsize diff --git a/test/tc_loader.py b/test/tc_loader.py index 22f866ec..53ac5617 100644 --- a/test/tc_loader.py +++ b/test/tc_loader.py @@ -18,7 +18,6 @@ if __name__ == '__main__': from __init__ import init; init() import unittest import os from os.path import realpath, join, dirname -from time import time from test import Fake from ranger.shared import FileManagerAware, SettingsAware @@ -29,115 +28,6 @@ from ranger.ext.openstruct import OpenStruct TESTDIR = realpath(join(dirname(__file__), 'testdir')) #TESTDIR = "/usr/sbin" -def raw_load_content(self): - """ - The method which is used in a Directory object to load stuff. - Keep this up to date! - """ - - from os.path import join, isdir, basename - from os import listdir - import ranger.ext.mount_path - - self.loading = True - self.load_if_outdated() - - try: - if self.exists and self.runnable: - self.mount_path = ranger.ext.mount_path.mount_path(self.path) - - filenames = [] - for fname in listdir(self.path): - if not self.settings.show_hidden: - hfilter = self.settings.hidden_filter - if hfilter: - if isinstance(hfilter, str) and hfilter in fname: - continue - if hasattr(hfilter, 'search') and \ - hfilter.search(fname): - continue - if isinstance(self.filter, str) and self.filter \ - and self.filter not in fname: - continue - filenames.append(join(self.path, fname)) - - self.load_content_mtime = os.stat(self.path).st_mtime - - marked_paths = [obj.path for obj in self.marked_items] - - files = [] - for name in filenames: - if isdir(name): - try: - item = self.fm.env.get_directory(name) - except: - item = Directory(name) - else: - item = File(name) - item.load_if_outdated() - files.append(item) - - self.disk_usage = sum(f.size for f in files if f.is_file) - - self.scroll_offset = 0 - self.filenames = filenames - self.files = files - - self._clear_marked_items() - for item in self.files: - if item.path in marked_paths: - self.mark_item(item, True) - else: - self.mark_item(item, False) - - self.sort() - - if len(self.files) > 0: - if self.pointed_obj is not None: - self.sync_index() - else: - self.move(to=0) - else: - self.filenames = None - self.files = None - - self.cycle_list = None - self.content_loaded = True - self.determine_infostring() - self.last_update_time = time() - self.correct_pointer() - - finally: - self.loading = False - - -def benchmark_load(n): - loader = Loader() - fm = OpenStruct(loader=loader) - SettingsAware.settings = Fake() - FileManagerAware.fm = fm - dir = Directory(TESTDIR) - - t1 = time() - for _ in range(n): - dir.load_content(schedule=True) - while loader.has_work(): - loader.work() - t2 = time() - return t2 - t1 - - -def benchmark_raw_load(n): - SettingsAware.settings = Fake() - dir = Directory(TESTDIR) - generator = dir.load_bit_by_bit() - t1 = time() - for _ in range(n): - raw_load_content(dir) - t2 = time() - return t2 - t1 - - class Test1(unittest.TestCase): def test_loader(self): loader = Loader() @@ -165,20 +55,20 @@ class Test1(unittest.TestCase): #print(iterations) self.assertNotEqual(None, dir.files) self.assertFalse(loader.has_work()) - - def test_get_overhead_of_loader(self): - N = 5 - tloader = benchmark_load(N) - traw = benchmark_raw_load(N) - #traw1k = 250.0 - #traw = traw1k * N / 1000.0 - #print("Loader: {0}s".format(tloader)) - #print("Raw: {0}s".format(traw)) - self.assertTrue(tloader > traw) - overhead = tloader * 100 / traw - 100 - self.assertTrue(overhead < 2, "overhead of loader too high: {0}" \ - .format(overhead)) - #print("Overhead: {0:.5}%".format(overhead)) +# +# def test_get_overhead_of_loader(self): +# N = 5 +# tloader = benchmark_load(N) +# traw = benchmark_raw_load(N) +# #traw1k = 250.0 +# #traw = traw1k * N / 1000.0 +# #print("Loader: {0}s".format(tloader)) +# #print("Raw: {0}s".format(traw)) +# self.assertTrue(tloader > traw) +# overhead = tloader * 100 / traw - 100 +# self.assertTrue(overhead < 2, "overhead of loader too high: {0}" \ +# .format(overhead)) +# #print("Overhead: {0:.5}%".format(overhead)) if __name__ == '__main__': diff --git a/test/tc_newkeys.py b/test/tc_newkeys.py index 8efb707d..c7a33025 100644 --- a/test/tc_newkeys.py +++ b/test/tc_newkeys.py @@ -17,6 +17,7 @@ if __name__ == '__main__': from __init__ import init; init() from unittest import TestCase, main +from test import TODO from ranger.ext.tree import Tree from ranger.container.keymap import * from ranger.container.keybuffer import KeyBuffer @@ -423,7 +424,7 @@ class Test(PressTestCase): self.assertPressFails(kb, 'xzy') self.assertPressIncomplete(kb, 'xx') self.assertPressIncomplete(kb, 'x') - if not sys.flags.optimize: + if not sys.flags.optimize: # asserts are ignored with python -O self.assertRaises(AssertionError, simulate_press, kb, 'xxx') kb.clear() @@ -587,8 +588,18 @@ class Test(PressTestCase): self.assertEqual(5, press('gh')) self.assertEqual(5, press('agh')) # self.assertPressFails(kb, 'agh') - # TODO: Make the next line work! For now, skip it. - # self.assertEqual(1, press('agg')) + + @TODO + def test_map_collision2(self): + directions = KeyMap() + directions.map('gg', dir=Direction(down=1)) + km = KeyMap() + km.map('agh', lambda _: 1) + km.map('a<dir>', lambda _: 2) + kb = KeyBuffer(km, directions) + press = self._mkpress(kb, km) + self.assertEqual(1, press('agh')) + self.assertEqual(2, press('agg')) def test_keymap_with_dir(self): def func(arg): diff --git a/test/test.py b/test/test.py index 5e8a9b9e..d0a69e5a 100644 --- a/test/test.py +++ b/test/test.py @@ -15,6 +15,6 @@ """Workaround to allow running single test cases directly""" try: - from __init__ import init, Fake, OK, raise_ok + from __init__ import init, Fake, OK, raise_ok, TODO except: - from test import init, Fake, OK, raise_ok + from test import init, Fake, OK, raise_ok, TODO |