test/test_vector3.rb in gmath3D-0.2.1 vs test/test_vector3.rb in gmath3D-0.2.2
- old
+ new
@@ -52,18 +52,61 @@
def test_assign_invalid_value
#TODO unimplemented!
@vector.x = "this is a pen"
end
+ def test_clone
+ vec1 = Vector3.new(1,2,3)
+ vec1_shallow_copy = vec1
+ vec1_shallow_copy.x = 4
+ assert_equal( 4, vec1.x )
+ assert_equal( 2, vec1.y )
+ assert_equal( 3, vec1.z )
+
+ vec1_deep_copy = vec1.clone
+ vec1_deep_copy.x = 6
+ assert_equal( 4, vec1.x )
+ assert_equal( 6, vec1_deep_copy.x )
+
+ vec_ary = [Vector3.new(1,2,3), Vector3.new(4,5,6)]
+ vec_ary_shallow = vec_ary
+ vec_ary_shallow[0].x = 4
+ vec_ary_shallow[1] = Vector3.new(7,8,9)
+ vec_ary_shallow.push(Vector3.new(10,11,12))
+ assert_equal(4, vec_ary_shallow[0].x)
+ assert_equal(Vector3.new(7,8,9), vec_ary_shallow[1])
+ assert_equal(3, vec_ary_shallow.size)
+ assert_equal(4, vec_ary[0].x)
+ assert_equal(Vector3.new(7,8,9), vec_ary[1])
+ assert_equal(3, vec_ary.size)
+
+ vec_ary = [Vector3.new(1,2,3), Vector3.new(4,5,6)]
+ vec_ary_deep = vec_ary.clone
+ vec_ary_deep[0].x = 4
+ vec_ary_deep[1] = Vector3.new(7,8,9)
+ vec_ary_deep.push(Vector3.new(10,11,12))
+ assert_equal(4, vec_ary_deep[0].x)
+ assert_equal(Vector3.new(7,8,9), vec_ary_deep[1])
+ assert_equal(3, vec_ary_deep.size)
+
+ # Array.clone does not call element.clone
+# assert_equal(1, vec_ary[0].x)
+ assert_equal(4, vec_ary[0].x)
+ assert_equal(Vector3.new(4,5,6), vec_ary[1])
+ assert_equal(2, vec_ary.size)
+ end
+
def test_equals
assert(!(@vector_init_zero == @vector))
assert(@vector_init_zero != @vector)
assert(@vector == @vector)
+ assert(@vector.eql?(@vector))
vector = Vector3.new(1,2,3)
assert(@vector == vector)
+ assert(@vector.eql?(vector))
# Floating error check
floatingError = Geom.default_tolerance*0.1
vector = Vector3.new(1.0 - floatingError, 2.0 + floatingError, 3.0)
assert(@vector == vector)
@@ -75,9 +118,31 @@
assert_equal(Vector3.new(1,2,3), Vector3.new(1.0,2.0,3.0))
#invlid value comparison
assert(@vector != "string")
assert(@vector != -4)
+ end
+
+ def test_equ_hash
+ # in case use Vector3 as Key of hash...
+ # Vector3#eql? and Vector3#hash should be implement
+ hash = Hash.new()
+ vec = Vector3.new(1,2,3)
+ hash[vec] = 1
+ assert_equal(1, hash.keys.count)
+ assert_equal(1, hash[vec])
+
+ hash[vec] = 2
+ assert_equal(1, hash.keys.count)
+ assert_equal(2, hash[vec])
+
+ hash[Vector3.new(3,2,1)] = 3
+ assert_equal(2, hash.keys.count)
+ assert_equal(2, hash[vec])
+
+ hash[@vector] = 3
+ assert_equal(2, hash.keys.count)
+ assert_equal(3, hash[vec])
end
def test_add
vector = Vector3.new(4,5,6)
addedVector = vector + @vector