|
| 1 | +// adaptive_threshold.cpp: test suite for adaptive threshold utilities |
| 2 | +// |
| 3 | +// Copyright (C) 2017 Stillwater Supercomputing, Inc. |
| 4 | +// SPDX-License-Identifier: MIT |
| 5 | +// |
| 6 | +// This file is part of the universal numbers project, which is released under an MIT Open Source license. |
| 7 | +#include <universal/utility/directives.hpp> |
| 8 | +#include <universal/number/ereal/ereal.hpp> |
| 9 | +#include <universal/verification/test_suite.hpp> |
| 10 | +#include <universal/verification/test_suite_mathlib_adaptive.hpp> |
| 11 | + |
| 12 | +int main() |
| 13 | +try { |
| 14 | + using namespace sw::universal; |
| 15 | + |
| 16 | + std::string test_suite = "ereal adaptive threshold utilities"; |
| 17 | + int nrOfFailedTestCases = 0; |
| 18 | + |
| 19 | + std::cout << "Adaptive Threshold Utilities Test\n"; |
| 20 | + std::cout << "==================================\n"; |
| 21 | + |
| 22 | + // Test 1: Verify adaptive thresholds scale with precision |
| 23 | + { |
| 24 | + std::cout << "\nTest 1: Adaptive threshold scaling\n"; |
| 25 | + |
| 26 | + double threshold_double = get_adaptive_threshold<double>(); |
| 27 | + std::cout << " double threshold (digits10=" << std::numeric_limits<double>::digits10 |
| 28 | + << "): " << threshold_double << "\n"; |
| 29 | + |
| 30 | + using Real32 = ereal<32>; |
| 31 | + double threshold_ereal32 = get_adaptive_threshold<Real32>(); |
| 32 | + std::cout << " ereal<32> threshold (digits10=" << std::numeric_limits<Real32>::digits10 |
| 33 | + << "): " << threshold_ereal32 << "\n"; |
| 34 | + |
| 35 | + // ereal should have tighter threshold if it has more precision |
| 36 | + if (std::numeric_limits<Real32>::digits10 > std::numeric_limits<double>::digits10) { |
| 37 | + if (threshold_ereal32 >= threshold_double) { |
| 38 | + std::cerr << "FAIL: ereal<32> should have tighter threshold than double\n"; |
| 39 | + ++nrOfFailedTestCases; |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + // Test 2: Verify exact value checking |
| 45 | + { |
| 46 | + std::cout << "\nTest 2: Exact value checking\n"; |
| 47 | + |
| 48 | + using Real = ereal<16>; |
| 49 | + Real one(1.0); |
| 50 | + Real also_one(1.0); |
| 51 | + Real not_quite_one(1.0000001); |
| 52 | + |
| 53 | + if (!check_exact_value(one, also_one)) { |
| 54 | + std::cerr << "FAIL: exact value check should pass for identical values\n"; |
| 55 | + ++nrOfFailedTestCases; |
| 56 | + } else { |
| 57 | + std::cout << " PASS: exact value check for identical values\n"; |
| 58 | + } |
| 59 | + |
| 60 | + if (check_exact_value(one, not_quite_one)) { |
| 61 | + std::cerr << "FAIL: exact value check should fail for different values\n"; |
| 62 | + ++nrOfFailedTestCases; |
| 63 | + } else { |
| 64 | + std::cout << " PASS: exact value check correctly rejects different values\n"; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // Test 3: Verify relative error checking |
| 69 | + { |
| 70 | + std::cout << "\nTest 3: Relative error checking\n"; |
| 71 | + |
| 72 | + using Real = ereal<16>; |
| 73 | + Real x(1.0); |
| 74 | + Real y(1.0 + 1e-16); // very close to x |
| 75 | + |
| 76 | + if (!check_relative_error(x, y)) { |
| 77 | + std::cerr << "FAIL: relative error check should pass for very close values\n"; |
| 78 | + ++nrOfFailedTestCases; |
| 79 | + } else { |
| 80 | + std::cout << " PASS: relative error check for close values\n"; |
| 81 | + } |
| 82 | + |
| 83 | + Real z(2.0); // far from x |
| 84 | + if (check_relative_error(x, z)) { |
| 85 | + std::cerr << "FAIL: relative error check should fail for distant values\n"; |
| 86 | + ++nrOfFailedTestCases; |
| 87 | + } else { |
| 88 | + std::cout << " PASS: relative error check correctly rejects distant values\n"; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Test 4: Verify identity checking |
| 93 | + { |
| 94 | + std::cout << "\nTest 4: Identity checking\n"; |
| 95 | + |
| 96 | + using Real = ereal<16>; |
| 97 | + Real x(1.5); |
| 98 | + Real lhs = x * x; // x² |
| 99 | + Real rhs = x * x; // should be identical |
| 100 | + |
| 101 | + int failures = verify_identity("x² == x²", lhs, rhs, 0.0, false); |
| 102 | + if (failures != 0) { |
| 103 | + std::cerr << "FAIL: identity check failed for identical expressions\n"; |
| 104 | + ++nrOfFailedTestCases; |
| 105 | + } else { |
| 106 | + std::cout << " PASS: identity check for identical expressions\n"; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // Test 5: Error reporting (manual check - should print nicely formatted output) |
| 111 | + { |
| 112 | + std::cout << "\nTest 5: Error reporting format\n"; |
| 113 | + std::cout << " (The following is a test of error reporting format, not a real failure)\n"; |
| 114 | + |
| 115 | + using Real = ereal<16>; |
| 116 | + Real result(2.71828); |
| 117 | + Real expected(2.71829); |
| 118 | + double threshold = get_adaptive_threshold<Real>(); |
| 119 | + |
| 120 | + std::cout << " Sample error report:\n"; |
| 121 | + std::cout << " --------------------\n"; |
| 122 | + report_error_detail("exp", "1.0", result, expected, threshold, true); |
| 123 | + std::cout << " --------------------\n"; |
| 124 | + } |
| 125 | + |
| 126 | + std::cout << "\n"; |
| 127 | + if (nrOfFailedTestCases > 0) { |
| 128 | + std::cout << "FAILED: " << nrOfFailedTestCases << " test case(s)\n"; |
| 129 | + } else { |
| 130 | + std::cout << "SUCCESS: All adaptive threshold utility tests passed\n"; |
| 131 | + } |
| 132 | + |
| 133 | + return (nrOfFailedTestCases > 0 ? EXIT_FAILURE : EXIT_SUCCESS); |
| 134 | +} |
| 135 | +catch (char const* msg) { |
| 136 | + std::cerr << "Caught ad-hoc exception: " << msg << std::endl; |
| 137 | + return EXIT_FAILURE; |
| 138 | +} |
| 139 | +catch (const sw::universal::universal_arithmetic_exception& err) { |
| 140 | + std::cerr << "Caught unexpected universal arithmetic exception: " << err.what() << std::endl; |
| 141 | + return EXIT_FAILURE; |
| 142 | +} |
| 143 | +catch (const sw::universal::universal_internal_exception& err) { |
| 144 | + std::cerr << "Caught unexpected universal internal exception: " << err.what() << std::endl; |
| 145 | + return EXIT_FAILURE; |
| 146 | +} |
| 147 | +catch (const std::runtime_error& err) { |
| 148 | + std::cerr << "Caught unexpected runtime exception: " << err.what() << std::endl; |
| 149 | + return EXIT_FAILURE; |
| 150 | +} |
| 151 | +catch (...) { |
| 152 | + std::cerr << "Caught unknown exception" << std::endl; |
| 153 | + return EXIT_FAILURE; |
| 154 | +} |
0 commit comments