Skip to content
Permalink
Browse files

Added Converter and some tests

  • Loading branch information...
robconery committed Apr 16, 2016
1 parent bc53e64 commit 06bbbd2cd77045a1fd1d17b36128557a834c672c
Showing with 36 additions and 12 deletions.
  1. +27 −10 lib/physics/rocketry.ex
  2. +9 −2 test/physics_test.exs
@@ -1,18 +1,35 @@
defmodule Physics.Rocketry do
defmodule Converter do
def to_nearest_tenth(val) do
Float.round(val, 1)
end

def escape_velocity(planet) when is_map(planet) do
planet
|> calculate_escape
|> convert_to_km
|> rounded_to_nearest_tenth
def to_km(velocity) do
velocity / 1000
end

defp rounded_to_nearest_tenth(val) do
Float.ceil(val, 1)
def to_light_seconds({:miles, miles} = val, precision: precision) do
(miles * 5.36819e-6) |> round_up
end

defp convert_to_km(velocity) do
velocity / 1000
def to_light_seconds({:meters, meters} = val, precision: precision) do
(meters * 3.335638620368e-9) |> round_up
end

def round_up(val) when is_float(val), do: trunc(val)
end

defmodule Physics.Rocketry do

def escape_velocity(:earth) do
%{mass: 5.972e24, radius: 6.371e6}
|> escape_velocity
end

def escape_velocity(planet) when is_map(planet) do
planet
|> calculate_escape
|> Converter.to_km
|> Converter.to_nearest_tenth
end

defp calculate_escape(%{mass: mass, radius: radius}) do
@@ -2,7 +2,14 @@ defmodule PhysicsTest do
use ExUnit.Case
doctest Physics

test "the truth" do
assert 1 + 1 == 2
test "escape velocity of earth is correct" do
#notice how you can pipe directly to IO.inspect? And still get the value returned...
ev = Physics.Rocketry.escape_velocity(:earth) |> IO.inspect
assert ev == 0 #what should this value be?
end
test "escape velocity of planet X is correct" do
planet_x = %{mass: 4.0e22, radius: 6.21e6}
ev = planet_x |> Physics.Rocketry.escape_velocity |> IO.inspect
#use assert here
end
end

0 comments on commit 06bbbd2

Please sign in to comment.
You can’t perform that action at this time.