Commit b13dd500 authored by Ari Trachtenberg's avatar Ari Trachtenberg
Browse files

Merge branch 'revert-f0516eca' into 'master'

Revert "Merge branch 'corner-testcases' into 'master'"

See merge request !3
parents f0516eca 9383bae4
Loading
Loading
Loading
Loading
+1 −49
Original line number Diff line number Diff line
@@ -190,57 +190,9 @@ bool test_8() {
  return true;
}

// -------------------- Corner cases: representation of 1 and trailing zeros --------------------
bool test_9() {
    const char* T = "Corner: multiple representations of 1";

    IntegerP one_default;

    // These also represent 1 mathematically, but internal vectors differ.
    IntegerP one_zero_list({0});
    IntegerP one_many_zeros({0,0,0});

    // All should truncate to 1
    if (one_default.trunc() != 1) return failExample(T, "default one trunc()","1",to_string(one_default.trunc()));
    if (one_zero_list.trunc() != 1) return failExample(T, "IntegerP({0}).trunc()","1",to_string(one_zero_list.trunc()));
    if (one_many_zeros.trunc() != 1) return failExample(T, "IntegerP({0,0,0}).trunc()","1",to_string(one_many_zeros.trunc()));

    return true;
}

// -------------------- Corner cases: identity element through operations --------------------
bool test_10() {
    const char* T = "Corner: identity element via * and / with 1";

    IntegerP one_default;                // empty vector in your implementation
    IntegerP one_zero_list({0});         // vector {0}
    IntegerP x = IntegerP::valueOf(30);  // 30 = 2 * 3 * 5

    // Multiplying by 1 should keep the value
    IntegerP p1 = x * one_default;
    if (p1.trunc() != 30) return failExample(T, "x * one_default trunc()","30",to_string(p1.trunc()));

    IntegerP p2 = x * one_zero_list;
    if (p2.trunc() != 30) return failExample(T, "x * IntegerP({0}) trunc()","30",to_string(p2.trunc()));

    // Dividing by 1 should keep the value
    IntegerP q1 = x / one_default;
    if (q1.trunc() != 30) return failExample(T, "x / one_default trunc()","30",to_string(q1.trunc()));

    IntegerP q2 = x / one_zero_list;
    if (q2.trunc() != 30) return failExample(T, "x / IntegerP({0}) trunc()","30",to_string(q2.trunc()));

    // Also check divisibleBy(1) is true for both representations of 1
    if (!x.divisibleBy(one_default))
        return failExample(T, "x.divisibleBy(one_default)","true","false");
    if (!x.divisibleBy(one_zero_list))
        return failExample(T, "x.divisibleBy(IntegerP({0}))","true","false");

    return true;
}

int main() {
  bool results[] = { test_0(), test_1(), test_2(), test_3(), test_4(), test_5(), test_6(), test_7(), test_8(), test_9(), test_10() };
  bool results[] = { test_0(), test_1(), test_2(), test_3(), test_4(), test_5(), test_6(), test_7(), test_8() };

  bool allPassed = true;
  for (size_t ii=0; ii<std::size(results);ii++) {
+1 −71
Original line number Diff line number Diff line
@@ -167,79 +167,9 @@ bool test_7() {
  return true;
}

// -------------------- 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;
}


int main() {
  bool results[] = { test_0(), test_1(), test_2(), test_3(), test_4(), test_5(), test_6(), test_7(), test_8(), test_9(), test_10()};
  bool results[] = { test_0(), test_1(), test_2(), test_3(), test_4(), test_5(), test_6(), test_7() };

  bool allPassed = true;
  for (size_t ii=0; ii<std::size(results);ii++) {