diff --git a/lib/sorted_containers/sorted_array.rb b/lib/sorted_containers/sorted_array.rb index add5356..e2abefb 100644 --- a/lib/sorted_containers/sorted_array.rb +++ b/lib/sorted_containers/sorted_array.rb @@ -50,6 +50,16 @@ def multiply(num) end alias * multiply + # Returns a new SortedArray with the values from both arrays. + # + # @param other [SortedArray] The other array to add. + # @return [SortedArray] The combined array. + def +(other) + new_instance = self.class.new + new_instance.update(to_a + other.to_a) + new_instance + end + # rubocop:disable Metrics/MethodLength # Adds a value to the sorted array. diff --git a/spec/sorted_array_spec.rb b/spec/sorted_array_spec.rb index 1c8cf74..3a24d80 100644 --- a/spec/sorted_array_spec.rb +++ b/spec/sorted_array_spec.rb @@ -27,6 +27,52 @@ end end + describe "*" do + it "should multiply the array by the given number" do + array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5]) + array *= 2 + expect(array.to_a).to eq([1, 1, 2, 2, 3, 3, 4, 4, 5, 5]) + end + + it "should multiply the array by the given large number" do + array = SortedContainers::SortedArray.new((1..1000).to_a) + array *= 2 + expect(array.to_a).to eq(((1..1000).to_a * 2).sort) + end + end + + describe "+" do + it "should concatenate two arrays" do + array1 = SortedContainers::SortedArray.new([1, 2, 3, 4, 5]) + array2 = SortedContainers::SortedArray.new([6, 7, 8, 9, 10]) + expect((array1 + array2).to_a).to eq([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) + end + + it "should concatenate two arrays with duplicates and sort them" do + array1 = SortedContainers::SortedArray.new([1, 2, 3, 4, 5]) + array2 = SortedContainers::SortedArray.new([3, 4, 5, 6, 7]) + expect((array1 + array2).to_a).to eq([1, 2, 3, 3, 4, 4, 5, 5, 6, 7]) + end + + it "should concatenate an empty array with a non-empty array" do + array1 = SortedContainers::SortedArray.new + array2 = SortedContainers::SortedArray.new([1, 2, 3, 4, 5]) + expect((array1 + array2).to_a).to eq([1, 2, 3, 4, 5]) + end + + it "should concatenate a non-empty array with an empty array" do + array1 = SortedContainers::SortedArray.new([1, 2, 3, 4, 5]) + array2 = SortedContainers::SortedArray.new + expect((array1 + array2).to_a).to eq([1, 2, 3, 4, 5]) + end + + it "should concatenate two empty arrays" do + array1 = SortedContainers::SortedArray.new + array2 = SortedContainers::SortedArray.new + expect((array1 + array2).to_a).to eq([]) + end + end + describe "add" do it "sorts items after being added in an arbitrary order" do array = SortedContainers::SortedArray.new @@ -401,20 +447,6 @@ end end - describe "*" do - it "should multiply the array by the given number" do - array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5]) - array *= 2 - expect(array.to_a).to eq([1, 1, 2, 2, 3, 3, 4, 4, 5, 5]) - end - - it "should multiply the array by the given large number" do - array = SortedContainers::SortedArray.new((1..1000).to_a) - array *= 2 - expect(array.to_a).to eq(((1..1000).to_a * 2).sort) - end - end - describe "min" do it "should return the minimum value in the array" do array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])