|
13 | 13 |
|
14 | 14 | using namespace executorch::backends::aoti::slim::c10; |
15 | 15 |
|
16 | | -class ScalarTypeTest : public ::testing::Test {}; |
| 16 | +// ============================================================================= |
| 17 | +// Test Data Structures for Parameterized Tests |
| 18 | +// ============================================================================= |
17 | 19 |
|
18 | | -TEST_F(ScalarTypeTest, FloatEnumValue) { |
19 | | - // Verify Float has the correct enum value (6) to match PyTorch |
20 | | - EXPECT_EQ(static_cast<int>(ScalarType::Float), 6); |
| 20 | +struct ScalarTypeTestData { |
| 21 | + ScalarType dtype; |
| 22 | + int expected_enum_value; |
| 23 | + size_t expected_element_size; |
| 24 | + const char* expected_name; |
| 25 | + bool is_floating; |
| 26 | + bool is_integral; |
| 27 | + bool is_integral_with_bool; |
| 28 | + bool is_bool; |
| 29 | +}; |
| 30 | + |
| 31 | +// All supported scalar types with their expected properties |
| 32 | +const std::vector<ScalarTypeTestData> kAllScalarTypes = { |
| 33 | + // dtype, enum_value, element_size, name, is_float, is_int, is_int_w_bool, |
| 34 | + // is_bool |
| 35 | + {ScalarType::Char, 1, 1, "Char", false, true, true, false}, |
| 36 | + {ScalarType::Short, 2, 2, "Short", false, true, true, false}, |
| 37 | + {ScalarType::Int, 3, 4, "Int", false, true, true, false}, |
| 38 | + {ScalarType::Long, 4, 8, "Long", false, true, true, false}, |
| 39 | + {ScalarType::Float, 6, 4, "Float", true, false, false, false}, |
| 40 | + {ScalarType::Bool, 11, 1, "Bool", false, false, true, true}, |
| 41 | + {ScalarType::BFloat16, 15, 2, "BFloat16", true, false, false, false}, |
| 42 | +}; |
| 43 | + |
| 44 | +// ============================================================================= |
| 45 | +// Parameterized Test Fixture |
| 46 | +// ============================================================================= |
| 47 | + |
| 48 | +class ScalarTypeParamTest |
| 49 | + : public ::testing::TestWithParam<ScalarTypeTestData> {}; |
| 50 | + |
| 51 | +TEST_P(ScalarTypeParamTest, EnumValue) { |
| 52 | + const auto& data = GetParam(); |
| 53 | + EXPECT_EQ(static_cast<int>(data.dtype), data.expected_enum_value) |
| 54 | + << "Failed for dtype: " << toString(data.dtype); |
| 55 | +} |
| 56 | + |
| 57 | +TEST_P(ScalarTypeParamTest, ElementSize) { |
| 58 | + const auto& data = GetParam(); |
| 59 | + EXPECT_EQ(elementSize(data.dtype), data.expected_element_size) |
| 60 | + << "Failed for dtype: " << toString(data.dtype); |
| 61 | +} |
| 62 | + |
| 63 | +TEST_P(ScalarTypeParamTest, ToString) { |
| 64 | + const auto& data = GetParam(); |
| 65 | + EXPECT_STREQ(toString(data.dtype), data.expected_name) |
| 66 | + << "Failed for dtype: " << toString(data.dtype); |
| 67 | +} |
| 68 | + |
| 69 | +TEST_P(ScalarTypeParamTest, IsFloatingType) { |
| 70 | + const auto& data = GetParam(); |
| 71 | + EXPECT_EQ(isFloatingType(data.dtype), data.is_floating) |
| 72 | + << "Failed for dtype: " << toString(data.dtype); |
| 73 | +} |
| 74 | + |
| 75 | +TEST_P(ScalarTypeParamTest, IsIntegralTypeWithoutBool) { |
| 76 | + const auto& data = GetParam(); |
| 77 | + EXPECT_EQ(isIntegralType(data.dtype, false), data.is_integral) |
| 78 | + << "Failed for dtype: " << toString(data.dtype); |
| 79 | +} |
| 80 | + |
| 81 | +TEST_P(ScalarTypeParamTest, IsIntegralTypeWithBool) { |
| 82 | + const auto& data = GetParam(); |
| 83 | + EXPECT_EQ(isIntegralType(data.dtype, true), data.is_integral_with_bool) |
| 84 | + << "Failed for dtype: " << toString(data.dtype); |
| 85 | +} |
| 86 | + |
| 87 | +TEST_P(ScalarTypeParamTest, IsBoolType) { |
| 88 | + const auto& data = GetParam(); |
| 89 | + EXPECT_EQ(isBoolType(data.dtype), data.is_bool) |
| 90 | + << "Failed for dtype: " << toString(data.dtype); |
| 91 | +} |
| 92 | + |
| 93 | +TEST_P(ScalarTypeParamTest, StreamOperator) { |
| 94 | + const auto& data = GetParam(); |
| 95 | + std::ostringstream oss; |
| 96 | + oss << data.dtype; |
| 97 | + EXPECT_EQ(oss.str(), data.expected_name) |
| 98 | + << "Failed for dtype: " << toString(data.dtype); |
| 99 | +} |
| 100 | + |
| 101 | +INSTANTIATE_TEST_SUITE_P( |
| 102 | + AllTypes, |
| 103 | + ScalarTypeParamTest, |
| 104 | + ::testing::ValuesIn(kAllScalarTypes), |
| 105 | + [](const ::testing::TestParamInfo<ScalarTypeTestData>& info) { |
| 106 | + return std::string(info.param.expected_name); |
| 107 | + }); |
| 108 | + |
| 109 | +// ============================================================================= |
| 110 | +// Type Constant Tests |
| 111 | +// ============================================================================= |
| 112 | + |
| 113 | +class ScalarTypeConstantsTest : public ::testing::Test {}; |
| 114 | + |
| 115 | +TEST_F(ScalarTypeConstantsTest, KCharConstant) { |
| 116 | + EXPECT_EQ(kChar, ScalarType::Char); |
| 117 | +} |
| 118 | + |
| 119 | +TEST_F(ScalarTypeConstantsTest, KShortConstant) { |
| 120 | + EXPECT_EQ(kShort, ScalarType::Short); |
21 | 121 | } |
22 | 122 |
|
23 | | -TEST_F(ScalarTypeTest, KFloatConstant) { |
24 | | - // Verify kFloat constant |
| 123 | +TEST_F(ScalarTypeConstantsTest, KIntConstant) { |
| 124 | + EXPECT_EQ(kInt, ScalarType::Int); |
| 125 | +} |
| 126 | + |
| 127 | +TEST_F(ScalarTypeConstantsTest, KLongConstant) { |
| 128 | + EXPECT_EQ(kLong, ScalarType::Long); |
| 129 | +} |
| 130 | + |
| 131 | +TEST_F(ScalarTypeConstantsTest, KFloatConstant) { |
25 | 132 | EXPECT_EQ(kFloat, ScalarType::Float); |
26 | 133 | } |
27 | 134 |
|
28 | | -TEST_F(ScalarTypeTest, ElementSizeFloat) { |
29 | | - // Verify elementSize returns correct size for Float (4 bytes) |
30 | | - EXPECT_EQ(elementSize(ScalarType::Float), sizeof(float)); |
31 | | - EXPECT_EQ(elementSize(ScalarType::Float), 4); |
| 135 | +TEST_F(ScalarTypeConstantsTest, KBoolConstant) { |
| 136 | + EXPECT_EQ(kBool, ScalarType::Bool); |
32 | 137 | } |
33 | 138 |
|
34 | | -TEST_F(ScalarTypeTest, ToStringFloat) { |
35 | | - // Verify toString returns correct string for Float |
36 | | - EXPECT_STREQ(toString(ScalarType::Float), "Float"); |
| 139 | +TEST_F(ScalarTypeConstantsTest, KBFloat16Constant) { |
| 140 | + EXPECT_EQ(kBFloat16, ScalarType::BFloat16); |
37 | 141 | } |
38 | 142 |
|
39 | | -TEST_F(ScalarTypeTest, ToStringUndefined) { |
40 | | - // Verify toString returns correct string for Undefined |
| 143 | +// ============================================================================= |
| 144 | +// Edge Cases and Special Values |
| 145 | +// ============================================================================= |
| 146 | + |
| 147 | +class ScalarTypeEdgeCasesTest : public ::testing::Test {}; |
| 148 | + |
| 149 | +TEST_F(ScalarTypeEdgeCasesTest, UndefinedToString) { |
41 | 150 | EXPECT_STREQ(toString(ScalarType::Undefined), "Undefined"); |
42 | 151 | } |
43 | 152 |
|
44 | | -TEST_F(ScalarTypeTest, IsFloatingType) { |
45 | | - // Verify isFloatingType works correctly |
46 | | - EXPECT_TRUE(isFloatingType(ScalarType::Float)); |
| 153 | +TEST_F(ScalarTypeEdgeCasesTest, UndefinedIsNotFloating) { |
| 154 | + EXPECT_FALSE(isFloatingType(ScalarType::Undefined)); |
47 | 155 | } |
48 | 156 |
|
49 | | -TEST_F(ScalarTypeTest, IsIntegralType) { |
50 | | - // Verify isIntegralType works correctly |
51 | | - // Currently no integral types are supported, so Float should return false |
52 | | - EXPECT_FALSE(isIntegralType(ScalarType::Float, false)); |
53 | | - EXPECT_FALSE(isIntegralType(ScalarType::Float, true)); |
| 157 | +TEST_F(ScalarTypeEdgeCasesTest, UndefinedIsNotIntegral) { |
| 158 | + EXPECT_FALSE(isIntegralType(ScalarType::Undefined, false)); |
| 159 | + EXPECT_FALSE(isIntegralType(ScalarType::Undefined, true)); |
54 | 160 | } |
55 | 161 |
|
56 | | -TEST_F(ScalarTypeTest, StreamOperator) { |
57 | | - // Verify stream operator works |
58 | | - std::ostringstream oss; |
59 | | - oss << ScalarType::Float; |
60 | | - EXPECT_EQ(oss.str(), "Float"); |
| 162 | +TEST_F(ScalarTypeEdgeCasesTest, UndefinedIsNotBool) { |
| 163 | + EXPECT_FALSE(isBoolType(ScalarType::Undefined)); |
| 164 | +} |
| 165 | + |
| 166 | +// ============================================================================= |
| 167 | +// Element Size Consistency Tests |
| 168 | +// ============================================================================= |
| 169 | + |
| 170 | +class ElementSizeConsistencyTest : public ::testing::Test {}; |
| 171 | + |
| 172 | +TEST_F(ElementSizeConsistencyTest, CharMatchesSizeofInt8) { |
| 173 | + EXPECT_EQ(elementSize(ScalarType::Char), sizeof(int8_t)); |
| 174 | +} |
| 175 | + |
| 176 | +TEST_F(ElementSizeConsistencyTest, ShortMatchesSizeofInt16) { |
| 177 | + EXPECT_EQ(elementSize(ScalarType::Short), sizeof(int16_t)); |
| 178 | +} |
| 179 | + |
| 180 | +TEST_F(ElementSizeConsistencyTest, IntMatchesSizeofInt32) { |
| 181 | + EXPECT_EQ(elementSize(ScalarType::Int), sizeof(int32_t)); |
| 182 | +} |
| 183 | + |
| 184 | +TEST_F(ElementSizeConsistencyTest, LongMatchesSizeofInt64) { |
| 185 | + EXPECT_EQ(elementSize(ScalarType::Long), sizeof(int64_t)); |
| 186 | +} |
| 187 | + |
| 188 | +TEST_F(ElementSizeConsistencyTest, FloatMatchesSizeofFloat) { |
| 189 | + EXPECT_EQ(elementSize(ScalarType::Float), sizeof(float)); |
| 190 | +} |
| 191 | + |
| 192 | +TEST_F(ElementSizeConsistencyTest, BoolMatchesSizeofBool) { |
| 193 | + EXPECT_EQ(elementSize(ScalarType::Bool), sizeof(bool)); |
| 194 | +} |
| 195 | + |
| 196 | +TEST_F(ElementSizeConsistencyTest, BFloat16MatchesSizeofBFloat16) { |
| 197 | + EXPECT_EQ(elementSize(ScalarType::BFloat16), sizeof(BFloat16)); |
61 | 198 | } |
0 commit comments