File: build_spec.rb

package info (click to toggle)
jruby 1.7.26-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,572 kB
  • sloc: ruby: 669,910; java: 253,056; xml: 35,152; ansic: 9,187; yacc: 7,267; cpp: 5,244; sh: 1,036; makefile: 345; jsp: 48; tcl: 40
file content (76 lines) | stat: -rw-r--r-- 2,277 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
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
require 'matrix'

ruby_version_is "1.9" do
  describe "Matrix.build" do

    it "returns a Matrix object of the given size" do
      m = Matrix.build(3, 4){1}
      m.should be_an_instance_of(Matrix)
      m.row_size.should == 3
      m.column_size.should == 4
    end

    it "builds the Matrix using the given block" do
      Matrix.build(2, 3){|col, row| 10*col - row}.should ==
        Matrix[[0, -1, -2], [10, 9, 8]]
    end

    it "iterates through the first row, then the second, ..." do
      acc = []
      Matrix.build(2, 3){|*args| acc << args}
      acc.should == [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2]]
    end

    it "returns an Enumerator is no block is given" do
      enum = Matrix.build(2, 1)
      enum.should  be_an_instance_of(enumerator_class)
      enum.each{1}.should == Matrix[[1], [1]]
    end

    it "requires integers as parameters" do
      lambda { Matrix.build("1", "2"){1} }.should raise_error(TypeError)
      lambda { Matrix.build(nil, nil){1} }.should raise_error(TypeError)
      lambda { Matrix.build(1..2){1} }.should raise_error(TypeError)
    end

    it "requires non-negative integers" do
      lambda { Matrix.build(-1, 1){1} }.should raise_error(ArgumentError)
      lambda { Matrix.build(+1,-1){1} }.should raise_error(ArgumentError)
    end

    it "returns empty Matrix if one argument is zero" do
      m = Matrix.build(0, 3){
        raise "Should not yield"
      }
      m.should be_empty
      m.column_size.should == 3

      m = Matrix.build(3, 0){
        raise "Should not yield"
      }
      m.should be_empty
      m.row_size.should == 3
    end

    it "tries to calls :to_int on arguments" do
      int = mock('int')
      int.should_receive(:to_int).twice.and_return(2)
      Matrix.build(int, int){ 1 }.should == Matrix[ [1,1], [1,1] ]
    end

    it "builds an nxn Matrix when given only one argument" do
      m = Matrix.build(3){1}
      m.row_size.should == 3
      m.column_size.should == 3
    end
  end

  describe "for a subclass of Matrix" do
    it "returns an instance of that subclass" do
      MatrixSub.build(3){1}.should be_an_instance_of(MatrixSub)
    end
  end
end