cxx11_tensor_roundings.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2016 Benoit Steiner <benoit.steiner.goog@gmail.com>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. #include "main.h"
  10. #include <Eigen/CXX11/Tensor>
  11. static void test_float_rounding()
  12. {
  13. Tensor<float, 2> ftensor(20,30);
  14. ftensor = ftensor.random() * 100.f;
  15. Tensor<float, 2> result = ftensor.round();
  16. for (int i = 0; i < 20; ++i) {
  17. for (int j = 0; j < 30; ++j) {
  18. VERIFY_IS_EQUAL(result(i,j), numext::round(ftensor(i,j)));
  19. }
  20. }
  21. }
  22. static void test_float_flooring()
  23. {
  24. Tensor<float, 2> ftensor(20,30);
  25. ftensor = ftensor.random() * 100.f;
  26. Tensor<float, 2> result = ftensor.floor();
  27. for (int i = 0; i < 20; ++i) {
  28. for (int j = 0; j < 30; ++j) {
  29. VERIFY_IS_EQUAL(result(i,j), numext::floor(ftensor(i,j)));
  30. }
  31. }
  32. }
  33. static void test_float_ceiling()
  34. {
  35. Tensor<float, 2> ftensor(20,30);
  36. ftensor = ftensor.random() * 100.f;
  37. Tensor<float, 2> result = ftensor.ceil();
  38. for (int i = 0; i < 20; ++i) {
  39. for (int j = 0; j < 30; ++j) {
  40. VERIFY_IS_EQUAL(result(i,j), numext::ceil(ftensor(i,j)));
  41. }
  42. }
  43. }
  44. EIGEN_DECLARE_TEST(cxx11_tensor_roundings)
  45. {
  46. CALL_SUBTEST(test_float_rounding());
  47. CALL_SUBTEST(test_float_ceiling());
  48. CALL_SUBTEST(test_float_flooring());
  49. }