test.cc 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/test - Tests for TestRecorder.
  3. // (c) Daniel Llorens - 2021
  4. // This library is free software; you can redistribute it and/or modify it under
  5. // the terms of the GNU Lesser General Public License as published by the Free
  6. // Software Foundation; either version 3 of the License, or (at your option) any
  7. // later version.
  8. #include <iostream>
  9. #include "ra/test.hh"
  10. #include "mpdebug.hh"
  11. using std::cout, std::endl, std::flush, ra::TestRecorder;
  12. constexpr auto QNAN = std::numeric_limits<double>::quiet_NaN();
  13. constexpr auto PINF = std::numeric_limits<double>::infinity();
  14. int main()
  15. {
  16. TestRecorder tr(std::cout);
  17. tr.section("amax_strict"); // cf amax() in reduction.cc.
  18. {
  19. tr.test(isnan(TestRecorder::amax_strict(ra::Small<double, 2>(QNAN))));
  20. tr.test(isnan(TestRecorder::amax_strict(ra::Small<double, 2>(1, QNAN))));
  21. tr.test(isnan(TestRecorder::amax_strict(ra::Small<double, 2>(QNAN, 1))));
  22. tr.test_eq(9, TestRecorder::amax_strict(ra::Small<double, 2>{1, 9}));
  23. tr.test_eq(9, TestRecorder::amax_strict(ra::Small<double, 2>{9, 1}));
  24. }
  25. tr.section("nan in numeric tests");
  26. {
  27. std::ostream devnull(nullptr);
  28. TestRecorder ut(devnull);
  29. tr.test(0==ut.summary());
  30. ut.test_rel(0, QNAN, 1e-15);
  31. tr.test(1==ut.summary());
  32. ut.test_abs(0, QNAN, 1e-15);
  33. tr.test(2==ut.summary());
  34. }
  35. tr.section("inf in numeric tests");
  36. {
  37. auto test = [&tr](bool fail, auto ref, auto a, auto err)
  38. {
  39. tr.expectfail(fail).test_abs(ref, a, err);
  40. tr.expectfail(fail).test_rel(ref, a, err);
  41. };
  42. test(false, QNAN, QNAN, 0.);
  43. test(false, PINF, PINF, 0.);
  44. test(false, -PINF, -PINF, 0.);
  45. test(true, QNAN, -PINF, 0.);
  46. test(true, QNAN, PINF, 0.);
  47. test(true, -PINF, QNAN, 0.);
  48. test(true, PINF, QNAN, 0.);
  49. test(true, PINF, -PINF, 0.);
  50. test(true, -PINF, PINF, 0.);
  51. test(true, 3., PINF, 0.);
  52. test(true, 3., -PINF, 0.);
  53. test(true, 3., QNAN, 0.);
  54. test(true, PINF, 3., 0.);
  55. test(true, -PINF, 3., 0.);
  56. test(true, QNAN, 3., 0.);
  57. }
  58. tr.section("fov in test_abs/test_rel");
  59. {
  60. std::array ref = { 1., 2., 3. };
  61. std::vector a = { 1., 2., 3. };
  62. tr.test_abs(ref, a, 0.);
  63. }
  64. tr.section("export assumption");
  65. {
  66. tr.test(isfinite(3.));
  67. tr.test(!isfinite(PINF));
  68. tr.test(!isfinite(QNAN));
  69. }
  70. return tr.summary();
  71. }