#include #include #include #include #include #include int main() { // test the basic behaviour { int counter { 0 }; Interval_Iterator<0> start(0.0); Interval_Iterator<0> end(10.0); while (start != end) { assert(*start++ == counter++); } } // test that subtraction and std::distance works { Interval_Iterator<2> start(0.0); Interval_Iterator<2> end(1.0); assert(end - start == 100); assert(std::distance(start, end) == 100); } // test addition and subtraction { Interval_Iterator<2> start(0.0); assert( *(start + 10) == 0.1 ); assert( *(start - 10) == -0.1 ); } { // Note: precision is 1, meaning second decimal digit doesn't matter Interval_Iterator<1> a { 1.12 }; Interval_Iterator<1> b { 1.13 }; Interval_Iterator<1> c { 2.14 }; assert( a == b ); assert( a <= b ); assert( a >= b ); assert( !(a < b) ); assert( !(a > b) ); assert( !(a != b) ); assert( a <= c ); assert( a < c ); assert( a != c ); assert( !(a >= c) ); assert( !(a > c) ); assert( !(a == c) ); } // Test that it works with an STL algorithm { // calculate arcsin(0.5) by binary searching over the value range of std::sin // this works for any monotone function (i.e. a function which is increasing or // decreasing on the specified interval) double x { 0.5 }; double result { *std::lower_bound(Interval_Iterator<3>(0.0), Interval_Iterator<3>(3.1415), x, [](double check, double pivot) { return std::sin(check) < pivot; }) }; assert( std::abs(x - std::sin(result)) < 1e-3 ); } }