From e85210320ab27e88e95a24a193bf4de84f234c07 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Sun, 21 Mar 2010 08:39:46 +0100 Subject: [PATCH] tests for unicode method optimisations --- tests/run/unicodemethods.pyx | 187 +++++++++++++++++++++++++++++++++++ 1 file changed, 187 insertions(+) create mode 100644 tests/run/unicodemethods.pyx diff --git a/tests/run/unicodemethods.pyx b/tests/run/unicodemethods.pyx new file mode 100644 index 00000000..4155f81c --- /dev/null +++ b/tests/run/unicodemethods.pyx @@ -0,0 +1,187 @@ +# -*- coding: utf-8 -*- + +cimport cython + +text = u'ab jd sdflk as sa sadas asdas fsdf ' +sep = u' ' + +multiline_text = u'''\ +ab jd +sdflk as sa +sadas asdas fsdf ''' + +def print_all(l): + for s in l: + print(s) + + +# unicode.split(s, [sep, [maxsplit]]) + +def split(unicode s): + """ + >>> print_all( text.split() ) + ab + jd + sdflk + as + sa + sadas + asdas + fsdf + >>> print_all( split(text) ) + ab + jd + sdflk + as + sa + sadas + asdas + fsdf + """ + return s.split() + +def split_sep(unicode s, sep): + """ + >>> print_all( text.split(sep) ) + ab jd + sdflk as sa + sadas asdas fsdf + >>> print_all( split_sep(text, sep) ) + ab jd + sdflk as sa + sadas asdas fsdf + """ + return s.split(sep) + +@cython.test_fail_if_path_exists( + "//CoerceToPyTypeNode", + "//CastNode", "//TypecastNode") +@cython.test_assert_path_exists( + "//CoerceFromPyTypeNode") +def split_sep_max(unicode s, sep, max): + """ + >>> print_all( text.split(sep, 1) ) + ab jd + sdflk as sa sadas asdas fsdf + >>> print_all( split_sep_max(text, sep, 1) ) + ab jd + sdflk as sa sadas asdas fsdf + """ + return s.split(sep, max) + +@cython.test_fail_if_path_exists( + "//CoerceToPyTypeNode", "//CoerceFromPyTypeNode", + "//CastNode", "//TypecastNode") +def split_sep_max_int(unicode s, sep): + """ + >>> print_all( text.split(sep, 1) ) + ab jd + sdflk as sa sadas asdas fsdf + >>> print_all( split_sep_max_int(text, sep) ) + ab jd + sdflk as sa sadas asdas fsdf + """ + return s.split(sep, 1) + + +# unicode.splitlines(s, [keepends]) + +def splitlines(unicode s): + """ + >>> len(multiline_text.splitlines()) + 3 + >>> print_all( multiline_text.splitlines() ) + ab jd + sdflk as sa + sadas asdas fsdf + >>> len(splitlines(multiline_text)) + 3 + >>> print_all( splitlines(multiline_text) ) + ab jd + sdflk as sa + sadas asdas fsdf + """ + return s.splitlines() + +def splitlines_keep(unicode s, keep): + """ + >>> len(multiline_text.splitlines(True)) + 3 + >>> print_all( multiline_text.splitlines(True) ) + ab jd + + sdflk as sa + + sadas asdas fsdf + >>> len(splitlines_keep(multiline_text, True)) + 3 + >>> print_all( splitlines_keep(multiline_text, True) ) + ab jd + + sdflk as sa + + sadas asdas fsdf + """ + return s.splitlines(keep) + +@cython.test_fail_if_path_exists( +# boolean conversion isn't currently smart enough for this ... +# "//CoerceToPyTypeNode", "//CoerceFromPyTypeNode", + "//CastNode", "//TypecastNode") +def splitlines_keep_bint(unicode s): + """ + >>> len(multiline_text.splitlines(True)) + 3 + >>> print_all( multiline_text.splitlines(True) ) + ab jd + + sdflk as sa + + sadas asdas fsdf + >>> print_all( multiline_text.splitlines(False) ) + ab jd + sdflk as sa + sadas asdas fsdf + >>> len(splitlines_keep_bint(multiline_text)) + 7 + >>> print_all( splitlines_keep_bint(multiline_text) ) + ab jd + + sdflk as sa + + sadas asdas fsdf + -- + ab jd + sdflk as sa + sadas asdas fsdf + """ + return s.splitlines(True) + ['--'] + s.splitlines(False) + + +# unicode.join(s, iterable) + +pipe_sep = u'|' + +def join(unicode sep, l): + """ + >>> l = text.split() + >>> len(l) + 8 + >>> print( pipe_sep.join(l) ) + ab|jd|sdflk|as|sa|sadas|asdas|fsdf + >>> print( join(pipe_sep, l) ) + ab|jd|sdflk|as|sa|sadas|asdas|fsdf + """ + return sep.join(l) + +def join_sep(l): + """ + >>> l = text.split() + >>> len(l) + 8 + >>> print( '|'.join(l) ) + ab|jd|sdflk|as|sa|sadas|asdas|fsdf + >>> print( join_sep(l) ) + ab|jd|sdflk|as|sa|sadas|asdas|fsdf + """ + return u'|'.join(l) -- 2.26.2