Permalink
Browse files

Initial import

  • Loading branch information...
0 parents commit db9f1ede13c70a2f5477a9a0b86d06cf8addc5a7 @clarkware committed Aug 21, 2008
21 README
@@ -0,0 +1,21 @@
+= Ruby Learning Tests
+
+Unit tests I wrote way back when I started learning Ruby,
+as described on my blog at:
+
+ http://clarkware.com/cgi/blosxom/2005/03/18#RLT1
+
+== Usage
+
+To run all the tests, use:
+
+ rake
+
+== Write Your Own!
+
+This project can be found on GitHub at the following URL.
+
+ http://github.com/clarkware/ruby-learning-tests/
+
+If you would like to write your own tests, please fork the
+repository and have a lot of fun!
@@ -0,0 +1,19 @@
+# Rakefile -*- ruby -*-
+
+require 'rake/testtask'
+
+task :default => [:test]
+
+desc "Run all the tests"
+Rake::TestTask.new("test") do |t|
+ t.pattern = '*_test.rb'
+end
+task :test
+
+task :task_info do |t|
+ puts t.name
+end
+
+task :environment_variable do
+ puts ENV['HOME']
+end
@@ -0,0 +1,148 @@
+#!/usr/bin/env ruby
+
+require 'test/unit'
+
+class ArrayTest < Test::Unit::TestCase
+
+ def test_new_array_literal
+ a = [3.14, "pie", 99]
+ assert_equal(Array, a.class)
+ assert_equal(3, a.length)
+ assert_equal(3.14, a[0])
+ assert_equal("pie", a[1])
+ assert_equal(99, a[2])
+ assert_nil(a[3])
+ end
+
+ def test_new_array
+ a = Array.new
+ assert_equal(Array, a.class)
+ a[0] = 3.14
+ a[1] = "pie"
+ a[2] = 99
+ assert_equal(3, a.length)
+ assert_equal([3.14, "pie", 99], a)
+ end
+
+ def test_new_array_with_block
+ a = Array.new(3) { |i| i + 2 }
+ assert_equal([2, 3, 4], a)
+ end
+
+ def test_array_of_words
+ a = %w{ ireland scotland england }
+ assert_equal(["ireland", "scotland", "england"], a)
+ end
+
+ def test_index_negative
+ a = [ 1, 3, 5 ]
+ assert_equal(5, a[-1])
+ assert_equal(3, a[-2])
+ assert_equal(1, a[-3])
+ assert_nil(a[-99])
+ end
+
+ def test_index_by_start_and_count
+ a = [ 1, 3, 5, 7 ]
+ assert_equal([1], a[0, 1])
+ assert_equal([1, 3], a[0, 2])
+ assert_equal([3, 5, 7], a[1, 3])
+ assert_equal([7], a[3, 1])
+ assert_equal([3, 5], a[-3, 2])
+ end
+
+ def test_index_by_range
+ a = [ 1, 3, 5, 7 ]
+ assert_equal([1, 3, 5], a[0..2])
+ assert_equal([1, 3], a[0...2])
+ assert_equal([3], a[1..1])
+ assert_equal([], a[1...1])
+ assert_equal([3, 5, 7], a[-3..-1])
+ end
+
+ def test_single_index_change
+ a = [ 1, 3, 5, 7, 9 ]
+
+ a[1] = "bat"
+ assert_equal([1, "bat", 5, 7, 9], a)
+
+ a[-3] = "cat"
+ assert_equal([1, "bat", "cat", 7, 9], a)
+
+ a[3] = [9, 8]
+ assert_equal([1, "bat", "cat", [9, 8], 9], a)
+
+ a[6] = 99
+ assert_equal([1, "bat", "cat", [9, 8], 9, nil, 99], a)
+ end
+
+ def test_range_change
+ a = [ 1, 3, 5, 7, 9 ]
+
+ a[2, 2] = 'cat'
+ assert_equal([1, 3, "cat", 9], a)
+
+ a[2, 0] = 'dog'
+ assert_equal([1, 3, "dog", "cat", 9], a)
+
+ a[1, 1] = [9, 8, 7]
+ assert_equal([1, 9, 8, 7, "dog", "cat", 9], a)
+
+ a[0..3] = []
+ assert_equal(["dog", "cat", 9], a)
+
+ a[5..6] = 99, 98
+ assert_equal(["dog", "cat", 9, nil, nil, 99, 98], a)
+ end
+
+ def test_clear
+ a = [1, 2, 3]
+ assert_equal([], a.clear())
+ end
+
+ def test_push_pop_shift
+ a = Array.new
+ a.push("A")
+ a.push("B")
+ a.push("C")
+ assert_equal(["A", "B", "C"], a)
+ assert_equal("A", a.shift)
+ assert_equal("C", a.pop)
+ assert_equal("B", a.pop)
+ assert_equal(nil, a.pop)
+ end
+
+ def test_each
+ a = ["A", "B", "C"]
+ s = ""
+ a.each { |element| s += element }
+ assert_equal("ABC", s)
+ end
+
+ def test_sort
+ a = [3, 1, 7, 0]
+ assert_equal([0, 1, 3, 7], a.sort)
+ end
+
+ def test_reverse
+ a = [3, 1, 7, 0]
+ assert_equal([0, 7, 1, 3], a.reverse)
+ end
+
+ def test_join
+ a = ["a", "b", "c"]
+ assert_equal("abc", a.join)
+ assert_equal("a-b-c", a.join("-"))
+ end
+
+ def test_array_of_hashes
+ a = Array.new(size=2) { Hash.new(0) }
+ assert_equal([{}, {}], a)
+ end
+
+ def test_array_of_arrays
+ a = Array.new(size=2) { Array.new(count=1, obj="A") }
+ assert_equal([["A"], ["A"]], a)
+ end
+
+end
@@ -0,0 +1,80 @@
+#!/usr/bin/env ruby
+
+require 'test/unit'
+
+class AssignmentTest < Test::Unit::TestCase
+
+ def test_chained
+ a = b = 1 + 2
+ assert_equal(3, a)
+ assert_equal(3, b)
+ assert_equal(3, a = b = 3)
+ end
+
+ def test_parallel
+ a = 1
+ b = 2
+ a, b = b, a
+ assert_equal(2, a)
+ assert_equal(1, b)
+ end
+
+ def test_more_lvalues_than_rvalues
+ a, b = 1
+ assert_equal(1, a)
+ assert_equal(nil, b)
+ end
+
+ def test_more_rvalues_than_lvalues
+ a, b = 1, 2, 3
+ assert_equal(1, a)
+ assert_equal(2, b)
+ end
+
+ def test_one_lvalue_many_rvalues
+ a = 1, 2, 3
+ assert_equal([1, 2, 3], a)
+ end
+
+ def test_array
+ a = [1, 2, 3]
+ b, c = a
+ assert_equal(1, b)
+ assert_equal(2, c)
+
+ b, c = 99, a
+ assert_equal(99, b)
+ assert_equal([1, 2, 3], c)
+ end
+
+ def test_collapse_array
+ a = [1, 2, 3]
+ b, *c = a
+ assert_equal(1, b)
+ assert_equal([2, 3], c)
+ end
+
+ def test_expand_array
+ a = [1, 2, 3]
+ b, c = 99, *a
+ assert_equal(99, b)
+ assert_equal(1, c)
+ end
+
+ def test_nested
+ a, (b, c), d = 1, 2, 3, 4
+ assert_equal(1, a)
+ assert_equal(2, b)
+ assert_equal(nil, c)
+ assert_equal(3, d)
+ end
+
+ def test_nested_array
+ a, (b, c), d = 1, [2, 3, 4], 5
+ assert_equal(1, a)
+ assert_equal(2, b)
+ assert_equal(3, c)
+ assert_equal(5, d)
+ end
+
+end
@@ -0,0 +1,24 @@
+#!/usr/bin/env ruby
+
+require "benchmark"
+
+Benchmark.bm(label_width = 10) do |test|
+
+ loops = 50000
+
+ test.report("for:") do
+ y = 0
+ (1..loops).each { |x| y += x }
+ end
+
+ test.report("times:") do
+ y = 0
+ loops.times { |x| y += x }
+ end
+
+ test.report("upto:") do
+ y = 0
+ 1.upto(loops) { |x| y += x }
+ end
+
+end
@@ -0,0 +1,86 @@
+#!/usr/bin/env ruby
+
+require 'test/unit'
+
+class BlockTest < Test::Unit::TestCase
+
+ def test_call_block_twice
+ x = 0
+ call_block_twice { x += 1 }
+ assert_equal(2, x)
+ end
+
+ def call_block_twice
+ yield
+ yield
+ end
+
+ def test_call_block_with_one_parameter
+ x = 0
+ call_block_with_one_param(1) { |y| x = y }
+ assert_equal(1, x)
+ end
+
+ def call_block_with_one_param(y)
+ yield y
+ end
+
+ def test_call_block_with_two_params
+ z = 0
+ call_block_with_two_params(1, 2) { |x, y| z = x + y }
+ assert_equal(3, z)
+ end
+
+ def call_block_with_two_params(x, y)
+ yield x, y
+ end
+
+ def test_call_block_with_unchanged_local_variable
+ x = 0
+ call_block_with_unchanged_local { |y| x = y = y+1 }
+ assert_equal(2, x)
+ end
+
+ def call_block_with_unchanged_local
+ y = 1
+ yield y
+ assert_equal(1, y)
+ end
+
+ def test_block_given
+ b = take_block("no block")
+ assert_equal(false, b)
+
+ b = take_block("block") { |param| param }
+ assert_equal(true, b)
+ end
+
+ def take_block(param)
+ if block_given?
+ yield true
+ else
+ false
+ end
+ end
+
+ def test_proc
+ y = 0
+ call_proc(1) { |x| y = x }
+ assert_equal(1, y)
+ end
+
+ def call_proc(x, &action)
+ action.call(x)
+ end
+
+ def test_closure
+ p = n_times(2)
+ assert_equal(4, p.call(2))
+ assert_equal(6, p.call(3))
+ end
+
+ def n_times(thing)
+ return proc { |n| thing * n }
+ end
+
+end
Oops, something went wrong.

0 comments on commit db9f1ed

Please sign in to comment.