Решение на Трета задача от Илия Тобов

Обратно към всички решения

Към профила на Илия Тобов

Резултати

  • 3 точки от тестове
  • 0 бонус точки
  • 3 точки общо
  • 37 успешни тест(а)
  • 32 неуспешни тест(а)

Код

module Graphics
class Point
attr_accessor :x, :y, :points
def initialize(x, y)
@points = []
@x = x
@y = y
end
def eql?(other)
@x == other.x and @y == other.y
end
alias :== :eql?
def lefter?(other)
@x <= other.x
end
def upper?(other)
@y <= other.y
end
def draw()
@points = [[@x, @y]]
end
end
class Line
attr_accessor :from, :to, :points
def initialize(from, to)
@points = []
if from.lefter? to
@from, @to = from, to
else
@from, @to= to, from
end
end
def eql?(other)
@from == other.from and @to == other.to
end
alias :== :eql?
def from()
if @from.x == @to.x
@from.y <= @to.y ? @from : @to
else
@from
end
end
def to()
if @from.x == @to.x
@from.y <= @to.y ? @to : @from
else
@to
end
end
def vertical?()
@from.x == @to.x
end
def horizontal?()
@from.y == @to.y
end
def draw()
vertical_draw if vertical?
horizontal_draw if horizontal?
draw_line() if points.empty?
points
end
def vertical_draw()
from.y.upto(to.y).each { |y| points << [from.x, y] } if points.empty?
end
def horizontal_draw()
from.x.upto(to.x).each { |x| points << [x, from.y] } if points.empty?
end
def draw_line()
end
end
class Rectangle
attr_accessor :points, :left, :right, :top_left, :top_right,
:bottom_left, :bottom_right
def initialize(left, right)
if left.lefter? right or left.upper? right
@left, @right = left, right
else
@left, @right = right, left
end
left_corners()
right_corners()
end
def left_corners()
other_left_point = Point.new left.x, right.y
if left.upper? other_left_point
@top_left, @bottom_left = left, other_left_point
else
@top_left, @bottom_left = other_left_point, left
end
end
def right_corners()
other_right_point = Point.new right.x, left.y
if right.upper? other_right_point
@top_right, @bottom_right = right, other_right_point
else
@top_right, @bottom_right = other_right_point, right
end
end
def left?()
@left
end
def right?()
@right
end
def draw()
@points = []
sides = [Line.new(bottom_left, top_left), Line.new(top_left, top_right),
Line.new(top_right, bottom_right), Line.new(bottom_left, bottom_right)]
sides.each { |side| points << side.draw }
points.flatten(1).uniq
end
def top_left()
@top_left
end
def bottom_left()
@bottom_left
end
def top_right
@top_right
end
def bottom_right
@bottom_right
end
end
class Canvas
attr_accessor :pixels
def initialize(width, height)
@pixels = {}
0.upto(height-1) do |y|
0.upto(width-1) do |x|
pixels[[x, y]]=false
end
end
@width = width
@height = height
end
def set_pixel(x, y)
@pixels[[x, y]]=true
end
def pixel_at?(x, y)
@pixels[[x, y]]
end
def draw(figure)
figure.draw.each { |point| set_pixel(*point) }
end
def render(renderer)
content = ""
length = renderer.symbols[:symbol_length]
pixels.values.each { |value| content += renderer.symbols[value] }
content.chars.each_slice(30*length).map(&:join).join(renderer.symbols[:new_line])
end
def render_as(renderer)
output = renderer.symbols[:start]
output += render(renderer)
output += renderer.symbols[:end]
output
end
end
module Renderers
class Ascii
@symbols = {true => "@", false => "-", :new_line => "\n",
:start => "", :end => "", :symbol_length => 1}
def Ascii.symbols()
@symbols
end
end
class Html
@start = "<!DOCTYPE html>
<html>
<head>
<title>Rendered Canvas</title>
<style type="'text/css'">
.canvas {
font-size: 1px;
line-height: 1px;
}
.canvas * {
display: inline-block;
width: 10px;
height: 10px;
border-radius: 5px;
}
.canvas i {
background-color: #eee;
}
.canvas b {
background-color: #333;
}
</style>
</head>
<body>
<div class="'canvas'">
"
@end = "</div>
</body>
</html>"
@symbols = {true => "<b></b>", false => "<i></i>", :new_line => "<br>",
:start => @start, :end => @end, :symbol_length => 7}
def Html.symbols()
@symbols
end
end
end
end

Лог от изпълнението

...F....F.FFFFFFFFFFFFFFFFF....F...F....F.........F....FF.FFFFFF...F.

Failures:

  1) Graphics Canvas exposes its width and height via getters
     Failure/Error: canvas.width.should eq 5
     NoMethodError:
       undefined method `width' for #<Graphics::Canvas:0xb914b39c>
     # /tmp/d20131223-4637-jz16rg/spec.rb:18:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) Graphics Canvas drawing of shapes and rasterization renders multiple drawn shapes
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "@@@@@@@@@@@@@@@\n@-------------@\n@-@@@@@@@@@@@-@\n@-@---------@-@\n@-@------@@-@-@\n@-@---@@@---@-@\n@-@-@@------@-@\n@-@---------@-@\n@-@-@@@@----@-@\n@-@-@-------@-@\n@-@---------@-@\n@-@---------@-@\n@-@@@@@@@@@@@-@\n@-------------@\n@@@@@@@@@@@@@@@"
            got: "@@@@@@@@@@@@@@@@-------------@\n@-@@@@@@@@@@@-@@-@---------@-@\n@-@---------@-@@-@---------@-@\n@-@---------@-@@-@---------@-@\n@-@-@@@@----@-@@-@-@-------@-@\n@-@---------@-@@-@---------@-@\n@-@@@@@@@@@@@-@@-------------@\n@@@@@@@@@@@@@@@"
       
       (compared using ==)
       
       Diff:
       @@ -1,16 +1,9 @@
       -@@@@@@@@@@@@@@@
       -@-------------@
       -@-@@@@@@@@@@@-@
       -@-@---------@-@
       -@-@------@@-@-@
       -@-@---@@@---@-@
       -@-@-@@------@-@
       -@-@---------@-@
       -@-@-@@@@----@-@
       -@-@-@-------@-@
       -@-@---------@-@
       -@-@---------@-@
       -@-@@@@@@@@@@@-@
       -@-------------@
       +@@@@@@@@@@@@@@@@-------------@
       +@-@@@@@@@@@@@-@@-@---------@-@
       +@-@---------@-@@-@---------@-@
       +@-@---------@-@@-@---------@-@
       +@-@-@@@@----@-@@-@-@-------@-@
       +@-@---------@-@@-@---------@-@
       +@-@@@@@@@@@@@-@@-------------@
        @@@@@@@@@@@@@@@
     # /tmp/d20131223-4637-jz16rg/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-jz16rg/spec.rb:211:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) Graphics Canvas drawing of shapes and rasterization of points works for multiple ones
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "@---\n@---\n-@@-\n----"
            got: "@---@----@@-----@"
       
       (compared using ==)
       
       Diff:
       @@ -1,5 +1,2 @@
       -@---
       -@---
       --@@-
       -----
       +@---@----@@-----@
     # /tmp/d20131223-4637-jz16rg/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-jz16rg/spec.rb:59:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) Graphics Canvas drawing of shapes and rasterization of lines works with simple horizontal lines
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "--------\n---@@@@-\n--------"
            got: "-----------@@@@---------"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,2 @@
       ---------
       ----@@@@-
       ---------
       +-----------@@@@---------
     # /tmp/d20131223-4637-jz16rg/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-jz16rg/spec.rb:73:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  5) Graphics Canvas drawing of shapes and rasterization of lines works with vertical lines
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "-@------\n-@------\n-@------\n-@------\n-@------\n-@------\n-@------\n--------"
            got: "-@-------@-------@-------@----\n---@-------@-------@----------\n----"
       
       (compared using ==)
       
       Diff:
       @@ -1,9 +1,4 @@
       --@------
       --@------
       --@------
       --@------
       --@------
       --@------
       --@------
       ---------
       +-@-------@-------@-------@----
       +---@-------@-------@----------
       +----
     # /tmp/d20131223-4637-jz16rg/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-jz16rg/spec.rb:84:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  6) Graphics Canvas drawing of shapes and rasterization of lines works with lines with a small slope
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "----------\n-@@-------\n---@@@@---\n-------@@-\n----------"
            got: "------------------------------\n--------------------"
       
       (compared using ==)
       
       Diff:
       @@ -1,6 +1,3 @@
       -----------
       --@@-------
       ----@@@@---
       --------@@-
       -----------
       +------------------------------
       +--------------------
     # /tmp/d20131223-4637-jz16rg/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-jz16rg/spec.rb:100:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  7) Graphics Canvas drawing of shapes and rasterization of lines works with lines with a significant slope, with swapped ends
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "----------\n-@--------\n-@--------\n--@-------\n--@-------\n--@-------\n--@-------\n---@------\n---@------\n----------"
            got: "------------------------------\n------------------------------\n------------------------------\n----------"
       
       (compared using ==)
       
       Diff:
       @@ -1,11 +1,5 @@
       -----------
       --@--------
       --@--------
       ---@-------
       ---@-------
       ---@-------
       ---@-------
       ----@------
       ----@------
       +------------------------------
       +------------------------------
       +------------------------------
        ----------
     # /tmp/d20131223-4637-jz16rg/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-jz16rg/spec.rb:113:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  8) Graphics Canvas drawing of shapes and rasterization of lines works with multiple lines
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "-@--------\n-@@-------\n-@-@@@@---\n-@-----@@-\n----------"
            got: "-@---------@---------@--------\n-@------------------"
       
       (compared using ==)
       
       Diff:
       @@ -1,6 +1,3 @@
       --@--------
       --@@-------
       --@-@@@@---
       --@-----@@-
       -----------
       +-@---------@---------@--------
       +-@------------------
     # /tmp/d20131223-4637-jz16rg/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-jz16rg/spec.rb:132:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  9) Graphics Canvas drawing of shapes and rasterization of lines draws lines with two equal ends as points
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "---\n-@-\n---"
            got: "----@----"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,2 @@
       ----
       --@-
       ----
       +----@----
     # /tmp/d20131223-4637-jz16rg/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-jz16rg/spec.rb:145:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  10) Graphics Canvas drawing of shapes and rasterization of rectangles works with simple rects
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "----------\n-@@@@@@@@-\n-@------@-\n-@@@@@@@@-\n----------"
            got: "-----------@@@@@@@@--@------@-\n-@@@@@@@@-----------"
       
       (compared using ==)
       
       Diff:
       @@ -1,6 +1,3 @@
       -----------
       --@@@@@@@@-
       --@------@-
       --@@@@@@@@-
       -----------
       +-----------@@@@@@@@--@------@-
       +-@@@@@@@@-----------
     # /tmp/d20131223-4637-jz16rg/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-jz16rg/spec.rb:158:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  11) Graphics Canvas drawing of shapes and rasterization of rectangles works with rects defined with their bottom left and top right points
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "----------\n-@@@@@@@@-\n-@------@-\n-@@@@@@@@-\n----------"
            got: "-----------@@@@@@@@--@------@-\n-@@@@@@@@-----------"
       
       (compared using ==)
       
       Diff:
       @@ -1,6 +1,3 @@
       -----------
       --@@@@@@@@-
       --@------@-
       --@@@@@@@@-
       -----------
       +-----------@@@@@@@@--@------@-
       +-@@@@@@@@-----------
     # /tmp/d20131223-4637-jz16rg/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-jz16rg/spec.rb:171:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  12) Graphics Canvas drawing of shapes and rasterization of rectangles works with rects with a zero height as a line
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "----------\n-@@@@@@@@-\n----------"
            got: "-----------@@@@@@@@-----------"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,2 @@
       -----------
       --@@@@@@@@-
       -----------
       +-----------@@@@@@@@-----------
     # /tmp/d20131223-4637-jz16rg/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-jz16rg/spec.rb:184:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  13) Graphics Canvas drawing of shapes and rasterization of rectangles works with rects with a zero width and height as a single point
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "---\n-@-\n---"
            got: "----@----"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,2 @@
       ----
       --@-
       ----
       +----@----
     # /tmp/d20131223-4637-jz16rg/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-jz16rg/spec.rb:195:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  14) Graphics Renderers Ascii renders a grid of the size of the canvas
     Failure/Error: lines.size.should eq 3
       
       expected: 3
            got: 1
       
       (compared using ==)
     # /tmp/d20131223-4637-jz16rg/spec.rb:240:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  15) Graphics Renderers Ascii renders blank canvases
     Failure/Error: canvas.render_as(ascii).should eq rendering('
       
       expected: "----\n----\n----"
            got: "------------"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,2 @@
       -----
       -----
       -----
       +------------
     # /tmp/d20131223-4637-jz16rg/spec.rb:245:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  16) Graphics Renderers Ascii renders simple canvases
     Failure/Error: canvas.render_as(ascii).should eq rendering('
       
       expected: "@---\n----\n---@"
            got: "@----------@"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,2 @@
       -@---
       -----
       ----@
       +@----------@
     # /tmp/d20131223-4637-jz16rg/spec.rb:256:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  17) Graphics Renderers Html returns html
     Failure/Error: rendering.should match /<divclass="canvas">/
       expected "<!doctypehtml><html><head><title>renderedcanvas</title><styletype=text/css>.canvas{font-size:1px;line-height:1px;}.canvas*{display:inline-block;width:10px;height:10px;border-radius:5px;}.canvasi{background-color:#eee;}.canvasb{background-color:#333;}</style></head><body><divclass=canvas><i></i><i></i><i></i><i></i><i></i><i></i><i></i><i></i><i></i><i></i><i></i><i></i></div></body></html>" to match /<divclass="canvas">/
     # /tmp/d20131223-4637-jz16rg/spec.rb:277:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  18) Graphics Renderers Html renders a grid of the size of the canvas
     Failure/Error: rendering.match(/<divclass="canvas">(.*?)<\/div>/)[1]
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20131223-4637-jz16rg/spec.rb:310:in `html_rendering_of'
     # /tmp/d20131223-4637-jz16rg/spec.rb:281:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  19) Graphics Renderers Html renders simple canvases
     Failure/Error: rendering.match(/<divclass="canvas">(.*?)<\/div>/)[1]
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20131223-4637-jz16rg/spec.rb:310:in `html_rendering_of'
     # /tmp/d20131223-4637-jz16rg/spec.rb:291:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  20) Graphics shapes Point does not allow setting its x and y
     Failure/Error: point.should_not respond_to :x=
       expected #<Graphics::Point:0xb8fefac0 @points=[], @x=1, @y=2> not to respond to :x=
     # /tmp/d20131223-4637-jz16rg/spec.rb:337:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  21) Graphics shapes Point comparison for equality returns the same hash for the same points
     Failure/Error: a1.hash.should eq a2.hash
       
       expected: -616031160
            got: -887980694
       
       (compared using ==)
     # /tmp/d20131223-4637-jz16rg/spec.rb:361:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  22) Graphics shapes Line initialization does not allow setting its from and to
     Failure/Error: line.should_not respond_to :from=
       expected #<Graphics::Line:0xb8fcf39c @points=[], @from=#<Graphics::Point:0xb8fcf428 @points=[], @x=1, @y=5>, @to=#<Graphics::Point:0xb8fcf3d8 @points=[], @x=25, @y=2>> not to respond to :from=
     # /tmp/d20131223-4637-jz16rg/spec.rb:390:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  23) Graphics shapes Line comparison for equality returns the same hash if the lines are the same
     Failure/Error: a.hash.should eq b.hash
       
       expected: 1026836960
            got: -483530300
       
       (compared using ==)
     # /tmp/d20131223-4637-jz16rg/spec.rb:465:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  24) Graphics shapes Rectangle initialization does not allow setting its x and y
     Failure/Error: rect.should_not respond_to :left=
       expected #<Graphics::Rectangle:0xb8fbe448 @right=#<Graphics::Point:0xb8fbc044 @points=[], @x=7, @y=8>, @left=#<Graphics::Point:0xb8fbc0bc @points=[], @x=3, @y=4>, @top_left=#<Graphics::Point:0xb8fbc0bc @points=[], @x=3, @y=4>, @bottom_left=#<Graphics::Point:0xb8fbbfcc @points=[], @x=3, @y=8>, @top_right=#<Graphics::Point:0xb8fbbf68 @points=[], @x=7, @y=4>, @bottom_right=#<Graphics::Point:0xb8fbc044 @points=[], @x=7, @y=8>> not to respond to :left=
     # /tmp/d20131223-4637-jz16rg/spec.rb:499:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  25) Graphics shapes Rectangle initialization puts the leftmost point in its left field
     Failure/Error: rect.left.x.should eq 4
       
       expected: 4
            got: 7
       
       (compared using ==)
     # /tmp/d20131223-4637-jz16rg/spec.rb:507:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  26) Graphics shapes Rectangle comparison for equality is true if rectangle points are the same
     Failure/Error: (a == b).should be_true
       expected: true value
            got: false
     # /tmp/d20131223-4637-jz16rg/spec.rb:526:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  27) Graphics shapes Rectangle comparison for equality is true if rectangle points are the same, even if swapped
     Failure/Error: (a == b).should be_true
       expected: true value
            got: false
     # /tmp/d20131223-4637-jz16rg/spec.rb:533:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  28) Graphics shapes Rectangle comparison for equality is true for rectangles defined with different diagonal corners
     Failure/Error: (a == b).should be_true
       expected: true value
            got: false
     # /tmp/d20131223-4637-jz16rg/spec.rb:540:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  29) Graphics shapes Rectangle comparison for equality works with eql? as well
     Failure/Error: a.should eql b
       
       expected: #<Graphics::Rectangle:0xb8f2db28 @right=#<Graphics::Point:0xb8f2db64 @points=[], @x=10, @y=14>, @left=#<Graphics::Point:0xb8f2dba0 @points=[], @x=1, @y=1>, @top_left=#<Graphics::Point:0xb8f2dba0 @points=[], @x=1, @y=1>, @bottom_left=#<Graphics::Point:0xb8f2db14 @points=[], @x=1, @y=14>, @top_right=#<Graphics::Point:0xb8f2dac4 @points=[], @x=10, @y=1>, @bottom_right=#<Graphics::Point:0xb8f2db64 @points=[], @x=10, @y=14>>
            got: #<Graphics::Rectangle:0xb8f2dc90 @right=#<Graphics::Point:0xb8f2dccc @points=[], @x=10, @y=14>, @left=#<Graphics::Point:0xb8f2dd08 @points=[], @x=1, @y=1>, @top_left=#<Graphics::Point:0xb8f2dd08 @points=[], @x=1, @y=1>, @bottom_left=#<Graphics::Point:0xb8f2dc7c @points=[], @x=1, @y=14>, @top_right=#<Graphics::Point:0xb8f2dc40 @points=[], @x=10, @y=1>, @bottom_right=#<Graphics::Point:0xb8f2dccc @points=[], @x=10, @y=14>>
       
       (compared using eql?)
       
       Diff:
       @@ -1,8 +1,8 @@
       -#<Graphics::Rectangle:0xb8f2db28
       - @bottom_left=#<Graphics::Point:0xb8f2db14 @points=[], @x=1, @y=14>,
       - @bottom_right=#<Graphics::Point:0xb8f2db64 @points=[], @x=10, @y=14>,
       - @left=#<Graphics::Point:0xb8f2dba0 @points=[], @x=1, @y=1>,
       - @right=#<Graphics::Point:0xb8f2db64 @points=[], @x=10, @y=14>,
       - @top_left=#<Graphics::Point:0xb8f2dba0 @points=[], @x=1, @y=1>,
       - @top_right=#<Graphics::Point:0xb8f2dac4 @points=[], @x=10, @y=1>>
       +#<Graphics::Rectangle:0xb8f2dc90
       + @bottom_left=#<Graphics::Point:0xb8f2dc7c @points=[], @x=1, @y=14>,
       + @bottom_right=#<Graphics::Point:0xb8f2dccc @points=[], @x=10, @y=14>,
       + @left=#<Graphics::Point:0xb8f2dd08 @points=[], @x=1, @y=1>,
       + @right=#<Graphics::Point:0xb8f2dccc @points=[], @x=10, @y=14>,
       + @top_left=#<Graphics::Point:0xb8f2dd08 @points=[], @x=1, @y=1>,
       + @top_right=#<Graphics::Point:0xb8f2dc40 @points=[], @x=10, @y=1>>
     # /tmp/d20131223-4637-jz16rg/spec.rb:548:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  30) Graphics shapes Rectangle comparison for equality returns the same hash if the rectangles are the same
     Failure/Error: a.hash.should eq b.hash
       
       expected: 546655682
            got: -209267014
       
       (compared using ==)
     # /tmp/d20131223-4637-jz16rg/spec.rb:556:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  31) Graphics shapes Rectangle comparison for equality returns the same hash for rectangles defined with different diagonal corners
     Failure/Error: a.hash.should eq b.hash
       
       expected: -676404406
            got: -511452338
       
       (compared using ==)
     # /tmp/d20131223-4637-jz16rg/spec.rb:563:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  32) Graphics shapes Rectangle corners bottom right
     Failure/Error: rect.bottom_right.x.should eq 5
       
       expected: 5
            got: 1
       
       (compared using ==)
     # /tmp/d20131223-4637-jz16rg/spec.rb:589:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.11247 seconds
69 examples, 32 failures

Failed examples:

rspec /tmp/d20131223-4637-jz16rg/spec.rb:16 # Graphics Canvas exposes its width and height via getters
rspec /tmp/d20131223-4637-jz16rg/spec.rb:203 # Graphics Canvas drawing of shapes and rasterization renders multiple drawn shapes
rspec /tmp/d20131223-4637-jz16rg/spec.rb:51 # Graphics Canvas drawing of shapes and rasterization of points works for multiple ones
rspec /tmp/d20131223-4637-jz16rg/spec.rb:69 # Graphics Canvas drawing of shapes and rasterization of lines works with simple horizontal lines
rspec /tmp/d20131223-4637-jz16rg/spec.rb:80 # Graphics Canvas drawing of shapes and rasterization of lines works with vertical lines
rspec /tmp/d20131223-4637-jz16rg/spec.rb:96 # Graphics Canvas drawing of shapes and rasterization of lines works with lines with a small slope
rspec /tmp/d20131223-4637-jz16rg/spec.rb:109 # Graphics Canvas drawing of shapes and rasterization of lines works with lines with a significant slope, with swapped ends
rspec /tmp/d20131223-4637-jz16rg/spec.rb:127 # Graphics Canvas drawing of shapes and rasterization of lines works with multiple lines
rspec /tmp/d20131223-4637-jz16rg/spec.rb:141 # Graphics Canvas drawing of shapes and rasterization of lines draws lines with two equal ends as points
rspec /tmp/d20131223-4637-jz16rg/spec.rb:154 # Graphics Canvas drawing of shapes and rasterization of rectangles works with simple rects
rspec /tmp/d20131223-4637-jz16rg/spec.rb:167 # Graphics Canvas drawing of shapes and rasterization of rectangles works with rects defined with their bottom left and top right points
rspec /tmp/d20131223-4637-jz16rg/spec.rb:180 # Graphics Canvas drawing of shapes and rasterization of rectangles works with rects with a zero height as a line
rspec /tmp/d20131223-4637-jz16rg/spec.rb:191 # Graphics Canvas drawing of shapes and rasterization of rectangles works with rects with a zero width and height as a single point
rspec /tmp/d20131223-4637-jz16rg/spec.rb:237 # Graphics Renderers Ascii renders a grid of the size of the canvas
rspec /tmp/d20131223-4637-jz16rg/spec.rb:244 # Graphics Renderers Ascii renders blank canvases
rspec /tmp/d20131223-4637-jz16rg/spec.rb:252 # Graphics Renderers Ascii renders simple canvases
rspec /tmp/d20131223-4637-jz16rg/spec.rb:271 # Graphics Renderers Html returns html
rspec /tmp/d20131223-4637-jz16rg/spec.rb:280 # Graphics Renderers Html renders a grid of the size of the canvas
rspec /tmp/d20131223-4637-jz16rg/spec.rb:287 # Graphics Renderers Html renders simple canvases
rspec /tmp/d20131223-4637-jz16rg/spec.rb:335 # Graphics shapes Point does not allow setting its x and y
rspec /tmp/d20131223-4637-jz16rg/spec.rb:360 # Graphics shapes Point comparison for equality returns the same hash for the same points
rspec /tmp/d20131223-4637-jz16rg/spec.rb:389 # Graphics shapes Line initialization does not allow setting its from and to
rspec /tmp/d20131223-4637-jz16rg/spec.rb:461 # Graphics shapes Line comparison for equality returns the same hash if the lines are the same
rspec /tmp/d20131223-4637-jz16rg/spec.rb:496 # Graphics shapes Rectangle initialization does not allow setting its x and y
rspec /tmp/d20131223-4637-jz16rg/spec.rb:504 # Graphics shapes Rectangle initialization puts the leftmost point in its left field
rspec /tmp/d20131223-4637-jz16rg/spec.rb:522 # Graphics shapes Rectangle comparison for equality is true if rectangle points are the same
rspec /tmp/d20131223-4637-jz16rg/spec.rb:529 # Graphics shapes Rectangle comparison for equality is true if rectangle points are the same, even if swapped
rspec /tmp/d20131223-4637-jz16rg/spec.rb:536 # Graphics shapes Rectangle comparison for equality is true for rectangles defined with different diagonal corners
rspec /tmp/d20131223-4637-jz16rg/spec.rb:543 # Graphics shapes Rectangle comparison for equality works with eql? as well
rspec /tmp/d20131223-4637-jz16rg/spec.rb:552 # Graphics shapes Rectangle comparison for equality returns the same hash if the rectangles are the same
rspec /tmp/d20131223-4637-jz16rg/spec.rb:559 # Graphics shapes Rectangle comparison for equality returns the same hash for rectangles defined with different diagonal corners
rspec /tmp/d20131223-4637-jz16rg/spec.rb:587 # Graphics shapes Rectangle corners bottom right

История (1 версия и 0 коментара)

Илия обнови решението на 22.12.2013 22:35 (преди над 10 години)

+module Graphics
+ class Point
+ attr_accessor :x, :y, :points
+
+ def initialize(x, y)
+ @points = []
+ @x = x
+ @y = y
+ end
+
+ def eql?(other)
+ @x == other.x and @y == other.y
+ end
+
+ alias :== :eql?
+
+ def lefter?(other)
+ @x <= other.x
+ end
+
+ def upper?(other)
+ @y <= other.y
+ end
+
+ def draw()
+ @points = [[@x, @y]]
+ end
+ end
+
+ class Line
+ attr_accessor :from, :to, :points
+
+ def initialize(from, to)
+ @points = []
+ if from.lefter? to
+ @from, @to = from, to
+ else
+ @from, @to= to, from
+ end
+ end
+
+ def eql?(other)
+ @from == other.from and @to == other.to
+ end
+
+ alias :== :eql?
+
+ def from()
+ if @from.x == @to.x
+ @from.y <= @to.y ? @from : @to
+ else
+ @from
+ end
+ end
+
+ def to()
+ if @from.x == @to.x
+ @from.y <= @to.y ? @to : @from
+ else
+ @to
+ end
+ end
+
+ def vertical?()
+ @from.x == @to.x
+ end
+
+ def horizontal?()
+ @from.y == @to.y
+ end
+
+ def draw()
+ vertical_draw if vertical?
+ horizontal_draw if horizontal?
+ draw_line() if points.empty?
+ points
+ end
+
+ def vertical_draw()
+ from.y.upto(to.y).each { |y| points << [from.x, y] } if points.empty?
+ end
+
+ def horizontal_draw()
+ from.x.upto(to.x).each { |x| points << [x, from.y] } if points.empty?
+ end
+
+ def draw_line()
+
+ end
+
+ end
+ class Rectangle
+ attr_accessor :points, :left, :right, :top_left, :top_right,
+ :bottom_left, :bottom_right
+
+ def initialize(left, right)
+ if left.lefter? right or left.upper? right
+ @left, @right = left, right
+ else
+ @left, @right = right, left
+ end
+ left_corners()
+ right_corners()
+ end
+
+ def left_corners()
+ other_left_point = Point.new left.x, right.y
+ if left.upper? other_left_point
+ @top_left, @bottom_left = left, other_left_point
+ else
+ @top_left, @bottom_left = other_left_point, left
+ end
+ end
+
+ def right_corners()
+ other_right_point = Point.new right.x, left.y
+ if right.upper? other_right_point
+ @top_right, @bottom_right = right, other_right_point
+ else
+ @top_right, @bottom_right = other_right_point, right
+ end
+ end
+
+ def left?()
+ @left
+ end
+
+ def right?()
+ @right
+ end
+
+ def draw()
+ @points = []
+ sides = [Line.new(bottom_left, top_left), Line.new(top_left, top_right),
+ Line.new(top_right, bottom_right), Line.new(bottom_left, bottom_right)]
+ sides.each { |side| points << side.draw }
+
+ points.flatten(1).uniq
+ end
+
+ def top_left()
+ @top_left
+ end
+
+ def bottom_left()
+ @bottom_left
+ end
+
+ def top_right
+ @top_right
+ end
+
+ def bottom_right
+ @bottom_right
+ end
+ end
+
+ class Canvas
+ attr_accessor :pixels
+
+ def initialize(width, height)
+ @pixels = {}
+ 0.upto(height-1) do |y|
+ 0.upto(width-1) do |x|
+ pixels[[x, y]]=false
+ end
+ end
+ @width = width
+ @height = height
+ end
+
+ def set_pixel(x, y)
+ @pixels[[x, y]]=true
+ end
+
+ def pixel_at?(x, y)
+ @pixels[[x, y]]
+ end
+
+ def draw(figure)
+ figure.draw.each { |point| set_pixel(*point) }
+ end
+
+ def render(renderer)
+ content = ""
+ length = renderer.symbols[:symbol_length]
+ pixels.values.each { |value| content += renderer.symbols[value] }
+ content.chars.each_slice(30*length).map(&:join).join(renderer.symbols[:new_line])
+ end
+
+ def render_as(renderer)
+ output = renderer.symbols[:start]
+ output += render(renderer)
+ output += renderer.symbols[:end]
+ output
+ end
+ end
+
+ module Renderers
+ class Ascii
+ @symbols = {true => "@", false => "-", :new_line => "\n",
+ :start => "", :end => "", :symbol_length => 1}
+
+ def Ascii.symbols()
+ @symbols
+ end
+ end
+
+ class Html
+ @start = "<!DOCTYPE html>
+<html>
+<head>
+ <title>Rendered Canvas</title>
+ <style type="'text/css'">
+ .canvas {
+ font-size: 1px;
+ line-height: 1px;
+ }
+ .canvas * {
+ display: inline-block;
+ width: 10px;
+ height: 10px;
+ border-radius: 5px;
+ }
+ .canvas i {
+ background-color: #eee;
+ }
+ .canvas b {
+ background-color: #333;
+ }
+ </style>
+</head>
+<body>
+ <div class="'canvas'">
+ "
+
+ @end = "</div>
+ </body>
+ </html>"
+ @symbols = {true => "<b></b>", false => "<i></i>", :new_line => "<br>",
+ :start => @start, :end => @end, :symbol_length => 7}
+
+ def Html.symbols()
+ @symbols
+ end
+ end
+ end
+end