Skip to content

Commit ee15559

Browse files
committed
- Reverted the incorrectly replaced resize and fixed some flawed initializations.
- Add Shape unittests.
1 parent a99b62a commit ee15559

3 files changed

Lines changed: 369 additions & 4 deletions

File tree

runtime/compute/cker/include/cker/Shape.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class Shape
6060
}
6161

6262
// Constructor that creates a shape from an array of dimension data.
63-
Shape(int dimensions_count, const int32_t *dims_data) : _size(0)
63+
Shape(int dimensions_count, const int32_t *dims_data) : _size(dimensions_count)
6464
{
6565
initStorage(dimensions_count);
6666
ReplaceWith(dimensions_count, dims_data);
@@ -205,6 +205,7 @@ class Shape
205205
// Replaces the current shape with a new one defined by dimensions_count and dims_data.
206206
inline void ReplaceWith(int dimensions_count, const int32_t *dims_data)
207207
{
208+
assert(dims_data != nullptr);
208209
Resize(dimensions_count);
209210
std::memcpy(DimsData(), dims_data, dimensions_count * sizeof(int32_t));
210211
}
@@ -266,6 +267,7 @@ class Shape
266267
// Helper function: initialize dims_ storage based on the number of dimensions.
267268
inline void initStorage(int dimensions_count)
268269
{
270+
assert(dimensions_count > 0);
269271
if (dimensions_count <= kMaxSmallSize)
270272
dims_ = std::array<int32_t, kMaxSmallSize>{};
271273
else
@@ -275,19 +277,18 @@ class Shape
275277
// For use only by ExtendedShape(), written to guarantee (return-value) copy
276278
// elision in C++17.
277279
// This creates a shape padded to the desired size with the specified value.
278-
Shape(int new_shape_size, const Shape &shape, int pad_value) : _size(0)
280+
Shape(int new_shape_size, const Shape &shape, int pad_value) : _size(new_shape_size)
279281
{
280282
assert(new_shape_size >= shape.DimensionsCount());
281283
assert(new_shape_size <= kMaxSmallSize);
282-
initStorage(new_shape_size);
284+
Resize(new_shape_size);
283285
const int size_increase = new_shape_size - shape.DimensionsCount();
284286
for (int i = 0; i < size_increase; ++i)
285287
{
286288
SetDim(i, pad_value);
287289
}
288290
std::memcpy(DimsData() + size_increase, shape.DimsData(),
289291
sizeof(int32_t) * shape.DimensionsCount());
290-
_size = new_shape_size;
291292
}
292293

293294
int32_t _size;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef __DEATH_TEST_MACROS_H__
18+
#define __DEATH_TEST_MACROS_H__
19+
20+
#include <gtest/gtest.h>
21+
22+
// In release mode, assertions might not trigger abort() via assert(),
23+
// so we use EXPECT_EXIT and require that the statement exits with EXIT_FAILURE.
24+
// In debug mode, we use EXPECT_DEATH as usual.
25+
#ifdef NDEBUG
26+
#define EXPECT_DEATH_DEBUG_ONLY(statement, regex)
27+
#else
28+
#define EXPECT_DEATH_DEBUG_ONLY(statement, regex) \
29+
EXPECT_EXIT(statement, ::testing::KilledBySignal(SIGABRT), regex)
30+
#endif
31+
32+
#endif // __DEATH_TEST_MACROS_H__

0 commit comments

Comments
 (0)