tests/tc_cartesian.rb in Cartesian-0.2.3 vs tests/tc_cartesian.rb in Cartesian-0.3.0
- old
+ new
@@ -1,9 +1,10 @@
require 'test/unit'
$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
-require 'cartesian'
+require 'cartesian'
+require 'extensions'
class TestCartesian < Test::Unit::TestCase
def test_arrays
foo = [1,2,3]
bar = %w{a b c}
@@ -24,7 +25,31 @@
foo = 1..3
bar = %w{a b c}
expected = [[1, "a"], [1, "b"], [1, "c"], [2, "a"], [2, "b"],
[2, "c"], [3, "a"], [3, "b"], [3, "c"]]
assert(foo.x(bar).to_a == expected)
+ end
+
+ def test_power
+ ary = [1,2,3]
+ assert_raise(ArgumentError) { ary**(-1) }
+ assert_equal [], ary**0
+ assert_equal ary, ary**1
+ expected = [[0, 0, 0], [0, 0, 1], [0, 1, 0],\
+ [0, 1, 1], [1, 0, 0], [1, 0, 1],\
+ [1, 1, 0], [1, 1, 1]]
+ assert_equal expected, ([0,1]**3).to_a
+ end
+
+ def test_argmin
+ assert_equal [0,0], ((-3..3)**2).argmin {|x,y| x**2+y**2 }
end
+
+ def test_argmax
+ values = []
+ -3.step(3, 0.25) {|val| values << val }
+ x, y = (values**2).argmax {|x,y| x**2+y**2 }
+ assert x.among?([-3,3])
+ assert y.among?([-3,3])
+ end
+
end