Решение на Трета задача от Иван Латунов

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

Към профила на Иван Латунов

Резултати

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

Код

module Graphics
class Canvas
attr_reader :width, :height
def initialize(width, height)
@width = width.to_i
@height = height.to_i
@canvas = [[false] * width] * height
end
def set_pixel(x, y)
@canvas[x] = @canvas[x].each_with_index.map {|elem, ind| y == ind ? true : elem}
end
def pixel_at?(x, y)
@canvas[x][y]
end
def draw(shape)
shape.draw_on self
end
def to_s
@canvas.each {|x| puts x.join(' ')}
end
def render_as(render_engine)
render_engine.render @canvas
end
end
class Point
attr_reader :x, :y
def min_x(other)
return @x < other.x ? self : other
end
def min_y(other)
return @y < other.y ? self : other
end
def max_x(other)
return @x > other.x ? self : other
end
def max_y(other)
return @y > other.y ? self : other
end
def initialize(x, y)
@x = x.to_i
@y = y.to_i
end
def ==(other)
return (other.x == @x and other.y == @y)
end
def eql?(other)
return (self.inspect == other.inspect)
end
def draw_on(canvas)
canvas.set_pixel(@x, @y)
end
end
class Line
attr_reader :from, :to
def initialize(from, to)
@from = from.freeze
@to = to.freeze
end
def ==(other)
return [@from, @to] - [other.from, other.to] == []
end
def eql?(other)
return self.inspect == other.inspect
end
def draw_on(canvas)
end
def straight?
return (@from.x == @to.x or @from.y == @to.y)
end
end
class Rectangle
attr_reader :left, :right
def initialize(point_1, point_2)
@left = point_1.min_x point_2
@right = point_1.max_y point_2
end
def draw_on(canvas)
end
def eql?(other)
return self.inspect == other.inspect
end
def ==(other)
return [@left, @right] - [other.left, other.right] == 0
end
end
module Renderers
HEAD_HTML = %w(
<!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">
).freeze
END_HTML = %w(
</div>
</body>
</html>).freeze
class BasicRenderer
def BasicRenderer.render_row(row, yes, no)
row.map {|x| x ? yes : no}
end
def BasicRenderer.render(canvas, start='', yes='#', no='@', newline="\n", post='')
result = start.dup
canvas.each do |row|
result << render_row(row, yes, no).join('')
result << newline
end
result << post
result
end
end
class Html < BasicRenderer
@@begin, @@true, @@false, @@newline, @@end =
HEAD_HTML, "<b></b>", "<i></i>", "<br />", END_HTML
def Html.render(canvas)
BasicRenderer.render canvas, @@begin, @@true, @@false, @@newline, @@end
end
end
class Ascii < BasicRenderer
@@begin, @@true, @@false, @@newline, @@end = '', '@', '-', "\n", ''
def Ascii.render(canvas)
BasicRenderer.render canvas, @@begin, @@true, @@false, @@newline, @@end
end
end
end
end

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

........F.FFFFFFFFFFF.FFFFFF......FF.....FFFF.FFFFF.....F.FFFFFF.FFFF

Failures:

  1) 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---------------\n---------------\n---------------\n---------------\n---------------\n---------------\n---------------\n---------------\n"
       
       (compared using ==)
       
       Diff:
       @@ -1,16 +1,16 @@
       -@@@@@@@@@@@@@@@
       -@-------------@
       -@-@@@@@@@@@@@-@
       -@-@---------@-@
       -@-@------@@-@-@
       -@-@---@@@---@-@
       -@-@-@@------@-@
       -@-@---------@-@
       -@-@-@@@@----@-@
       -@-@-@-------@-@
       -@-@---------@-@
       -@-@---------@-@
       -@-@@@@@@@@@@@-@
       -@-------------@
       -@@@@@@@@@@@@@@@
       +---------------
       +---------------
       +---------------
       +---------------
       +---------@-----
       +---------------
       +---------------
       +---------------
       +---------------
       +---------------
       +---------------
       +---------------
       +---------------
       +---------------
       +---------------
     # /tmp/d20131223-4637-gi3lo/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-gi3lo/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)>'

  2) Graphics Canvas drawing of shapes and rasterization of points works for multiple ones
     Failure/Error: canvas.set_pixel 4, 4
     NoMethodError:
       undefined method `each_with_index' for nil:NilClass
     # /tmp/d20131223-4637-gi3lo/solution.rb:13:in `set_pixel'
     # /tmp/d20131223-4637-gi3lo/spec.rb:57: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)>'

  3) 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: "--------\n--------\n--------\n"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,4 @@
        --------
       ----@@@@-
       +--------
        --------
     # /tmp/d20131223-4637-gi3lo/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-gi3lo/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)>'

  4) 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--------\n--------\n--------\n--------\n--------\n--------\n"
       
       (compared using ==)
       
       Diff:
       @@ -1,9 +1,9 @@
       --@------
       --@------
       --@------
       --@------
       --@------
       --@------
       --@------
       +--------
       +--------
       +--------
       +--------
       +--------
       +--------
       +--------
        --------
     # /tmp/d20131223-4637-gi3lo/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-gi3lo/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)>'

  5) 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----------\n----------\n----------\n----------\n"
       
       (compared using ==)
       
       Diff:
       @@ -1,6 +1,6 @@
        ----------
       --@@-------
       ----@@@@---
       --------@@-
       +----------
       +----------
       +----------
        ----------
     # /tmp/d20131223-4637-gi3lo/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-gi3lo/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)>'

  6) 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----------\n----------\n----------\n----------\n----------\n----------\n----------\n"
       
       (compared using ==)
       
       Diff:
       @@ -1,11 +1,11 @@
        ----------
       --@--------
       --@--------
       ---@-------
       ---@-------
       ---@-------
       ---@-------
       ----@------
       ----@------
       +----------
       +----------
       +----------
       +----------
       +----------
       +----------
       +----------
       +----------
        ----------
     # /tmp/d20131223-4637-gi3lo/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-gi3lo/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)>'

  7) 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----------\n----------\n----------\n----------\n"
       
       (compared using ==)
       
       Diff:
       @@ -1,6 +1,6 @@
       --@--------
       --@@-------
       --@-@@@@---
       --@-----@@-
       +----------
       +----------
       +----------
       +----------
        ----------
     # /tmp/d20131223-4637-gi3lo/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-gi3lo/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)>'

  8) 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: "---\n---\n---\n"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,4 @@
        ---
       --@-
       +---
        ---
     # /tmp/d20131223-4637-gi3lo/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-gi3lo/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)>'

  9) 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----------\n----------\n----------\n----------\n"
       
       (compared using ==)
       
       Diff:
       @@ -1,6 +1,6 @@
        ----------
       --@@@@@@@@-
       --@------@-
       --@@@@@@@@-
       +----------
       +----------
       +----------
        ----------
     # /tmp/d20131223-4637-gi3lo/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-gi3lo/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)>'

  10) 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----------\n----------\n----------\n----------\n"
       
       (compared using ==)
       
       Diff:
       @@ -1,6 +1,6 @@
        ----------
       --@@@@@@@@-
       --@------@-
       --@@@@@@@@-
       +----------
       +----------
       +----------
        ----------
     # /tmp/d20131223-4637-gi3lo/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-gi3lo/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)>'

  11) 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: "----------\n----------\n----------\n"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,4 @@
        ----------
       --@@@@@@@@-
       +----------
        ----------
     # /tmp/d20131223-4637-gi3lo/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-gi3lo/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)>'

  12) 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: "---\n---\n---\n"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,4 @@
        ---
       --@-
       +---
        ---
     # /tmp/d20131223-4637-gi3lo/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-gi3lo/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)>'

  13) Graphics Renderers Ascii renders blank canvases
     Failure/Error: canvas.render_as(ascii).should eq rendering('
       
       expected: "----\n----\n----"
            got: "----\n----\n----\n"
       
       (compared using ==)
       
       Diff:
     # /tmp/d20131223-4637-gi3lo/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)>'

  14) Graphics Renderers Ascii renders simple canvases
     Failure/Error: canvas.set_pixel 3, 2
     NoMethodError:
       undefined method `each_with_index' for nil:NilClass
     # /tmp/d20131223-4637-gi3lo/solution.rb:13:in `set_pixel'
     # /tmp/d20131223-4637-gi3lo/spec.rb:254: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 Html returns html
     Failure/Error: html.gsub(/\s+/, '').downcase
     NoMethodError:
       undefined method `gsub' for #<Array:0xb920c45c>
     # /tmp/d20131223-4637-gi3lo/spec.rb:314:in `normalize_html'
     # /tmp/d20131223-4637-gi3lo/spec.rb:272: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 Html renders a grid of the size of the canvas
     Failure/Error: html.gsub(/\s+/, '').downcase
     NoMethodError:
       undefined method `gsub' for #<Array:0xb920e3b0>
     # /tmp/d20131223-4637-gi3lo/spec.rb:314:in `normalize_html'
     # /tmp/d20131223-4637-gi3lo/spec.rb:309:in `html_rendering_of'
     # /tmp/d20131223-4637-gi3lo/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)>'

  17) Graphics Renderers Html renders simple canvases
     Failure/Error: html.gsub(/\s+/, '').downcase
     NoMethodError:
       undefined method `gsub' for #<Array:0xb9202c04>
     # /tmp/d20131223-4637-gi3lo/spec.rb:314:in `normalize_html'
     # /tmp/d20131223-4637-gi3lo/spec.rb:309:in `html_rendering_of'
     # /tmp/d20131223-4637-gi3lo/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)>'

  18) Graphics Renderers Html returns the same rendering when called twice
     Failure/Error: html.gsub(/\s+/, '').downcase
     NoMethodError:
       undefined method `gsub' for #<Array:0xb9201b4c>
     # /tmp/d20131223-4637-gi3lo/spec.rb:314:in `normalize_html'
     # /tmp/d20131223-4637-gi3lo/spec.rb:302: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 shapes Point comparison for equality works for eql? as well
     Failure/Error: a1.should eql a2
       
       expected: #<Graphics::Point:0xb91ed840 @x=4, @y=5>
            got: #<Graphics::Point:0xb91ed868 @x=4, @y=5>
       
       (compared using eql?)
       
       Diff:
       @@ -1,2 +1,2 @@
       -#<Graphics::Point:0xb91ed840 @x=4, @y=5>
       +#<Graphics::Point:0xb91ed868 @x=4, @y=5>
     # /tmp/d20131223-4637-gi3lo/spec.rb:356: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)>'

  20) Graphics shapes Point comparison for equality returns the same hash for the same points
     Failure/Error: a1.hash.should eq a2.hash
       
       expected: -199591794
            got: 279019722
       
       (compared using ==)
     # /tmp/d20131223-4637-gi3lo/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)>'

  21) Graphics shapes Line initialization with swapped points puts the leftmost point in the from field
     Failure/Error: inverted_line.from.x.should eq 1
       
       expected: 1
            got: 25
       
       (compared using ==)
     # /tmp/d20131223-4637-gi3lo/spec.rb:402:in `block (6 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 with swapped points puts the rightmost point in the to field
     Failure/Error: inverted_line.to.x.should eq 25
       
       expected: 25
            got: 1
       
       (compared using ==)
     # /tmp/d20131223-4637-gi3lo/spec.rb:407:in `block (6 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 initialization with swapped points puts the top point of vertical lines in the from field
     Failure/Error: vertical_line.from.y.should eq 1
       
       expected: 1
            got: 8
       
       (compared using ==)
     # /tmp/d20131223-4637-gi3lo/spec.rb:413:in `block (6 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 Line initialization with swapped points puts the bottom point of vertical lines in the to field
     Failure/Error: vertical_line.to.y.should eq 8
       
       expected: 8
            got: 1
       
       (compared using ==)
     # /tmp/d20131223-4637-gi3lo/spec.rb:418:in `block (6 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 Line comparison for equality is true if line ends are the same
     Failure/Error: (a == b).should be_true
       expected: true value
            got: false
     # /tmp/d20131223-4637-gi3lo/spec.rb:435: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 Line comparison for equality is true if line ends are the same, even if swapped
     Failure/Error: (a == b).should be_true
       expected: true value
            got: false
     # /tmp/d20131223-4637-gi3lo/spec.rb:442: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 Line comparison for equality is true if line is vertical and the bottom is given first
     Failure/Error: (a == b).should be_true
       expected: true value
            got: false
     # /tmp/d20131223-4637-gi3lo/spec.rb:449: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 Line comparison for equality works with eql? as well
     Failure/Error: a.should eql b
       
       expected: #<Graphics::Line:0xb91c0b88 @from=#<Graphics::Point:0xb91c0bec @x=1, @y=1>, @to=#<Graphics::Point:0xb91c0bb0 @x=10, @y=14>>
            got: #<Graphics::Line:0xb91c0c28 @from=#<Graphics::Point:0xb91c0cb4 @x=1, @y=1>, @to=#<Graphics::Point:0xb91c0c78 @x=10, @y=14>>
       
       (compared using eql?)
       
       Diff:
       @@ -1,4 +1,4 @@
       -#<Graphics::Line:0xb91c0b88
       - @from=#<Graphics::Point:0xb91c0bec @x=1, @y=1>,
       - @to=#<Graphics::Point:0xb91c0bb0 @x=10, @y=14>>
       +#<Graphics::Line:0xb91c0c28
       + @from=#<Graphics::Point:0xb91c0cb4 @x=1, @y=1>,
       + @to=#<Graphics::Point:0xb91c0c78 @x=10, @y=14>>
     # /tmp/d20131223-4637-gi3lo/spec.rb:457: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 Line comparison for equality returns the same hash if the lines are the same
     Failure/Error: a.hash.should eq b.hash
       
       expected: 85038402
            got: 731418720
       
       (compared using ==)
     # /tmp/d20131223-4637-gi3lo/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)>'

  30) Graphics shapes Rectangle initialization puts the leftmost point in its left field
     Failure/Error: rect.right.x.should eq 7
       
       expected: 7
            got: 4
       
       (compared using ==)
     # /tmp/d20131223-4637-gi3lo/spec.rb:509: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 is true if rectangle points are the same
     Failure/Error: (a == b).should be_true
       expected: true value
            got: false
     # /tmp/d20131223-4637-gi3lo/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)>'

  32) 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-gi3lo/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)>'

  33) 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-gi3lo/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)>'

  34) Graphics shapes Rectangle comparison for equality works with eql? as well
     Failure/Error: a.should eql b
       
       expected: #<Graphics::Rectangle:0xb9178d38 @left=#<Graphics::Point:0xb9178db0 @x=1, @y=1>, @right=#<Graphics::Point:0xb9178d74 @x=10, @y=14>>
            got: #<Graphics::Rectangle:0xb9178dd8 @left=#<Graphics::Point:0xb9178e3c @x=1, @y=1>, @right=#<Graphics::Point:0xb9178e14 @x=10, @y=14>>
       
       (compared using eql?)
       
       Diff:
       @@ -1,4 +1,4 @@
       -#<Graphics::Rectangle:0xb9178d38
       - @left=#<Graphics::Point:0xb9178db0 @x=1, @y=1>,
       - @right=#<Graphics::Point:0xb9178d74 @x=10, @y=14>>
       +#<Graphics::Rectangle:0xb9178dd8
       + @left=#<Graphics::Point:0xb9178e3c @x=1, @y=1>,
       + @right=#<Graphics::Point:0xb9178e14 @x=10, @y=14>>
     # /tmp/d20131223-4637-gi3lo/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)>'

  35) 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: 141533714
            got: 502075490
       
       (compared using ==)
     # /tmp/d20131223-4637-gi3lo/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)>'

  36) 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: 569487526
            got: 914166668
       
       (compared using ==)
     # /tmp/d20131223-4637-gi3lo/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)>'

  37) Graphics shapes Rectangle corners top left
     Failure/Error: rect.top_left.x.should eq 1
     NoMethodError:
       undefined method `top_left' for #<Graphics::Rectangle:0xb9156274>
     # /tmp/d20131223-4637-gi3lo/spec.rb:577: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)>'

  38) Graphics shapes Rectangle corners top right
     Failure/Error: rect.top_right.x.should eq 5
     NoMethodError:
       undefined method `top_right' for #<Graphics::Rectangle:0xb91553b0>
     # /tmp/d20131223-4637-gi3lo/spec.rb:583: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)>'

  39) Graphics shapes Rectangle corners bottom right
     Failure/Error: rect.bottom_right.x.should eq 5
     NoMethodError:
       undefined method `bottom_right' for #<Graphics::Rectangle:0xb915467c>
     # /tmp/d20131223-4637-gi3lo/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)>'

  40) Graphics shapes Rectangle corners bottom left
     Failure/Error: rect.bottom_left.x.should eq 1
     NoMethodError:
       undefined method `bottom_left' for #<Graphics::Rectangle:0xb9147a08>
     # /tmp/d20131223-4637-gi3lo/spec.rb:595: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.09361 seconds
69 examples, 40 failures

Failed examples:

rspec /tmp/d20131223-4637-gi3lo/spec.rb:203 # Graphics Canvas drawing of shapes and rasterization renders multiple drawn shapes
rspec /tmp/d20131223-4637-gi3lo/spec.rb:51 # Graphics Canvas drawing of shapes and rasterization of points works for multiple ones
rspec /tmp/d20131223-4637-gi3lo/spec.rb:69 # Graphics Canvas drawing of shapes and rasterization of lines works with simple horizontal lines
rspec /tmp/d20131223-4637-gi3lo/spec.rb:80 # Graphics Canvas drawing of shapes and rasterization of lines works with vertical lines
rspec /tmp/d20131223-4637-gi3lo/spec.rb:96 # Graphics Canvas drawing of shapes and rasterization of lines works with lines with a small slope
rspec /tmp/d20131223-4637-gi3lo/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-gi3lo/spec.rb:127 # Graphics Canvas drawing of shapes and rasterization of lines works with multiple lines
rspec /tmp/d20131223-4637-gi3lo/spec.rb:141 # Graphics Canvas drawing of shapes and rasterization of lines draws lines with two equal ends as points
rspec /tmp/d20131223-4637-gi3lo/spec.rb:154 # Graphics Canvas drawing of shapes and rasterization of rectangles works with simple rects
rspec /tmp/d20131223-4637-gi3lo/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-gi3lo/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-gi3lo/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-gi3lo/spec.rb:244 # Graphics Renderers Ascii renders blank canvases
rspec /tmp/d20131223-4637-gi3lo/spec.rb:252 # Graphics Renderers Ascii renders simple canvases
rspec /tmp/d20131223-4637-gi3lo/spec.rb:271 # Graphics Renderers Html returns html
rspec /tmp/d20131223-4637-gi3lo/spec.rb:280 # Graphics Renderers Html renders a grid of the size of the canvas
rspec /tmp/d20131223-4637-gi3lo/spec.rb:287 # Graphics Renderers Html renders simple canvases
rspec /tmp/d20131223-4637-gi3lo/spec.rb:298 # Graphics Renderers Html returns the same rendering when called twice
rspec /tmp/d20131223-4637-gi3lo/spec.rb:355 # Graphics shapes Point comparison for equality works for eql? as well
rspec /tmp/d20131223-4637-gi3lo/spec.rb:360 # Graphics shapes Point comparison for equality returns the same hash for the same points
rspec /tmp/d20131223-4637-gi3lo/spec.rb:401 # Graphics shapes Line initialization with swapped points puts the leftmost point in the from field
rspec /tmp/d20131223-4637-gi3lo/spec.rb:406 # Graphics shapes Line initialization with swapped points puts the rightmost point in the to field
rspec /tmp/d20131223-4637-gi3lo/spec.rb:411 # Graphics shapes Line initialization with swapped points puts the top point of vertical lines in the from field
rspec /tmp/d20131223-4637-gi3lo/spec.rb:416 # Graphics shapes Line initialization with swapped points puts the bottom point of vertical lines in the to field
rspec /tmp/d20131223-4637-gi3lo/spec.rb:431 # Graphics shapes Line comparison for equality is true if line ends are the same
rspec /tmp/d20131223-4637-gi3lo/spec.rb:438 # Graphics shapes Line comparison for equality is true if line ends are the same, even if swapped
rspec /tmp/d20131223-4637-gi3lo/spec.rb:445 # Graphics shapes Line comparison for equality is true if line is vertical and the bottom is given first
rspec /tmp/d20131223-4637-gi3lo/spec.rb:452 # Graphics shapes Line comparison for equality works with eql? as well
rspec /tmp/d20131223-4637-gi3lo/spec.rb:461 # Graphics shapes Line comparison for equality returns the same hash if the lines are the same
rspec /tmp/d20131223-4637-gi3lo/spec.rb:504 # Graphics shapes Rectangle initialization puts the leftmost point in its left field
rspec /tmp/d20131223-4637-gi3lo/spec.rb:522 # Graphics shapes Rectangle comparison for equality is true if rectangle points are the same
rspec /tmp/d20131223-4637-gi3lo/spec.rb:529 # Graphics shapes Rectangle comparison for equality is true if rectangle points are the same, even if swapped
rspec /tmp/d20131223-4637-gi3lo/spec.rb:536 # Graphics shapes Rectangle comparison for equality is true for rectangles defined with different diagonal corners
rspec /tmp/d20131223-4637-gi3lo/spec.rb:543 # Graphics shapes Rectangle comparison for equality works with eql? as well
rspec /tmp/d20131223-4637-gi3lo/spec.rb:552 # Graphics shapes Rectangle comparison for equality returns the same hash if the rectangles are the same
rspec /tmp/d20131223-4637-gi3lo/spec.rb:559 # Graphics shapes Rectangle comparison for equality returns the same hash for rectangles defined with different diagonal corners
rspec /tmp/d20131223-4637-gi3lo/spec.rb:575 # Graphics shapes Rectangle corners top left
rspec /tmp/d20131223-4637-gi3lo/spec.rb:581 # Graphics shapes Rectangle corners top right
rspec /tmp/d20131223-4637-gi3lo/spec.rb:587 # Graphics shapes Rectangle corners bottom right
rspec /tmp/d20131223-4637-gi3lo/spec.rb:593 # Graphics shapes Rectangle corners bottom left

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

Иван обнови решението на 22.12.2013 23:08 (преди почти 11 години)

+module Graphics
+
+ class Canvas
+ attr_reader :width, :height
+
+ def initialize(width, height)
+ @width = width.to_i
+ @height = height.to_i
+ @canvas = [[false] * width] * height
+ end
+
+ def set_pixel(x, y)
+ @canvas[x] = @canvas[x].each_with_index.map {|elem, ind| y == ind ? true : elem}
+ end
+
+ def pixel_at?(x, y)
+ @canvas[x][y]
+ end
+
+ def draw(shape)
+ shape.draw_on self
+ end
+
+ def to_s
+ @canvas.each {|x| puts x.join(' ')}
+ end
+
+ def render_as(render_engine)
+ render_engine.render @canvas
+ end
+ end
+
+ class Point
+ attr_reader :x, :y
+
+ def initialize(x, y)
+ @x = x.to_i
+ @y = y.to_i
+ end
+
+ def ==(other)
+ return (other.x == @x and other.y == @y)
+ end
+
+ def eql?(other)
+ return (self.inspect == other.inspect)
+ end
+
+ def draw_on(canvas)
+ canvas.set_pixel(@x, @y)
+ end
+ end
+
+ class Line
+ attr_reader :from, :to
+
+ def initialize(from, to)
+ @from = from.freeze
+ @to = to.freeze
+ end
+
+ def ==(other)
+ return [@from, @to] - [other.from, other.to] == []
+ end
+
+ def eql?(other)
+ return self.inspect == other.inspect
+ end
+
+ def draw_on(canvas)
+ end
+
+ def straight?
+ return (@from.x == @to.x or @from.y == @to.y)
+ end
+ end
+
+ class Rectangle
+ def initialize(point_1, point_2)
+ if point_2 == point_1 then
+ @top_left = @top_right = @bottom_left = @bottom_right = point_1.freeze
+ else
+ end
+ end
+ end
+
+ module Renderers
+ HEAD_HTML = %w(
+ <!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">
+ ).freeze
+ END_HTML = %w(
+ </div>
+ </body>
+ </html>).freeze
+
+ class BasicRenderer
+
+ def BasicRenderer.render_row(row, yes, no)
+ row.map {|x| x ? yes : no}
+ end
+
+ def BasicRenderer.render(canvas, start='', yes='#', no='@', newline="\n", post='')
+ result = start.dup
+ canvas.each do |row|
+ result << render_row(row, yes, no).join('')
+ result << newline
+ end
+ result << post
+ result
+ end
+ end
+
+ class Html < BasicRenderer
+ @@begin, @@true, @@false, @@newline, @@end =
+ HEAD_HTML, "<b></b>", "<i></i>", "<br />", END_HTML
+ def Html.render(canvas)
+ BasicRenderer.render canvas, @@begin, @@true, @@false, @@newline, @@end
+ end
+ end
+
+ class Ascii < BasicRenderer
+ @@begin, @@true, @@false, @@newline, @@end = '', '@', '-', "\n", ''
+ def Ascii.render(canvas)
+ BasicRenderer.render canvas, @@begin, @@true, @@false, @@newline, @@end
+ end
+ end
+ end
+end

Иван обнови решението на 22.12.2013 23:28 (преди почти 11 години)

module Graphics
class Canvas
attr_reader :width, :height
def initialize(width, height)
@width = width.to_i
@height = height.to_i
@canvas = [[false] * width] * height
end
def set_pixel(x, y)
@canvas[x] = @canvas[x].each_with_index.map {|elem, ind| y == ind ? true : elem}
end
def pixel_at?(x, y)
@canvas[x][y]
end
def draw(shape)
shape.draw_on self
end
def to_s
@canvas.each {|x| puts x.join(' ')}
end
def render_as(render_engine)
render_engine.render @canvas
end
end
class Point
attr_reader :x, :y
+ def min_x(other)
+ return @x < other.x ? self : other
+ end
+
+ def max_y(other)
+ return @y < other.y ? self : other
+ end
+
def initialize(x, y)
@x = x.to_i
@y = y.to_i
end
def ==(other)
return (other.x == @x and other.y == @y)
end
def eql?(other)
return (self.inspect == other.inspect)
end
def draw_on(canvas)
canvas.set_pixel(@x, @y)
end
end
class Line
attr_reader :from, :to
def initialize(from, to)
@from = from.freeze
@to = to.freeze
end
def ==(other)
return [@from, @to] - [other.from, other.to] == []
end
def eql?(other)
return self.inspect == other.inspect
end
def draw_on(canvas)
end
def straight?
return (@from.x == @to.x or @from.y == @to.y)
end
end
class Rectangle
+ attr_reader :left, :right
def initialize(point_1, point_2)
- if point_2 == point_1 then
- @top_left = @top_right = @bottom_left = @bottom_right = point_1.freeze
- else
- end
+ @left = point_1.min_x point_2
+ @right = point_1.max_y point_2
+ end
+
+ def draw_on(canvas)
+ end
+
+ def eql?(other)
+ return self.inspect == other.inspect
+ end
+
+ def ==(other)
+ return [@left, @right] - [other.left, other.right] == 0
end
end
module Renderers
HEAD_HTML = %w(
<!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">
).freeze
END_HTML = %w(
</div>
</body>
</html>).freeze
class BasicRenderer
def BasicRenderer.render_row(row, yes, no)
row.map {|x| x ? yes : no}
end
def BasicRenderer.render(canvas, start='', yes='#', no='@', newline="\n", post='')
result = start.dup
canvas.each do |row|
result << render_row(row, yes, no).join('')
result << newline
end
result << post
result
end
end
class Html < BasicRenderer
@@begin, @@true, @@false, @@newline, @@end =
HEAD_HTML, "<b></b>", "<i></i>", "<br />", END_HTML
def Html.render(canvas)
BasicRenderer.render canvas, @@begin, @@true, @@false, @@newline, @@end
end
end
class Ascii < BasicRenderer
@@begin, @@true, @@false, @@newline, @@end = '', '@', '-', "\n", ''
def Ascii.render(canvas)
BasicRenderer.render canvas, @@begin, @@true, @@false, @@newline, @@end
end
end
end
end

Иван обнови решението на 22.12.2013 23:30 (преди почти 11 години)

module Graphics
class Canvas
attr_reader :width, :height
def initialize(width, height)
@width = width.to_i
@height = height.to_i
@canvas = [[false] * width] * height
end
def set_pixel(x, y)
@canvas[x] = @canvas[x].each_with_index.map {|elem, ind| y == ind ? true : elem}
end
def pixel_at?(x, y)
@canvas[x][y]
end
def draw(shape)
shape.draw_on self
end
def to_s
@canvas.each {|x| puts x.join(' ')}
end
def render_as(render_engine)
render_engine.render @canvas
end
end
class Point
attr_reader :x, :y
def min_x(other)
return @x < other.x ? self : other
end
- def max_y(other)
+ def min_y(other)
return @y < other.y ? self : other
+ end
+
+ def max_x(other)
+ return @x > other.x ? self : other
+ end
+
+ def max_y(other)
+ return @y > other.y ? self : other
end
def initialize(x, y)
@x = x.to_i
@y = y.to_i
end
def ==(other)
return (other.x == @x and other.y == @y)
end
def eql?(other)
return (self.inspect == other.inspect)
end
def draw_on(canvas)
canvas.set_pixel(@x, @y)
end
end
class Line
attr_reader :from, :to
def initialize(from, to)
@from = from.freeze
@to = to.freeze
end
def ==(other)
return [@from, @to] - [other.from, other.to] == []
end
def eql?(other)
return self.inspect == other.inspect
end
def draw_on(canvas)
end
def straight?
return (@from.x == @to.x or @from.y == @to.y)
end
end
class Rectangle
attr_reader :left, :right
def initialize(point_1, point_2)
@left = point_1.min_x point_2
@right = point_1.max_y point_2
end
def draw_on(canvas)
end
def eql?(other)
return self.inspect == other.inspect
end
def ==(other)
return [@left, @right] - [other.left, other.right] == 0
end
end
module Renderers
HEAD_HTML = %w(
<!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">
).freeze
END_HTML = %w(
</div>
</body>
</html>).freeze
class BasicRenderer
def BasicRenderer.render_row(row, yes, no)
row.map {|x| x ? yes : no}
end
def BasicRenderer.render(canvas, start='', yes='#', no='@', newline="\n", post='')
result = start.dup
canvas.each do |row|
result << render_row(row, yes, no).join('')
result << newline
end
result << post
result
end
end
class Html < BasicRenderer
@@begin, @@true, @@false, @@newline, @@end =
HEAD_HTML, "<b></b>", "<i></i>", "<br />", END_HTML
def Html.render(canvas)
BasicRenderer.render canvas, @@begin, @@true, @@false, @@newline, @@end
end
end
class Ascii < BasicRenderer
@@begin, @@true, @@false, @@newline, @@end = '', '@', '-', "\n", ''
def Ascii.render(canvas)
BasicRenderer.render canvas, @@begin, @@true, @@false, @@newline, @@end
end
end
end
end