float_tools_test.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <float.h>
  2. #include <float_tools.h>
  3. #include "../minunit.h"
  4. MU_TEST(float_tools_equal_test) {
  5. mu_check(float_is_equal(FLT_MAX, FLT_MAX));
  6. mu_check(float_is_equal(FLT_MIN, FLT_MIN));
  7. mu_check(float_is_equal(-FLT_MAX, -FLT_MAX));
  8. mu_check(float_is_equal(-FLT_MIN, -FLT_MIN));
  9. mu_check(!float_is_equal(FLT_MIN, FLT_MAX));
  10. mu_check(!float_is_equal(-FLT_MIN, FLT_MAX));
  11. mu_check(!float_is_equal(FLT_MIN, -FLT_MAX));
  12. mu_check(!float_is_equal(-FLT_MIN, -FLT_MAX));
  13. const float pi = 3.14159f;
  14. mu_check(float_is_equal(pi, pi));
  15. mu_check(float_is_equal(-pi, -pi));
  16. mu_check(!float_is_equal(pi, -pi));
  17. mu_check(!float_is_equal(-pi, pi));
  18. const float one_third = 1.f / 3.f;
  19. const float one_third_dec = 0.3333333f;
  20. mu_check(one_third != one_third_dec);
  21. mu_check(float_is_equal(one_third, one_third_dec));
  22. const float big_num = 1.e12f;
  23. const float med_num = 95.389f;
  24. const float smol_num = 1.e-12f;
  25. mu_check(float_is_equal(big_num, big_num));
  26. mu_check(float_is_equal(med_num, med_num));
  27. mu_check(float_is_equal(smol_num, smol_num));
  28. mu_check(!float_is_equal(smol_num, big_num));
  29. mu_check(!float_is_equal(med_num, smol_num));
  30. mu_check(!float_is_equal(big_num, med_num));
  31. const float more_than_one = 1.f + FLT_EPSILON;
  32. const float less_than_one = 1.f - FLT_EPSILON;
  33. mu_check(!float_is_equal(more_than_one, less_than_one));
  34. mu_check(!float_is_equal(more_than_one, -less_than_one));
  35. mu_check(!float_is_equal(-more_than_one, less_than_one));
  36. mu_check(!float_is_equal(-more_than_one, -less_than_one));
  37. const float slightly_more_than_one = 1.f + FLT_EPSILON / 2.f;
  38. const float slightly_less_than_one = 1.f - FLT_EPSILON / 2.f;
  39. mu_check(float_is_equal(slightly_more_than_one, slightly_less_than_one));
  40. mu_check(float_is_equal(-slightly_more_than_one, -slightly_less_than_one));
  41. mu_check(!float_is_equal(slightly_more_than_one, -slightly_less_than_one));
  42. mu_check(!float_is_equal(-slightly_more_than_one, slightly_less_than_one));
  43. }
  44. MU_TEST_SUITE(float_tools_suite) {
  45. MU_RUN_TEST(float_tools_equal_test);
  46. }
  47. int run_minunit_test_float_tools() {
  48. MU_RUN_SUITE(float_tools_suite);
  49. return MU_EXIT_CODE;
  50. }