[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master e3c0e51 02/10: Improve PETE unit test
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master e3c0e51 02/10: Improve PETE unit test |
Date: |
Mon, 1 Mar 2021 09:39:22 -0500 (EST) |
branch: master
commit e3c0e513d504283d7bd54d5fb91aec558f76d413
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>
Improve PETE unit test
A unit test that actually tests things is better than one that doesn't.
Took care to choose examples such that testing for floating-point
equality is valid (for binary hardware).
---
et_vector_0_test.cpp | 85 ++++++++++++++++++++++++++++++----------------------
1 file changed, 50 insertions(+), 35 deletions(-)
diff --git a/et_vector_0_test.cpp b/et_vector_0_test.cpp
index 2405ef3..23f0602 100644
--- a/et_vector_0_test.cpp
+++ b/et_vector_0_test.cpp
@@ -23,64 +23,79 @@
#include "test_tools.hpp"
-#include <algorithm>
-#include <functional>
-#include <iostream>
-#include <iterator>
-
-template <typename T>
-void show_vector(std::vector<T> const& v)
-{
- std::ostream_iterator<double> osi(std::cout, " ");
- std::copy(v.begin(), v.end(), osi);
- std::cout << std::endl;
-}
+#include <functional> // multiplies(), negate(), plus()
int test_main(int, char*[])
{
- std::vector<double> v0;
-
- v0.push_back(0.0);
- v0.push_back(1.1);
- v0.push_back(2.2);
-
+ {
+ std::vector<double> v0 = {0.0, 1.25, 2.5};
v0 *= v0;
-
- show_vector(v0);
+ std::vector<double> const r0 = {0.0, 1.5625, 6.25};
+ BOOST_TEST(r0 == v0);
+ }
// Test peteCast().
+ {
+ std::vector<double> v0 = {0.0, 1.25, 2.5};
std::vector<int> v1(v0.size());
- peteCast(int{}, v0);
+ peteCast(int{}, v0); // Legal, but feckless.
assign(v1, peteCast(int{}, v0));
- show_vector(v1);
+ std::vector<int> const r1 = {0, 1, 2};
+ BOOST_TEST(r1 == v1);
+ }
- // Test std::unary_function.
+ // Test what used to be called std::unary_function.
+ {
+ std::vector<double> v0 = {-1.0, 0.0, 3.875};
assign(v0, apply_unary(std::negate<double>(), v0));
- show_vector(v0);
+ std::vector<double> const r0 = {1.0, 0.0, -3.875};
+ BOOST_TEST(r0 == v0);
+ }
- // Test std::binary_function.
+ // Test what used to be called std::binary_function.
+ {
+ std::vector<double> v0 = {0.0, -1.5625, -6.25};
assign(v0, apply_binary(std::multiplies<double>(), -1.0, v0));
- show_vector(v0);
+ std::vector<double> const r0 = {0.0, 1.5625, 6.25};
+ BOOST_TEST(r0 == v0);
+
assign(v0, sqrt(v0));
- show_vector(v0);
+ std::vector<double> const r1 = {0.0, 1.25, 2.5};
+ BOOST_TEST(r1 == v0);
+
+ // Above, scalar -1.0 was LHS; here, it's RHS.
assign(v0, apply_binary(std::multiplies<double>(), v0, -1.0));
- show_vector(v0);
+ std::vector<double> const r2 = {0.0, -1.25, -2.5};
+ BOOST_TEST(r2 == v0);
+
assign(v0, apply_binary(std::multiplies<double>(), v0, v0));
- show_vector(v0);
+ std::vector<double> const r3 = {0.0, 1.5625, 6.25};
+ BOOST_TEST(r3 == v0);
+
+ // Right-add 100, left-add 10000 .
assign(v0, apply_binary(std::plus<double>(), v0, 100.0));
assign(v0, apply_binary(std::plus<double>(), 10000.0, v0));
- show_vector(v0);
+ std::vector<double> const r4 = {10100.0, 10101.5625, 10106.25};
+ BOOST_TEST(r4 == v0);
+ }
// Test Min() and Max().
- std::vector<double> v2 = {1.2, 2.3, 3.4, 7.7};
- std::vector<double> v3 = {1.9, 2.9, 3.9, 0.0};
+ {
+ std::vector<double> v2 = {1.125, 2.25, 3.375, 7.75};
+ std::vector<double> v3 = {1.875, 2.875, 3.875, 0.0};
std::vector<double> v4(v2.size());
assign(v4, Max(v2, v3));
- show_vector(v4);
+ std::vector<double> const r0 = {1.875, 2.875, 3.875, 7.75};
+ BOOST_TEST(r0 == v4);
+
assign(v4, Min(v2, v3));
- show_vector(v4);
+ std::vector<double> const r1 = {1.125, 2.25, 3.375, 0.0};
+ BOOST_TEST(r1 == v4);
- std::cout << "Completed." << std::endl;
+ assign(v4, Min(Max(1.25, Min(v2, v3)), 3.125));
+ std::vector<double> const r2 = {1.25, 2.25, 3.125, 1.25};
+ BOOST_TEST(r2 == v4);
+ }
return 0;
}
- [lmi-commits] [lmi] master updated (e5b0ac2 -> 8ffc9d2), Greg Chicares, 2021/03/01
- [lmi-commits] [lmi] master e3c0e51 02/10: Improve PETE unit test,
Greg Chicares <=
- [lmi-commits] [lmi] master 56f9792 06/10: Turn a member into a local variable, Greg Chicares, 2021/03/01
- [lmi-commits] [lmi] master c695cd7 01/10: Add PETE vector tests, Greg Chicares, 2021/03/01
- [lmi-commits] [lmi] master 4d37390 03/10: Mention lmi's improved unit test, Greg Chicares, 2021/03/01
- [lmi-commits] [lmi] master 53186b7 05/10: Reorder independent blocks, Greg Chicares, 2021/03/01
- [lmi-commits] [lmi] master aab3295 07/10: Rename a local variable, Greg Chicares, 2021/03/01
- [lmi-commits] [lmi] master 3ef5a8e 08/10: Draw attention to an improvement deferred, Greg Chicares, 2021/03/01
- [lmi-commits] [lmi] master ecd20b1 04/10: Improve Max() and Min() implementations, Greg Chicares, 2021/03/01
- [lmi-commits] [lmi] master 8ffc9d2 10/10: Calculate 7702 'ig' more carefully, Greg Chicares, 2021/03/01
- [lmi-commits] [lmi] master f135b80 09/10: Assert correspondence between DB_GuarInt and DB_NaarDiscount, Greg Chicares, 2021/03/01