require 'test/unit'
require 'fox'

include Fox

class TC_FXVec < Test::Unit::TestCase
  def test_getitem
    v = FXVec.new
    assert_kind_of(Float, v[0])
    assert_kind_of(Float, v[1])
    assert_kind_of(Float, v[2])
    assert_raises(IndexError) { v[-1] }
    assert_raises(IndexError) { v[3]  }
  end

  def test_setitem
    v = FXVec.new
    assert_kind_of(Float, v[0] = 0.0)
    assert_kind_of(Float, v[1] = 1.0)
    assert_kind_of(Float, v[2] = 2.0)
    assert_raises(IndexError) { v[-1] = 0.0 }
    assert_raises(IndexError) { v[3]  = 0.0 }
  end
  
  def test_divide_by_zero
    assert_raises(ZeroDivisionError) {
      vec = FXVec.new
      vec/0.0
    }
  end
  
  def test_normalize
    vec1 = FXVec.new(1.0, 1.0, 1.0)
    vec2 = vec1.normalize
    assert_in_delta(1.0, vec2.len, 1.0e-7)
    assert_in_delta(1.0/Math.sqrt(3.0), vec2[0], 1.0e-7)
    assert_in_delta(1.0/Math.sqrt(3.0), vec2[1], 1.0e-7)
    assert_in_delta(1.0/Math.sqrt(3.0), vec2[2], 1.0e-7)
  end
  
  def test_lo
    vec1 = FXVec.new(1.0, 2.0, 3.0)
    vec2 = FXVec.new(3.0, 2.0, 1.0)
    vec3 = vec1.lo(vec2)
    assert_equal(1.0, vec3[0])
    assert_equal(2.0, vec3[1])
    assert_equal(1.0, vec3[2])
  end
  
  def test_hi
    vec1 = FXVec.new(1.0, 2.0, 3.0)
    vec2 = FXVec.new(3.0, 2.0, 1.0)
    vec3 = vec1.hi(vec2)
    assert_equal(3.0, vec3[0])
    assert_equal(2.0, vec3[1])
    assert_equal(3.0, vec3[2])
  end
  
  def test_normalize!
    vec1 = FXVec.new(1.0, 1.0, 1.0)
    vec2 = vec1.normalize!
    assert_same(vec1, vec2)
    assert_in_delta(1.0, vec1.len, 1.0e-7)
    assert_in_delta(1.0/Math.sqrt(3.0), vec1[0], 1.0e-7)
    assert_in_delta(1.0/Math.sqrt(3.0), vec1[1], 1.0e-7)
    assert_in_delta(1.0/Math.sqrt(3.0), vec1[2], 1.0e-7)
  end
end
