From f58626842bbb2bd3f8446044a44a67180b737f4e Mon Sep 17 00:00:00 2001 From: hut Date: Fri, 11 Dec 2009 13:36:48 +0100 Subject: moved/fixed tests --- test/dirsize_benchmark.py | 28 ----------------- test/stuff/dirsize_benchmark.py | 28 +++++++++++++++++ test/stuff/test1.py | 70 +++++++++++++++++++++++++++++++++++++++++ test/stuff/test2.py | 7 +++++ test/stuff/test3.py | 69 ++++++++++++++++++++++++++++++++++++++++ test/stuff/test4.py | 36 +++++++++++++++++++++ test/stuff/test5.py | 26 +++++++++++++++ test/tc_history.py | 2 +- 8 files changed, 237 insertions(+), 29 deletions(-) delete mode 100644 test/dirsize_benchmark.py create mode 100644 test/stuff/dirsize_benchmark.py create mode 100644 test/stuff/test1.py create mode 100644 test/stuff/test2.py create mode 100644 test/stuff/test3.py create mode 100644 test/stuff/test4.py create mode 100644 test/stuff/test5.py (limited to 'test') diff --git a/test/dirsize_benchmark.py b/test/dirsize_benchmark.py deleted file mode 100644 index 5784ee80..00000000 --- a/test/dirsize_benchmark.py +++ /dev/null @@ -1,28 +0,0 @@ -import os, time -class Dirsize(): - def a(path): - return len(os.listdir(path)) - - def b(path): - for _, dirs, files in os.walk(path): - return len(files) + len(dirs) - - def c(path): - first = next(os.walk(path)) - return len(first[1]) + len(first[2]) - -paths = { - '/usr/lib': None, - '/usr/bin': None, - '/home/hut': None -} - -for key in paths.keys(): - paths[key] = Dirsize.a(key) # assume Dirsize.a() returns a correct result - for algo in ['a', 'b', 'c']: - t = time.time() - for i in range(4): - assert Dirsize.__dict__[algo](key) == paths[key] - print("algorithm %s: %20s: %f" % (algo, key, time.time() - t)) - -# a !! diff --git a/test/stuff/dirsize_benchmark.py b/test/stuff/dirsize_benchmark.py new file mode 100644 index 00000000..5784ee80 --- /dev/null +++ b/test/stuff/dirsize_benchmark.py @@ -0,0 +1,28 @@ +import os, time +class Dirsize(): + def a(path): + return len(os.listdir(path)) + + def b(path): + for _, dirs, files in os.walk(path): + return len(files) + len(dirs) + + def c(path): + first = next(os.walk(path)) + return len(first[1]) + len(first[2]) + +paths = { + '/usr/lib': None, + '/usr/bin': None, + '/home/hut': None +} + +for key in paths.keys(): + paths[key] = Dirsize.a(key) # assume Dirsize.a() returns a correct result + for algo in ['a', 'b', 'c']: + t = time.time() + for i in range(4): + assert Dirsize.__dict__[algo](key) == paths[key] + print("algorithm %s: %20s: %f" % (algo, key, time.time() - t)) + +# a !! diff --git a/test/stuff/test1.py b/test/stuff/test1.py new file mode 100644 index 00000000..97505c41 --- /dev/null +++ b/test/stuff/test1.py @@ -0,0 +1,70 @@ +#!/usr/bin/python3 +# coding=utf-8 +# some tests with curses, threads and unicode +import os +import curses +import time +import threading +import locale +import _thread + +lock = _thread.allocate_lock() + +locale.setlocale(locale.LC_ALL, 'en_US.utf8') + +blocked = False +stringy = 'るでか' +stdscr = curses.initscr() + +curses.noecho() +curses.cbreak() +curses.halfdelay(3) +stdscr.keypad(1) +#curses.curs_set(0) + +stdscr.addstr(4, 0, stringy) +stdscr.refresh() + +class ThreadTest(threading.Thread): + def __init__(self, *a, **b): + threading.Thread.__init__(self, *a, **b) + self.killed = False + + def run(self): + global stdscr + global blocked + for i in range(1,50): + while blocked: time.sleep(0.1) + blocked = True + stdscr.addstr(1, 0, str(i)) + stdscr.refresh() + blocked = False + time.sleep(0.1) + if self.killed: raise SystemExit() + + def kill(self): + self.killed = True + +thr = ThreadTest() +thr.start() + +try: + while 1: + c = stdscr.getch() + if c == ord('q'): raise + while blocked: time.sleep(0.1) + blocked = True + stdscr.addstr(0, 0, str(c)) + stdscr.refresh() + blocked = False + +except Exception: + thr.kill() + raise +finally: + curses.nocbreak() + stdscr.keypad(1) + curses.echo() + curses.endwin() +# curses.curs_set(1) + diff --git a/test/stuff/test2.py b/test/stuff/test2.py new file mode 100644 index 00000000..21c5a5f9 --- /dev/null +++ b/test/stuff/test2.py @@ -0,0 +1,7 @@ +from code import cli +import signal + +with cli.lock: + with cli.lock: + print("this will be delayed forever") + print("hi!") diff --git a/test/stuff/test3.py b/test/stuff/test3.py new file mode 100644 index 00000000..3b1986b7 --- /dev/null +++ b/test/stuff/test3.py @@ -0,0 +1,69 @@ +#!/usr/bin/python3 +# coding=utf-8 +# some tests with curses, threads and unicode +import os +import curses +import time +import locale + +lock = _thread.allocate_lock() + +locale.setlocale(locale.LC_ALL, 'en_US.utf8') + +blocked = False +stringy = 'るでか' +stdscr = curses.initscr() +#win1 = curses.newwin( + +curses.noecho() +curses.cbreak() +curses.halfdelay(3) +stdscr.keypad(1) +#curses.curs_set(0) + +stdscr.addstr(4, 0, stringy) +stdscr.refresh() + +class ThreadTest(threading.Thread): + def __init__(self, *a, **b): + threading.Thread.__init__(self, *a, **b) + self.killed = False + + def run(self): + global stdscr + global blocked + for i in range(1,50): + while blocked: time.sleep(0.1) + blocked = True + stdscr.addstr(1, 0, str(i)) + stdscr.refresh() + blocked = False + time.sleep(0.1) + if self.killed: raise SystemExit() + + def kill(self): + self.killed = True + +thr = ThreadTest() +thr.start() + +try: + while 1: + c = stdscr.getch() + if c == ord('q'): raise + while blocked: time.sleep(0.1) + blocked = True + stdscr.addstr(0, 0, str(c)) + stdscr.refresh() + blocked = False + +except Exception: + thr.kill() + raise +finally: + curses.nocbreak() + stdscr.keypad(1) + curses.echo() + curses.endwin() +# curses.curs_set(1) + diff --git a/test/stuff/test4.py b/test/stuff/test4.py new file mode 100644 index 00000000..b2390a03 --- /dev/null +++ b/test/stuff/test4.py @@ -0,0 +1,36 @@ +import random, time + +class DelValue(): + def a(d): + return dict((k, v) for k, v in d.items() if v is not 0) + + def b(d): + for k, v in d.copy().items(): + if v == 0: del d[k] + return d + + def c(d): + for k in tuple(d.keys()): + if d[k] == 0: del d[k] + return d + + def d(d): + for k, v in tuple(d.items()): + if v == 0: del d[k] + return d + + +basedict = {} +for i in range(200): + basedict[i] = random.randint(0, 1) + +expected = DelValue.a(basedict.copy()) + +for algo in ['a', 'b', 'c', 'd']: + copy = basedict.copy() + t = time.time() + for i in range(100): + assert DelValue.__dict__[algo](copy) == expected + print("algorithm %s: %f" % (algo, time.time() - t)) + +# c it is, although b is faster with smaller dictionaries diff --git a/test/stuff/test5.py b/test/stuff/test5.py new file mode 100644 index 00000000..682a716f --- /dev/null +++ b/test/stuff/test5.py @@ -0,0 +1,26 @@ +class A: + def foo(self, x): + y = x + 1 + print(y) + return y + + @staticmethod + def zar(): + print("l o l") + +class B(A): + def bar(self, x): + y = self.foo(x) + 3 + print(y) + return y + +class C(): + def foo(self, x): + y = x - 1 + print(y) + return y + +a = C() +A.foo(a, 5) + +B.zar() diff --git a/test/tc_history.py b/test/tc_history.py index 3e5adcff..e78af0aa 100644 --- a/test/tc_history.py +++ b/test/tc_history.py @@ -3,7 +3,7 @@ if __name__ == '__main__': import sys sys.path.append(abspath(join(sys.path[0], '..'))) -from ranger.history import History +from ranger.container import History from unittest import TestCase, main import unittest -- cgit 1.4.1-2-gfad0