feat: finish robustness testing

This commit is contained in:
2023-11-01 19:03:50 +08:00
parent ec04b327e9
commit ddde29da3a
2 changed files with 30 additions and 16 deletions

View File

@ -726,15 +726,15 @@ inline void UnsignedDivide(int2048 &A, const int2048 *pB) {
A = std::move(res_hat); A = std::move(res_hat);
} }
int2048 &int2048::Divide(const int2048 &B) { 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) { if (B.num_length == 1 && B.val[0] == 0) {
*this = std::move(int2048(0)); *this = std::move(int2048(0));
return *this; return *this;
// throw "Divide: divide by zero"; // throw "Divide: divide by zero";
} }
if (this == &B) {
*this = std::move(int2048(1));
return *this;
}
int2048 origin_A(*this); int2048 origin_A(*this);
int flag_store = this->flag * B.flag; int flag_store = this->flag * B.flag;
UnsignedDivide(*this, &B); UnsignedDivide(*this, &B);

View File

@ -36,20 +36,21 @@ opt_python=[]
if True: if True:
for i in range(0,10): for i in range(0,10):
val=randint(-10**999,10**999) val=randint(-10**18,10**18)
if i==0: if randint(0,1)==0:
val=randint(-10**999,10**999) val=randint(-10**2,10**2)
opt_cpp.append("a_"+str(i)+"=int2048(\""+str(val)+"\");") opt_cpp.append("a_"+str(i)+"=int2048(\""+str(val)+"\");")
opt_python.append("a_"+str(i)+"="+str(val)) opt_python.append("a_"+str(i)+"="+str(val))
opt_cpp.append("a_"+str(i)+".print(); puts(\"\");") opt_cpp.append("a_"+str(i)+".print(); puts(\"\");")
opt_python.append("print(a_"+str(i)+")") opt_python.append("print(a_"+str(i)+")")
if True: if True:
for i in range(1): for i in range(10):
aid=randint(0,9) aid=randint(0,9)
bid=randint(0,0) bid=randint(0,9)
cid=randint(1,9) cid=randint(0,9)
op='/' oplist=['+','-','*','/']
op=oplist[randint(0,3)]
bflag="+" bflag="+"
if randint(0,1)==0: if randint(0,1)==0:
bflag="-" bflag="-"
@ -57,7 +58,13 @@ if True:
if randint(0,1)==0: if randint(0,1)==0:
cflag="-" cflag="-"
opt_cpp.append("a_"+str(aid)+"=("+bflag+"a_"+str(bid)+")"+op+"("+cflag+"a_"+str(cid)+");") opt_cpp.append("a_"+str(aid)+"=("+bflag+"a_"+str(bid)+")"+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(" 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_cpp.append("a_"+str(aid)+".print(); puts(\"\");")
opt_python.append("print(a_"+str(aid)+")") opt_python.append("print(a_"+str(aid)+")")
opt_cpp.append("a_"+str(bid)+".print(); puts(\"\");") opt_cpp.append("a_"+str(bid)+".print(); puts(\"\");")
@ -65,13 +72,20 @@ if True:
opt_cpp.append("a_"+str(cid)+".print(); puts(\"\");") opt_cpp.append("a_"+str(cid)+".print(); puts(\"\");")
opt_python.append("print(a_"+str(cid)+")") opt_python.append("print(a_"+str(cid)+")")
if False: if True:
for i in range(10): for i in range(10):
aid=randint(0,9) aid=randint(0,9)
bid=randint(0,9) bid=randint(0,9)
op='/=' oplist=['+','-','*','/']
opt_cpp.append("a_"+str(aid)+op+"a_"+str(bid)+";") op=oplist[randint(0,3)]
opt_python.append("a_"+str(aid)+op+"a_"+str(bid)) 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_cpp.append("a_"+str(aid)+".print(); puts(\"\");")
opt_python.append("print(a_"+str(aid)+")") opt_python.append("print(a_"+str(aid)+")")