Skip to content

Commit 73ad954

Browse files
[ET][Test] Scalar overflow test macro
Pull Request resolved: #12013 ghstack-source-id: 292926436 @exported-using-ghexport Differential Revision: [D77386636](https://our.internmc.facebook.com/intern/diff/D77386636/)
1 parent 5d4060b commit 73ad954

File tree

3 files changed

+41
-24
lines changed

3 files changed

+41
-24
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
#pragma once
10+
11+
// Macro to generate scalar overflow test cases for a given test suite.
12+
// The test suite must have a method called expect_bad_scalar_value_dies
13+
// that takes a template parameter for ScalarType and a Scalar value.
14+
#define GENERATE_SCALAR_OVERFLOW_TESTS(TEST_SUITE_NAME) \
15+
TEST_F(TEST_SUITE_NAME, ByteTensorTooLargeScalarDies) { \
16+
/* Cannot be represented by a uint8_t. */ \
17+
expect_bad_scalar_value_dies<ScalarType::Byte>(256); \
18+
} \
19+
\
20+
TEST_F(TEST_SUITE_NAME, CharTensorTooSmallScalarDies) { \
21+
/* Cannot be represented by a int8_t. */ \
22+
expect_bad_scalar_value_dies<ScalarType::Char>(-129); \
23+
} \
24+
\
25+
TEST_F(TEST_SUITE_NAME, ShortTensorTooLargeScalarDies) { \
26+
/* Cannot be represented by a int16_t. */ \
27+
expect_bad_scalar_value_dies<ScalarType::Short>(32768); \
28+
} \
29+
\
30+
TEST_F(TEST_SUITE_NAME, FloatTensorTooSmallScalarDies) { \
31+
/* Cannot be represented by a float. */ \
32+
expect_bad_scalar_value_dies<ScalarType::Float>(-3.41e+38); \
33+
} \
34+
\
35+
TEST_F(TEST_SUITE_NAME, FloatTensorTooLargeScalarDies) { \
36+
/* Cannot be represented by a float. */ \
37+
expect_bad_scalar_value_dies<ScalarType::Float>(3.41e+38); \
38+
}

kernels/test/op_fill_test.cpp

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
#include <executorch/kernels/test/FunctionHeaderWrapper.h> // Declares the operator
10+
#include <executorch/kernels/test/ScalarOverflowTestMacros.h>
1011
#include <executorch/kernels/test/TestUtil.h>
1112
#include <executorch/kernels/test/supported_features.h>
1213
#include <executorch/runtime/core/exec_aten/exec_aten.h>
@@ -167,27 +168,4 @@ TEST_F(OpFillTest, MismatchedOutputDtypeDies) {
167168
ET_EXPECT_KERNEL_FAILURE(context_, op_fill_scalar_out(self, 0.0, out));
168169
}
169170

170-
TEST_F(OpFillTest, ByteTensorTooLargeScalarDies) {
171-
// Cannot be represented by a uint8_t.
172-
expect_bad_scalar_value_dies<ScalarType::Byte>(256);
173-
}
174-
175-
TEST_F(OpFillTest, CharTensorTooSmallScalarDies) {
176-
// Cannot be represented by a int8_t.
177-
expect_bad_scalar_value_dies<ScalarType::Char>(-129);
178-
}
179-
180-
TEST_F(OpFillTest, ShortTensorTooLargeScalarDies) {
181-
// Cannot be represented by a int16_t.
182-
expect_bad_scalar_value_dies<ScalarType::Short>(32768);
183-
}
184-
185-
TEST_F(OpFillTest, FloatTensorTooSmallScalarDies) {
186-
// Cannot be represented by a float.
187-
expect_bad_scalar_value_dies<ScalarType::Float>(-3.41e+38);
188-
}
189-
190-
TEST_F(OpFillTest, FloatTensorTooLargeScalarDies) {
191-
// Cannot be represented by a float.
192-
expect_bad_scalar_value_dies<ScalarType::Float>(3.41e+38);
193-
}
171+
GENERATE_SCALAR_OVERFLOW_TESTS(OpFillTest)

kernels/test/targets.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def define_common_targets():
5050
],
5151
exported_headers = [
5252
"BinaryLogicalOpTest.h",
53+
"ScalarOverflowTestMacros.h",
5354
"UnaryUfuncRealHBBF16ToFloatHBF16Test.h",
5455
],
5556
visibility = [

0 commit comments

Comments
 (0)