Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 83 lines (63 sloc) 1.599 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
#!/usr/bin/env ruby

require 'test/unit'

class RangeTest < Test::Unit::TestCase
  
  def test_inclusive
    range = 0..10
    assert_equal(true, range.include?(0))
    assert_equal(true, range.include?(10))
    assert_equal(false, range.include?(11))
  end

  def test_exclusive
    range = 0...10
    assert_equal(true, range.include?(0))
    assert_equal(false, range.include?(10))
  end
  
  def test_min_and_max
    range = 0..10
    assert_equal(0, range.min)
    assert_equal(10, range.max)
  end
  
  def test_to_array
    assert_equal([0, 1, 2], (0..2).to_a)
    assert_equal([0, 1], (0...2).to_a)
  end
  
  def test_reject
    range = 0..10
    accepted = range.reject { |i| i > 2 }
    assert_equal([0, 1, 2], accepted)
  end
  
  def test_each
    range = 0..3
    result = []
    range.each { |i| result << i }
    assert_equal([0, 1, 2, 3], result)
  end
  
  def test_interval
    range = 0..3
    assert_equal(true, range.member?(3))
    assert_equal(true, range === 3)

    range = 0...3
    assert_equal(false, range === 3)
    assert_equal(false, range.member?(3))
  end

  def test_range_of_objects
    medium = Volume.new(4)..Volume.new(7)
    assert_equal(false, medium.include?(Volume.new(3)))
    assert_equal(true, medium.include?(Volume.new(5)))
  end
  
end

class Volume

  include Comparable

  attr :volume

  def initialize(volume) # 0..9
    @volume = volume
  end

  # Support for ranges

  def <=>(other)
    self.volume <=> other.volume
  end

  def succ
    raise(IndexError, "Volume too big") if @volume >= 9
    Volume.new(@volume.succ)
  end
end
Something went wrong with that request. Please try again.