|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#include <executorch/kernels/portable/cpu/util/sort_util.h> |
| 10 | +#include <executorch/runtime/core/exec_aten/testing_util/tensor_factory.h> |
| 11 | +#include <executorch/runtime/core/exec_aten/testing_util/tensor_util.h> |
| 12 | +#include <executorch/runtime/core/exec_aten/util/tensor_util.h> |
| 13 | +#include <executorch/test/utils/DeathTest.h> |
| 14 | + |
| 15 | +#include <gtest/gtest.h> |
| 16 | + |
| 17 | +using namespace ::testing; |
| 18 | +using exec_aten::ScalarType; |
| 19 | +using exec_aten::Tensor; |
| 20 | +using torch::executor::ArrayRef; |
| 21 | +using torch::executor::testing::TensorFactory; |
| 22 | + |
| 23 | +TEST(SortUtilTest, SortTensorTest) { |
| 24 | + TensorFactory<ScalarType::Float> tf; |
| 25 | + TensorFactory<ScalarType::Long> lf; |
| 26 | + |
| 27 | + Tensor a = tf.make({4}, {3, 2, 1, 4}); |
| 28 | + Tensor b = tf.zeros({4}); |
| 29 | + Tensor c = lf.zeros({4}); |
| 30 | + |
| 31 | + // Ascending order sort test |
| 32 | + sort_tensor(a, b, c); |
| 33 | + |
| 34 | + Tensor expected = tf.make({4}, {1, 2, 3, 4}); |
| 35 | + Tensor expected_indices = lf.make({4}, {2, 1, 0, 3}); |
| 36 | + EXPECT_TENSOR_EQ(b, expected); |
| 37 | + EXPECT_TENSOR_EQ(c, expected_indices); |
| 38 | + |
| 39 | + // Descending order sort test |
| 40 | + sort_tensor(a, b, c, true); |
| 41 | + expected = tf.make({4}, {4, 3, 2, 1}); |
| 42 | + expected_indices = lf.make({4}, {3, 0, 1, 2}); |
| 43 | + EXPECT_TENSOR_EQ(b, expected); |
| 44 | + EXPECT_TENSOR_EQ(c, expected_indices); |
| 45 | +} |
0 commit comments