OSDN Git Service

use auto-generated square numbers
[mint/mint-lib.git] / spec / generator / quadratic_equation_spec.rb
1 # -*- coding: utf-8 -*-
2
3 require File.dirname(__FILE__) + '/../spec_helper.rb'
4
5 module Mint::Generator
6
7   describe QuadraticEquation do
8
9     subject { QuadraticEquation.new }
10
11     context 'problems' do
12
13       before { @problem = subject.generate }
14
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 }
18     end
19
20     context 'generate expression' do
21
22       before do
23         type = :imaginary
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')
30       end
31
32       it { subject.__send__(:expression).should == 'x^2 + 2 * x + 3' }
33     end
34
35     context 'generate params' do
36
37       before do
38         @options = {
39           :r_min => 1,
40           :r_max => 10,
41           :p_min => -10,
42           :p_max => 10,
43           :q_max => 10,
44           :q_min => 0,
45         }
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) }
49       end
50
51       it { subject.__send__(:squares, 0, 10).should == [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100] }
52
53       [:rational, :irrational, :imaginary].each do |type|
54         it do
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]
59         end
60       end
61
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))}
64
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")) }
69     end
70
71     context 'generate coefficient' do
72
73       [
74         [[1, 1, 1], [1, -2, 0]],
75         [[2, 2, 2], [2, -4, 1]],
76         [[10, 10, 10], [10, -20, 9]],
77
78       ].each do |params, coefficients|
79         it { subject.__send__(:generate_coefficients, *params).should == coefficients }
80       end
81     end
82
83     context 'generate term' do
84
85       [
86         [0,  ''],
87         [1,  'x^2'],
88         [-1,  '-x^2'],
89         [2,  '2 * x^2'],
90         [-2,  '-2 * x^2'],
91         [10, '10 * x^2'],
92
93       ].each do |a, expr|
94         it { subject.__send__(:term1, a).should == expr }
95       end
96
97       [
98         [0,   ''],
99         [1,   ' + x'],
100         [-1,  ' - x'],
101         [2,   ' + 2 * x'],
102         [-2,  ' - 2 * x'],
103         [10,  ' + 10 * x'],
104         [-10, ' - 10 * x'],
105
106       ].each do |b, expr|
107         it { subject.__send__(:term2, b).should == expr }
108       end
109
110       [
111         [0,   ''],
112         [1,   ' + 1'],
113         [-1,  ' - 1'],
114         [2,   ' + 2'],
115         [-2,  ' - 2'],
116         [100, ' + 100'],
117
118       ].each do |c, expr|
119         it { subject.__send__(:term3, c).should == expr }
120       end
121     end
122
123     context 'rational' do
124
125       before do
126         options = { :answer_type => :rational }
127         @problem = subject.generate(options)
128       end
129
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/)
135       end
136     end
137
138     context 'irrational' do
139
140       before do
141         options = { :answer_type => :irrational }
142         @problem = subject.generate(options)
143       end
144
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/)
150       end
151     end
152
153     context 'imaginary' do
154
155       before do
156         options = { :answer_type => :imaginary }
157         @problem = subject.generate(options)
158       end
159
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/)
165       end
166     end
167   end
168 end
169