1 # -*- coding: utf-8 -*-
3 require File.dirname(__FILE__) + '/../spec_helper.rb'
7 describe QuadraticEquation do
9 subject { QuadraticEquation.new }
13 before { @problem = subject.generate }
15 it { @problem.should have(1).problem }
16 it { @problem.first.scan('x').should have(2).variables }
17 it { @problem.first.split(/[+-]/).should have(3).terms }
20 context 'generate expression' do
24 subject.__send__(:options=, {:answer_type => type})
25 subject.should_receive(:generate_quadeqn_params).with(type).and_return([3, 2, 1])
26 subject.should_receive(:generate_coefficients).with(3, 2, 1).and_return([1, 2, 3])
27 subject.should_receive(:term1).with(1).and_return('x^2')
28 subject.should_receive(:term2).with(2).and_return(' + 2 * x')
29 subject.should_receive(:term3).with(3).and_return(' + 3')
32 it { subject.__send__(:expression).should == 'x^2 + 2 * x + 3' }
35 context 'generate params' do
46 subject.__send__(:options=, @options)
47 @squares = subject.__send__(:squares, @options[:q_min], @options[:q_max])
48 @prime_numbers = (@options[:q_min]..@options[:q_max]).to_a.select {|i| Prime.prime?(i) }
51 it { subject.__send__(:squares, 0, 10).should == [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100] }
53 [:rational, :irrational, :imaginary].each do |type|
55 subject.should_receive(:generate_r).and_return(1)
56 subject.should_receive(:generate_p).and_return(1)
57 subject.should_receive(:"#{type}_q").and_return(1)
58 subject.__send__(:generate_quadeqn_params, type).should == [1, 1, 1]
62 it { (@options[:r_min]..@options[:r_max]).should include(subject.__send__(:generate_r)) }
63 it { (@options[:p_min]..@options[:p_max]).should include(subject.__send__(:generate_p))}
65 it { @squares.should include(subject.__send__(:"rational_q")) }
66 it { @squares.should_not include(subject.__send__(:"irrational_q")) }
67 it { @prime_numbers.should_not include(subject.__send__(:"irrational_q")) }
68 it { @prime_numbers.should_not include(subject.__send__(:"imaginary_q")) }
71 context 'generate coefficient' do
74 [[1, 1, 1], [1, -2, 0]],
75 [[2, 2, 2], [2, -4, 1]],
76 [[10, 10, 10], [10, -20, 9]],
78 ].each do |params, coefficients|
79 it { subject.__send__(:generate_coefficients, *params).should == coefficients }
83 context 'generate term' do
94 it { subject.__send__(:term1, a).should == expr }
107 it { subject.__send__(:term2, b).should == expr }
119 it { subject.__send__(:term3, c).should == expr }
123 context 'rational' do
126 options = { :answer_type => :rational }
127 @problem = subject.generate(options)
130 it 'correct answer' do
131 problem = Mint::Builder.build(@problem.first)
132 query = "solve(#{problem}, x);"
133 ans = Mint::Solver::Maxima.exec_maxima(query)
134 ans.should_not match(/sqrt/)
138 context 'irrational' do
141 options = { :answer_type => :irrational }
142 @problem = subject.generate(options)
145 it 'correct answer' do
146 problem = Mint::Builder.build(@problem.first)
147 query = "solve(#{problem}, x);"
148 ans = Mint::Solver::Maxima.exec_maxima(query)
149 ans.should match(/sqrt/)
153 context 'imaginary' do
156 options = { :answer_type => :imaginary }
157 @problem = subject.generate(options)
160 it 'correct answer' do
161 problem = Mint::Builder.build(@problem.first)
162 query = "solve(#{problem}, x);"
163 ans = Mint::Solver::Maxima.exec_maxima(query)
164 ans.should match(/%i/)