-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wrote detailed unit tests for the ComplexNumber class using RSpec and…
… 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
Showing
6 changed files
with
157 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |