Skip to content

Commit 4cfab49

Browse files
authored
Test the floordiv.int and mod.int division identity in prim ops (#25317)
### Summary Follow-up to #25213, covering the test gaps raised in its review. `TestFloordivRoundsTowardNegativeInfinity` gets `{0, -8, 0}` and `{16, -8, -2}`, where the signs differ but the remainder is zero, so the `quot - 1` correction must not apply. A new test, `TestFloordivAndModIntSatisfyDivisionIdentity`, checks `floordiv.int(a, b) * b + mod.int(a, b) == a` now that #25076 and #25213 are both in. `floordiv.int` is added to the wrong-argument-count block, which already covered `floordiv.Scalar`, `mod.int` and `mod.Scalar`. Tests only, no change to the ops. ### Test plan Built with `-DEXECUTORCH_BUILD_TESTS=ON`: ``` cmake --build cmake-out --target kernels_prim_ops_test ./cmake-out/kernels/prim_ops/test/kernels_prim_ops_test ``` 26 tests pass on this branch. With `register_prim_ops.cpp` from e559e66 (before #25213), the two floordiv tests fail. I used Claude to help with this PR. cc @nil-is-all @JakeStevens
1 parent e3b3e86 commit 4cfab49

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

‎kernels/prim_ops/test/prim_ops_test.cpp‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,8 @@ TEST_F(RegisterPrimOpsTest, TestFloordivRoundsTowardNegativeInfinity) {
355355
{-5, -8, 0},
356356
{-17, 6, -3},
357357
{-16, 8, -2},
358+
{0, -8, 0},
359+
{16, -8, -2},
358360
};
359361
for (const char* op_name :
360362
{"executorch_prim::floordiv.int", "executorch_prim::floordiv.Scalar"}) {
@@ -368,6 +370,30 @@ TEST_F(RegisterPrimOpsTest, TestFloordivRoundsTowardNegativeInfinity) {
368370
}
369371
}
370372

373+
TEST_F(RegisterPrimOpsTest, TestFloordivAndModIntSatisfyDivisionIdentity) {
374+
const int64_t cases[][2] = {
375+
{7, 3},
376+
{-5, 8},
377+
{5, -8},
378+
{-5, -8},
379+
{-17, 6},
380+
{-16, 8},
381+
{0, -8},
382+
{16, -8},
383+
};
384+
for (const auto& c : cases) {
385+
EValue values[4] = {
386+
EValue(c[0]), EValue(c[1]), EValue(int64_t(0)), EValue(int64_t(0))};
387+
EValue* div_stack[3] = {&values[0], &values[1], &values[2]};
388+
EValue* mod_stack[3] = {&values[0], &values[1], &values[3]};
389+
getOpsFn("executorch_prim::floordiv.int")(
390+
context_, Span<EValue*>(div_stack));
391+
getOpsFn("executorch_prim::mod.int")(context_, Span<EValue*>(mod_stack));
392+
EXPECT_EQ(values[2].toInt() * c[1] + values[3].toInt(), c[0])
393+
<< "(" << c[0] << ", " << c[1] << ")";
394+
}
395+
}
396+
371397
TEST_F(RegisterPrimOpsTest, TestETCopyIndex) {
372398
EXPECT_TRUE(hasOpsFn("executorch_prim::et_copy_index.tensor"));
373399

@@ -907,6 +933,12 @@ TEST_F(RegisterPrimOpsTest, TestInvalidProgramErrorOnShortStack) {
907933
context_, Span<EValue*>(stack)));
908934
EXPECT_EQ(context_.failure_state(), Error::InvalidProgram);
909935

936+
ET_EXPECT_KERNEL_FAILURE(
937+
context_,
938+
getOpsFn("executorch_prim::floordiv.int")(
939+
context_, Span<EValue*>(stack)));
940+
EXPECT_EQ(context_.failure_state(), Error::InvalidProgram);
941+
910942
ET_EXPECT_KERNEL_FAILURE(
911943
context_,
912944
getOpsFn("executorch_prim::truediv.Scalar")(

0 commit comments

Comments
 (0)