Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andersonpd committed Oct 31, 2012
1 parent e5aaf43 commit cfa595f
Show file tree
Hide file tree
Showing 8 changed files with 787 additions and 658 deletions.
66 changes: 63 additions & 3 deletions decimal/arithmetic.d
Expand Up @@ -347,19 +347,26 @@ public T nextToward(T)(const T arg1, const T arg2,
public int compare(T)(const T arg1, const T arg2,
const DecimalContext context = T.context,
bool roundResult = true) if (isDecimal!T) {

//writeln("compare 1");
// any operation with a signaling NaN is invalid.
// if both are signaling, return as if arg1 > arg2.
if (arg1.isSignaling || arg2.isSignaling) {
contextFlags.setFlags(INVALID_OPERATION);
return arg1.isSignaling ? 1 : -1;
}
//writeln("compare 2");

// NaN returns > any number, including NaN
// if both are NaN, return as if arg1 > arg2.
if (arg1.isNaN || arg2.isNaN) {
return arg1.isNaN ? 1 : -1;
}
//writeln("compare 3");

// // if either is infinite...
// if (arg1.isInfinite || arg2.isInfinite) {
// return (arg1.isInfinite && arg2.isInfinite && arg1.isSigned == arg2.isSigned);
// }

// if signs differ, just compare the signs
if (arg1.sign != arg2.sign) {
Expand All @@ -369,6 +376,7 @@ public int compare(T)(const T arg1, const T arg2,
}
return arg1.sign ? -1 : 1;
}
//writeln("compare 4");

// otherwise, compare the numbers numerically
int diff = (arg1.exponent + arg1.digits) - (arg2.exponent + arg2.digits);
Expand All @@ -380,9 +388,11 @@ public int compare(T)(const T arg1, const T arg2,
if (diff > 0) return -1;
if (diff < 0) return 1;
}
//writeln("compare 5");

// when all else fails, subtract
T result = sub!T(arg1, arg2, context, roundResult);
//writefln("result = %s", result);

// test the coefficient
// result.isZero may not be true if the result hasn't been rounded
Expand All @@ -408,29 +418,35 @@ public bool equals(T)(const T arg1, const T arg2,
//writefln("arg1 = %s", arg1);
//writefln("arg2 = %s", arg2);
// any operation with a signaling NaN is invalid.
//writeln("equals 1");
if (arg1.isSignaling || arg2.isSignaling) {
contextFlags.setFlags(INVALID_OPERATION);
return false;
}
//writeln("equals 2");

// if either is NaN...
// NaN is never equal to any number, not even another NaN
if (arg1.isNaN || arg2.isNaN) return false;

//writeln("equals 3");
// if either is infinite...
if (arg1.isInfinite || arg2.isInfinite) {
return (arg1.isInfinite && arg2.isInfinite && arg1.isSigned == arg2.isSigned);
}

//writeln("equals 4");
// if either is zero...
if (arg1.isZero || arg2.isZero) {
return (arg1.isZero && arg2.isZero);
}

//writeln("equals 5");
// if their signs differ...
if (arg1.sign != arg2.sign) {
return false;
}
//writeln("equals 6");

//writefln("arg1.coefficient = %s", arg1.coefficient);
//writefln("arg1.digits = %s", arg1.digits);
Expand All @@ -443,6 +459,7 @@ public bool equals(T)(const T arg1, const T arg2,
if (diff != 0) {
return false;
}
//writeln("equals 7");

// if they have the same representation, they are equal
auto op1c = arg1.coefficient;
Expand All @@ -453,6 +470,7 @@ public bool equals(T)(const T arg1, const T arg2,
if (arg1.exponent == arg2.exponent && op1c == op2c) { //arg1.coefficient == arg2.coefficient) {
return true;
}
//writeln("equals 8");

// otherwise they are equal if they represent the same value
T result = sub!T(arg1, arg2, context, roundResult);
Expand Down Expand Up @@ -752,7 +770,50 @@ public const (T) quantum(T)(const T arg) if (isDecimal!T) {
}

//--------------------------------
// shift and rotate
// binary shift
//--------------------------------

public T shl(T)(const T arg, const int n,
const DecimalContext context = T.context) if (isDecimal!T) {

T result = T.nan;
if (invalidOperand!T(arg, result)) {
return result;
}
result = arg;
result.coefficient = result.coefficient << n;
result.digits = result.digits + 1;
return round(result, context);
}

public T shr(T)(const T arg, const int n,
const DecimalContext context = T.context) if (isDecimal!T) {

T result = T.nan;
if (invalidOperand!T(arg, result)) {
return result;
}
result = arg;
result.coefficient = result.coefficient >> n;
result.digits = result.digits - 1;
return round(result, context);
}

unittest {
write("shr, shl...");
Decimal big, expect, actual;
big = Decimal(4);
expect = Decimal(16);
actual = shl!Decimal(big, 2);
writefln("expect = %s", expect.toAbstract);
writefln("actual = %s", actual.toAbstract);
assertEqual!Decimal(expect, actual);
writeln("test missing");
}


//--------------------------------
// decimal shift and rotate
//--------------------------------

/// Shifts the first operand by the specified number of decimal digits.
Expand Down Expand Up @@ -1151,7 +1212,6 @@ public T mulLong(T)(const T arg1, long arg2,
return result;
}


/// Multiplies the first two operands and adds the third operand to the result.
/// The result of the multiplication is not rounded prior to the addition.
/// The result may be rounded and context flags may be set.
Expand Down
24 changes: 11 additions & 13 deletions decimal/context.d
Expand Up @@ -86,25 +86,23 @@ public enum : ubyte {
/// General Decimal Arithmetic Specification, p. 13-14.
public struct DecimalContext {

/// Maximum length of the coefficient in (decimal) digits.
/// Maximum length of the coefficient in decimal digits.
public uint precision;
/// Maximum value of the adjusted exponent.
public int maxExpo;
/// Smallest normalized exponent.
public int minExpo;
/// Smallest non-normalized exponent.
public int tinyExpo;
/// Rounding mode.
public Rounding rounding;

/// Constructs a context with the specified parameters.
public this(const uint precision, const int maxExpo,
const Rounding rounding) {
this.precision = precision;
this.maxExpo = maxExpo;
this.minExpo = 1 - maxExpo;
this.tinyExpo = minExpo - precision + 1;
this.rounding = rounding;
/// Smallest normalized exponent.
@property
public const int minExpo() {
return 1 - maxExpo;
}

/// Smallest non-normalized exponent.
@property
public const int tinyExpo() {
return 2 - maxExpo - precision;
}

/* @property
Expand Down
50 changes: 25 additions & 25 deletions decimal/conv.d
Expand Up @@ -47,7 +47,7 @@ T to(T: string)(const long n) {
return format("%d", n);
}

/// to!string(USizedInt!Z).
/// to!string(uint128).
T to(T: string)(const uint128 n) {
return n.toString();
}
Expand All @@ -56,11 +56,11 @@ T to(T: string)(const uint128 n) {
// uint128 conversions
//--------------------------------

BigInt toBigInt(const uint128 arg) {
/*BigInt toBigInt(const uint128 arg) {
BigInt big = BigInt(0);
big = BigInt(arg.toString);
return big;
}
}*/

//--------------------------------
// decimal tests
Expand Down Expand Up @@ -110,19 +110,13 @@ public T toDecimal(T, U)(const U num) if (isDecimal!T && isFixedDecimal!U) {

/// Converts a decimal number to a big decimal
public Decimal toBigDecimal(T)(const T num) if (isDecimal!T) {
//writefln("toBigDecimal(num) = %s", num);
static if (is(typeof(num) == Decimal)) {
//writefln("num.dup = %s", num.dup);
return num.dup;
}
bool sign = num.sign;
if (num.isFinite) {
auto mant = num.coefficient;
int expo = num.exponent;
//writefln("mant = %s", mant);
//writefln("expo = %s", expo);
//writefln("Decimal(sign, mant, expo) = %s", Decimal(sign, mant, expo));
//writeln("*****");
return Decimal(sign, mant, expo);
} else if (num.isInfinite) {
return Decimal.infinity(sign);
Expand Down Expand Up @@ -380,10 +374,6 @@ unittest {
writeln("passed");
}

//public string toString(T)(const T num, string fmt) {
// return "surprise!";
//}

private void writeTo(T)(const T num, scope void delegate(const(char)[]) sink,
const char formatChar, const int precision) if (isDecimal!T) {

Expand Down Expand Up @@ -463,15 +453,6 @@ private string setWidth(const string str, int width,
return rightJustify!string(str, width, fillChar);
}

/*
auto f = FormatSpec!char(formatString);
f.writeUpToNextSpec(sink);
toString(sink, f);
void toString(scope void delegate(const(char)[]) sink, string fmt) {
auto spec = FormatSpec(fmt);
}*/

private void sink(const(char)[] str) {
auto app = std.array.appender!(string)();
app.put(str);
Expand Down Expand Up @@ -979,14 +960,33 @@ unittest { // toNumber
assertEqual(expect, actual);
}

unittest {
unittest { // toAbstract
write("toAbstract...");
writeln("test missing");
Decimal num;
string str;
num = Decimal("-inf");
str = "[1,inf]";
assert(num.toAbstract == str);
num = Decimal("nan");
str = "[0,qNaN]";
assert(num.toAbstract == str);
num = Decimal("snan1234");
str = "[0,sNaN1234]";
assert(num.toAbstract == str);
writeln("passed");
}

unittest {
write("toExact...");
writeln("test missing");
Decimal num;
assertTrue(num.toExact == "+NaN");
num = +9999999E+90;
assertEqual!string("+9999999E+90", num.toExact);
num = 1;
assertTrue(num.toExact == "+1E+00");
num = Decimal.infinity(true);
assertTrue(num.toExact == "-Infinity");
writeln("passed");
}

unittest {
Expand Down
5 changes: 2 additions & 3 deletions decimal/dec32.d
Expand Up @@ -1696,10 +1696,9 @@ unittest {
assertEqual(num, sqrt(num));
num = 2.0;
assertEqual(Dec32(std.math.sqrt(2.0)), sqrt(num));
// num = PI;
assertEqual!Dec32(Dec32.SQRT_PI, sqrt(Dec32.PI));
num = 2.0;
assertEqual(Dec32(std.math.sqrt(2.0)), sqrt(num));
num = 2174;
assertEqual(Dec32(std.math.sqrt(2174.0)), sqrt(num));
writeln("test missing");
}

Expand Down

0 comments on commit cfa595f

Please sign in to comment.