From 32b01d9559c77c395b40b15d7a71e42e9e0002fd Mon Sep 17 00:00:00 2001 From: Haoyu Bai Date: Sun, 28 Sep 2008 09:36:06 +0000 Subject: [PATCH] merge test case pybuf and benchmark pybuf_benchmark into a single test case git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@10884 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- Examples/test-suite/python/pybuf.i | 32 ++++++++++++- Examples/test-suite/python/pybuf_benchmark.i | 31 ------------ .../python/pybuf_benchmark_runme.py | 16 ------- Examples/test-suite/python/pybuf_runme3.py | 47 +++++++++++++++---- 4 files changed, 68 insertions(+), 58 deletions(-) delete mode 100644 Examples/test-suite/python/pybuf_benchmark.i delete mode 100644 Examples/test-suite/python/pybuf_benchmark_runme.py diff --git a/Examples/test-suite/python/pybuf.i b/Examples/test-suite/python/pybuf.i index 207b5b7e9..88dd1aa6a 100644 --- a/Examples/test-suite/python/pybuf.i +++ b/Examples/test-suite/python/pybuf.i @@ -1,6 +1,7 @@ %module pybuf %include - +%include +/*functions for the test case*/ %pybuffer_mutable_binary(char *buf1, int len); %pybuffer_mutable_string(char *buf2); %pybuffer_binary(const char *buf3, int len); @@ -32,3 +33,32 @@ return strlen(buf4); } %} + +/*functions for the benchmark*/ +%pybuffer_mutable_string(char *str1); +%cstring_mutable(char *str2); + +%inline %{ +void title(char *str) { + int outword = 1; + while(*str) { + if (isalnum(*str)) { + if (outword) { + outword = 0; + *str = toupper(*str); + } + } + else { + outword = 0; + } + str++; + } +} + +void title1(char *str1) { + title(str1); +} +void title2(char *str2) { + title(str2); +} +%} diff --git a/Examples/test-suite/python/pybuf_benchmark.i b/Examples/test-suite/python/pybuf_benchmark.i deleted file mode 100644 index 8a793f906..000000000 --- a/Examples/test-suite/python/pybuf_benchmark.i +++ /dev/null @@ -1,31 +0,0 @@ -%module pybuf_benchmark - -%include -%include -%pybuffer_mutable_string(char *str1); -%cstring_mutable(char *str2); - -%inline %{ -void title(char *str) { - int outword = 0; - while(*str) { - if (isalnum(*str)) { - if (outword) { - outword = 1; - *str = toupper(*str); - } - } - else { - outword = 0; - } - str++; - } -} - -void title1(char *str1) { - title(str1); -} -void title2(char *str2) { - title(str2); -} -%} diff --git a/Examples/test-suite/python/pybuf_benchmark_runme.py b/Examples/test-suite/python/pybuf_benchmark_runme.py deleted file mode 100644 index 6676a910b..000000000 --- a/Examples/test-suite/python/pybuf_benchmark_runme.py +++ /dev/null @@ -1,16 +0,0 @@ -import pybuf -import time -k=1000000 -n=7 - -t=time.time() -a = bytearray(b'hello world') -for i in range(k): - pybuf.title1(a) -print "Time used by bytearray:",time.time()-t - -t=time.time() -b = 'hello world' -for i in range(k): - pybuf.title2(b) -print "Time used by string:",time.time()-t diff --git a/Examples/test-suite/python/pybuf_runme3.py b/Examples/test-suite/python/pybuf_runme3.py index 462736bf0..9294c6b72 100644 --- a/Examples/test-suite/python/pybuf_runme3.py +++ b/Examples/test-suite/python/pybuf_runme3.py @@ -1,15 +1,42 @@ +#run: +# python pybuf_runme3.py benchmark +#for the benchmark, other wise the test case will be run import pybuf -buf1 = bytearray(10) -buf2 = bytearray(50) +import sys +if len(sys.argv)>=2 and sys.argv[1]=="benchmark": + #run the benchmark + import time + k=1000000 #number of times to excute the functions -pybuf.func1(buf1) -assert buf1 == b'a'*10 + t=time.time() + a = bytearray(b'hello world') + for i in range(k): + pybuf.title1(a) + print("Time used by bytearray:",time.time()-t) -pybuf.func2(buf2) -assert buf2.startswith(b"Hello world!\x00") + t=time.time() + b = 'hello world' + for i in range(k): + pybuf.title2(b) + print("Time used by string:",time.time()-t) +else: + #run the test case + buf1 = bytearray(10) + buf2 = bytearray(50) -count = pybuf.func3(buf2) -assert count==10 #number of alpha and number in 'Hello world!' + pybuf.func1(buf1) + assert buf1 == b'a'*10 -length = pybuf.func4(buf2) -assert length==12 + pybuf.func2(buf2) + assert buf2.startswith(b"Hello world!\x00") + + count = pybuf.func3(buf2) + assert count==10 #number of alpha and number in 'Hello world!' + + length = pybuf.func4(buf2) + assert length==12 + + buf3 = bytearray(b"hello") + pybuf.title1(buf3) + assert buf3==b'Hello' +