flare-floss/tests/test_ranges.py
lakshit verma ae271e6b1a
refactor(tests): drop qs prefix from remaining test modules
Rename test_qs_* to names that match layout/tags/ranges scope after
QuantumStrand removal (test_layout_*, test_oss_db, test_ranges, etc.).
2026-08-03 01:01:37 +05:30

89 lines
2.4 KiB
Python

# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import pytest
from floss.ranges import Range, Slice
def test_range_slice():
r = Range(offset=10, length=20)
assert r.end == 30
# Valid slice
s = r.slice(5, 10)
assert s.offset == 15
assert s.length == 10
# Boundary: offset 0
s = r.slice(0, 5)
assert s.offset == 10
assert s.length == 5
# Boundary: offset + size == length
s = r.slice(15, 5)
assert s.offset == 25
assert s.length == 5
# Boundary: offset == length, size 0
s = r.slice(20, 0)
assert s.offset == 30
assert s.length == 0
# Invalid: offset < 0
with pytest.raises(AssertionError):
r.slice(-1, 5)
# Invalid: size < 0
with pytest.raises(AssertionError):
r.slice(5, -1)
# Invalid: offset > length
with pytest.raises(AssertionError):
r.slice(21, 0)
# Invalid: offset + size > length
with pytest.raises(AssertionError):
r.slice(15, 6)
def test_slice_contains_range():
buf = b"A" * 100
s = Slice(buf=buf, range=Range(offset=10, length=20))
# s covers buf[10:30]
# Valid sub-ranges (relative to slice start)
assert s.contains_range(0, 20) is True
assert s.contains_range(5, 10) is True
assert s.contains_range(0, 0) is True
assert s.contains_range(20, 0) is True # Boundary at the very end
# Invalid: offset < 0
assert s.contains_range(-1, 5) is False
# Invalid: offset > length
assert s.contains_range(21, 0) is False
# Invalid: size < 0
assert s.contains_range(5, -1) is False
# Invalid: offset + size > length
assert s.contains_range(15, 6) is False
assert s.contains_range(20, 1) is False
# Edge case: offset == length, size == 0
assert s.contains_range(20, 0) is True
# Edge case: offset == length, size == -1 (handled by size < 0)
assert s.contains_range(20, -1) is False