File: test-histogram.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 (126 lines) | stat: -rw-r--r-- 4,558 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
class HistogramTest < Test::Unit::TestCase
  include Helper::Fixture
  include Helper::WithTerm

  sub_test_case("UnicodePlot.histogram") do
    def setup
      @x = fixture_path("randn.txt").read.lines.map(&:to_f)
    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.histogram(@x, border: :invalid_border_name)
        end
      end
    end

    test("default") do
      plot = UnicodePlot.histogram(@x)
      _, output = with_term { plot.render($stdout) }
      assert_equal("\n", output[-1])
      assert_equal(fixture_path("histogram/default.txt").read,
                   output.chomp)
    end

    test("nocolor") do
      plot = UnicodePlot.histogram(@x)
      output = StringIO.open do |sio|
        plot.render(sio)
        sio.close
        sio.string
      end
      assert_equal("\n", output[-1])
      assert_equal(fixture_path("histogram/default_nocolor.txt").read,
                   output.chomp)
    end

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

    test("x 100") do
      x100 = @x.map {|a| a * 100 }
      plot = UnicodePlot.histogram(x100)
      _, output = with_term { plot.render($stdout, newline: false) }
      assert_equal(fixture_path("histogram/default_1e2.txt").read,
                   output)
    end

    test("x0.01") do
      x100 = @x.map {|a| a * 0.01 }
      plot = UnicodePlot.histogram(x100)
      _, output = with_term { plot.render($stdout, newline: false) }
      assert_equal(fixture_path("histogram/default_1e-2.txt").read,
                   output)
    end

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

    test("xscale: :log10 with custom label") do
      plot = UnicodePlot.histogram(@x, xscale: :log10, xlabel: "custom label")
      _, output = with_term { plot.render($stdout, newline: false) }
      assert_equal(fixture_path("histogram/log10_label.txt").read,
                   output)
    end

    test("nbins: 5, closed: :right") do
      plot = UnicodePlot.histogram(@x, nbins: 5, closed: :right)
      _, output = with_term { plot.render($stdout, newline: false) }
      assert_equal(fixture_path("histogram/hist_params.txt").read,
                   output)
    end

    test("with title, xlabel, color, margin, and padding") do
      plot = UnicodePlot.histogram(@x,
                                   title: "My Histogram",
                                   xlabel: "Absolute Frequency",
                                   color: :blue,
                                   margin: 7,
                                   padding: 3)
      _, output = with_term { plot.render($stdout, newline: false) }
      assert_equal(fixture_path("histogram/parameters1.txt").read,
                   output)
    end

    test("with title, xlabel, color, margin, padding, and labels: false") do
      plot = UnicodePlot.histogram(@x,
                                   title: "My Histogram",
                                   xlabel: "Absolute Frequency",
                                   color: :blue,
                                   margin: 7,
                                   padding: 3,
                                   labels: false)
      _, output = with_term { plot.render($stdout, newline: false) }
      assert_equal(fixture_path("histogram/parameters1_nolabels.txt").read,
                   output)
    end

    test("with title, xlabel, color, border, symbol, and width") do
      plot = UnicodePlot.histogram(@x,
                                   title: "My Histogram",
                                   xlabel: "Absolute Frequency",
                                   color: :yellow,
                                   border: :solid,
                                   symbol: "=",
                                   width: 50)
      _, output = with_term { plot.render($stdout, newline: false) }
      assert_equal(fixture_path("histogram/parameters2.txt").read,
                   output)
    end

    test("issue #24") do
      assert_nothing_raised do
        UnicodePlot.histogram([1, 2])
      end
    end
  end
end