test_float16.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright (c) 2016 UAVCAN Team
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. *
  22. * Contributors: https://github.com/UAVCAN/libcanard/contributors
  23. */
  24. #include <catch.hpp>
  25. #include <cmath>
  26. #include "canard.h"
  27. TEST_CASE("Float16, FromNative")
  28. {
  29. // Reference values were generated manually with libuavcan and numpy.float16()
  30. REQUIRE(0b0000000000000000 == canardConvertNativeFloatToFloat16(0));
  31. REQUIRE(0b0011110000000000 == canardConvertNativeFloatToFloat16(1));
  32. REQUIRE(0b1100000000000000 == canardConvertNativeFloatToFloat16(-2));
  33. REQUIRE(0b0111110000000000 == canardConvertNativeFloatToFloat16(999999)); // +inf
  34. REQUIRE(0b1111101111111111 == canardConvertNativeFloatToFloat16(-65519)); // -max
  35. REQUIRE(0b0111111111111111 == canardConvertNativeFloatToFloat16(std::nanf(""))); // nan
  36. }
  37. TEST_CASE("Float16, ToNative")
  38. {
  39. // Reference values were generated manually with libuavcan and numpy.float16()
  40. REQUIRE(Approx(0.0F) == canardConvertFloat16ToNativeFloat(0b0000000000000000));
  41. REQUIRE(Approx(1.0F) == canardConvertFloat16ToNativeFloat(0b0011110000000000));
  42. REQUIRE(Approx(-2.0F) == canardConvertFloat16ToNativeFloat(0b1100000000000000));
  43. REQUIRE(Approx(-65504.0F) == canardConvertFloat16ToNativeFloat(0b1111101111111111));
  44. REQUIRE(std::isinf(canardConvertFloat16ToNativeFloat(0b0111110000000000)));
  45. REQUIRE(bool(std::isnan(canardConvertFloat16ToNativeFloat(0b0111111111111111))));
  46. }
  47. TEST_CASE("Float16, BackAndForth")
  48. {
  49. float x = -1000.0F;
  50. while (x <= 1000.0F)
  51. {
  52. REQUIRE(Approx(x) == canardConvertFloat16ToNativeFloat(canardConvertNativeFloatToFloat16(x)));
  53. x += 0.5F;
  54. }
  55. }