|
| 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/test/FunctionHeaderWrapper.h> // Declares the operator |
| 10 | +#include <executorch/kernels/test/TestUtil.h> |
| 11 | +#include <executorch/runtime/core/exec_aten/exec_aten.h> |
| 12 | +#include <executorch/runtime/core/exec_aten/testing_util/tensor_factory.h> |
| 13 | +#include <executorch/runtime/core/exec_aten/testing_util/tensor_util.h> |
| 14 | + |
| 15 | +#include <gtest/gtest.h> |
| 16 | + |
| 17 | +using namespace ::testing; |
| 18 | +using executorch::aten::Scalar; |
| 19 | +using executorch::aten::ScalarType; |
| 20 | +using executorch::aten::Tensor; |
| 21 | +using torch::executor::testing::TensorFactory; |
| 22 | + |
| 23 | +class OpBitwiseLeftShiftTensorOutTest : public OperatorTest { |
| 24 | + protected: |
| 25 | + Tensor& op_bitwise_left_shift_tensor_out( |
| 26 | + const Tensor& self, |
| 27 | + const Tensor& other, |
| 28 | + Tensor& out) { |
| 29 | + return torch::executor::aten::bitwise_left_shift_outf( |
| 30 | + context_, self, other, out); |
| 31 | + } |
| 32 | +}; |
| 33 | + |
| 34 | +class OpBitwiseLeftShiftScalarOutTest : public OperatorTest { |
| 35 | + protected: |
| 36 | + Tensor& op_bitwise_left_shift_scalar_out( |
| 37 | + const Tensor& self, |
| 38 | + const Scalar& other, |
| 39 | + Tensor& out) { |
| 40 | + return torch::executor::aten::bitwise_left_shift_outf( |
| 41 | + context_, self, other, out); |
| 42 | + } |
| 43 | +}; |
| 44 | + |
| 45 | +TEST_F(OpBitwiseLeftShiftTensorOutTest, SmokeTestInt) { |
| 46 | + TensorFactory<ScalarType::Int> tf; |
| 47 | + |
| 48 | + // Test basic left shift: [1, 2, 4, 8] << [1, 2, 1, 2] = [2, 8, 8, 32] |
| 49 | + Tensor a = tf.make({2, 2}, {1, 2, 4, 8}); |
| 50 | + Tensor b = tf.make({2, 2}, {1, 2, 1, 2}); |
| 51 | + |
| 52 | + Tensor out = tf.zeros({2, 2}); |
| 53 | + |
| 54 | + op_bitwise_left_shift_tensor_out(a, b, out); |
| 55 | + EXPECT_TENSOR_EQ(out, tf.make({2, 2}, {2, 8, 8, 32})); |
| 56 | +} |
| 57 | + |
| 58 | +TEST_F(OpBitwiseLeftShiftTensorOutTest, SmokeTestByte) { |
| 59 | + TensorFactory<ScalarType::Byte> tf; |
| 60 | + |
| 61 | + // Test with byte values: [1, 5, 10, 15] << [0, 1, 2, 3] = [1, 10, 40, 120] |
| 62 | + Tensor a = tf.make({2, 2}, {1, 5, 10, 15}); |
| 63 | + Tensor b = tf.make({2, 2}, {0, 1, 2, 3}); |
| 64 | + |
| 65 | + Tensor out = tf.zeros({2, 2}); |
| 66 | + |
| 67 | + op_bitwise_left_shift_tensor_out(a, b, out); |
| 68 | + EXPECT_TENSOR_EQ(out, tf.make({2, 2}, {1, 10, 40, 120})); |
| 69 | +} |
| 70 | + |
| 71 | +TEST_F(OpBitwiseLeftShiftTensorOutTest, ZeroShift) { |
| 72 | + TensorFactory<ScalarType::Int> tf; |
| 73 | + |
| 74 | + // Shifting by 0 should return the original value |
| 75 | + Tensor a = tf.make({2, 2}, {5, 10, 15, 20}); |
| 76 | + Tensor b = tf.zeros({2, 2}); |
| 77 | + |
| 78 | + Tensor out = tf.zeros({2, 2}); |
| 79 | + |
| 80 | + op_bitwise_left_shift_tensor_out(a, b, out); |
| 81 | + EXPECT_TENSOR_EQ(out, tf.make({2, 2}, {5, 10, 15, 20})); |
| 82 | +} |
| 83 | + |
| 84 | +TEST_F(OpBitwiseLeftShiftScalarOutTest, SmokeTestInt) { |
| 85 | + TensorFactory<ScalarType::Int> tf; |
| 86 | + |
| 87 | + // Test shifting by scalar: [1, 2, 3, 4] << 2 = [4, 8, 12, 16] |
| 88 | + Tensor a = tf.make({2, 2}, {1, 2, 3, 4}); |
| 89 | + Scalar b = 2; |
| 90 | + |
| 91 | + Tensor out = tf.zeros({2, 2}); |
| 92 | + |
| 93 | + op_bitwise_left_shift_scalar_out(a, b, out); |
| 94 | + EXPECT_TENSOR_EQ(out, tf.make({2, 2}, {4, 8, 12, 16})); |
| 95 | +} |
| 96 | + |
| 97 | +TEST_F(OpBitwiseLeftShiftScalarOutTest, ShiftByOne) { |
| 98 | + TensorFactory<ScalarType::Int> tf; |
| 99 | + |
| 100 | + // Shifting by 1 should double the value |
| 101 | + Tensor a = tf.make({2, 2}, {1, 5, 10, 100}); |
| 102 | + Scalar b = 1; |
| 103 | + |
| 104 | + Tensor out = tf.zeros({2, 2}); |
| 105 | + |
| 106 | + op_bitwise_left_shift_scalar_out(a, b, out); |
| 107 | + EXPECT_TENSOR_EQ(out, tf.make({2, 2}, {2, 10, 20, 200})); |
| 108 | +} |
| 109 | + |
| 110 | +TEST_F(OpBitwiseLeftShiftScalarOutTest, ShiftByZero) { |
| 111 | + TensorFactory<ScalarType::Int> tf; |
| 112 | + |
| 113 | + // Shifting by 0 should return the original value |
| 114 | + Tensor a = tf.make({2, 2}, {7, 14, 21, 28}); |
| 115 | + Scalar b = 0; |
| 116 | + |
| 117 | + Tensor out = tf.zeros({2, 2}); |
| 118 | + |
| 119 | + op_bitwise_left_shift_scalar_out(a, b, out); |
| 120 | + EXPECT_TENSOR_EQ(out, tf.make({2, 2}, {7, 14, 21, 28})); |
| 121 | +} |
0 commit comments