Commit 3a5a8234 authored by Ari Trachtenberg's avatar Ari Trachtenberg
Browse files

Adding additional corner cases.

parent b13dd500
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
bool test_10() {
  const char* T = "Trunc overflow/underflow";

  // Huge positive exponent: 2^20000 is astronomically large; trunc likely becomes inf
  RationalP huge_pos({20000});
  long double v1 = huge_pos.trunc();

  if (v1 < 0)
    return failExample(T, "2^20000 trunc() should not be negative","non-negative",to_string(v1));

  // Huge negative exponent: 2^-20000 ~ 0; trunc may underflow to 0
  RationalP huge_neg({-20000});
  long double v2 = huge_neg.trunc();

  if (v2 < 0)
    return failExample(T, "2^-20000 trunc() should not be negative","non-negative",to_string(v2));

  RationalP prod = huge_pos * huge_neg;
  if (!approxEq(prod.trunc(), 1.0L))
    return failExample(T, "(2^20000)*(2^-20000) trunc()","1",to_string(prod.trunc()));

  return true;
}
+70 −0
Original line number Diff line number Diff line
// -------------------- multiple representations of 1 --------------------
bool test_8() {
    const char* T = "multiple representations of 1 (RationalP)";

    RationalP one_default;          // default ctor
    RationalP one_zero_basis({0});  // explicitly basis with a trailing 0

    // Both should approximate to 1.0
    if (!approxEq(one_default.trunc(), 1.0L))
        return failExample(T, "default one trunc()","1",to_string(one_default.trunc()));
    if (!approxEq(one_zero_basis.trunc(), 1.0L))
        return failExample(T, "RationalP({0}).trunc()","1",to_string(one_zero_basis.trunc()));

    return true;
}

// -------------------- operations that produce 1 --------------------
bool test_9() {
    const char* T = "operations producing 1";

    // 3/4 * 4/3 = 1
    RationalP a = RationalP::valueOf(3,4);
    RationalP b = RationalP::valueOf(4,3);
    RationalP prod = a * b;

    // should be about 1
    if (!approxEq(prod.trunc(), 1.0L))
        return failExample(T, "(3/4)*(4/3) trunc()","1",to_string(prod.trunc()));

    // toString() should be "1" for the identity element
    if (prod.toString() != "1")
        return failExample(T, "(3/4)*(4/3) toString()","1",prod.toString());

    // Now create "1" via division: (7/3) / (7/3) = 1
    RationalP c = RationalP::valueOf(7,3);
    RationalP quot = c / c;

    if (!approxEq(quot.trunc(), 1.0L))
        return failExample(T, "(7/3)/(7/3) trunc()","1",to_string(quot.trunc()));
    if (quot.toString() != "1")
        return failExample(T, "(7/3)/(7/3) toString()","1",quot.toString());

    return true;
}

// -------------------- trunc overflow / underflow --------------------
bool test_10() {
    const char* T = "Trunc overflow/underflow";

    // Huge positive exponent: 2^20000 is astronomically large; trunc likely becomes inf
    RationalP huge_pos({20000});
    long double v1 = huge_pos.trunc();

    if (v1 < 0)
        return failExample(T, "2^20000 trunc() should not be negative","non-negative",to_string(v1));

    // Huge negative exponent: 2^-20000 ~ 0; trunc may underflow to 0
    RationalP huge_neg({-20000});
    long double v2 = huge_neg.trunc();

    if (v2 < 0)
        return failExample(T, "2^-20000 trunc() should not be negative","non-negative",to_string(v2));

    RationalP prod = huge_pos * huge_neg;
    if (!approxEq(prod.trunc(), 1.0L))
        return failExample(T, "(2^20000)*(2^-20000) trunc()","1",to_string(prod.trunc()));

    return true;
}