Last active
March 16, 2016 08:18
-
-
Save thomasfl/51f6788c9f5e6045313d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# All purpose minimalistic random generator | |
def random(params) | |
if(params.kind_of?(Array)) | |
return params[rand(params.size)] | |
end | |
if(params.kind_of?(Range)) | |
return (rand(params.end - params.begin) + params.begin).to_s | |
end | |
if(params.kind_of?(Integer)) | |
value = "" | |
params.times do | |
value << (48 + rand(10)).chr | |
end | |
return value | |
end | |
if(params.kind_of?(TrueClass) or params.kind_of?(FalseClass)) | |
return (rand(2) == 0) | |
end | |
end | |
def test_random | |
print "Testing random helper " | |
assert_equals("Should pick random element from array", | |
random(['one','two','thr']).length, 3) | |
assert_equals("should generate numbers between range", | |
random(10..99).to_s.length, 2) | |
assert_equals("should generate a n digit number", | |
random(6).length, 6) | |
b = random(true) | |
assert_equals("Should generate boolean value", | |
(b == true || b == false), true) | |
puts "All tests passed." | |
end | |
def assert_equals(message, expected, actual) | |
print "." | |
if(expected.to_s != actual.to_s) | |
puts "Assert error: #{message}" | |
puts "Expected #{expected} got #{actual}" | |
exit(1) | |
end | |
end | |
# test_random() # Uncomment to test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment