Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: fc0b3b60de
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 186 lines (163 sloc) 4.198 kb
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
require File.expand_path(File.dirname(__FILE__))+'/helper'

class TestRglpk < Test::Unit::TestCase

  def test_create
    assert_instance_of Rglpk::Problem, Rglpk::Problem.new
  end

  def test_name
    p = Rglpk::Problem.new
    p.name = 'test'
    assert_equal 'test', p.name
  end

  def test_obj_fun_name
    p = Rglpk::Problem.new
    p.obj.name = 'test'
    assert_equal 'test', p.obj.name
  end

  def test_obj_fun_dir
    p = Rglpk::Problem.new
    p.obj.dir = Rglpk::GLP_MIN
    assert_equal Rglpk::GLP_MIN, p.obj.dir
    p.obj.dir = Rglpk::GLP_MAX
    assert_equal Rglpk::GLP_MAX, p.obj.dir
    assert_raise(ArgumentError){p.obj.dir = 3}
  end

  def test_add_rows
    p = Rglpk::Problem.new
    p.add_rows(2)
    assert_equal 2, p.rows.size
    p.add_rows(2)
    assert_equal 4, p.rows.size
  end

  def test_add_cols
    p = Rglpk::Problem.new
    p.add_cols(2)
    assert_equal 2, p.cols.size
    p.add_cols(2)
    assert_equal 4, p.cols.size
  end

  def test_set_row_name
    p = Rglpk::Problem.new
    p.add_rows(10)
    p.rows[1].name = 'test'
    assert_equal 'test', p.rows[1].name
    assert_nil p.rows[2].name
  end

  def test_set_col_name
    p = Rglpk::Problem.new
    p.add_cols(2)
    p.cols[0].name = 'test'
    assert_equal 'test', p.cols[0].name
    assert_nil p.cols[1].name
  end

  def test_set_row_bounds
    p = Rglpk::Problem.new
    p.add_rows(2)
    p.rows[1].set_bounds(Rglpk::GLP_FR, nil, nil)
    assert_equal [Rglpk::GLP_FR, nil, nil], p.rows[1].bounds
  end

  def test_set_col_bounds
    p = Rglpk::Problem.new
    p.add_cols(2)
    p.cols[1].set_bounds(Rglpk::GLP_FR, nil, nil)
    assert_equal [Rglpk::GLP_FR, nil, nil], p.cols[1].bounds
  end

  def test_obj_coef
    p = Rglpk::Problem.new
    p.add_cols(2)
    p.obj.set_coef(1, 2)
    assert_equal [2, 0], p.obj.coefs
    p.obj.coefs = [1, 2]
    assert_equal [1, 2], p.obj.coefs
  end

  def test_set_row
    p = Rglpk::Problem.new
    p.add_rows(2)
    assert_raise(RuntimeError){p.rows[1].set([1, 2])}
    p.add_cols(2)
    p.rows[1].set([1, 2])
    assert_equal [1, 2], p.rows[1].get
  end

  def test_set_col
    p = Rglpk::Problem.new
    p.add_cols(2)
    assert_raise(RuntimeError){p.cols[1].set([1, 2])}
    p.add_rows(2)
    p.cols[1].set([1, 2])
    assert_equal [1, 2], p.cols[1].get
  end

  def test_set_mat
    p = Rglpk::Problem.new
    p.add_cols(2)
    p.add_rows(2)
    p.set_matrix([1, 2, 3, 4])
    assert_equal [1, 2], p.rows[0].get
    assert_equal [3, 4], p.rows[1].get
    assert_equal [1, 3], p.cols[0].get
    assert_equal [2, 4], p.cols[1].get
  end

  def test_del_row
    p = Rglpk::Problem.new
    p.add_cols(2)
    p.add_rows(2)
    p.set_matrix([1, 2, 3, 4])
    assert_equal [1, 2], p.rows[0].get
    p.del_rows([1])
    assert_equal [3, 4], p.rows[0].get
    assert_equal [3], p.cols[0].get
  end

  def test_del_col
    p = Rglpk::Problem.new
    p.add_cols(2)
    p.add_rows(2)
    p.set_matrix([1, 2, 3, 4])
    assert_equal [1, 3], p.cols[0].get
    p.del_cols([1])
    assert_equal [2, 4], p.cols[0].get
    assert_equal [2], p.rows[0].get
  end

  def test_nz
    p = Rglpk::Problem.new
    p.add_cols(2)
    p.add_rows(2)
    p.set_matrix([1, 2, 3, 4])
    assert_equal 4, p.nz
  end

  def test_row_get_by_name
    p = Rglpk::Problem.new
    assert_raises(RuntimeError){ p.rows['test'] }
    p.add_cols(2)
    p.add_rows(2)
    p.set_matrix([1, 2, 3, 4])
    assert_raises(ArgumentError){ p.rows['test'] }
    p.rows[0].name = 'test'
    assert_equal [1, 2], p.rows['test'].get
  end

  def test_col_get_by_name
    p = Rglpk::Problem.new
    assert_raises(RuntimeError){ p.cols['test'] }
    p.add_cols(2)
    p.add_rows(2)
    p.set_matrix([1, 2, 3, 4])
    assert_raises(ArgumentError){ p.cols['test'] }
    p.cols[0].name = 'test'
    assert_equal [1, 3], p.cols['test'].get
  end

  def test_solve
    p = Rglpk::Problem.new
    assert_raises(RuntimeError){ p.cols['test'] }
    p.add_cols(2)
    p.add_rows(2)
    p.set_matrix([1, 2, 3, 4])
    p.simplex({:msg_lev => 1})
  end

  class D < Rglpk::Problem
    attr_accessor :species
    def initialize
      @species = []
      super
    end
  end

  def test_derived
    D.new.add_rows(10)
  end
end
Something went wrong with that request. Please try again.