1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 00:37:15 +01:00

Fix assertion functions.

This commit is contained in:
Sandu Liviu Catalin 2021-02-03 21:59:48 +02:00
parent fc0751c6da
commit 70a5eaa5d1
3 changed files with 75 additions and 56 deletions

View File

@ -838,40 +838,6 @@ static SQInteger SqNameFilterCheck(HSQUIRRELVM vm)
return 1;
}
// ------------------------------------------------------------------------------------------------
struct CmpEQ {
static const String FSTR;
inline bool operator() (SQInteger r) const noexcept { return (r == 0); }
};
struct CmpNE {
static const String FSTR;
inline bool operator() (SQInteger r) const noexcept { return (r != 0); }
};
struct CmpLT {
static const String FSTR;
inline bool operator() (SQInteger r) const noexcept { return (r < 0); }
};
struct CmpGT {
static const String FSTR;
inline bool operator() (SQInteger r) const noexcept { return (r > 0); }
};
struct CmpLE {
static const String FSTR;
inline bool operator() (SQInteger r) const noexcept { return (r <= 0); }
};
struct CmpGE {
static const String FSTR;
inline bool operator() (SQInteger r) const noexcept { return (r >= 0); }
};
// ------------------------------------------------------------------------------------------------
const String CmpEQ::FSTR("Assertion passed: {0} == {1}"); // NOLINT(cert-err58-cpp)
const String CmpNE::FSTR("Assertion passed: {0} != {1}"); // NOLINT(cert-err58-cpp)
const String CmpLT::FSTR("Assertion passed: {0} < {1}"); // NOLINT(cert-err58-cpp)
const String CmpGT::FSTR("Assertion passed: {0} > {1}"); // NOLINT(cert-err58-cpp)
const String CmpLE::FSTR("Assertion passed: {0} <= {1}"); // NOLINT(cert-err58-cpp)
const String CmpGE::FSTR("Assertion passed: {0} >= {1}"); // NOLINT(cert-err58-cpp)
// ------------------------------------------------------------------------------------------------
SQMOD_DECL_TYPENAME(SqAssertRes, _SC("SqAssertResult"))
@ -1013,6 +979,55 @@ struct SqAssertResult
// ------------------------------------------------------------------------------------------------
const String SqAssertResult::FSTR("Assertion passed ({0})."); // NOLINT(cert-err58-cpp)
// ------------------------------------------------------------------------------------------------
struct CmpEQ {
static const String FSTR;
static const String FSTRV;
inline bool operator() (SQInteger r) const noexcept { return (r == 0); }
};
struct CmpNE {
static const String FSTR;
static const String FSTRT;
static const String FSTRV;
inline bool operator() (SQInteger r) const noexcept { return (r != 0); }
};
struct CmpLT {
static const String FSTR;
static const String FSTRV;
inline bool operator() (SQInteger r) const noexcept { return (r < 0); }
};
struct CmpGT {
static const String FSTR;
static const String FSTRV;
inline bool operator() (SQInteger r) const noexcept { return (r > 0); }
};
struct CmpLE {
static const String FSTR;
static const String FSTRV;
inline bool operator() (SQInteger r) const noexcept { return (r <= 0); }
};
struct CmpGE {
static const String FSTR;
static const String FSTRV;
inline bool operator() (SQInteger r) const noexcept { return (r >= 0); }
};
// ------------------------------------------------------------------------------------------------
const String CmpEQ::FSTR("Assertion passed: {0} == {1}"); // NOLINT(cert-err58-cpp)
const String CmpNE::FSTR("Assertion passed: {0} != {1}"); // NOLINT(cert-err58-cpp)
const String CmpLT::FSTR("Assertion passed: {0} < {1}"); // NOLINT(cert-err58-cpp)
const String CmpGT::FSTR("Assertion passed: {0} > {1}"); // NOLINT(cert-err58-cpp)
const String CmpLE::FSTR("Assertion passed: {0} <= {1}"); // NOLINT(cert-err58-cpp)
const String CmpGE::FSTR("Assertion passed: {0} >= {1}"); // NOLINT(cert-err58-cpp)
// ------------------------------------------------------------------------------------------------
const String CmpEQ::FSTRV("Assertion failed. Value mismatch: {0} == {1}"); // NOLINT(cert-err58-cpp)
const String CmpNE::FSTRV("Assertion failed. Value mismatch: {0} != {1}"); // NOLINT(cert-err58-cpp)
const String CmpLT::FSTRV("Assertion failed. Value mismatch: {0} < {1}"); // NOLINT(cert-err58-cpp)
const String CmpGT::FSTRV("Assertion failed. Value mismatch: {0} > {1}"); // NOLINT(cert-err58-cpp)
const String CmpLE::FSTRV("Assertion failed. Value mismatch: {0} <= {1}"); // NOLINT(cert-err58-cpp)
const String CmpGE::FSTRV("Assertion failed. Value mismatch: {0} >= {1}"); // NOLINT(cert-err58-cpp)
// ------------------------------------------------------------------------------------------------
static SQInteger SqNameFilterCheckInsensitive(HSQUIRRELVM vm)
{
@ -1097,35 +1112,23 @@ template < class C > static SQInteger SqAssertValue(HSQUIRRELVM vm)
{
return sq_throwerror(vm, "Not enough parameters.");
}
StackStrF a(vm, 2);
StackStrF b(vm, 3);
// Get the first value as string
if (SQ_FAILED(a.Proc(false)))
{
return a.mRes; // Propagate the error
}
// Get the second value as string
if (SQ_FAILED(b.Proc(false)))
{
return b.mRes; // Propagate the error
}
// Create the instance and guard it to make sure it gets deleted in case of exceptions
DeleteGuard< SqAssertResult > ar(new SqAssertResult(vm, top, C::FSTR));
// Compare values
SQInteger r = sq_cmp(vm);
SQInteger r = sq_cmpr(vm);
// Validate result
if (C{}(r) == false)
{
// Throw the error
return sq_throwerrorf(vm, "Assertion failed. Value mismatch: %s != %s", a.mPtr, b.mPtr);
return sq_throwerrorf(vm, fmt::format(C::FSTRV, ar.Get()->mA, ar.Get()->mB).c_str());
}
// Try to return assert result
try
{
// Create the instance and guard it to make sure it gets deleted in case of exceptions
DeleteGuard< SqAssertResult > instance(new SqAssertResult(vm, top, C::FSTR));
// Push the instance on the stack
ClassType< SqAssertResult >::PushInstance(vm, instance);
ClassType< SqAssertResult >::PushInstance(vm, ar);
// Stop guarding the instance
instance.Release();
ar.Release();
// Specify that we returned a value
return 1;
}
@ -1154,15 +1157,23 @@ template < class C > static SQInteger SqAssertSame(HSQUIRRELVM vm)
return sq_throwerrorf(vm, "Assertion failed. Type mismatch: %s != %s",
SqTypeName(vm, 2).c_str(), SqTypeName(vm, 3).c_str());
}
// Create the instance and guard it to make sure it gets deleted in case of exceptions
DeleteGuard< SqAssertResult > ar(new SqAssertResult(vm, top, C::FSTR));
// Compare values
SQInteger r = sq_cmpr(vm);
// Validate result
if (C{}(r) == false)
{
// Throw the error
return sq_throwerrorf(vm, fmt::format(C::FSTRV, ar.Get()->mA, ar.Get()->mB).c_str());
}
// Try to return assert result
try
{
// Create the instance and guard it to make sure it gets deleted in case of exceptions
DeleteGuard< SqAssertResult > instance(new SqAssertResult(vm, top, C::FSTR));
// Push the instance on the stack
ClassType< SqAssertResult >::PushInstance(vm, instance);
ClassType< SqAssertResult >::PushInstance(vm, ar);
// Stop guarding the instance
instance.Release();
ar.Release();
// Specify that we returned a value
return 1;
}

View File

@ -16,6 +16,7 @@ SQUIRREL_API SQRESULT sq_pushstringf(HSQUIRRELVM v,const SQChar *s,...);
SQUIRREL_API SQRESULT sq_vpushstringf(HSQUIRRELVM v,const SQChar *s,va_list l);
SQUIRREL_API SQRESULT sq_getnativeclosurepointer(HSQUIRRELVM v,SQInteger idx,SQFUNCTION *f);
SQUIRREL_API SQRESULT sq_arrayreserve(HSQUIRRELVM v,SQInteger idx,SQInteger newcap);
SQUIRREL_API SQInteger sq_cmpr(HSQUIRRELVM v);
#ifdef __cplusplus
} /*extern "C"*/

View File

@ -119,3 +119,10 @@ SQRESULT sq_arrayreserve(HSQUIRRELVM v,SQInteger idx,SQInteger newcap)
}
return sq_throwerror(v,_SC("negative capacity"));
}
SQInteger sq_cmpr(HSQUIRRELVM v)
{
SQInteger res;
v->ObjCmp(stack_get(v, -2), stack_get(v, -1),res);
return res;
}