Skip to content

Commit

Permalink
try fix filecheck
Browse files Browse the repository at this point in the history
Signed-off-by: martinvuyk <[email protected]>
  • Loading branch information
martinvuyk committed Jan 2, 2025
1 parent f46e2d2 commit cc3f26a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
6 changes: 3 additions & 3 deletions stdlib/src/collections/_index_normalization.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,6 @@ fn normalize_index[

@parameter
if ignore_zero_length:
return idx if idx > -1 else (idx + c_len if c_len != 0 else 0)
else:
return idx if idx > -1 else idx + c_len
if c_len == 0:
return 0
return idx if idx > -1 else idx + c_len
23 changes: 22 additions & 1 deletion stdlib/test/collections/test_index_normalization.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# ===----------------------------------------------------------------------=== #
# RUN: %bare-mojo -D ASSERT=warn %s
# RUN: %bare-mojo -D ASSERT=warn %s | FileCheck %s

from collections._index_normalization import normalize_index

Expand Down Expand Up @@ -48,9 +48,30 @@ def _test[branchless: Bool]():
assert_equal(clamp(2, container), 2)
assert_equal(clamp(3, container), 3)

# test clamp to container length overflow
# CHECK: TestContainer has length: 4. Index out of bounds: -8 should be between -4 and 3
assert_equal(clamp(-8, container), 0)
# CHECK: TestContainer has length: 4. Index out of bounds: -7 should be between -4 and 3
assert_equal(clamp(-7, container), 0)
# CHECK: TestContainer has length: 4. Index out of bounds: -6 should be between -4 and 3
assert_equal(clamp(-6, container), 0)
# CHECK: TestContainer has length: 4. Index out of bounds: -5 should be between -4 and 3
assert_equal(clamp(-5, container), 0)
# CHECK: TestContainer has length: 4. Index out of bounds: 4 should be between -4 and 3
assert_equal(clamp(4, container), 3)
# CHECK: TestContainer has length: 4. Index out of bounds: 5 should be between -4 and 3
assert_equal(clamp(5, container), 3)
# CHECK: TestContainer has length: 4. Index out of bounds: 6 should be between -4 and 3
assert_equal(clamp(6, container), 3)
# CHECK: TestContainer has length: 4. Index out of bounds: 7 should be between -4 and 3
assert_equal(clamp(7, container), 3)

# test container with zero length
container = List[Int]()
# CHECK: Indexing into a TestContainer that has 0 elements
_ = clamp(-8, container)
# CHECK: Indexing into a TestContainer that has 0 elements
_ = no_clamp(-8, container)
alias ign_zero_clamp = normalize_index[
t,
ignore_zero_length=True,
Expand Down

0 comments on commit cc3f26a

Please sign in to comment.