-
-
Save anonymous/7e9907ed99138b5ae550 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## MyEnumerable.rb, which includes my redefined Enumerable methods: | |
module Enumerable | |
def all? | |
# If the given condition is false for any element, return false. | |
self.each do |x| | |
return false if !yield(x) | |
end | |
return true | |
end | |
def any? | |
return false | |
end | |
end | |
## tc_Enumerable.rb, which includes my test cases: | |
require "test/unit" | |
require_relative "MyEnumerable.rb" | |
class ExampleEnumerable | |
include Enumerable | |
def each | |
yield "a" | |
yield "b" | |
yield "c" | |
end | |
end | |
class TestEnumerable < Test::Unit::TestCase | |
def setup | |
@int_arr = [1, 2, 3, 100] | |
@int_arr2 = [1, 2, 3, 1] | |
@mixed_arr = ["", 500, 910.4, nil, {}] | |
@string_arr = ["Prof.", "Ding", "is", "the", "man"] | |
end | |
def test_all? | |
# All values are positive | |
assert(@int_arr.all? { |num| num > 0 }) | |
# Not all values are > 1 | |
assert_equal(false, @int_arr.all? { |num| num > 1 }) | |
# Not all values are non-nil | |
assert_equal(false, @mixed_arr.all? { |x| x != nil }) | |
# All strings are at least 2 chars long | |
assert(@string_arr.all? { |str| str.length >= 2}) | |
end | |
def test_any? | |
# One value is > 50 | |
assert(@int_arr.any? { |num| num > 50 }) | |
# One value is nil | |
assert(@mixed_arr.any? { |x| x.nil? }) | |
# Some strings start with capital letters | |
assert(@string_arr.any? { |str| str[0..0] =~ /[A-Z]/}) | |
assert_equal(false, @int_arr.any? { |num| num < 0}) | |
end | |
def test_collect | |
assert_equal([1, 2, 3, 100], @int_arr.collect {|x| x}) | |
assert_equal([false, true, true, false, false], | |
@mixed_arr.collect {|x| x.is_a? Numeric}) | |
assert_equal([5, 4, 2, 3, 3], @string_arr.collect {|x| x.length}) | |
end | |
def test_collect_concat | |
assert_equal([1, 1, 1, 1], @int_arr.collect_concat { [1] }) | |
assert_equal([1, 2, 3, 100], @int_arr.collect_concat {|x| x}) | |
assert_equal([1, 2, 3, 100], @int_arr.collect_concat {|x| [x]}) | |
end | |
def test_count | |
# int_arr has 4 elements | |
assert_equal(4, @int_arr.count) | |
# int_arr2 has 2 elements equal to 1 | |
assert_equal(2, @int_arr2.count(1)) | |
# int_arr contains 2 even values | |
assert_equal(2, @int_arr.count {|x| x % 2 == 0}) | |
# string_arr contains 2 strings of length 3 | |
assert_equal(2, @string_arr.count {|x| x.length == 3}) | |
end | |
def test_cycle | |
result = [] | |
@int_arr.cycle(2) {|x| result << x} | |
assert_equal([1,2,3,100,1,2,3,100], result) | |
end | |
def test_detect | |
# First element not starting with a capital letter is "is" | |
assert_equal("is", @string_arr.detect {|str| str[0..0] !~ /[A-Z]/}) | |
# No negative elements | |
assert_nil(@int_arr.detect {|x| x < 0}) | |
end | |
def test_drop | |
assert_equal(["is", "the", "man"], @string_arr.drop(2)) | |
assert_equal([], @int_arr.drop(4)) | |
end | |
def test_drop_while | |
assert_equal(["is", "the", "man"], | |
@string_arr.drop_while {|str| str[0..0] =~ /[A-Z]/}) | |
assert_equal([], @string_arr.drop_while {|str| str.is_a? String}) | |
assert_equal([1, 2, 3, 100], @int_arr.drop_while {|x| x < 0}) | |
end | |
def test_each_cons | |
result = [] | |
@int_arr.each_cons(2) {|cons| result << cons} | |
assert_equal([[1,2], [2,3], [3,100]], result) | |
end | |
def test_each_entry | |
result = [] | |
@int_arr.each_entry {|x| result << x} | |
assert_equal([1,2,3,100], result) | |
result = [] | |
ExampleEnumerable.new.each_entry {|x| result << x} | |
assert_equal(["a", "b", "c"], result) | |
end | |
def test_each_slice | |
result = [] | |
@int_arr.each_slice(2) {|slice| result << slice} | |
assert_equal([[1,2], [3,100]], result) | |
end | |
def test_each_with_index | |
@int_arr.each_with_index{|x, i| @int_arr[i] = i} | |
assert_equal([0,1,2,3], @int_arr) | |
end | |
def test_each_with_object | |
assert_equal(["PROF.", "DING", "IS", "THE", "MAN"], | |
@string_arr.each_with_object([]) {|x, caps| caps << x.upcase}) | |
end | |
def test_entries | |
assert_equal([-1,0,1], (-1..1).entries) | |
end | |
# Copied from test_detect, since detect is a synonym of find | |
def test_find | |
# First element not starting with a capital letter is "is" | |
assert_equal("is", @string_arr.find {|str| str[0..0] !~ /[A-Z]/}) | |
# No negative elements | |
assert_nil(@int_arr.find {|x| x < 0}) | |
end | |
def test_find_all | |
assert_equal([2,100], @int_arr.find_all {|x| x % 2 == 0}) | |
assert_equal(["Prof.", "Ding"], | |
@string_arr.find_all { |str| str[0..0] =~ /[A-Z]/}) | |
end | |
def test_find_index | |
assert_equal(1, @int_arr.find_index {|x| x % 2 == 0}) | |
assert_equal(2, @string_arr.find_index {|str| str[0..0] !~ /[A-Z]/}) | |
end | |
def test_first | |
assert_equal("Prof.", @string_arr.first) | |
assert_equal(["Prof.", "Ding"], @string_arr.first(2)) | |
assert_nil([].first) | |
end | |
# Copied from test_collect_concat, since flat_map is a synonym of | |
# collect_concat | |
def test_flat_map | |
assert_equal([1, 1, 1, 1], @int_arr.flat_map { [1] }) | |
assert_equal([1, 2, 3, 100], @int_arr.flat_map {|x| x}) | |
assert_equal([1, 2, 3, 100], @int_arr.flat_map {|x| [x]}) | |
end | |
def test_grep | |
assert_equal(["Ding", "is"], @string_arr.grep(/i/)) | |
result = @string_arr.grep(/i/) {|str| str.length} | |
assert_equal([4, 2], result) | |
end | |
def test_group_by | |
assert_equal({0 => ["Prof.", "Ding"], nil => ["is", "the", "man"]}, | |
@string_arr.group_by {|str| str[0..0] =~ /[A-Z]/}) | |
end | |
def test_include? | |
assert(@string_arr.include?("is")) | |
assert_equal(false, @int_arr.include?(4)) | |
end | |
def test_inject | |
assert_equal(106, @int_arr.inject {|sum, x| sum + x}) | |
assert_equal(206, @int_arr.inject(100) {|sum, x| sum + x}) | |
end | |
# Copied from test_collect, since map is a synonym of collect | |
def test_map | |
assert_equal([1, 2, 3, 100], @int_arr.map {|x| x}) | |
assert_equal([false, true, true, false, false], | |
@mixed_arr.map {|x| x.is_a? Numeric}) | |
assert_equal([5, 4, 2, 3, 3], @string_arr.map {|x| x.length}) | |
end | |
def test_max | |
assert_equal("the", @string_arr.max) | |
assert_equal(1, @int_arr.max {|a, b| -a <=> -b}) | |
end | |
def test_max_by | |
assert_equal(1, @int_arr.max_by {|x| -x}) | |
assert_equal([1,2], @int_arr.max_by(2) {|x| -x}) | |
end | |
# Copied from test_member?, since member? is a synonym of include? | |
def test_member? | |
assert(@string_arr.member?("is")) | |
assert_equal(false, @int_arr.member?(4)) | |
end | |
def test_min | |
assert("Ding", @string_arr.min) | |
assert_equal(100, @int_arr.min {|a, b| -a <=> -b}) | |
end | |
def test_min_by | |
assert_equal(100, @int_arr.min_by {|x| -x}) | |
assert_equal([100,3], @int_arr.min_by(2) {|x| -x}) | |
end | |
def test_minmax | |
assert_equal(["Ding", "the"], @string_arr.minmax) | |
assert_equal([100,1], @int_arr.minmax {|a, b| -a <=> -b}) | |
end | |
def test_minmax_by | |
assert_equal([100,1], @int_arr.minmax_by {|x| -x}) | |
end | |
def test_none? | |
assert(@int_arr.none? {|x| x < 0}) | |
assert_equal(false, @mixed_arr.none? {|x| x.nil?}) | |
end | |
def test_one? | |
assert(@mixed_arr.one? {|x| x.nil?}) | |
assert_equal(false, @int_arr.one? {|x| x % 2 == 0}) | |
end | |
def test_partition | |
assert_equal([[2,100],[1,3]], @int_arr.partition {|x| x % 2 == 0}) | |
end | |
def test_reduce | |
assert_equal(106, @int_arr.reduce(:+)) | |
assert_equal(206, @int_arr.reduce(100, :+)) | |
end | |
def test_reject | |
assert_equal([1,3], @int_arr.reject {|x| x % 2 == 0}) | |
assert_equal(["is", "the", "man"], | |
@string_arr.reject {|str| str[0..0] =~ /[A-Z]/}) | |
end | |
def test_reverse_each | |
result = [] | |
@int_arr.reverse_each {|x| result << x} | |
assert_equal([100,3,2,1], result) | |
end | |
def test_select | |
assert_equal([2,100], @int_arr.select {|x| x % 2 == 0}) | |
assert_equal(["Prof.", "Ding"], | |
@string_arr.select {|str| str[0..0] =~ /[A-Z]/}) | |
end | |
def test_sort | |
assert_equal(["Ding", "Prof.", "is", "man", "the"], | |
@string_arr.sort) | |
assert_equal([100,3,2,1], @int_arr.sort {|a,b| -a <=> -b}) | |
end | |
def test_sort_by | |
assert_equal([100,3,2,1], @int_arr.sort_by {|x| -x}) | |
end | |
def test_take | |
assert_equal(["Prof."], @string_arr.take(1)) | |
assert_equal(["Prof.", "Ding"], @string_arr.take(2)) | |
end | |
def test_take_while | |
assert_equal([1,2,3], @int_arr.take_while {|x| x < 50}) | |
assert_equal(["", 500, 910.4], @mixed_arr.take_while {|x| x != nil}) | |
end | |
def test_to_a | |
assert_equal(@int_arr, @int_arr.to_a) | |
assert_equal(["a", "b", "c"], ExampleEnumerable.new.to_a) | |
assert_equal([-1,0,1], (-1..1).to_a) | |
end | |
def test_to_h | |
hash = @int_arr.each_with_index.to_h | |
assert_equal({1 => 0, 2 => 1, 3 => 2, 100 => 3}, hash) | |
secondlevelhash = hash.each_with_index.to_h | |
assert_equal({[1,0] => 0, [2,1] => 1, [3,2] => 2, [100,3] => 3}, | |
secondlevelhash) | |
end | |
def test_zip | |
assert_equal([[1,1],[2,2],[3,3],[100,1]], @int_arr.zip(@int_arr2)) | |
assert_equal([[1, "Prof."], [2, "Ding"], [3, "is"], [100, "the"]], | |
@int_arr.zip(@string_arr)) | |
pairsMerged = 0 | |
@int_arr.zip(@int_arr2) { |_| pairsMerged += 1 } | |
assert_equal(4, pairsMerged) | |
pairsMerged = 0 | |
@int_arr.zip(@string_arr) { |_| pairsMerged += 1 } | |
assert_equal(4, pairsMerged) | |
pairsMerged = 0 | |
[].zip(@string_arr) { |_| pairsMerged += 1} | |
assert_equal(0, pairsMerged) | |
result = [] | |
@int_arr.zip(@int_arr2) { |pair| result << pair } | |
assert_equal([[1,1],[2,2],[3,3],[100,1]], result) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment