From ddde29da3a98d1b750e423aab9bbdc3cad99897c Mon Sep 17 00:00:00 2001 From: ZhuangYumin Date: Wed, 1 Nov 2023 19:03:50 +0800 Subject: [PATCH] feat: finish robustness testing --- src/int2048.cpp | 8 ++++---- tester/cases/3.py | 38 ++++++++++++++++++++++++++------------ 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/int2048.cpp b/src/int2048.cpp index 80fbfbd..ae86a90 100644 --- a/src/int2048.cpp +++ b/src/int2048.cpp @@ -726,15 +726,15 @@ inline void UnsignedDivide(int2048 &A, const int2048 *pB) { A = std::move(res_hat); } int2048 &int2048::Divide(const int2048 &B) { - if (this == &B) { - *this = std::move(int2048(1)); - return *this; - } if (B.num_length == 1 && B.val[0] == 0) { *this = std::move(int2048(0)); return *this; // throw "Divide: divide by zero"; } + if (this == &B) { + *this = std::move(int2048(1)); + return *this; + } int2048 origin_A(*this); int flag_store = this->flag * B.flag; UnsignedDivide(*this, &B); diff --git a/tester/cases/3.py b/tester/cases/3.py index 9dcd4f3..5faea51 100755 --- a/tester/cases/3.py +++ b/tester/cases/3.py @@ -36,20 +36,21 @@ opt_python=[] if True: for i in range(0,10): - val=randint(-10**999,10**999) - if i==0: - val=randint(-10**999,10**999) + val=randint(-10**18,10**18) + if randint(0,1)==0: + val=randint(-10**2,10**2) opt_cpp.append("a_"+str(i)+"=int2048(\""+str(val)+"\");") opt_python.append("a_"+str(i)+"="+str(val)) opt_cpp.append("a_"+str(i)+".print(); puts(\"\");") opt_python.append("print(a_"+str(i)+")") if True: - for i in range(1): + for i in range(10): aid=randint(0,9) - bid=randint(0,0) - cid=randint(1,9) - op='/' + bid=randint(0,9) + cid=randint(0,9) + oplist=['+','-','*','/'] + op=oplist[randint(0,3)] bflag="+" if randint(0,1)==0: bflag="-" @@ -57,7 +58,13 @@ if True: if randint(0,1)==0: cflag="-" opt_cpp.append("a_"+str(aid)+"=("+bflag+"a_"+str(bid)+")"+op+"("+cflag+"a_"+str(cid)+");") - opt_python.append("a_"+str(aid)+"=("+bflag+"a_"+str(bid)+")"+op+op+"("+cflag+"a_"+str(cid)+")") + if op=='/': + opt_python.append("try:") + opt_python.append(" a_"+str(aid)+"=("+bflag+"a_"+str(bid)+")"+op+op+"("+cflag+"a_"+str(cid)+")") + opt_python.append("except ZeroDivisionError:") + opt_python.append(" a_"+str(aid)+"=0") + else: + opt_python.append("a_"+str(aid)+"=("+bflag+"a_"+str(bid)+")"+op+"("+cflag+"a_"+str(cid)+")") opt_cpp.append("a_"+str(aid)+".print(); puts(\"\");") opt_python.append("print(a_"+str(aid)+")") opt_cpp.append("a_"+str(bid)+".print(); puts(\"\");") @@ -65,13 +72,20 @@ if True: opt_cpp.append("a_"+str(cid)+".print(); puts(\"\");") opt_python.append("print(a_"+str(cid)+")") -if False: +if True: for i in range(10): aid=randint(0,9) bid=randint(0,9) - op='/=' - opt_cpp.append("a_"+str(aid)+op+"a_"+str(bid)+";") - opt_python.append("a_"+str(aid)+op+"a_"+str(bid)) + oplist=['+','-','*','/'] + op=oplist[randint(0,3)] + opt_cpp.append("a_"+str(aid)+op+"=a_"+str(bid)+";") + if op=='/': + opt_python.append("try:") + opt_python.append(" a_"+str(aid)+op+op+"=a_"+str(bid)) + opt_python.append("except ZeroDivisionError:") + opt_python.append(" a_"+str(aid)+"=0") + else: + opt_python.append("a_"+str(aid)+op+"=a_"+str(bid)) opt_cpp.append("a_"+str(aid)+".print(); puts(\"\");") opt_python.append("print(a_"+str(aid)+")")