summaryrefslogtreecommitdiff
path: root/spec/ruby/shared/kernel/complex.rb
blob: 98ee0b2b3fbea7915c1d34f846b520d542809ce6 (plain)
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
# Specs shared by Kernel#Complex() and String#to_c()
describe :kernel_complex, shared: true do

  it "returns a Complex object" do
    @object.send(@method, '9').should be_an_instance_of(Complex)
  end

  it "understands integers" do
    @object.send(@method, '20').should == Complex(20)
  end

  it "understands negative integers" do
    @object.send(@method, '-3').should == Complex(-3)
  end

  it "understands fractions (numerator/denominator) for the real part" do
    @object.send(@method, '2/3').should == Complex(Rational(2, 3))
  end

  it "understands fractions (numerator/denominator) for the imaginary part" do
    @object.send(@method, '4+2/3i').should == Complex(4, Rational(2, 3))
  end

  it "understands negative fractions (-numerator/denominator) for the real part" do
    @object.send(@method, '-2/3').should == Complex(Rational(-2, 3))
  end

  it "understands negative fractions (-numerator/denominator) for the imaginary part" do
    @object.send(@method, '7-2/3i').should == Complex(7, Rational(-2, 3))
  end

  it "understands floats (a.b) for the real part" do
    @object.send(@method, '2.3').should == Complex(2.3)
  end

  it "understands floats (a.b) for the imaginary part" do
    @object.send(@method, '4+2.3i').should == Complex(4, 2.3)
  end

  it "understands negative floats (-a.b) for the real part" do
    @object.send(@method, '-2.33').should == Complex(-2.33)
  end

  it "understands negative floats (-a.b) for the imaginary part" do
    @object.send(@method, '7-28.771i').should == Complex(7, -28.771)
  end

  it "understands an integer followed by 'i' to mean that integer is the imaginary part" do
    @object.send(@method, '35i').should == Complex(0,35)
  end

  it "understands a negative integer followed by 'i' to mean that negative integer is the imaginary part" do
    @object.send(@method, '-29i').should == Complex(0,-29)
  end

  it "understands an 'i' by itself as denoting a complex number with an imaginary part of 1" do
    @object.send(@method, 'i').should == Complex(0,1)
  end

  it "understands a '-i' by itself as denoting a complex number with an imaginary part of -1" do
    @object.send(@method, '-i').should == Complex(0,-1)
  end

  it "understands 'a+bi' to mean a complex number with 'a' as the real part, 'b' as the imaginary" do
    @object.send(@method, '79+4i').should == Complex(79,4)
  end

  it "understands 'a-bi' to mean a complex number with 'a' as the real part, '-b' as the imaginary" do
    @object.send(@method, '79-4i').should == Complex(79,-4)
  end

  it "understands 'a+i' to mean a complex number with 'a' as the real part, 1i as the imaginary" do
    @object.send(@method, '79+i').should == Complex(79, 1)
  end

  it "understands 'a-i' to mean a complex number with 'a' as the real part, -1i as the imaginary" do
    @object.send(@method, '79-i').should == Complex(79, -1)
  end

  it "understands i, I, j, and J imaginary units" do
    @object.send(@method, '79+4i').should == Complex(79, 4)
    @object.send(@method, '79+4I').should == Complex(79, 4)
    @object.send(@method, '79+4j').should == Complex(79, 4)
    @object.send(@method, '79+4J').should == Complex(79, 4)
  end

  it "understands scientific notation for the real part" do
    @object.send(@method, '2e3+4i').should == Complex(2e3,4)
  end

  it "understands negative scientific notation for the real part" do
    @object.send(@method, '-2e3+4i').should == Complex(-2e3,4)
  end

  it "understands scientific notation for the imaginary part" do
    @object.send(@method, '4+2e3i').should == Complex(4, 2e3)
  end

  it "understands negative scientific notation for the imaginary part" do
    @object.send(@method, '4-2e3i').should == Complex(4, -2e3)
  end

  it "understands scientific notation for the real and imaginary part in the same String" do
    @object.send(@method, '2e3+2e4i').should == Complex(2e3,2e4)
  end

  it "understands negative scientific notation for the real and imaginary part in the same String" do
    @object.send(@method, '-2e3-2e4i').should == Complex(-2e3,-2e4)
  end

  it "understands scientific notation with e and E" do
    @object.send(@method, '2e3+2e4i').should == Complex(2e3, 2e4)
    @object.send(@method, '2E3+2E4i').should == Complex(2e3, 2e4)
  end

  it "understands 'm@a' to mean a complex number in polar form with 'm' as the modulus, 'a' as the argument" do
    @object.send(@method, '79@4').should == Complex.polar(79, 4)
    @object.send(@method, '-79@4').should == Complex.polar(-79, 4)
    @object.send(@method, '79@-4').should == Complex.polar(79, -4)
  end

  it "ignores leading whitespaces" do
    @object.send(@method, '  79+4i').should == Complex(79, 4)
  end

  it "ignores trailing whitespaces" do
    @object.send(@method, '79+4i  ').should == Complex(79, 4)
  end

  it "understands _" do
    @object.send(@method, '7_9+4_0i').should == Complex(79, 40)
  end
end