Skip to content

Commit

Permalink
Wrote detailed unit tests for the ComplexNumber class using RSpec and…
Browse files Browse the repository at this point in the history
… Shoulda; wrote about the Ruby profiler and how to profile and optimize method calls; started writing the CLI interface for the complex number calculator.
  • Loading branch information
Filip Dimovski committed Dec 22, 2016
1 parent f0833d4 commit f4829a0
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 4 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2228,7 +2228,22 @@ Finished in 0.000556456 seconds.
the tests it will perform the required ones as well


## RSpec
## Profiling

* Ruby comes with a built-in profiler tool that will show you which
methods have been called throughout the runtime of the program, how
many times have been called, and how much time did the program spend
executing that code
* Run your Ruby application in the command line like this:
`ruby -r profile my_program.rb` - calling Ruby with `-r profile` as an
argument will profile the application and give you statistics in the
standard output once the application exits
* The profiler is a very helpful tool, to see what is your application
doing most of the time, so you can rewrite the methods that are
called the most and make them work faster and more efficient


## RSpec and Shoulda

* Behavior-driven development encourages people to write tests in terms
of their expectations of program's behavior for given circumstances
Expand All @@ -2239,7 +2254,8 @@ Finished in 0.000556456 seconds.
Rspec, and is focused on tests
* Install RSpec by typing the following in your command line:
`sudo gem install rspec`
*
* Also, install Should by typing the following in your command line:
`sudo gem install shoulda`



Expand Down
25 changes: 25 additions & 0 deletions code/unit_tests/complex/bin/complex
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env ruby

# CLI interface for the Complex number calculator
#
# Copyright 2016 Filip Dimovski <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.

require_relative '../lib/runner.rb'

runner = Rex::Runner.new(ARGV)
runner.run
3 changes: 1 addition & 2 deletions code/unit_tests/complex/lib/operations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# Module of complex number operations

module Rex

module ComplexOperations

# Addition that returns a new object
Expand Down Expand Up @@ -133,6 +132,6 @@ def sqrt
end
end
end
end

end
end
13 changes: 13 additions & 0 deletions code/unit_tests/complex/lib/runner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env ruby

module Rex
class Runner
def initialize(args)
@args = args
end

def run
p @args
end
end
end
52 changes: 52 additions & 0 deletions code/unit_tests/complex/test/test_unit_shoulda.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env ruby

# Unit tests using RSpec and Shoulda

require_relative '../lib/complex.rb'
require 'test/unit'
require 'shoulda'

class RexTestComplexNumber < Test::Unit::TestCase

context "creation" do
results = { "0.0" => ["0.0", "0i"], "2.0i" => ["0", "2i"], "-17.0-4.0i" => ["-17", "-4i"] }
results.each do |result, param|
should "should be #{result} for #{param}" do
assert_equal result, Rex::ComplexNumber.new(param[0], param[1]).to_s
end
end
end

context "perform basic arithmetical operations" do
i1 = Rex::ComplexNumber.new("-17", "-4i")
i2 = Rex::ComplexNumber.new("0", "2i")
results1 = [ "-17.0-2.0i", "-17.0-6.0i", "8.0-34.0i", "-2.0+8.5i" ]
i = 0

should "return correct result" do
results1.each do |result|
assert_equal result, (i1 + i2).to_s if i == 0
assert_equal result, (i1 - i2).to_s if i == 1
assert_equal result, (i1 * i2).to_s if i == 2
assert_equal result, (i1 / i2).to_s if i == 3
i += 1
end
end
end

context "perform advanced operations" do
i3 = Rex::ComplexNumber.new("-17", "-4i")
results2 = [ "0.48179310734638936-4.151159428194307i", "-17.0+4.0i", "-0.05573770491803279-0.013114754098360656i" ]
j = 0

should "return correct result" do
results2.each do |result|
assert_equal result, i3.sqrt.to_s if j == 0
assert_equal result, i3.conjugate.to_s if j == 1
assert_equal result, i3.reciprocal.to_s if j == 2
j += 1
end
end
end

end
48 changes: 48 additions & 0 deletions code/unit_tests/complex/test/ts_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env ruby

# RSpec description
require_relative '../lib/complex.rb'

describe "RexTestComplexNumber", "creation" do
results = { "0.0" => ["0.0", "0i"], "2.0i" => ["0", "2i"], "-17.0-4.0i" => ["-17", "-4i"] }
results.each do |a, b|
it "should be #{a} for #{b}" do
expect Rex::ComplexNumber.new(b[0], b[1]).to_s == a
end
end
end


describe "RexTestComplexNumber", "perform basic arithmetical operations" do
i1 = Rex::ComplexNumber.new("-17", "-4i")
i2 = Rex::ComplexNumber.new("0", "2i")
results = [ "-17.0-2.0i", "-17.0-6.0i", "8.0-34.0i", "-2.0+8.5i" ]

it "#{i1} + #{i2} should be #{results[0]}" do
expect(results[0]).to eq((i1 + i2).to_s)
end
it "#{i1} - #{i2} should be #{results[1]}" do
expect(results[1]).to eq((i1 - i2).to_s)
end
it "#{i1} * #{i2} should be #{results[2]}" do
expect(results[2]).to eq((i1 * i2).to_s)
end
it "#{i1} / #{i2} should be #{results[3]}" do
expect(results[3]).to eq((i1 / i2).to_s)
end
end

describe "RexTestComplexNumber", "perform advanced operations" do
i = Rex::ComplexNumber.new("-17", "-4i")
results = [ "0.48179310734638936-4.151159428194307i", "-17.0+4.0i", "-0.05573770491803279-0.013114754098360656i" ]

it "Square root of #{i} should be #{results[0]}" do
expect(results[0]).to eq(i.sqrt.to_s)
end
it "Conjugation of #{i} should be #{results[1]}" do
expect(results[1]).to eq(i.conjugate.to_s)
end
it "Reciprocal of #{i} should be #{results[2]}" do
expect(results[2]).to eq(i.reciprocal.to_s)
end
end

0 comments on commit f4829a0

Please sign in to comment.