From 7726fdd6bac86d9e825ec8ef2043ac99ac056e3b Mon Sep 17 00:00:00 2001 From: Xingyu Date: Tue, 3 Feb 2026 17:39:26 -0500 Subject: [PATCH] add corner-case tests for hw0 --- tests/testIntegerP.cpp | 50 +++++++++++++++++++++++++++- tests/testRationalP.cpp | 72 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 120 insertions(+), 2 deletions(-) diff --git a/tests/testIntegerP.cpp b/tests/testIntegerP.cpp index aa75acd..356e6b1 100644 --- a/tests/testIntegerP.cpp +++ b/tests/testIntegerP.cpp @@ -190,9 +190,57 @@ 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() }; + 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 allPassed = true; for (size_t ii=0; ii