How to Test Floating-Point and Custom Values
We first encountered the need to test floating-point values in Chapter 5, Adding More Confirm Types, and created a simple solution that would let us compare floating-point values within a margin of error. We need the small margin because floating-point values that are close and might even look identical when displayed are almost always not exactly equal. These small differences make it hard to verify test results.
The main topics in this chapter are as follows:
- More precise floating-point comparisons
- Adding floating-point Hamcrest matchers
- Writing custom Hamcrest matchers
We’re going to improve the simple solution developed earlier into a much better way to compare floating-point values that is more precise and works for both small and big values. We’ll use the better comparison for both the earlier classical-style confirmations and the new Hamcrest-style confirmations.
You’ll also learn...