summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjan.nijtmans <nijtmans@users.sourceforge.net>2016-11-17 11:38:13 (GMT)
committerjan.nijtmans <nijtmans@users.sourceforge.net>2016-11-17 11:38:13 (GMT)
commitcbb0665d7d54dbcecf538a4c5564c6f233294a11 (patch)
treea1f61c9de56c4a7809d5e015d43d517db3066100
parent0804f008f2a4ae9667481215fae5a901a6466b94 (diff)
downloadtcl-cbb0665d7d54dbcecf538a4c5564c6f233294a11.zip
tcl-cbb0665d7d54dbcecf538a4c5564c6f233294a11.tar.gz
tcl-cbb0665d7d54dbcecf538a4c5564c6f233294a11.tar.bz2
Add test-case for mp_iseven(). If mp_iseven(0) ever returns 0, we will be warned that we are using the wrong mp_iseven() function.
-rw-r--r--generic/tclTestObj.c25
-rw-r--r--generic/tclTomMath.h2
-rw-r--r--tests/obj.test7
3 files changed, 31 insertions, 3 deletions
diff --git a/generic/tclTestObj.c b/generic/tclTestObj.c
index 6053ae3..1cbe67a 100644
--- a/generic/tclTestObj.c
+++ b/generic/tclTestObj.c
@@ -152,10 +152,10 @@ TestbignumobjCmd(
Tcl_Obj *const objv[]) /* Argument vector */
{
const char *const subcmds[] = {
- "set", "get", "mult10", "div10", NULL
+ "set", "get", "mult10", "div10", "iseven", NULL
};
enum options {
- BIGNUM_SET, BIGNUM_GET, BIGNUM_MULT10, BIGNUM_DIV10
+ BIGNUM_SET, BIGNUM_GET, BIGNUM_MULT10, BIGNUM_DIV10, BIGNUM_ISEVEN
};
int index, varIndex;
const char *string;
@@ -274,6 +274,27 @@ TestbignumobjCmd(
} else {
SetVarToObj(varPtr, varIndex, Tcl_NewBignumObj(&newValue));
}
+ break;
+
+ case BIGNUM_ISEVEN:
+ if (objc != 3) {
+ Tcl_WrongNumArgs(interp, 2, objv, "varIndex");
+ return TCL_ERROR;
+ }
+ if (CheckIfVarUnset(interp, varPtr,varIndex)) {
+ return TCL_ERROR;
+ }
+ if (Tcl_GetBignumFromObj(interp, varPtr[varIndex],
+ &bignumValue) != TCL_OK) {
+ return TCL_ERROR;
+ }
+ if (!Tcl_IsShared(varPtr[varIndex])) {
+ Tcl_SetIntObj(varPtr[varIndex], mp_iseven(&bignumValue));
+ } else {
+ SetVarToObj(varPtr, varIndex, Tcl_NewIntObj(mp_iseven(&bignumValue)));
+ }
+ mp_clear(&bignumValue);
+ break;
}
Tcl_SetObjResult(interp, varPtr[varIndex]);
diff --git a/generic/tclTomMath.h b/generic/tclTomMath.h
index 6b96d2c..9b899fe 100644
--- a/generic/tclTomMath.h
+++ b/generic/tclTomMath.h
@@ -243,7 +243,7 @@ int mp_init_size(mp_int *a, int size);
/* ---> Basic Manipulations <--- */
#define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
-#define mp_iseven(a) ((((a)->used > 0) && (((a)->dp[0] & 1u) == 0u)) ? MP_YES : MP_NO)
+#define mp_iseven(a) ((((a)->used == 0) || (((a)->dp[0] & 1u) == 0u)) ? MP_YES : MP_NO)
#define mp_isodd(a) ((((a)->used > 0) && (((a)->dp[0] & 1u) == 1u)) ? MP_YES : MP_NO)
#define mp_isneg(a) (((a)->sign != MP_ZPOS) ? MP_YES : MP_NO)
diff --git a/tests/obj.test b/tests/obj.test
index a8d2d20..c354d69 100644
--- a/tests/obj.test
+++ b/tests/obj.test
@@ -625,6 +625,13 @@ test obj-33.7 {integer overflow on input} {
list [string is integer $x] [expr { wide($x) }]
} {0 -4294967296}
+test obj-34.1 {mp_iseven} testobj {
+ set result ""
+ lappend result [testbignumobj set 1 0]
+ lappend result [testbignumobj iseven 1] ;
+ lappend result [testobj type 1]
+} {0 1 int}
+
if {[testConstraint testobj]} {
testobj freeallvars
}