Skip to content

Commit

Permalink
start replacing spec tests with minitest
Browse files Browse the repository at this point in the history
  • Loading branch information
monkstone committed Sep 20, 2015
1 parent 658f038 commit c780a07
Show file tree
Hide file tree
Showing 8 changed files with 301 additions and 2 deletions.
5 changes: 4 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ end

desc 'Test'
task :test do
ruby "test/k9_test.rb"
sh "jruby test/math_tool_test.rb"
sh "jruby test/helper_methods_test.rb"
sh "jruby test/aabb_spec_test.rb"
ruby "test/k9_test.rb"
end

desc 'clean'
Expand Down
17 changes: 17 additions & 0 deletions ext/monkstone/vecmath/vec2/Vec2.java
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,23 @@ public static IRubyObject from_angle(ThreadContext context, IRubyObject klazz, I
runtime.newFloat(Math.sin(scalar))});
}

/**
* Example of a regular ruby class method Use Math rather than RadLut
* here!!!
*
* @param context
* @param klazz
* @return new Vec2 object (ruby)
*/
@JRubyMethod(name = "random", meta = true)
public static IRubyObject random_direction(ThreadContext context, IRubyObject klazz) {
Ruby runtime = context.getRuntime();
double angle = Math.random() * Math.PI * 2;
return Vec2.rbNew(context, klazz, new IRubyObject[]{
runtime.newFloat(Math.cos(angle)),
runtime.newFloat(Math.sin(angle))});
}

/**
*
* @param context
Expand Down
22 changes: 22 additions & 0 deletions ext/monkstone/vecmath/vec3/Vec3.java
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,28 @@ public IRubyObject normalize(ThreadContext context) {
runtime.newFloat(jy / mag),
runtime.newFloat(jz / mag)});
}

/**
* Example of a regular ruby class method
*
* @param context
* @param klazz
* @return new Vec2 object (ruby)
*/
@JRubyMethod(name = "random", meta = true)

public static IRubyObject random_direction(ThreadContext context, IRubyObject klazz) {
Ruby runtime = context.getRuntime();
double angle = Math.random() * Math.PI * 2;
double vz = Math.random() * 2 - 1;
double vx = Math.sqrt(1 - vz * vz) * Math.cos(angle);
double vy = Math.sqrt(1 - vz * vz) * Math.sin(angle);

return Vec3.rbNew(context, klazz, new IRubyObject[]{
runtime.newFloat(vx),
runtime.newFloat(vy),
runtime.newFloat(vz)});
}

/**
*
Expand Down
18 changes: 17 additions & 1 deletion spec/jruby_art/vecmath_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,15 @@
describe 'Vec2D#heading' do
it 'should return Vec2D#heading in radians' do
a = Vec2D.new(1, 1)
expect(a.heading).to be_within(EPSILON).of(Math::PI / 4.0)
expect(a.heading).to be_within(EPSILON).of(Math::PI / 4.0)
end
end

describe 'Vec2D#random' do
it 'should return Vec2D#random' do
a = Vec2D.random
expect(a).to be_instance_of Vec2D
expect(a.mag).to be_within(EPSILON).of(1.0)
end
end

Expand Down Expand Up @@ -412,6 +420,14 @@
end
end

describe 'Vec3D#random' do
it 'should return Vec2D#random' do
a = Vec3D.random
expect(a).to be_instance_of Vec3D
expect(a.mag).to be_within(EPSILON).of(1.0)
end
end

describe 'Vec3D#to_a' do
it 'should return x, y as an array' do
x, y, z = 1.0000001, 1.01, 1.001
Expand Down
36 changes: 36 additions & 0 deletions test/aabb_spec_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
gem 'minitest' # don't use bundled minitest
require 'java'
require 'minitest/autorun'

require_relative '../lib/rpextras'
require_relative '../lib/jruby_art/helpers/aabb'

Java::MonkstoneVecmathVec2::Vec2Library.new.load(JRuby.runtime, false)
Java::MonkstoneVecmathVec3::Vec3Library.new.load(JRuby.runtime, false)

EPSILON ||= 1.0e-04

Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)

Dir.chdir(File.dirname(__FILE__))

class MathToolTest < Minitest::Test
def test_aabb_new
x, y = 1.0000001, 1.01
a = Vec2D.new(x, y)
assert AaBb.new(center: Vec2D.new, extent: a).kind_of? AaBb
x0, y0 = -4, -4
a = Vec2D.new(x0, y0)
b = a *= -1
assert AaBb.from_min_max(min: a, max: b).kind_of? AaBb
x, y = 1.0000001, 1.01
a = AaBb.new(center: Vec2D.new, extent: Vec2D.new(x, y))
a.position(Vec2D.new(4, 6))
assert a.center == Vec2D.new(4, 6)
x, y = 1.0000001, 1.01
a = AaBb.new(center: Vec2D.new, extent: Vec2D.new(x, y))
a.position(Vec2D.new(4, 6)) { false }
assert a.center == Vec2D.new
end
end

55 changes: 55 additions & 0 deletions test/helper_methods_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
gem 'minitest' # don't use bundled minitest
require 'java'
require 'minitest/autorun'

require_relative '../lib/jruby_art/helper_methods'
require_relative '../lib/core'

include Processing::HelperMethods

EPSILON ||= 1.0e-04

Java::Monkstone::MathToolLibrary.new.load(JRuby.runtime, false)

include Processing::HelperMethods
include Processing::MathTool

EPSILON ||= 1.0e-04

Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)

Dir.chdir(File.dirname(__FILE__))

class HelperMethodsTest < Minitest::Test
def test_hex_color
col_double = 0.5
hexcolor = 0xFFCC6600
dodgy_hexstring = '*56666'
hexstring = '#CC6600'
assert hex_color(col_double) == 0.5, 'double as a color'
assert hex_color(hexcolor) == -3381760, 'hexadecimal fixnum color'
assert hex_color(hexstring) == -3381760, 'hexadecimal string color'
assert_raises(StandardError, 'Dodgy Hexstring') do
hex_color(dodgy_hexstring)
end
end

def test_dist
ax, ay, bx, by = 0, 0, 1.0, 1.0
assert dist(ax, ay, bx, by) == Math.sqrt(2), '2D distance'
by = 0.0
assert dist(ax, ay, bx, by) == 1.0, 'when y dimension is zero'
ax, ay, bx, by = 0, 0, 0.0, 0.0
assert dist(ax, ay, bx, by) == 0.0, 'when x and y dimension are zero'
ax, ay, bx, by = 1, 1, -2.0, -3.0
assert dist(ax, ay, bx, by) == 5, 'classic triangle dimensions'
ax, ay, bx, by, cx, cy = -1, -1, 100, 2.0, 3.0, 100
assert dist(ax, ay, bx, by, cx, cy) == 5, 'classic triangle dimensions'
ax, ay, bx, by, cx, cy = 0, 0, -1.0, -1.0, 0, 0
assert dist(ax, ay, bx, by, cx, cy) == Math.sqrt(2)
ax, ay, bx, by, cx, cy = 0, 0, 0.0, 0.0, 0, 0
assert dist(ax, ay, bx, by, cx, cy) == 0.0
ax, ay, bx, by, cx, cy = 0, 0, 1.0, 0.0, 0, 0
assert dist(ax, ay, bx, by, cx, cy) == 1.0, 'when x and z dimension are zero'
end
end
75 changes: 75 additions & 0 deletions test/math_tool_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
gem 'minitest' # don't use bundled minitest
require 'java'
require 'minitest/autorun'

require_relative '../lib/rpextras'
require_relative '../lib/jruby_art/helper_methods'

Java::Monkstone::MathToolLibrary.new.load(JRuby.runtime, false)

include Processing::HelperMethods
include Processing::MathTool

EPSILON ||= 1.0e-04

Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)

Dir.chdir(File.dirname(__FILE__))

class MathToolTest < Minitest::Test
def test_map1d
x = [0, 5, 7.5, 10]
range1 = (0..10)
range2 = (100..1)
range3 = (0..10)
range4 = (5..105)
assert map1d(x[0], range1, range2) == 100, 'map to first'
assert map1d(x[1], range1, range2) == 50.5, 'map to reversed intermediate'
assert map1d(x[2], range3, range4) == 80.0, 'map to intermediate'
assert map1d(x[3], range1, range2) == 1, 'map to last'
end

def test_p5map # as map1d except not using range input
x = [0, 5, 7.5, 10]
range1 = (0..10)
range2 = (100..1)
range3 = (0..10)
range4 = (5..105)
assert p5map(x[0], range1.first, range1.last, range2.first, range2.last) == 100
assert p5map(x[1], range1.first, range1.last, range2.first, range2.last) == 50.5
assert p5map(x[2], range3.first, range3.last, range4.first, range4.last) == 80.0
assert p5map(x[3], range1.first, range1.last, range2.first, range2.last) == 1
end

def test_norm
x = [10, 140, 210]
start0, last0 = 30, 200
start1, last1 = 0, 200
assert norm(x[0], start0, last0) == -0.11764705882352941, 'unclamped map'
assert norm(x[1], start1, last1) == 0.7, 'map to intermediate'
assert norm(x[2], start1, last1) == 1.05, 'unclamped map'
end

def test_norm_strict
x = [10, 140, 210]
start0, last0 = 30, 200
assert norm_strict(x[0], start0, last0) == 0, 'clamped map to 0..1.0'
end

def test_lerp # behaviour is deliberately different to processing which is unclamped
x = [0.5, 0.8, 2.0]
start0, last0 = 300, 200
start1, last1 = 0, 200
assert lerp(start0, last0, x[0]) == 250, 'produces a intermediate value of a reversed range'
assert lerp(start1, last1, x[1]) == 160, 'lerps tp an intermediate value'
assert lerp(start1, last1, x[2]) == 200, 'lerps to the last value of a range'
end

def test_constrain
x = [15, 2_500, -2_500]
start1, last1 = 0, 200
assert constrain(x[0], start1, last1) == 15
assert constrain(x[1], start1, last1) == 200
assert constrain(x[2], start1, last1) == 0
end
end
75 changes: 75 additions & 0 deletions test/test_map1d.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
gem 'minitest' # don't use bundled minitest
require 'java'
require 'minitest/autorun'

require_relative '../lib/rpextras'
require_relative '../lib/jruby_art/helper_methods'

Java::Monkstone::MathToolLibrary.new.load(JRuby.runtime, false)

include Processing::HelperMethods
include Processing::MathTool

EPSILON ||= 1.0e-04

Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)

Dir.chdir(File.dirname(__FILE__))

class Rp5Test < Minitest::Test
def test_map1d
x = [0, 5, 7.5, 10]
range1 = (0..10)
range2 = (100..1)
range3 = (0..10)
range4 = (5..105)
assert map1d(x[0], range1, range2) == 100, 'map to first'
assert map1d(x[1], range1, range2) == 50.5, 'map to reversed intermediate'
assert map1d(x[2], range3, range4) == 80.0, 'map to intermediate'
assert map1d(x[3], range1, range2) == 1, 'map to last'
end

def test_p5map # as map1d except not using range input
x = [0, 5, 7.5, 10]
range1 = (0..10)
range2 = (100..1)
range3 = (0..10)
range4 = (5..105)
assert p5map(x[0], range1.first, range1.last, range2.first, range2.last) == 100
assert p5map(x[1], range1.first, range1.last, range2.first, range2.last) == 50.5
assert p5map(x[2], range3.first, range3.last, range4.first, range4.last) == 80.0
assert p5map(x[3], range1.first, range1.last, range2.first, range2.last) == 1
end

def test_norm
x = [10, 140, 210]
start0, last0 = 30, 200
start1, last1 = 0, 200
assert norm(x[0], start0, last0) == -0.11764705882352941, 'unclamped map'
assert norm(x[1], start1, last1) == 0.7, 'map to intermediate'
assert norm(x[2], start1, last1) == 1.05, 'unclamped map'
end

def test_norm_strict
x = [10, 140, 210]
start0, last0 = 30, 200
assert norm_strict(x[0], start0, last0) == 0, 'clamped map to 0..1.0'
end

def test_lerp # behaviour is deliberately different to processing which is unclamped
x = [0.5, 0.8, 2.0]
start0, last0 = 300, 200
start1, last1 = 0, 200
assert lerp(start0, last0, x[0]) == 250, 'produces a intermediate value of a reversed range'
assert lerp(start1, last1, x[1]) == 160, 'lerps tp an intermediate value'
assert lerp(start1, last1, x[2]) == 200, 'lerps to the last value of a range'
end

def test_constrain
x = [15, 2_500, -2_500]
start1, last1 = 0, 200
assert constrain(x[0], start1, last1) == 15
assert constrain(x[1], start1, last1) == 200
assert constrain(x[2], start1, last1) == 0
end
end

0 comments on commit c780a07

Please sign in to comment.