150 lines
3.4 KiB
Crystal
150 lines
3.4 KiB
Crystal
require "spec"
|
|
require "../../src/engine/vector"
|
|
|
|
describe Vector do
|
|
it "allocate vectors" do
|
|
Vector[1i64, 2i64, 3i64, 4i64]
|
|
Vector[1i32, 2i64, 3i64, 4i64]
|
|
Vector[4i64]
|
|
Vector[1, 2.0, 3u64, 4.1f64]
|
|
v = Vector(Int32, 4).new { |i| i + 1 }
|
|
v[0].should eq(1)
|
|
v[1].should eq(2)
|
|
v[2].should eq(3)
|
|
v[3].should eq(4)
|
|
v06 = Vector(Float64, 6).zero
|
|
v06[0].should eq(0.0)
|
|
v06.size.should eq(6)
|
|
v06.tuple_type.should eq(Float64)
|
|
end
|
|
|
|
it "has a size" do
|
|
Vector[1].size.should eq(1)
|
|
Vector[1, 2].size.should eq(2)
|
|
Vector[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.0].size.should eq(11)
|
|
end
|
|
|
|
it "has a tuple_type" do
|
|
Vector[1].tuple_type.should eq(Int32)
|
|
Vector[1, 2].tuple_type.should eq(Int32)
|
|
Vector[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.0].tuple_type.should eq(Int32 | Float64)
|
|
end
|
|
|
|
it "equality test" do
|
|
(Vector[1, 2] == Vector[1, 2]).should be_true
|
|
(Vector[1, 2] != Vector[2, 2]).should be_true
|
|
(Vector[1, 2, 3] != Vector[1, 2]).should be_true
|
|
(Vector[1, 2] != Vector[1, 2, 3]).should be_true
|
|
(Vector[1, 2] != Vector[1, 2, 0]).should be_true
|
|
end
|
|
|
|
it "clones vectors" do
|
|
v1 = Vector[1, 2, 3]
|
|
v2 = v1.clone
|
|
v1.to_unsafe[0] = 0
|
|
v1[0].should_not eq(v2[0])
|
|
v1[1].should eq(v2[1])
|
|
end
|
|
|
|
it "mutates adds vectors" do
|
|
v1 = Vector[1, 2]
|
|
v2 = Vector[0, 1]
|
|
v3 = v1.add!(v2)
|
|
v1.object_id.should eq(v3.object_id)
|
|
v1[0].should eq(1)
|
|
v1[1].should eq(3)
|
|
v2[0].should eq(0)
|
|
v2[1].should eq(1)
|
|
end
|
|
|
|
it "adds vectors" do
|
|
v1 = Vector[1, 2]
|
|
v2 = Vector[0, 1]
|
|
v3 = v1 + v2
|
|
v1[0].should eq(1)
|
|
v2[0].should eq(0)
|
|
v3[0].should eq(1)
|
|
v1[1].should eq(2)
|
|
v2[1].should eq(1)
|
|
v3[1].should eq(3)
|
|
end
|
|
|
|
it "negates a vector" do
|
|
v1 = Vector[1, 2]
|
|
v2 = -v1
|
|
v1[0].should eq(1)
|
|
v1[1].should eq(2)
|
|
v2[0].should eq(-1)
|
|
v2[1].should eq(-2)
|
|
end
|
|
|
|
it "substract vectors" do
|
|
v1 = Vector[1, 2]
|
|
v2 = Vector[2, 1]
|
|
v3 = v1 - v2
|
|
v1[0].should eq(1)
|
|
v2[0].should eq(2)
|
|
v3[0].should eq(-1)
|
|
v1[1].should eq(2)
|
|
v2[1].should eq(1)
|
|
v3[1].should eq(1)
|
|
end
|
|
|
|
it "mutate multiply vector" do
|
|
v1 = Vector[1, 2]
|
|
v2 = v1.mult!(2)
|
|
v2.object_id.should eq(v1.object_id)
|
|
v1[0].should eq(2)
|
|
v1[1].should eq(4)
|
|
end
|
|
|
|
it "multiply vector" do
|
|
v1 = Vector[1, 2]
|
|
v2 = v1 * 2
|
|
v1[0].should eq(1)
|
|
v1[1].should eq(2)
|
|
v2[0].should eq(2)
|
|
v2[1].should eq(4)
|
|
end
|
|
|
|
# it "mutate divide vector" do
|
|
# v1 = Vector[5.0, 21]
|
|
# v2 = v1.div!(2)
|
|
# v2.object_id.should eq(v1.object_id)
|
|
# v1[0].should eq(2.5)
|
|
# v1[1].should eq(10)
|
|
# end
|
|
|
|
it "divide vector" do
|
|
v1 = Vector[5.0, 21]
|
|
v2 = v1 / 2
|
|
v1[0].should eq(5.0)
|
|
v1[1].should eq(21)
|
|
v2[0].should eq(2.5)
|
|
v2[1].should eq(10)
|
|
end
|
|
|
|
it "modulo vectors" do
|
|
v1 = Vector[1, 2, 3, 4]
|
|
v2 = v1 % 3
|
|
v2[0].should eq(1)
|
|
v2[1].should eq(2)
|
|
v2[2].should eq(0)
|
|
v2[3].should eq(1)
|
|
end
|
|
|
|
it "magnitude of vector" do
|
|
Vector[1, 1].magnitude.should eq(Math.sqrt(2))
|
|
Vector[1, 2].magnitude.should eq(Math.sqrt(1**2 + 2**2))
|
|
Vector[2, 2].magnitude.should eq(Math.sqrt(2**2 + 2**2))
|
|
Vector[2, 2, 2].magnitude.should eq((2**2 + 2**2 + 2**2)**(1.0/2.0))
|
|
end
|
|
|
|
it "normalized vector" do
|
|
Vector[1].normalize == Vector[1]
|
|
Vector[2].normalize == Vector[1]
|
|
Vector[2.3].normalize == Vector[1.0]
|
|
(Vector[1, 2].normalize == Vector[1 / Math.sqrt(5), 2 / Math.sqrt(5)])
|
|
end
|
|
end
|