Repository: dotnet/runtime
File: src/libraries/System.Numerics.Tensors/tests/TensorTests.cs (C#)

Code before:
            Assert.Equal(6, t1[1][1, 0, 0]);
            Assert.Equal(7, t1[1][1, 0, 1]);

            t1 = Tensor.Split<int>(t0, 2, 2);
            Assert.Equal([2, 2, 1], t1[0].Lengths);
            Assert.Equal([2, 2, 1], t1[1].Lengths);
            Assert.Equal(0, t1[0][0, 0, 0]);
            Assert.Equal(2, t1[0][0, 1, 0]);
            Assert.Equal(4, t1[0][1, 0, 0]);
            Assert.Equal(6, t1[0][1, 1, 0]);
            Assert.Equal(1, t1[1][0, 0, 0]);
            Assert.Equal(3, t1[1][0, 1, 0]);
            Assert.Equal(5, t1[1][1, 0, 0]);
            Assert.Equal(7, t1[1][1, 1, 0]);
        }

        [Fact]
        public static void TensorReverseTests()
        {
            // Helper: verify Reverse correctness for any shape
            static void AssertReverseCorrect(nint[] shape)
            {
                int totalLength = 1;
                foreach (var s in shape)
                    totalLength *= (int)s;

                // Use 1-based values so 0 (default) is never a valid value - makes bugs obvious
                int[] data = new int[totalLength];
                for (int i = 0; i < totalLength; i++)
                    data[i] = i + 1;

                var tensor = Tensor.Create<int>(data, shape);
                var reversed = Tensor.Reverse<int>(tensor);

                // Shape must be preserved
                Assert.Equal(tensor.Lengths.ToArray(), reversed.Lengths.ToArray());

                // Flattened elements should be in reverse order
                var flatOriginal = new int[totalLength];
                tensor.FlattenTo(flatOriginal);
                Array.Reverse(flatOriginal);

                var actualFlat = new int[totalLength];
                reversed.FlattenTo(actualFlat);

                Assert.Equal(flatOriginal, actualFlat);
            }

            // Test the reproduction case from issue #124105
            AssertReverseCorrect([1, 3]);


Review comment (bug):
The product of dimensions is being accumulated in an `int` with unchecked multiplication and a cast from `nint` to `int`. Even though the current test cases are small, this pattern can silently overflow if additional larger shapes are added later. Consider using `checked` and/or accumulate in `nint`/`long` (matching the `nint[] shape`) before allocating arrays.
```suggestion
                nint totalLengthNative = 1;
                foreach (var s in shape)
                    totalLengthNative = checked(totalLengthNative * s);

                int totalLength = checked((int)totalLengthNative);
```

Code after:
            Assert.Equal(6, t1[1][1, 0, 0]);
            Assert.Equal(7, t1[1][1, 0, 1]);

            t1 = Tensor.Split<int>(t0, 2, 2);
            Assert.Equal([2, 2, 1], t1[0].Lengths);
            Assert.Equal([2, 2, 1], t1[1].Lengths);
            Assert.Equal(0, t1[0][0, 0, 0]);
            Assert.Equal(2, t1[0][0, 1, 0]);
            Assert.Equal(4, t1[0][1, 0, 0]);
            Assert.Equal(6, t1[0][1, 1, 0]);
            Assert.Equal(1, t1[1][0, 0, 0]);
            Assert.Equal(3, t1[1][0, 1, 0]);
            Assert.Equal(5, t1[1][1, 0, 0]);
            Assert.Equal(7, t1[1][1, 1, 0]);
        }

        [Fact]
        public static void TensorReverseTests()
        {
            // Helper: verify Reverse correctness for any shape
            static void AssertReverseCorrect(nint[] shape)
            {
                nint totalLengthNative = 1;
                foreach (var s in shape)
                    totalLengthNative = checked(totalLengthNative * s);

                int totalLength = checked((int)totalLengthNative);

                // Use 1-based values so 0 (default) is never a valid value - makes bugs obvious
                int[] data = new int[totalLength];
                for (int i = 0; i < totalLength; i++)
                    data[i] = i + 1;

                var tensor = Tensor.Create<int>(data, shape);
                var reversed = Tensor.Reverse<int>(tensor);

                // Shape must be preserved
                Assert.Equal(tensor.Lengths.ToArray(), reversed.Lengths.ToArray());

                // Flattened elements should be in reverse order
                var flatOriginal = new int[totalLength];
                tensor.FlattenTo(flatOriginal);
                Array.Reverse(flatOriginal);

                var actualFlat = new int[totalLength];
                reversed.FlattenTo(actualFlat);

                Assert.Equal(flatOriginal, actualFlat);
            }

            // Test the reproduction case from issue #124105