Skip to content

Commit

Permalink
addressed empty ranges + updated spec
Browse files Browse the repository at this point in the history
  • Loading branch information
Connor committed Dec 6, 2024
1 parent 771e3e4 commit 67ebd95
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
18 changes: 9 additions & 9 deletions spec/std/range_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ describe "Range" do
# Int ranges
(1..5).union(3..7).should eq(1..7) # Overlapping integer ranges
(1..5).union(6..10).should eq(1..10) # Adjacent ranges
(1..5).union(10..15).should eq(nil) # Disjoint integer ranges
(1..5).union(10..15).should eq(1...1) # Disjoint integer ranges

# Float ranges
(1.0..5.5).union(3.2..7.8).should eq(1.0..7.8) # Overlapping float ranges
(1.0..2.5).union(3.0..4.0).should eq(nil) # Non-overlapping float ranges
(1.0..2.5).union(3.0..4.0).should eq(1.0...1.0) # Non-overlapping float ranges

# String ranges
('a'..'e').union('c'..'g').should eq('a'..'g') # Overlapping string ranges
('a'..'c').union('e'..'f').should eq(nil) # Non-overlapping string ranges
('a'..'c').union('e'..'f').should eq('a'...'a') # Non-overlapping string ranges
('a'..'c').union('d'..'f').should eq('a'..'f') # Adjacent string ranges

# Time ranges
Expand All @@ -100,7 +100,7 @@ describe "Range" do
t3 = Time.local(2024, 10, 10)
t4 = Time.local(2024, 10, 15)
(t1..t2).union(t2..t3).should eq(t1..t3) # Adjacent time ranges
(t1..t2).union(t3..t4).should eq(nil) # Disjoint time ranges
(t1..t2).union(t3..t4).should eq(t1...t1) # Disjoint time ranges

# Exclusive ranges
(1...5).union(5...10).should eq(1...10) # Adjacent exclusive integer ranges
Expand All @@ -113,25 +113,25 @@ describe "Range" do
it "does intersection" do
# Int ranges
(1..5).intersection(3..7).should eq(3..5) # Overlapping integer ranges
(1..5).intersection(6..10).should eq(nil) # Non-overlapping integer ranges
(1..5).intersection(6..10).should eq(1...1) # Non-overlapping integer ranges
(1..5).intersection(5..10).should eq(5..5)
# (1...1).intersection(0..10).should eq(nil)
(1...1).intersection(0..10).should eq(1...1)

# Float ranges
(1.0..5.5).intersection(3.2..7.8).should eq(3.2..5.5) # Overlapping float ranges
(1.0..2.5).intersection(3.0..4.0).should eq(nil) # Non-overlapping float ranges
(1.0..2.5).intersection(3.0..4.0).should eq(1.0...1.0) # Non-overlapping float ranges

# String ranges
('a'..'e').intersection('c'..'g').should eq('c'..'e') # Overlapping string ranges
('a'..'c').intersection('d'..'f').should eq(nil) # Non-overlapping string ranges
('a'..'c').intersection('d'..'f').should eq('a'...'a') # Non-overlapping string ranges

# Time ranges
t1 = Time.local(2024, 10, 1)
t2 = Time.local(2024, 10, 5)
t3 = Time.local(2024, 10, 10)
t4 = Time.local(2024, 10, 15)
(t1..t3).intersection(t2..t4).should eq(t2..t3) # Overlapping time ranges
(t1..t2).intersection(t3..t4).should eq(nil) # Non-overlapping time ranges
(t1..t2).intersection(t3..t4).should eq(t1...t1) # Non-overlapping time ranges

# Exclusive ranges
(1...5).intersection(3...7).should eq(3...5) # Overlapping exclusive integer ranges
Expand Down
10 changes: 5 additions & 5 deletions src/range.cr
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,13 @@ struct Range(B, E)
e_s : E = self.end
e_o : E = other.end

return if self.end < other.begin && (!e_s.responds_to?(:succ) || e_s.succ != other.begin)
return if other.end < self.begin && (!e_o.responds_to?(:succ) || e_o.succ != self.begin)
return Range.new(self.begin, self.begin, exclusive: true) if self.end < other.begin && (!e_s.responds_to?(:succ) || e_s.succ != other.begin)
return Range.new(self.begin, self.begin, exclusive: true) if other.end < self.begin && (!e_o.responds_to?(:succ) || e_o.succ != self.begin)

Range.new(
Math.min(self.begin, other.begin),
Math.max(self.end, other.end),
exclusive: {self, other}.max_by(&.end).excludes_end?,
exclusive: self.excludes_end? || other.excludes_end?,
)
end

Expand All @@ -281,12 +281,12 @@ struct Range(B, E)
# (1...10).intersection(7..12) # => 7...10
# ```
def intersection(other : Range)
return if self.end < other.begin || other.end < self.begin
return Range.new(self.begin, self.begin, exclusive: true) if self.end < other.begin || other.end < self.begin

Range.new(
Math.max(self.begin, other.begin),
Math.min(self.end, other.end),
exclusive: {self, other}.max_by(&.end).excludes_end?,
exclusive: self.excludes_end? || other.excludes_end?,
)
end

Expand Down

0 comments on commit 67ebd95

Please sign in to comment.