This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
covers "facets/binding/block_exec"
|
||||
|
||||
testcase Binding do
|
||||
|
||||
# TODO: How do we test this?
|
||||
|
||||
method :block_exec do
|
||||
|
||||
#setup do
|
||||
# c = Class.new
|
||||
# $block_exec_binding = binding
|
||||
#end
|
||||
|
||||
#test do
|
||||
# q = $block_exec_binding.block_exec { x }
|
||||
# q.assert == 5
|
||||
#end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,13 @@
|
||||
require 'facets/math/abs'
|
||||
|
||||
test_case Math do
|
||||
|
||||
class_method :abs do
|
||||
test do
|
||||
Math.abs(-3.1).assert == 3.1
|
||||
Math.abs( 0.0).assert == 0.0
|
||||
Math.abs( 3.1).assert == 3.1
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,23 @@
|
||||
require 'facets/math/acot'
|
||||
require 'facets/numeric/range' # used to test within a precision
|
||||
|
||||
test_case Math do
|
||||
|
||||
class_method :acot do
|
||||
test do
|
||||
Math.acot(-1.0).range(1.0e-6).assert === 0.5 * Math::PI - Math.atan(-1.0)
|
||||
Math.acot( 1.0).range(1.0e-6).assert === 0.5 * Math::PI - Math.atan( 1.0)
|
||||
end
|
||||
end
|
||||
|
||||
# TODO: Check complementarity of acot and cot
|
||||
#assert_equal_float(1.0, Math.acot(Math.cot(1.0)), 1.0e-6)
|
||||
#assert_equal_float(1.0, Math.cot(Math.acot(1.0)), 1.0e-6)
|
||||
#assert_equal_float(2.0, Math.acot(Math.cot(2.0)), 1.0e-6)
|
||||
#assert_equal_float(2.0, Math.cot(Math.acot(2.0)), 1.0e-6)
|
||||
|
||||
# TODO: Check consistency of atan2 and acot
|
||||
#assert_equal_float(Math.atan2(1.0, 1.0), Math.acot(1.0), 1.0e-6)
|
||||
#assert_equal_float(Math.atan2(1.0, 2.0), Math.acot(2.0), 1.0e-6)
|
||||
|
||||
end
|
||||
@@ -0,0 +1,22 @@
|
||||
require 'facets/math/acoth'
|
||||
require 'facets/math/coth'
|
||||
require 'facets/numeric/range' # used to test within a precision
|
||||
|
||||
test_case Math do
|
||||
|
||||
class_method :acoth do
|
||||
test do
|
||||
Math.acoth(1.5).range(1.0e-6).assert === 0.5 * Math.log(5.0)
|
||||
Math.acoth(5.0).range(1.0e-6).assert === 0.5 * Math.log(1.5)
|
||||
end
|
||||
|
||||
# 1.0e-6
|
||||
test "check complementarity of acoth and coth" do
|
||||
Math.acoth(Math.coth( 1.5)).range(1.0e-6).assert === 1.5
|
||||
Math.coth(Math.acoth( 1.5)).range(1.0e-6).assert === 1.5
|
||||
Math.acoth(Math.coth(-1.5)).range(1.0e-6).assert === -1.5
|
||||
Math.coth(Math.acoth(-1.5)).range(1.0e-6).assert === -1.5
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,19 @@
|
||||
require 'facets/math/beta'
|
||||
|
||||
test_case Math do
|
||||
|
||||
class_method :beta do
|
||||
test do
|
||||
Math.beta(1.0, 1.0).range(1.0e-6).assert === 1.0
|
||||
Math.beta(1.0, 2.0).range(1.0e-6).assert === 0.5
|
||||
Math.beta(1.0, 3.0).range(1.0e-6).assert === 1.0 / 3.0
|
||||
Math.beta(2.0, 1.0).range(1.0e-6).assert === 0.5
|
||||
Math.beta(2.0, 2.0).range(1.0e-6).assert === 1.0 / 6.0
|
||||
Math.beta(2.0, 3.0).range(1.0e-6).assert === 1.0 / 12.0
|
||||
Math.beta(3.0, 1.0).range(1.0e-6).assert === 1.0 / 3.0
|
||||
Math.beta(3.0, 2.0).range(1.0e-6).assert === 1.0 / 12.0
|
||||
Math.beta(3.0, 3.0).range(1.0e-6).assert === 1.0 / 30.0
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,22 @@
|
||||
require 'facets/math/ceil'
|
||||
|
||||
test_case Math do
|
||||
|
||||
class_method :ceil do
|
||||
test do
|
||||
Math.ceil( 1.99999).assert == 2
|
||||
Math.ceil( 1.5 ).assert == 2
|
||||
Math.ceil( 1.00001).assert == 2
|
||||
Math.ceil( 0.99999).assert == 1
|
||||
Math.ceil( 0.5 ).assert == 1
|
||||
Math.ceil( 0.00001).assert == 1
|
||||
Math.ceil(-0.00001).assert == 0
|
||||
Math.ceil(-0.5 ).assert == 0
|
||||
Math.ceil(-0.99999).assert == 0
|
||||
Math.ceil(-1.00001).assert == -1
|
||||
Math.ceil(-1.5 ).assert == -1
|
||||
Math.ceil(-1.99999).assert == -1
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,19 @@
|
||||
require 'facets/math/cosec'
|
||||
|
||||
test_case Math do
|
||||
|
||||
class_method :cosec do
|
||||
test do
|
||||
Math.cosec(-1.0).assert == 1.0 / Math.sin(-1.0)
|
||||
Math.cosec( 1.0).assert == 1.0 / Math.sin( 1.0)
|
||||
end
|
||||
end
|
||||
|
||||
# TODO: Check complementarity of cosec and sin
|
||||
#assert_equal_float(1.0, Math.cosec(-2.0) * Math.sin(-2.0), 1.0e-6)
|
||||
#assert_equal_float(1.0, Math.cosec(-1.0) * Math.sin(-1.0), 1.0e-6)
|
||||
#assert_equal_float(1.0, Math.cosec( 1.0) * Math.sin( 1.0), 1.0e-6)
|
||||
#assert_equal_float(1.0, Math.cosec( 2.0) * Math.sin( 2.0), 1.0e-6)
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
require 'facets/math/cosech'
|
||||
|
||||
test_case Math do
|
||||
|
||||
class_method :cosech do
|
||||
test do
|
||||
Math.cosech(-1.0).assert == 1.0 / Math.sinh(-1.0)
|
||||
Math.cosech( 1.0).assert == 1.0 / Math.sinh( 1.0)
|
||||
end
|
||||
|
||||
test "check complementarity of cosech and sinh" do
|
||||
(Math.cosech(-2.0) * Math.sinh(-2.0)).assert == 1.0
|
||||
(Math.cosech(-1.0) * Math.sinh(-1.0)).assert == 1.0
|
||||
(Math.cosech( 1.0) * Math.sinh( 1.0)).assert == 1.0
|
||||
(Math.cosech( 2.0) * Math.sinh( 2.0)).assert == 1
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,13 @@
|
||||
require 'facets/math/cot'
|
||||
require 'facets/numeric/range' # used to test within a precision
|
||||
|
||||
test_case Math do
|
||||
|
||||
class_method :cot do
|
||||
test do
|
||||
Math.cot(1.0).range(1.0e-6).assert === 1.0 / Math.tan(1.0)
|
||||
Math.cot(2.0).range(1.0e-6).assert === 1.0 / Math.tan(2.0)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,13 @@
|
||||
require 'facets/math/coth'
|
||||
require 'facets/numeric/range' # used to test within a precision
|
||||
|
||||
test_case Math do
|
||||
|
||||
class_method :coth do
|
||||
test do
|
||||
Math.coth(1.0).range(1.0e-6).assert === 1.0 / Math.tanh(1.0)
|
||||
Math.coth(2.0).range(1.0e-6).assert === 1.0 / Math.tanh(2.0)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,16 @@
|
||||
require 'facets/math/distance'
|
||||
|
||||
test_case Math do
|
||||
|
||||
class_method :distance do
|
||||
test do
|
||||
Math.distance(1, 2).assert == 1.0
|
||||
Math.distance([1,2], [3,4]).assert >= 2.828427
|
||||
Math.distance([1,2], [3,4]).assert <= 2.8284271247461903
|
||||
Math.distance([1,2,3], [4,5,6]).assert >= 5.196152
|
||||
Math.distance([1,2,3], [4,5,6]).assert <= 5.196152422706632
|
||||
Math.distance([1,2,3,4], [5,6,7,8]).assert == 8.0
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,14 @@
|
||||
require 'facets/math/exp10'
|
||||
require 'facets/numeric/range' # used to test within a precision
|
||||
|
||||
test_case Math do
|
||||
|
||||
class_method :exp10 do
|
||||
test do
|
||||
Math.exp10(0.0).range(1.0e-6).assert === 1.0
|
||||
Math.exp10(0.5).range(1.0e-6).assert === Math.sqrt(10.0)
|
||||
Math.exp10(1.0).range(1.0e-6).assert === 10.0
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,14 @@
|
||||
require 'facets/math/exp2'
|
||||
require 'facets/numeric/range' # used to test within a precision
|
||||
|
||||
test_case Math do
|
||||
|
||||
class_method :exp2 do
|
||||
test do
|
||||
Math.exp2(0.0).range(1.0e-6).assert === 1.0
|
||||
Math.exp2(0.5).range(1.0e-6).assert === Math.sqrt(2.0)
|
||||
Math.exp2(1.0).range(1.0e-6).assert === 2.0
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,13 @@
|
||||
require 'facets/math/factorial'
|
||||
|
||||
test_case Math do
|
||||
|
||||
class_method :factorial do
|
||||
fac = 1
|
||||
1.upto(100) do |i|
|
||||
fac *= i
|
||||
Math.factorial(i).assert == fac
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,23 @@
|
||||
require 'facets/math/floor'
|
||||
|
||||
test_case Math do
|
||||
|
||||
class_method :floor do
|
||||
test do
|
||||
Math.floor( 1.99999).assert == 1
|
||||
Math.floor( 1.5 ).assert == 1
|
||||
Math.floor( 1.00001).assert == 1
|
||||
Math.floor( 0.99999).assert == 0
|
||||
Math.floor( 0.5 ).assert == 0
|
||||
Math.floor( 0.00001).assert == 0
|
||||
Math.floor(-0.00001).assert == -1
|
||||
Math.floor(-0.5 ).assert == -1
|
||||
Math.floor(-0.99999).assert == -1
|
||||
Math.floor(-1.00001).assert == -2
|
||||
Math.floor(-1.5 ).assert == -2
|
||||
Math.floor(-1.99999).assert == -2
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
require 'facets/math/gcd'
|
||||
require 'facets/math/factorial'
|
||||
|
||||
test_case Math do
|
||||
|
||||
class_method :gcd do
|
||||
test do
|
||||
Math.gcd(4732, 4700).assert == 4
|
||||
Math.gcd(3651, 5023).assert == 1
|
||||
Math.gcd(612221610, 67461255771).assert == 5565651
|
||||
end
|
||||
|
||||
test "on a factorial" do
|
||||
Math.factorial(47).assert == Math.gcd(Math.factorial(90), Math.factorial(47))
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,13 @@
|
||||
require 'facets/math/lcm'
|
||||
|
||||
test_case Math do
|
||||
|
||||
class_method :lcm do
|
||||
test do
|
||||
Math.lcm( 77, 221).assert == 17017
|
||||
Math.lcm(143, 187).assert == 2431
|
||||
Math.lcm( 91, 119).assert == 1547
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,17 @@
|
||||
covers 'facets/math/median'
|
||||
|
||||
test_case Math do
|
||||
|
||||
class_method :median do
|
||||
test "fractional" do
|
||||
r = Math.median [1, 2, 3, 3, 6, 11, 20, 20]
|
||||
r.assert == 4.5
|
||||
end
|
||||
|
||||
test "whole value" do
|
||||
r = Math.median [1, 2, 4, 6, 8]
|
||||
r.assert == 4
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,13 @@
|
||||
covers 'facets/math/percentile'
|
||||
|
||||
test_case Math do
|
||||
|
||||
class_method :percentile do
|
||||
test do
|
||||
a = [1,1,2,2,3,3,3,4]
|
||||
r = Math.percentile(a,75)
|
||||
r.assert == 3.0
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,30 @@
|
||||
require 'facets/math/pow'
|
||||
require 'facets/numeric/range'
|
||||
|
||||
test_case Math do
|
||||
|
||||
class_method :pow do
|
||||
test do
|
||||
Math.pow( 4.0, 3.0).range(1.0e-6).assert === 64.0
|
||||
Math.pow( 0.9, 3.0).range(1.0e-6).assert === 0.729
|
||||
Math.pow( 0.25, -0.5).range(1.0e-6).assert === 2.0
|
||||
Math.pow( 64.0, -1.0/3).range(1.0e-6).assert === 0.25
|
||||
end
|
||||
end
|
||||
|
||||
class_method :pwr do
|
||||
test do
|
||||
Math.pwr( 4.0, 3.0).range(1.0e-6).assert === 64.0
|
||||
Math.pwr( 0.9, 3.0).range(1.0e-6).assert === 0.729
|
||||
Math.pwr( 0.25, -0.5).range(1.0e-6).assert === 2.0
|
||||
Math.pwr( 64.0, -1.0/3).range(1.0e-6).assert === 0.25
|
||||
end
|
||||
end
|
||||
|
||||
# TODO: Check complementarity of pwr and root
|
||||
# assert_equal_float( 4.0, Math.root(Math.pwr(2.0, 6.0), 3.0), 1.0e-6)
|
||||
# assert_equal_float( 2.0, Math.root(Math.pwr(4.0, 4.0), 8.0), 1.0e-6)
|
||||
# assert_equal_float(Math.sqrt(2.0), Math.pwr(Math.root(2.0, 6.0), 3.0), 1.0e-6)
|
||||
# assert_equal_float( 16.0, Math.pwr(Math.root(4.0, 4.0), 8.0), 1.0e-6)
|
||||
|
||||
end
|
||||
@@ -0,0 +1,15 @@
|
||||
require 'facets/math/root'
|
||||
require 'facets/numeric/range'
|
||||
|
||||
test_case Math do
|
||||
|
||||
class_method :root do
|
||||
test do
|
||||
Math.root( 64.0, 3.0).range(1.0e-6).assert === 4.0
|
||||
Math.root( 0.729, 3.0).range(1.0e-6).assert === 0.9
|
||||
Math.root( 2.0, -0.5).range(1.0e-6).assert === 0.25
|
||||
Math.root( 0.25, -1.0/3).range(1.0e-6).assert === 64.0
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,19 @@
|
||||
require 'facets/math/sec'
|
||||
require 'facets/numeric/range' # used to test within a precision
|
||||
|
||||
test_case Math do
|
||||
|
||||
class_method :sec do
|
||||
test do
|
||||
Math.sec(1.0).range(1.0e-6).assert === 1.0 / Math.cos(1.0)
|
||||
Math.sec(2.0).range(1.0e-6).assert === 1.0 / Math.cos(2.0)
|
||||
end
|
||||
end
|
||||
|
||||
# TODO: Check complementarity of cos and sec
|
||||
#assert_equal_float(1.0, Math.sec(-2.0) * Math.cos(-2.0), 1.0e-6)
|
||||
#assert_equal_float(1.0, Math.sec(-1.0) * Math.cos(-1.0), 1.0e-6)
|
||||
#assert_equal_float(1.0, Math.sec( 1.0) * Math.cos( 1.0), 1.0e-6)
|
||||
#assert_equal_float(1.0, Math.sec( 2.0) * Math.cos( 2.0), 1.0e-6)
|
||||
|
||||
end
|
||||
@@ -0,0 +1,19 @@
|
||||
require 'facets/math/sech'
|
||||
require 'facets/numeric/range' # used to test within a precision
|
||||
|
||||
test_case Math do
|
||||
|
||||
class_method :sech do
|
||||
test do
|
||||
Math.sech(1.0).range(1.0e-6).assert === 1.0 / Math.cosh(1.0)
|
||||
Math.sech(2.0).range(1.0e-6).assert === 1.0 / Math.cosh(2.0)
|
||||
end
|
||||
end
|
||||
|
||||
# TODO: Check complementarity of cosh and sech
|
||||
#assert_equal_float(1.0, Math.sech(-2.0) * Math.cosh(-2.0), 1.0e-6)
|
||||
#assert_equal_float(1.0, Math.sech(-1.0) * Math.cosh(-1.0), 1.0e-6)
|
||||
#assert_equal_float(1.0, Math.sech( 1.0) * Math.cosh( 1.0), 1.0e-6)
|
||||
#assert_equal_float(1.0, Math.sech( 2.0) * Math.cosh( 2.0), 1.0e-6)
|
||||
|
||||
end
|
||||
@@ -0,0 +1,20 @@
|
||||
require 'facets/math/sign'
|
||||
|
||||
test_case Math do
|
||||
|
||||
class_method :sign do
|
||||
test do
|
||||
Math.sign(-3.1).assert == -1.0
|
||||
Math.sign( 0.0).assert == 0.0
|
||||
Math.sign( 3.1).assert == 1.0
|
||||
end
|
||||
|
||||
# Check complementarity of abs and sign
|
||||
#assert_equal_float(-2.0, Math.abs(-2.0) * Math.sign(-2.0), 1e-6)
|
||||
#assert_equal_float( 0.0, Math.abs( 0.0) * Math.sign( 0.0), 1e-6)
|
||||
#assert_equal_float( 3.0, Math.abs( 3.0) * Math.sign( 3.0), 1e-6)
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
require 'facets/math/sinc'
|
||||
|
||||
test_case Math do
|
||||
|
||||
class_method :sinc do
|
||||
test do
|
||||
Math.sinc( 0.0).assert == 1.0
|
||||
Math.sinc( 1.0).assert == Math.sin(1.0)
|
||||
Math.sinc(-1.0).assert == Math.sin(1.0)
|
||||
Math.sinc( 2.0).assert == 0.5 * Math.sin(2.0)
|
||||
Math.sinc(-2.0).assert == 0.5 * Math.sin(2.0)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,20 @@
|
||||
require 'facets/math/sqr'
|
||||
require 'facets/numeric/range'
|
||||
|
||||
test_case Math do
|
||||
|
||||
class_method :sqr do
|
||||
test do
|
||||
Math.sqr(-0.9).range(1.0e-6).assert === 0.81
|
||||
Math.sqr(-0.7).range(1.0e-6).assert === 0.49
|
||||
Math.sqr(-0.7).range(1.0e-6).assert === 0.49
|
||||
Math.sqr( 0.9).range(1.0e-6).assert === 0.81
|
||||
end
|
||||
end
|
||||
|
||||
# TODO: Check complementarity of sqrt and sqr
|
||||
# assert_equal_float(0.3, Math.sqrt(Math.sqr( 0.3)), 1.0e-6)
|
||||
# assert_equal_float(0.3, Math.sqrt(Math.sqr(-0.3)), 1.0e-6)
|
||||
# assert_equal_float(0.3, Math.sqr(Math.sqrt( 0.3)), 1.0e-6)
|
||||
|
||||
end
|
||||
@@ -0,0 +1,16 @@
|
||||
covers 'facets/pathname/chdir'
|
||||
|
||||
test_case Pathname do
|
||||
|
||||
method :chdir do
|
||||
|
||||
test do
|
||||
Pathname.pwd.basename.to_s.assert != 'tmp'
|
||||
Pathname.new('tmp').chdir do
|
||||
Pathname.pwd.basename.to_s.assert == 'tmp'
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
require 'pathname'
|
||||
|
||||
covers 'facets/pathname/readline'
|
||||
|
||||
test_case Pathname do
|
||||
|
||||
method :readline do
|
||||
|
||||
setup do
|
||||
@test_file = Pathname.new('tmp/test_file')
|
||||
File.open(@test_file, "w") { |f| f << "line1\nline2" }
|
||||
end
|
||||
|
||||
test do
|
||||
@test_file.readline.assert == "line1\n"
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
covers 'facets/argvector'
|
||||
|
||||
test_case Argvector do
|
||||
|
||||
method :parameters do
|
||||
|
||||
test do
|
||||
line = "-x baz --foo=8 bar"
|
||||
cargs = Argvector.new(line)
|
||||
args, keys = cargs.parameters
|
||||
args.assert == ['baz','bar']
|
||||
keys.assert == {'foo'=>'8','x'=>true}
|
||||
end
|
||||
|
||||
test "with arity" do
|
||||
line = "-g a -x b -x c"
|
||||
cargs = Argvector.new(line, :g=>1, :x=>1)
|
||||
args, keys = cargs.parameters
|
||||
keys.assert == {'g'=>'a','x'=>['b','c']}
|
||||
args.assert == []
|
||||
end
|
||||
|
||||
test "repeated option" do
|
||||
line = "-x baz --foo=1 --foo=2 bar"
|
||||
cargs = Argvector.new(line)
|
||||
args, keys = cargs.parameters
|
||||
args.assert == ['baz','bar']
|
||||
keys.assert == {'x'=>true,'foo'=>['1','2']}
|
||||
end
|
||||
|
||||
test do
|
||||
line = "-q baz --aq 5 bar"
|
||||
cargs = Argvector.new(line,'aq'=>1)
|
||||
words, flags = cargs.parameters
|
||||
words.assert == ['baz','bar']
|
||||
flags.assert == {'q'=>true,'aq'=>'5'}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :preoptions do
|
||||
|
||||
test do
|
||||
line = "-x --foo=7 baz -y bar"
|
||||
cargs = Argvector.new(line)
|
||||
flags = cargs.preoptions
|
||||
flags.assert == {'x'=>true,'foo'=>'7'}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :flags do
|
||||
|
||||
test do
|
||||
line = "-x baz --foo=8 bar"
|
||||
cargs = Argvector.new(line)
|
||||
flags = cargs.flags
|
||||
flags.assert == ['x']
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
require 'facets/cloneable'
|
||||
|
||||
test_case Cloneable do
|
||||
|
||||
foo = Class.new do
|
||||
include Cloneable
|
||||
def initialize
|
||||
@bar=[]
|
||||
end
|
||||
def bar_id
|
||||
@bar.object_id
|
||||
end
|
||||
end
|
||||
|
||||
method :dup do
|
||||
test do
|
||||
a = foo.new
|
||||
b = a.dup
|
||||
a.bar_id.refute == b.bar_id
|
||||
end
|
||||
|
||||
test "tainted" do
|
||||
a = foo.new
|
||||
a.taint
|
||||
b = a.dup
|
||||
b.assert.tainted?
|
||||
end
|
||||
|
||||
test "frozen" do
|
||||
a = foo.new
|
||||
a.freeze
|
||||
b = a.dup
|
||||
b.refute.frozen?
|
||||
end
|
||||
end
|
||||
|
||||
method :clone do
|
||||
test do
|
||||
a = foo.new
|
||||
b = a.clone
|
||||
assert(a.bar_id != b.bar_id, "should not be equal")
|
||||
end
|
||||
|
||||
test "tainted" do
|
||||
a = foo.new
|
||||
a.taint
|
||||
b = a.clone
|
||||
assert b.tainted?, "b should be tainted"
|
||||
end
|
||||
|
||||
test "frozen" do
|
||||
a = foo.new
|
||||
a.freeze
|
||||
b = a.clone
|
||||
assert b.frozen?, "b should be frozen"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,82 @@
|
||||
covers 'facets/date'
|
||||
|
||||
test_case Date do
|
||||
|
||||
setup "Date 2005-05-20" do
|
||||
@d = Date.civil(2005, 04, 20)
|
||||
end
|
||||
|
||||
method :to_date do
|
||||
test do
|
||||
@d.to_date.assert.instance_of?(::Date)
|
||||
end
|
||||
end
|
||||
|
||||
method :to_time do
|
||||
test do
|
||||
@d.to_time.assert.instance_of?(::Time)
|
||||
end
|
||||
end
|
||||
|
||||
method :to_s do
|
||||
test do
|
||||
@d.to_s.assert == "2005-04-20"
|
||||
end
|
||||
end
|
||||
|
||||
method :stamp do
|
||||
test do
|
||||
@d.stamp.assert == "2005-04-20"
|
||||
end
|
||||
end
|
||||
|
||||
method :stamp do
|
||||
test do
|
||||
@d.stamp(:short).assert == "20 Apr"
|
||||
end
|
||||
end
|
||||
|
||||
method :stamp do
|
||||
test do
|
||||
@d.stamp(:long).assert == "April 20, 2005"
|
||||
end
|
||||
end
|
||||
|
||||
method :days_in_month do
|
||||
test do
|
||||
@d.days_in_month.assert == 30
|
||||
end
|
||||
end
|
||||
|
||||
method :days_of_month do
|
||||
test do
|
||||
@d.days_of_month.assert == (1..(@d.days_in_month)).to_a
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
test_case String do
|
||||
|
||||
method :to_date do
|
||||
test do
|
||||
s = "2005-10-31"
|
||||
d = s.to_date
|
||||
d.day.assert == 31
|
||||
d.month.assert == 10
|
||||
d.year.assert == 2005
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
test_case Time do
|
||||
|
||||
method :to_date do
|
||||
test do
|
||||
t = Time.now #parse('4/20/2005 15:37')
|
||||
t.to_date.assert.instance_of?(::Date)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,105 @@
|
||||
covers 'facets/enumargs'
|
||||
|
||||
test_case Enumerable::Arguments do
|
||||
|
||||
setup do
|
||||
@PlusArray = Class.new do
|
||||
include Enumerable::Argumentable
|
||||
def initialize(arr)
|
||||
@arr = arr
|
||||
end
|
||||
def each(n=0)
|
||||
@arr.each{ |e| yield(e+n) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
method :collect do
|
||||
test do
|
||||
t = @PlusArray.new([1,2,3])
|
||||
r = t.collect(4){ |e| e }
|
||||
r.assert == [5,6,7]
|
||||
end
|
||||
end
|
||||
|
||||
#method :each_slice do
|
||||
# test do
|
||||
# t = PlusArray.new([1,2,3,4])
|
||||
# a = []
|
||||
# t.each_slice(2,4){ |e,f| a << [e,f] }
|
||||
# a.assert == [[5,6],[7,8]]
|
||||
# end
|
||||
#end
|
||||
|
||||
#method :find do
|
||||
# test do
|
||||
# t = PlusArray.new([1,2,3,4])
|
||||
# f = t.find(2, :ifnone=>lambda{:NOPE}) { |a| a == 10 }
|
||||
# f.assert == :NOPE
|
||||
# end
|
||||
#end
|
||||
|
||||
method :grep do
|
||||
end
|
||||
|
||||
method :to_a do
|
||||
test do
|
||||
t = @PlusArray.new([1,2,3])
|
||||
t.to_a(4).assert == [5,6,7]
|
||||
end
|
||||
end
|
||||
|
||||
# TODO: These two methods have issues! Any way to fix?
|
||||
skip do
|
||||
method :min do
|
||||
test do
|
||||
t = @PlusArray.new([1,2,3])
|
||||
|
||||
# test is failing. min has a variable number
|
||||
# of arguments which makes it's .arity = -1.
|
||||
# given the way this works for arity -1
|
||||
# the method always passes the single argument to
|
||||
# the min method, and passes nothing to the each
|
||||
# resulting it taking the X smallest values of the
|
||||
# given and unchanged array
|
||||
t.min(4).assert = 5 # returning [1,2,3]
|
||||
end
|
||||
end
|
||||
|
||||
method :max do
|
||||
# test is failing. max has a variable number of arguments which makes it
|
||||
# .arity = -1. given the way this works for arity -1 the method
|
||||
# always passes the single argument to the max method, and passes nothing
|
||||
# to the each resulting it taking the X largest values of the given and
|
||||
# unchanged array
|
||||
test do
|
||||
t = @PlusArray.new([1,2,3])
|
||||
t.max(4).assert == 7
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
method :include? do
|
||||
test do
|
||||
t = @PlusArray.new([1,2,3])
|
||||
t.assert.include?(7,4)
|
||||
end
|
||||
end
|
||||
|
||||
method :select do
|
||||
test do
|
||||
t = @PlusArray.new([1,2,3])
|
||||
r = t.select(4){ |x| x == 6 }
|
||||
r.assert == [6]
|
||||
end
|
||||
end
|
||||
|
||||
method :reject do
|
||||
test do
|
||||
t = @PlusArray.new([1,2,3])
|
||||
r = t.reject(4){ |x| x == 6 }
|
||||
r.assert == [5,7]
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,46 @@
|
||||
require 'facets/equitable'
|
||||
|
||||
test_case Equitable do
|
||||
|
||||
c = Class.new
|
||||
c.class_eval {
|
||||
include Equitable(:a,:b)
|
||||
attr_accessor :a, :b
|
||||
}
|
||||
|
||||
method :== do
|
||||
test "equatable with arguments" do
|
||||
c1,c2 = c.new,c.new
|
||||
c1.a = 10; c1.b = 20
|
||||
c2.a = 10; c2.b = 20
|
||||
assert(c1 == c2)
|
||||
|
||||
c1.a = 10; c1.b = 10
|
||||
c2.a = 10; c2.b = 20
|
||||
refute(c1 == c2)
|
||||
|
||||
c1.a = 10; c1.b = 20
|
||||
c2.a = 20; c2.b = 20
|
||||
refute(c1 == c2)
|
||||
end
|
||||
end
|
||||
|
||||
=begin
|
||||
def test_equate_on_old
|
||||
c = Class.new
|
||||
c.class_eval { attr_accessor :a, :b ; equate_on :a,:b }
|
||||
c1,c2 = c.new,c.new
|
||||
c1.a = 10; c1.b = 20
|
||||
c2.a = 10; c2.b = 20
|
||||
assert_equal( c1, c2 )
|
||||
c1.a = 10; c1.b = 10
|
||||
c2.a = 10; c2.b = 20
|
||||
assert_not_equal( c1, c2 )
|
||||
c1.a = 10; c1.b = 20
|
||||
c2.a = 20; c2.b = 20
|
||||
assert_not_equal( c1, c2 )
|
||||
end
|
||||
=end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
require 'facets/instantiable'
|
||||
|
||||
test_case Instantiable do
|
||||
|
||||
setup do
|
||||
@m = Module.new do
|
||||
extend Instantiable
|
||||
|
||||
attr :a
|
||||
|
||||
def initialize( a )
|
||||
@a = a
|
||||
end
|
||||
end
|
||||
|
||||
@n = Module.new do
|
||||
include Instantiable
|
||||
|
||||
attr :a
|
||||
|
||||
def initialize( a )
|
||||
@a = a
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
method :new do
|
||||
test do
|
||||
m = @m.new(1)
|
||||
1.assert == m.a
|
||||
end
|
||||
|
||||
test do
|
||||
m = @n.new(1)
|
||||
1.assert == m.a
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
# test the math require
|
||||
require 'facets/math'
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
covers 'facets/memoizable'
|
||||
|
||||
test_case Memoizable do
|
||||
|
||||
method :memoize do
|
||||
|
||||
test "returns expected values" do
|
||||
c = Class.new do
|
||||
include Memoizable
|
||||
def initialize(a)
|
||||
@a = a
|
||||
end
|
||||
attr_accessor :a
|
||||
memoize :a
|
||||
end
|
||||
|
||||
o = c.new("A")
|
||||
o.a.assert == "A"
|
||||
o.a = "B"
|
||||
o.a.assert == "A"
|
||||
end
|
||||
|
||||
test "returns identical objects" do
|
||||
c = Class.new do
|
||||
include Memoizable
|
||||
def initialize(a)
|
||||
@a = a
|
||||
end
|
||||
attr_accessor :a
|
||||
memoize :a
|
||||
end
|
||||
|
||||
o = c.new("A")
|
||||
o.a.__id__.assert == o.a.__id__
|
||||
o.a = "B"
|
||||
o.a.__id__.assert == o.a.__id__
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
covers 'facets/multipliers'
|
||||
|
||||
test_case Fixnum do
|
||||
|
||||
method :deka do
|
||||
test do
|
||||
1.deka.assert == 10
|
||||
end
|
||||
end
|
||||
|
||||
method :hecto do
|
||||
test do
|
||||
1.hecto.assert == 100
|
||||
end
|
||||
end
|
||||
|
||||
method :kilo do
|
||||
test do
|
||||
1.kilo.assert == 1000
|
||||
end
|
||||
end
|
||||
|
||||
method :mega do
|
||||
test do
|
||||
1.mega.assert == 1000000
|
||||
end
|
||||
end
|
||||
|
||||
method :giga do
|
||||
test do
|
||||
1.giga.assert == 1000000000
|
||||
end
|
||||
end
|
||||
|
||||
method :tera do
|
||||
test do
|
||||
1.tera.assert == 1000000000000
|
||||
end
|
||||
end
|
||||
|
||||
method :peta do
|
||||
test do
|
||||
1.peta.assert == 1000000000000000
|
||||
end
|
||||
end
|
||||
|
||||
method :exa do
|
||||
test do
|
||||
1.exa.assert == 1000000000000000000
|
||||
end
|
||||
end
|
||||
|
||||
# Fractional
|
||||
|
||||
method :deci do
|
||||
test do
|
||||
1.deci.assert == 0.1
|
||||
end
|
||||
end
|
||||
|
||||
method :centi do
|
||||
test do
|
||||
1.centi.assert == 0.01
|
||||
end
|
||||
end
|
||||
|
||||
method :milli do
|
||||
test do
|
||||
1.milli.assert == 0.001
|
||||
end
|
||||
end
|
||||
|
||||
method :micro do
|
||||
test do
|
||||
1.micro.assert == 0.000001
|
||||
end
|
||||
end
|
||||
|
||||
method :nano do
|
||||
test do
|
||||
1.nano.assert == 0.000000001
|
||||
end
|
||||
end
|
||||
|
||||
method :pico do
|
||||
test do
|
||||
1.pico.assert == 0.000000000001
|
||||
end
|
||||
end
|
||||
|
||||
method :femto do
|
||||
test do
|
||||
1.femto.assert == 0.000000000000001
|
||||
end
|
||||
end
|
||||
|
||||
method :atto do
|
||||
test do
|
||||
1.atto.assert == 0.000000000000000001
|
||||
end
|
||||
end
|
||||
|
||||
# SI Binary
|
||||
|
||||
method :kibi do
|
||||
test do
|
||||
1.kibi.assert == 1024
|
||||
end
|
||||
end
|
||||
|
||||
method :mebi do
|
||||
test do
|
||||
1.mebi.assert == 1024**2
|
||||
end
|
||||
end
|
||||
|
||||
method :gibi do
|
||||
test do
|
||||
1.gibi.assert == 1024**3
|
||||
end
|
||||
end
|
||||
|
||||
method :tebi do
|
||||
test do
|
||||
1.tebi.assert == 1024**4
|
||||
end
|
||||
end
|
||||
|
||||
method :pebi do
|
||||
test do
|
||||
1.pebi.assert == 1024**5
|
||||
end
|
||||
end
|
||||
|
||||
method :exbi do
|
||||
test do
|
||||
1.exbi.assert == 1024**6
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,191 @@
|
||||
covers 'facets/ostruct'
|
||||
|
||||
test_case OpenStruct do
|
||||
|
||||
method :merge! do
|
||||
test do
|
||||
o = OpenStruct.new(:a => 1)
|
||||
h = { :b => 2 }
|
||||
o.merge!(h)
|
||||
o.b.assert == 2
|
||||
end
|
||||
|
||||
test do
|
||||
o = OpenStruct.new( { :a => 1 } )
|
||||
h = { :b => 2 }
|
||||
o.merge!(h)
|
||||
o.b.assert == 2
|
||||
end
|
||||
end
|
||||
|
||||
method :__merge__ do
|
||||
test do
|
||||
o = OpenStruct.new( { :a => 1 } )
|
||||
h = { :b => 2 }
|
||||
q = o.__merge__( h )
|
||||
q.a.assert == 1
|
||||
q.b.assert == 2
|
||||
end
|
||||
|
||||
test do
|
||||
o1 = OpenStruct.new( { :a => 1 } )
|
||||
o2 = OpenStruct.new( { :b => 2 } )
|
||||
q = o1.__merge__( o2 )
|
||||
q.a.assert == 1
|
||||
q.b.assert == 2
|
||||
end
|
||||
|
||||
test "with_hash" do
|
||||
o = OpenStruct.new( { :a => 1 } )
|
||||
h = { :b => 2 }
|
||||
q = o.__merge__(h)
|
||||
q.a.assert == 1
|
||||
q.b.assert == 2
|
||||
end
|
||||
|
||||
test "two open structs" do
|
||||
o1 = OpenStruct.new( { :a => 1 } )
|
||||
o2 = OpenStruct.new( { :b => 2 } )
|
||||
q = o1.__merge__(o2)
|
||||
q.a.assert == 1
|
||||
q.b.assert == 2
|
||||
end
|
||||
end
|
||||
|
||||
# @deprecated
|
||||
#method :instance_delegate do
|
||||
# test "store" do
|
||||
# o = OpenStruct.new(:a => 1)
|
||||
# o.instance_delegate.store(:a,1)
|
||||
# o.a.assert == 1
|
||||
# end
|
||||
#
|
||||
# test "update" do
|
||||
# o = OpenStruct.new
|
||||
# o.instance_delegate.update(:a=>1)
|
||||
# o.a.assert == 1
|
||||
# end
|
||||
#end
|
||||
|
||||
method :[] do
|
||||
test do
|
||||
o = OpenStruct.new( { :a => 1 } )
|
||||
o[:a].assert == 1
|
||||
end
|
||||
end
|
||||
|
||||
method :[]= do
|
||||
test do
|
||||
o = OpenStruct.new( { :a => 1 } )
|
||||
o[:b] = 2
|
||||
o.b.assert == 2
|
||||
end
|
||||
end
|
||||
|
||||
class_method :new do
|
||||
test "old functionality" do
|
||||
o = OpenStruct.new
|
||||
o.foo.assert.nil?
|
||||
o.foo = :bar
|
||||
o.foo.assert ==:bar
|
||||
o.delete_field(:foo)
|
||||
o.foo.assert.nil?
|
||||
|
||||
o1 = OpenStruct.new(:x => 1, :z => 2)
|
||||
o1.x.assert == 1
|
||||
o1.z.assert == 2
|
||||
|
||||
o2 = OpenStruct.new(:x => 1, :z => 2)
|
||||
o1.assert == o2
|
||||
end
|
||||
|
||||
test "via block" do
|
||||
person = OpenStruct.new do |p|
|
||||
p.name = 'John Smith'
|
||||
p.gender = :M
|
||||
p.age = 71
|
||||
end
|
||||
person.name.assert == 'John Smith'
|
||||
person.gender.assert ==:M
|
||||
person.age.assert == 71
|
||||
person.address.assert ==nil
|
||||
end
|
||||
|
||||
test "via hash and block" do
|
||||
person = OpenStruct.new(:gender => :M, :age => 71) do |p|
|
||||
p.name = 'John Smith'
|
||||
end
|
||||
person.name.assert == 'John Smith'
|
||||
person.gender.assert == :M
|
||||
person.age.assert == 71
|
||||
person.address.assert == nil
|
||||
end
|
||||
|
||||
test "subclass via block" do
|
||||
person_class = Class.new(OpenStruct)
|
||||
person = person_class.new do |p|
|
||||
p.name = 'John Smith'
|
||||
p.gender = :M
|
||||
p.age = 71
|
||||
end
|
||||
person.name.assert == 'John Smith'
|
||||
person.gender.assert == :M
|
||||
person.age.assert == 71
|
||||
person.address.assert == nil
|
||||
end
|
||||
|
||||
test "subclass via hash and block" do
|
||||
person_class = Class.new(OpenStruct)
|
||||
person = person_class.new(:gender => :M, :age => 71) do |p|
|
||||
p.name = 'John Smith'
|
||||
end
|
||||
person.name.assert == 'John Smith'
|
||||
person.gender.assert == :M
|
||||
person.age.assert == 71
|
||||
person.address.assert ==nil
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :to_ostruct do
|
||||
|
||||
test do
|
||||
a = { :a => 1, :b => 2, :c => 3 }
|
||||
ao = a.to_ostruct
|
||||
ao.a.assert == a[:a]
|
||||
ao.b.assert == a[:b]
|
||||
ao.c.assert == a[:c]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :to_ostruct_recurse do
|
||||
|
||||
test do
|
||||
a = { :a => 1, :b => 2, :c => { :x => 4 } }
|
||||
ao = a.to_ostruct_recurse
|
||||
ao.a.assert == a[:a]
|
||||
ao.b.assert == a[:b]
|
||||
ao.c.x.assert == a[:c][:x]
|
||||
end
|
||||
|
||||
test "with recursion" do
|
||||
a = {}
|
||||
a[:a] = a
|
||||
ao = a.to_ostruct_recurse
|
||||
ao.a.assert == ao
|
||||
end
|
||||
|
||||
test "advanced usage" do
|
||||
h = { 'a' => { 'b' => 1 } }
|
||||
o = h.to_ostruct_recurse( { h['a'] => h['a'] } )
|
||||
o.a['b'].assert == 1
|
||||
Hash.assert === o.a
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,241 @@
|
||||
covers 'facets/random'
|
||||
|
||||
test_case Random do
|
||||
|
||||
class_method :letter do
|
||||
test do
|
||||
100.times { |i| /[a-zA-z]/.assert =~ Random.letter }
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
test_case Range do
|
||||
|
||||
# helper method
|
||||
def assert_at_rand_table(table)
|
||||
table.each do |x, y|
|
||||
20.times do
|
||||
x.at_rand.assert == y
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# helper method
|
||||
def assert_random_range(range)
|
||||
20.times do
|
||||
range.assert.include?( range.at_rand )
|
||||
end
|
||||
end
|
||||
|
||||
method :at_rand do
|
||||
|
||||
test "within range" do
|
||||
list = [ 1..4, 1...4, 1.5..2.5, 1.5...2.5, -4..-1, -4...-1,
|
||||
-2.5..-1.5, -2.5...-1.5, 'a'..'d', 'a'...'d' ]
|
||||
list.each{ |rng| assert_random_range(rng) }
|
||||
end
|
||||
|
||||
test "within range special cases" do
|
||||
assert_at_rand_table(
|
||||
(5..5) => 5,
|
||||
(5...5) => nil,
|
||||
(5..-1) => nil,
|
||||
(5.0..5.0) => 5.0,
|
||||
(5.0...5.0) => nil,
|
||||
(5.0...-1.0) => nil
|
||||
)
|
||||
end
|
||||
|
||||
test "reverse ranges" do
|
||||
assert_at_rand_table(
|
||||
(4..1) => nil,
|
||||
(4...1) => nil,
|
||||
(-1..-4) => nil,
|
||||
(-1...-4) => nil
|
||||
)
|
||||
#assert((1..4).include?((4..1).at_rand))
|
||||
#assert((-4...-1).include?((-1...-4).at_rand))
|
||||
#assert((-4..-1).include?((-1..-4).at_rand))
|
||||
end
|
||||
|
||||
# I'd rather it return the first sentinel than +nil+.
|
||||
# Really, ruby should be raising an error as an invalid range value.
|
||||
test "exclusive range" do
|
||||
(5...5).at_rand.assert == nil
|
||||
#expect RangeError do
|
||||
# (5...5).at_rand
|
||||
#end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :at_rand do
|
||||
test do
|
||||
a = [1,2,3,4,5]
|
||||
20.times{ a.assert.include?( a.at_rand ) }
|
||||
end
|
||||
end
|
||||
|
||||
method :at_rand! do
|
||||
test do
|
||||
a = ['a','b','c']
|
||||
a.at_rand!.length.assert == 1
|
||||
a.length.assert == 2
|
||||
end
|
||||
end
|
||||
|
||||
method :pick do
|
||||
test do
|
||||
a = ['a','b','c']
|
||||
a.pick(3).length.assert == 3
|
||||
a.length.assert == 3
|
||||
a = ['a','b','c']
|
||||
a.pick(4).length.assert == 3
|
||||
a.length.assert == 3
|
||||
end
|
||||
end
|
||||
|
||||
method :pick! do
|
||||
test do
|
||||
a = ['a','b','c']
|
||||
a.pick!(3).length.assert == 3
|
||||
a.length.assert == 0
|
||||
a = ['a','b','c']
|
||||
a.pick!(4).length.assert == 3
|
||||
a.length.assert == 0
|
||||
end
|
||||
end
|
||||
|
||||
method :rand_index do
|
||||
test do
|
||||
10.times {
|
||||
i = [1,2,3].rand_index
|
||||
(0..2).assert.include?(i)
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
method :rand_subset do
|
||||
test do
|
||||
10.times {
|
||||
a = [1,2,3,4].rand_subset
|
||||
a.size.assert <= 4
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
method :shuffle do
|
||||
test do
|
||||
a = [1,2,3,4,5]
|
||||
b = a.shuffle
|
||||
b.sort.assert == a
|
||||
end
|
||||
end
|
||||
|
||||
method :shuffle! do
|
||||
test do
|
||||
a = [1,2,3,4,5]
|
||||
b = a.dup
|
||||
b.shuffle!
|
||||
b.sort.assert == a
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :rand_key do
|
||||
test do
|
||||
h = { :a=>1, :b=>2, :c=>3 }
|
||||
10.times { h.keys.assert.include?( h.rand_key ) }
|
||||
end
|
||||
end
|
||||
|
||||
method :rand_pair do
|
||||
test do
|
||||
h = { :a=>1, :b=>2, :c=>3 }
|
||||
10.times { k,v = *h.rand_pair; h[k].assert == v }
|
||||
end
|
||||
end
|
||||
|
||||
method :rand_value do
|
||||
test do
|
||||
h = { :a=>1, :b=>2, :c=>3 }
|
||||
10.times { h.values.assert.include?( h.rand_value ) }
|
||||
end
|
||||
end
|
||||
|
||||
method :shuffle do
|
||||
test do
|
||||
h = {:a=>1, :b=>2, :c=>3 }
|
||||
h.shuffle
|
||||
end
|
||||
end
|
||||
|
||||
method :shuffle! do
|
||||
test do
|
||||
h = {:a=>1, :b=>2, :c=>3 }
|
||||
h.shuffle!
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
test_case String do
|
||||
|
||||
method :at_rand do
|
||||
test do
|
||||
a = '12345'
|
||||
20.times{ a.assert.include?( a.at_rand ) }
|
||||
end
|
||||
end
|
||||
|
||||
method :at_rand! do
|
||||
test do
|
||||
x = 'ab'
|
||||
r = x.at_rand!
|
||||
assert( r == 'a' || r == 'b' )
|
||||
assert( x == 'a' || x == 'b' )
|
||||
end
|
||||
end
|
||||
|
||||
method :rand_index do
|
||||
test do
|
||||
10.times{ (0..2).assert.include?( 'abc'.rand_index ) }
|
||||
end
|
||||
end
|
||||
|
||||
method :rand_byte do
|
||||
test do
|
||||
2.times do
|
||||
s = 'ab'
|
||||
r = s.rand_byte
|
||||
if RUBY_VERSION > '1.9'
|
||||
assert( r == 'a' || r == 'b' )
|
||||
else
|
||||
assert( r == 97 || r == 98 )
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
method :shuffle do
|
||||
test do
|
||||
"abc 123".shuffle
|
||||
#"abc 123".shuffle!
|
||||
end
|
||||
end
|
||||
|
||||
method :shuffle! do
|
||||
test do
|
||||
"abc 123".shuffle!
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,74 @@
|
||||
covers 'facets/shellwords'
|
||||
|
||||
test_case Shellwords do
|
||||
|
||||
class_method :shellwords do
|
||||
|
||||
test do
|
||||
cmd = "ruby my_prog.rb | less"
|
||||
Shellwords.shellwords(cmd).assert.instance_of?(Array)
|
||||
Shellwords.shellwords(cmd).length.assert == 4
|
||||
end
|
||||
|
||||
test "unmatched double quote" do
|
||||
bad_cmd = 'one two "three'
|
||||
expect ArgumentError do
|
||||
Shellwords.shellwords(bad_cmd)
|
||||
end
|
||||
end
|
||||
|
||||
test "unmatched single quote" do
|
||||
bad_cmd = "one two 'three"
|
||||
expect ArgumentError do
|
||||
Shellwords.shellwords(bad_cmd)
|
||||
end
|
||||
end
|
||||
|
||||
test "unmatched quotes" do
|
||||
bad_cmd = "one '"'"''""'""
|
||||
expect ArgumentError do
|
||||
Shellwords.shellwords(bad_cmd)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :shellwords do
|
||||
test do
|
||||
e = ["a", "b"]
|
||||
a = ["a", "b"].shellwords
|
||||
a.assert == e
|
||||
end
|
||||
|
||||
test do
|
||||
e = ["-d", "a", "b"]
|
||||
a = ["a", "b", {:d=>true}].shellwords
|
||||
a.assert == e
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :shellwords do
|
||||
test do
|
||||
e = ['--debug']
|
||||
a = {:debug=>true}.shellwords
|
||||
a.assert == e
|
||||
end
|
||||
|
||||
test do
|
||||
e = ['--name="jim"', '--debug']
|
||||
a = {:debug=>true, :name=>"jim"}.shellwords
|
||||
a.each do |x|
|
||||
e.assert.include?(x)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,24 @@
|
||||
covers 'facets/thread.rb'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :threaded_map do
|
||||
test do
|
||||
r = [1,2,3].threaded_map{ |e| e + 1 }
|
||||
r.assert == [2,3,4]
|
||||
end
|
||||
end
|
||||
|
||||
method :threaded_map_send do
|
||||
test do
|
||||
r = [1,2,3].threaded_map_send(:+, 1)
|
||||
r.assert == [2,3,4]
|
||||
end
|
||||
|
||||
test "with_block" do
|
||||
r = [[1,2,3],[2,3,4],[3,4,5]].threaded_map_send(:select){ |x| x > 2 }
|
||||
r.assert == [[3],[3,4],[3,4,5]]
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,62 @@
|
||||
covers 'facets/timer'
|
||||
|
||||
test_case Timer do
|
||||
|
||||
class_method :time do
|
||||
test "can keep time of a block" do
|
||||
Timer.time do |timer|
|
||||
timer.total_time.round.assert == 0
|
||||
sleep 1
|
||||
timer.total_time.round.assert == 1
|
||||
end
|
||||
end
|
||||
|
||||
test "can be stopped" do
|
||||
Timer.time do |timer|
|
||||
sleep 0.4
|
||||
timer.stop
|
||||
sleep 0.6
|
||||
timer.total_time.round.assert == 0
|
||||
end
|
||||
end
|
||||
|
||||
test "timer can be started again" do
|
||||
Timer.time do |timer|
|
||||
timer.stop
|
||||
sleep 0.5
|
||||
timer.total_time.round.assert == 0
|
||||
timer.start
|
||||
sleep 1
|
||||
timer.total_time.round.assert == 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
method :start do
|
||||
test "out of time" do
|
||||
t = Timer.new(0.5)
|
||||
expect Timeout::Error do
|
||||
t.start
|
||||
sleep 1
|
||||
t.stop
|
||||
end
|
||||
end
|
||||
|
||||
test "in time" do
|
||||
t = Timer.new(2)
|
||||
t.start
|
||||
sleep 0.5
|
||||
t.stop
|
||||
end
|
||||
end
|
||||
|
||||
method :stop do
|
||||
test "in time" do
|
||||
t = Timer.new(2)
|
||||
t.start
|
||||
sleep 0.5
|
||||
t.stop
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,68 @@
|
||||
covers 'facets/tuple'
|
||||
|
||||
test_case Tuple do
|
||||
|
||||
class_method :[] do
|
||||
test "tuples with same content are the same object" do
|
||||
t1 = Tuple[1,2,3]
|
||||
t2 = Tuple[1,2,3]
|
||||
t1.object_id.assert == t2.object_id
|
||||
end
|
||||
end
|
||||
|
||||
method :< do
|
||||
test do
|
||||
t1 = Tuple[1,2,3]
|
||||
t2 = Tuple[2,4,5]
|
||||
t1.assert < t2
|
||||
end
|
||||
end
|
||||
|
||||
method :> do
|
||||
test do
|
||||
t1 = Tuple[1,2,3]
|
||||
t2 = Tuple[2,4,5]
|
||||
t2.assert > t1
|
||||
end
|
||||
end
|
||||
|
||||
method :to_a do
|
||||
test do
|
||||
t1 = Tuple[1,2,3]
|
||||
a1 = t1.to_a
|
||||
assert( Array === a1 )
|
||||
end
|
||||
end
|
||||
|
||||
method :<< do
|
||||
test do
|
||||
t1 = Tuple[1,2,3]
|
||||
t1 = t1 << 4
|
||||
t1.assert.is_a? Tuple
|
||||
t2 = Tuple[1,2,3,4]
|
||||
t1.object_id.assert == t2.object_id
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
test_case String do
|
||||
|
||||
method :to_t do
|
||||
test do
|
||||
t1 = "1.2.3".to_t
|
||||
t1.assert.is_a? Tuple
|
||||
t2 = Tuple[1,2,3]
|
||||
t1.object_id.assert == t2.object_id
|
||||
end
|
||||
|
||||
test do
|
||||
t1 = "1.2.3a".to_t
|
||||
t1.assert.is_a? Tuple
|
||||
t2 = Tuple[1,2,'3a']
|
||||
t1.assert == t2
|
||||
t1.object_id.assert == t2.object_id
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,50 @@
|
||||
covers 'facets/uri'
|
||||
|
||||
test_case URI do
|
||||
|
||||
class_method :query do
|
||||
test do
|
||||
h = { :a=>1, :b=>2 }
|
||||
r = URI.query(h)
|
||||
assert(r == "a=1;b=2" || r == "b=2;a=1")
|
||||
end
|
||||
end
|
||||
|
||||
# this is an alias of the above
|
||||
class_method :hash_to_query do
|
||||
test do
|
||||
h = { :a=>1, :b=>2 }
|
||||
r = URI.hash_to_query(h)
|
||||
assert(r == "a=1;b=2" || r == "b=2;a=1")
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
test_case Object do
|
||||
|
||||
method :uri do
|
||||
test do
|
||||
"abc%3Fxyz".assert == uri("abc?xyz")
|
||||
end
|
||||
end
|
||||
|
||||
method :unuri do
|
||||
test do
|
||||
"abc?xyz".assert == unuri("abc%3Fxyz")
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :to_uri do
|
||||
test do
|
||||
h = { :a=>1, :b=>2 }
|
||||
r = h.to_uri
|
||||
assert(r == "a=1;b=2" || r == "b=2;a=1")
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user