1 // test_suite.hpp: reusable test suite for small number systems
2 //
3 // Copyright (C) 2017-2021 Stillwater Supercomputing, Inc.
4 //
5 // This file is part of the universal numbers project, which is released under an MIT Open Source license.
6 #if defined(_MSC_VER)
7 #pragma warning(disable : 4514)
8 #pragma warning(disable : 4710)
9 #endif
10 #include <iostream>
11 #include <iomanip>
12 #include <string>
13
14 #include <universal/verification/test_status.hpp>
15 #include <universal/verification/test_case.hpp>
16 #include <universal/verification/test_suite_exceptions.hpp>
17 #include <universal/verification/test_suite_conversion.hpp>
18 #include <universal/verification/test_suite_logic.hpp>
19 #include <universal/verification/test_suite_arithmetic.hpp>
20
21 template<typename TestType>
ExhaustiveNumberSystemTest(const std::string & tag,bool bReportIndividualTestCases)22 int ExhaustiveNumberSystemTest(const std::string& tag, bool bReportIndividualTestCases) {
23 using namespace std;
24 using namespace sw::universal;
25 int nrOfFailedTestCases = 0;
26 TestType v;
27
28 // special cases
29 v = 0;
30 if (!v.iszero()) {
31 cout << "FAIL: test of zero: " << to_binary(v, true) << " : " << v << '\n';
32 ++nrOfFailedTestCases;
33 }
34 v = NAN;
35 if (!v.isnan()) {
36 cout << "FAIL: test of float assign to NaN: " << to_binary(v, true) << " : " << v << '\n';
37 ++nrOfFailedTestCases;
38 }
39 v = INFINITY;
40 if (!v.isinf()) {
41 cout << "FAIL: test of float assign to INF: " << to_binary(v, true) << " : " << v << '\n';
42 ++nrOfFailedTestCases;
43 }
44 v = double(NAN);
45 if (!v.isnan()) {
46 cout << "FAIL: test of double assign to NaN: " << to_binary(v, true) << " : " << v << '\n';
47 ++nrOfFailedTestCases;
48 }
49 v = double(INFINITY);
50 if (!v.isinf()) {
51 cout << "FAIL: test of double assign to INF: " << to_binary(v, true) << " : " << v << '\n';
52 ++nrOfFailedTestCases;
53 }
54
55 // logic tests
56 nrOfFailedTestCases += ReportTestResult(VerifyLogicEqual <TestType>(), tag, " == ");
57 nrOfFailedTestCases += ReportTestResult(VerifyLogicNotEqual <TestType>(), tag, " != ");
58 // nrOfFailedTestCases += ReportTestResult(VerifyLogicLessThan <TestType>(), tag, " < ");
59 // nrOfFailedTestCases += ReportTestResult(VerifyLogicLessOrEqualThan <TestType>(), tag, " <= ");
60 // nrOfFailedTestCases += ReportTestResult(VerifyLogicGreaterThan <TestType>(), tag, " > ");
61 // nrOfFailedTestCases += ReportTestResult(VerifyLogicGreaterOrEqualThan<TestType>(), tag, " >= ");
62
63 // conversion tests
64 cout << "Assignment/conversion tests " << endl;
65 nrOfFailedTestCases += ReportTestResult(VerifyIntegerConversion <TestType>(bReportIndividualTestCases), tag, "integer assign (native) ");
66 // nrOfFailedTestCases += ReportTestResult(VerifyConversion <TestType,float>(bReportIndividualTestCases), tag, "float assign (native) ");
67 // nrOfFailedTestCases += ReportTestResult(VerifyConversion <TestType,double>(bReportIndividualTestCases), tag, "double assign (native) ");
68
69 #if 0
70 // arithmetic tests
71 cout << "Arithmetic tests " << endl;
72 nrOfFailedTestCases += ReportTestResult(VerifyAddition <TestType>(bReportIndividualTestCases), tag, "add (native) ");
73 nrOfFailedTestCases += ReportTestResult(VerifyInPlaceAddition <TestType>(bReportIndividualTestCases), tag, "+= (native) ");
74 nrOfFailedTestCases += ReportTestResult(VerifySubtraction <TestType>(bReportIndividualTestCases), tag, "subtract (native) ");
75 nrOfFailedTestCases += ReportTestResult(VerifyInPlaceSubtraction <TestType>(bReportIndividualTestCases), tag, "-= (native) ");
76 nrOfFailedTestCases += ReportTestResult(VerifyMultiplication <TestType>(bReportIndividualTestCases), tag, "multiply (native) ");
77 nrOfFailedTestCases += ReportTestResult(VerifyInPlaceMultiplication <TestType>(bReportIndividualTestCases), tag, "*= (native) ");
78 nrOfFailedTestCases += ReportTestResult(VerifyDivision <TestType>(bReportIndividualTestCases), tag, "divide (native) ");
79 nrOfFailedTestCases += ReportTestResult(VerifyInPlaceDivision <TestType>(bReportIndividualTestCases), tag, "/= (native) ");
80 nrOfFailedTestCases += ReportTestResult(VerifyNegation <TestType>(bReportIndividualTestCases), tag, "negate (native) ");
81 nrOfFailedTestCases += ReportTestResult(VerifyReciprocation <TestType>(bReportIndividualTestCases), tag, "reciprocate (native) ");
82
83 // elementary function tests
84 cout << "Elementary function tests " << endl;
85 nrOfFailedTestCases += ReportTestResult(VerifySqrt <TestType>(bReportIndividualTestCases), tag, "sqrt (native) ");
86 nrOfFailedTestCases += ReportTestResult(VerifyExp <TestType>(bReportIndividualTestCases), tag, "exp ");
87 nrOfFailedTestCases += ReportTestResult(VerifyExp2 <TestType>(bReportIndividualTestCases), tag, "exp2 ");
88 nrOfFailedTestCases += ReportTestResult(VerifyLog <TestType>(bReportIndividualTestCases), tag, "log ");
89 nrOfFailedTestCases += ReportTestResult(VerifyLog2 <TestType>(bReportIndividualTestCases), tag, "log2 ");
90 nrOfFailedTestCases += ReportTestResult(VerifyLog10 <TestType>(bReportIndividualTestCases), tag, "log10 ");
91 nrOfFailedTestCases += ReportTestResult(VerifySine <TestType>(bReportIndividualTestCases), tag, "sin ");
92 nrOfFailedTestCases += ReportTestResult(VerifyCosine <TestType>(bReportIndividualTestCases), tag, "cos ");
93 nrOfFailedTestCases += ReportTestResult(VerifyTangent <TestType>(bReportIndividualTestCases), tag, "tan ");
94 nrOfFailedTestCases += ReportTestResult(VerifyAtan <TestType>(bReportIndividualTestCases), tag, "atan ");
95 nrOfFailedTestCases += ReportTestResult(VerifyAsin <TestType>(bReportIndividualTestCases), tag, "asin ");
96 nrOfFailedTestCases += ReportTestResult(VerifyAcos <TestType>(bReportIndividualTestCases), tag, "acos ");
97 nrOfFailedTestCases += ReportTestResult(VerifySinh <TestType>(bReportIndividualTestCases), tag, "sinh ");
98 nrOfFailedTestCases += ReportTestResult(VerifyCosh <TestType>(bReportIndividualTestCases), tag, "cosh ");
99 nrOfFailedTestCases += ReportTestResult(VerifyTanh <TestType>(bReportIndividualTestCases), tag, "tanh ");
100 nrOfFailedTestCases += ReportTestResult(VerifyAtanh <TestType>(bReportIndividualTestCases), tag, "atanh ");
101 nrOfFailedTestCases += ReportTestResult(VerifyAcosh <TestType>(bReportIndividualTestCases), tag, "acosh ");
102 nrOfFailedTestCases += ReportTestResult(VerifyAsinh <TestType>(bReportIndividualTestCases), tag, "asinh ");
103
104 nrOfFailedTestCases += ReportTestResult(VerifyPowerFunction <TestType>(bReportIndividualTestCases), tag, "pow ");
105 #endif
106 return nrOfFailedTestCases;
107 }
108