File: test-scatterplot.rb

package info (click to toggle)
ruby-unicode-plot 0.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,492 kB
  • sloc: ruby: 2,871; makefile: 4
file content (154 lines) | stat: -rw-r--r-- 5,197 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
class ScatterplotTest < Test::Unit::TestCase
  include Helper::Fixture
  include Helper::WithTerm

  def setup
    @x = [-1, 1, 3, 3, -1]
    @y = [2, 0, -5, 2, -5]
  end

  test("errors") do
    assert_raise(ArgumentError) do
      UnicodePlot.scatterplot()
    end
    assert_raise(ArgumentError) do
      UnicodePlot.scatterplot([1, 2], [1, 2, 3])
    end
    assert_raise(ArgumentError) do
      UnicodePlot.scatterplot([1, 2, 3], [1, 2])
    end
    assert_raise(ArgumentError) do
      UnicodePlot.scatterplot(1..3, 1..2)
    end
  end

  sub_test_case("with invalid arguments") do
    test("unknown border type") do
      assert_raise(ArgumentError.new("unknown border type: invalid_border_name")) do
        UnicodePlot.scatterplot(@x, @y, border: :invalid_border_name)
      end
    end
  end

  test("default") do
    plot = UnicodePlot.scatterplot(@x, @y)
    _, output = with_term { plot.render($stdout, newline: false) }
    assert_equal(fixture_path("scatterplot/default.txt").read,
                 output)
  end

  test("y only") do
    plot = UnicodePlot.scatterplot(@y)
    _, output = with_term { plot.render($stdout, newline: false) }
    assert_equal(fixture_path("scatterplot/y_only.txt").read,
                 output)
  end

  test("one range") do
    plot = UnicodePlot.scatterplot(6..10)
    _, output = with_term { plot.render($stdout, newline: false) }
    assert_equal(fixture_path("scatterplot/range1.txt").read,
                 output)
  end

  test("two ranges") do
    plot = UnicodePlot.scatterplot(11..15, 6..10)
    _, output = with_term { plot.render($stdout, newline: false) }
    assert_equal(fixture_path("scatterplot/range2.txt").read,
                 output)
  end

  test("scale1") do
    x = @x.map {|a| a * 1e3 + 15 }
    y = @y.map {|a| a * 1e-3 - 15 }
    plot = UnicodePlot.scatterplot(x, y)
    _, output = with_term { plot.render($stdout, newline: false) }
    assert_equal(fixture_path("scatterplot/scale1.txt").read,
                 output)
  end

  test("scale2") do
    x = @x.map {|a| a * 1e-3 + 15 }
    y = @y.map {|a| a * 1e3 - 15 }
    plot = UnicodePlot.scatterplot(x, y)
    _, output = with_term { plot.render($stdout, newline: false) }
    assert_equal(fixture_path("scatterplot/scale2.txt").read,
                 output)
  end

  test("scale3") do
    miny = -1.2796649117521434e218
    maxy = -miny
    plot = UnicodePlot.scatterplot([1], [miny], xlim: [1, 1], ylim: [miny, maxy])
    _, output = with_term { plot.render($stdout, newline: false) }
    expected = fixture_path("scatterplot/scale3.txt").read
    assert_equal(expected, output)
  end

  test("limits") do
    plot = UnicodePlot.scatterplot(@x, @y, xlim: [-1.5, 3.5], ylim: [-5.5, 2.5])
    _, output = with_term { plot.render($stdout, newline: false) }
    assert_equal(fixture_path("scatterplot/limits.txt").read,
                 output)
  end

  test("nogrid") do
    plot = UnicodePlot.scatterplot(@x, @y, grid: false)
    _, output = with_term { plot.render($stdout, newline: false) }
    assert_equal(fixture_path("scatterplot/nogrid.txt").read,
                 output)
  end

  test("blue") do
    plot = UnicodePlot.scatterplot(@x, @y, color: :blue, name: "points1")
    _, output = with_term { plot.render($stdout, newline: false) }
    assert_equal(fixture_path("scatterplot/blue.txt").read,
                 output)
  end

  test("parameters") do
    plot = UnicodePlot.scatterplot(@x, @y,
                                   name: "points1",
                                   title: "Scatter",
                                   xlabel: "x",
                                   ylabel: "y")
    _, output = with_term { plot.render($stdout, newline: false) }
    expected = fixture_path("scatterplot/parameters1.txt").read
    assert_equal(expected, output)

    assert_same(plot,
                UnicodePlot.scatterplot!(plot,
                                         [0.5, 1, 1.5],
                                         name: "points2"))
    _, output = with_term { plot.render($stdout, newline: false) }
    assert_equal(fixture_path("scatterplot/parameters2.txt").read,
                 output)

    assert_same(plot,
                UnicodePlot.scatterplot!(plot,
                                         [-0.5, 0.5, 1.5],
                                         [0.5, 1, 1.5],
                                         name: "points3"))
    _, output = with_term { plot.render($stdout, newline: false) }
    assert_equal(fixture_path("scatterplot/parameters3.txt").read,
                 output)
    output = StringIO.open do |sio|
      plot.render(sio, newline: false)
      sio.close
      sio.string
    end
    assert_equal(fixture_path("scatterplot/nocolor.txt").read,
                 output)
  end

  test("canvas size") do
    plot = UnicodePlot.scatterplot(@x, @y,
                                   title: "Scatter",
                                   canvas: :dot,
                                   width: 10,
                                   height: 5)
    _, output = with_term { plot.render($stdout, newline: false) }
    expected = fixture_path("scatterplot/canvassize.txt").read
    assert_equal(expected, output)
  end
end