ruby-oci8 2.0.4-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/ChangeLog +1912 -0
  2. data/Makefile +96 -0
  3. data/NEWS +223 -0
  4. data/README +86 -0
  5. data/VERSION +1 -0
  6. data/dist-files +77 -0
  7. data/doc/api.en.html +527 -0
  8. data/doc/api.en.rd +554 -0
  9. data/doc/api.ja.html +525 -0
  10. data/doc/api.ja.rd +557 -0
  11. data/doc/manual.css +35 -0
  12. data/lib/.document +1 -0
  13. data/lib/dbd/OCI8.rb +591 -0
  14. data/lib/oci8.rb +82 -0
  15. data/lib/oci8.rb.in +82 -0
  16. data/lib/oci8/.document +5 -0
  17. data/lib/oci8/bindtype.rb +319 -0
  18. data/lib/oci8/compat.rb +113 -0
  19. data/lib/oci8/datetime.rb +619 -0
  20. data/lib/oci8/encoding-init.rb +40 -0
  21. data/lib/oci8/encoding.yml +537 -0
  22. data/lib/oci8/metadata.rb +2077 -0
  23. data/lib/oci8/object.rb +562 -0
  24. data/lib/oci8/oci8.rb +571 -0
  25. data/lib/oci8/oracle_version.rb +144 -0
  26. data/lib/oci8lib_18.so +0 -0
  27. data/lib/oci8lib_191.so +0 -0
  28. data/metaconfig +142 -0
  29. data/pre-distclean.rb +7 -0
  30. data/ruby-oci8.gemspec +63 -0
  31. data/setup.rb +1331 -0
  32. data/test/README +4 -0
  33. data/test/config.rb +109 -0
  34. data/test/test_all.rb +50 -0
  35. data/test/test_appinfo.rb +63 -0
  36. data/test/test_array_dml.rb +333 -0
  37. data/test/test_bind_raw.rb +46 -0
  38. data/test/test_bind_time.rb +178 -0
  39. data/test/test_break.rb +83 -0
  40. data/test/test_clob.rb +79 -0
  41. data/test/test_connstr.rb +81 -0
  42. data/test/test_datetime.rb +622 -0
  43. data/test/test_dbi.rb +366 -0
  44. data/test/test_dbi_clob.rb +53 -0
  45. data/test/test_encoding.rb +100 -0
  46. data/test/test_metadata.rb +257 -0
  47. data/test/test_oci8.rb +434 -0
  48. data/test/test_oracle_version.rb +70 -0
  49. data/test/test_oradate.rb +256 -0
  50. data/test/test_oranumber.rb +655 -0
  51. data/test/test_rowid.rb +33 -0
  52. metadata +108 -0
@@ -0,0 +1,70 @@
1
+ require 'oci8'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/config'
4
+
5
+ class TestOracleVersion < Test::Unit::TestCase
6
+
7
+ def test_init
8
+ oraver = OCI8::OracleVersion.new('8.1.6.2.3')
9
+ assert_equal(8, oraver.major)
10
+ assert_equal(1, oraver.minor)
11
+ assert_equal(6, oraver.update)
12
+ assert_equal(2, oraver.patch)
13
+ assert_equal(3, oraver.port_update)
14
+
15
+ oraver = OCI8::OracleVersion.new('8.1.6')
16
+ assert_equal(8, oraver.major)
17
+ assert_equal(1, oraver.minor)
18
+ assert_equal(6, oraver.update)
19
+ assert_equal(0, oraver.patch)
20
+ assert_equal(0, oraver.port_update)
21
+
22
+ oraver = OCI8::OracleVersion.new('10')
23
+ assert_equal(10, oraver.major)
24
+ assert_equal(0, oraver.minor)
25
+ assert_equal(0, oraver.update)
26
+ assert_equal(0, oraver.patch)
27
+ assert_equal(0, oraver.port_update)
28
+
29
+ oraver = OCI8::OracleVersion.new(0x08106203)
30
+ assert_equal(8, oraver.major)
31
+ assert_equal(1, oraver.minor)
32
+ assert_equal(6, oraver.update)
33
+ assert_equal(2, oraver.patch)
34
+ assert_equal(3, oraver.port_update)
35
+ end
36
+
37
+ def test_compare
38
+ oraver = OCI8::OracleVersion.new('8.1.6.2.3')
39
+ assert_operator(oraver, :==, OCI8::OracleVersion.new('8.1.6.2.3'))
40
+ assert_operator(oraver, :<, OCI8::OracleVersion.new('9.1.6.2.3'))
41
+ assert_operator(oraver, :<, OCI8::OracleVersion.new('8.2.6.2.3'))
42
+ assert_operator(oraver, :<, OCI8::OracleVersion.new('8.1.7.2.3'))
43
+ assert_operator(oraver, :<, OCI8::OracleVersion.new('8.1.6.3.3'))
44
+ assert_operator(oraver, :<, OCI8::OracleVersion.new('8.1.6.2.4'))
45
+ assert_operator(oraver, :>, OCI8::OracleVersion.new('7.1.6.2.3'))
46
+ assert_operator(oraver, :>, OCI8::OracleVersion.new('8.0.6.2.3'))
47
+ assert_operator(oraver, :>, OCI8::OracleVersion.new('8.1.5.2.3'))
48
+ assert_operator(oraver, :>, OCI8::OracleVersion.new('8.1.6.1.3'))
49
+ assert_operator(oraver, :>, OCI8::OracleVersion.new('8.1.6.2.2'))
50
+ end
51
+
52
+ def test_to_s
53
+ oraver = OCI8::OracleVersion.new('8.1.6.2.3')
54
+ assert_equal('8.1.6.2.3', oraver.to_s)
55
+ end
56
+
57
+ def test_to_i
58
+ oraver = OCI8::OracleVersion.new('8.1.6.2.3')
59
+ assert_equal(0x08106203, oraver.to_i)
60
+ end
61
+
62
+ def test_eql
63
+ oraver = OCI8::OracleVersion.new('8.1.6.2.3')
64
+ assert_equal(true, oraver.eql?(OCI8::OracleVersion.new('8.1.6.2.3')))
65
+ assert_equal(false, oraver.eql?(OCI8::OracleVersion.new('8.2.6.2.3')))
66
+ assert_equal(false, oraver.eql?('8.1.6.2.3'))
67
+ end
68
+ end
69
+
70
+ Test::Unit::AutoRunner.run() if $0 == __FILE__
@@ -0,0 +1,256 @@
1
+ # Low-level API
2
+ require 'oci8'
3
+ require 'test/unit'
4
+ require File.dirname(__FILE__) + '/config'
5
+
6
+ class TestOraDate < Test::Unit::TestCase
7
+
8
+ YEAR_CHECK_TARGET = [-4712, -1, 1, 1192, 1868, 2002, 9999]
9
+ MONTH_CHECK_TARGET = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
10
+ DAY_CHECK_TARGET = [1, 10, 20, 31] # days of January.
11
+ HOUR_CHECK_TARGET = [0, 6, 12, 18, 23]
12
+ MINUTE_CHECK_TARGET = [0, 15, 30, 45, 59]
13
+ SECOND_CHECK_TARGET = [0, 15, 30, 45, 59]
14
+
15
+ def setup
16
+ @conn = get_oci8_connection
17
+ end
18
+
19
+ def check_oradate(target, year, month, day, hour, minute, second)
20
+ assert_equal(year, target.year)
21
+ assert_equal(month, target.month)
22
+ assert_equal(day, target.day)
23
+ assert_equal(hour, target.hour)
24
+ assert_equal(minute, target.minute)
25
+ assert_equal(second, target.second)
26
+ end
27
+
28
+ def test_new()
29
+ check_oradate(OraDate.new(), 1, 1, 1, 0, 0, 0)
30
+ end
31
+
32
+ def test_set_year
33
+ cursor = @conn.parse("BEGIN :year := TO_NUMBER(TO_CHAR(:date, 'SYYYY'), '9999'); END;")
34
+ cursor.bind_param(:date, OraDate)
35
+ cursor.bind_param(:year, Fixnum)
36
+ date = OraDate.new()
37
+ YEAR_CHECK_TARGET.each do |i|
38
+ # set year
39
+ date.year = i
40
+ # check result via oracle.
41
+ cursor[:date] = date
42
+ cursor.exec
43
+ assert_equal(i, cursor[:year])
44
+ end
45
+ end
46
+
47
+ def test_get_year
48
+ cursor = @conn.parse("BEGIN :date := TO_DATE(TO_CHAR(:year, '0999'), 'SYYYY'); END;")
49
+ cursor.bind_param(:year, Fixnum)
50
+ cursor.bind_param(:date, OraDate)
51
+ YEAR_CHECK_TARGET.each do |i|
52
+ # set date via oracle.
53
+ cursor[:year] = i
54
+ cursor.exec
55
+ # check OraDate#year
56
+ assert_equal(i, cursor[:date].year)
57
+ end
58
+ end
59
+
60
+ def test_set_month
61
+ cursor = @conn.parse("BEGIN :month := TO_NUMBER(TO_CHAR(:date, 'MM'), '99'); END;")
62
+ cursor.bind_param(:date, OraDate)
63
+ cursor.bind_param(:month, Fixnum)
64
+ date = OraDate.new()
65
+ MONTH_CHECK_TARGET.each do |i|
66
+ # set month
67
+ date.month = i
68
+ # check result via oracle.
69
+ cursor[:date] = date
70
+ cursor.exec
71
+ assert_equal(i, cursor[:month])
72
+ end
73
+ end
74
+
75
+ def test_get_month
76
+ cursor = @conn.parse("BEGIN :date := TO_DATE(TO_CHAR(:month, '99'), 'MM'); END;")
77
+ cursor.bind_param(:month, Fixnum)
78
+ cursor.bind_param(:date, OraDate)
79
+ MONTH_CHECK_TARGET.each do |i|
80
+ # set date via oracle.
81
+ cursor[:month] = i
82
+ cursor.exec
83
+ # check OraDate#month
84
+ assert_equal(i, cursor[:date].month)
85
+ end
86
+ end
87
+
88
+ def test_set_day
89
+ cursor = @conn.parse("BEGIN :day := TO_NUMBER(TO_CHAR(:date, 'DD'), '99'); END;")
90
+ cursor.bind_param(:date, OraDate)
91
+ cursor.bind_param(:day, Fixnum)
92
+ date = OraDate.new()
93
+ DAY_CHECK_TARGET.each do |i|
94
+ # set day
95
+ date.day = i
96
+ # check result via oracle.
97
+ cursor[:date] = date
98
+ cursor.exec
99
+ assert_equal(i, cursor[:day])
100
+ end
101
+ end
102
+
103
+ def test_get_day
104
+ cursor = @conn.parse("BEGIN :date := TO_DATE('200101' || TO_CHAR(:day, 'FM00'), 'YYYYMMDD'); END;")
105
+ cursor.bind_param(:day, Fixnum)
106
+ cursor.bind_param(:date, OraDate)
107
+ DAY_CHECK_TARGET.each do |i|
108
+ # set date via oracle.
109
+ cursor[:day] = i
110
+ cursor.exec
111
+ # check OraDate#day
112
+ assert_equal(i, cursor[:date].day)
113
+ end
114
+ end
115
+
116
+ def test_set_hour
117
+ cursor = @conn.parse("BEGIN :hour := TO_NUMBER(TO_CHAR(:date, 'HH24'), '99'); END;")
118
+ cursor.bind_param(:date, OraDate)
119
+ cursor.bind_param(:hour, Fixnum)
120
+ date = OraDate.new()
121
+ HOUR_CHECK_TARGET.each do |i|
122
+ # set hour
123
+ date.hour = i
124
+ # check result via oracle.
125
+ cursor[:date] = date
126
+ cursor.exec
127
+ assert_equal(i, cursor[:hour])
128
+ end
129
+ end
130
+
131
+ def test_get_hour
132
+ cursor = @conn.parse("BEGIN :date := TO_DATE(TO_CHAR(:hour, '99'), 'HH24'); END;")
133
+ cursor.bind_param(:hour, Fixnum)
134
+ cursor.bind_param(:date, OraDate)
135
+ HOUR_CHECK_TARGET.each do |i|
136
+ # set date via oracle.
137
+ cursor[:hour] = i
138
+ cursor.exec
139
+ # check OraDate#hour
140
+ assert_equal(i, cursor[:date].hour)
141
+ end
142
+ end
143
+
144
+ def test_set_minute
145
+ cursor = @conn.parse("BEGIN :minute := TO_NUMBER(TO_CHAR(:date, 'MI'), '99'); END;")
146
+ cursor.bind_param(:date, OraDate)
147
+ cursor.bind_param(:minute, Fixnum)
148
+ date = OraDate.new()
149
+ MINUTE_CHECK_TARGET.each do |i|
150
+ # set minute
151
+ date.minute = i
152
+ # check result via oracle.
153
+ cursor[:date] = date
154
+ cursor.exec
155
+ assert_equal(i, cursor[:minute])
156
+ end
157
+ end
158
+
159
+ def test_get_minute
160
+ cursor = @conn.parse("BEGIN :date := TO_DATE(TO_CHAR(:minute, '99'), 'MI'); END;")
161
+ cursor.bind_param(:minute, Fixnum)
162
+ cursor.bind_param(:date, OraDate)
163
+ MINUTE_CHECK_TARGET.each do |i|
164
+ # set date via oracle.
165
+ cursor[:minute] = i
166
+ cursor.exec
167
+ # check OraDate#minute
168
+ assert_equal(i, cursor[:date].minute)
169
+ end
170
+ end
171
+
172
+ def test_set_second
173
+ cursor = @conn.parse("BEGIN :second := TO_NUMBER(TO_CHAR(:date, 'SS'), '99'); END;")
174
+ cursor.bind_param(:date, OraDate)
175
+ cursor.bind_param(:second, Fixnum)
176
+ date = OraDate.new()
177
+ SECOND_CHECK_TARGET.each do |i|
178
+ # set second
179
+ date.second = i
180
+ # check result via oracle.
181
+ cursor[:date] = date
182
+ cursor.exec
183
+ assert_equal(i, cursor[:second])
184
+ end
185
+ end
186
+
187
+ def test_get_second
188
+ cursor = @conn.parse("BEGIN :date := TO_DATE(TO_CHAR(:second, '99'), 'SS'); END;")
189
+ cursor.bind_param(:second, Fixnum)
190
+ cursor.bind_param(:date, OraDate)
191
+ SECOND_CHECK_TARGET.each do |i|
192
+ # set date via oracle.
193
+ cursor[:second] = i
194
+ cursor.exec
195
+ # check OraDate#second
196
+ assert_equal(i, cursor[:date].second)
197
+ end
198
+ end
199
+
200
+ def test_compare
201
+ d1 = OraDate.new(2003,03,15,18,55,35)
202
+ d2 = OraDate.new(2003,03,15,18,55,35)
203
+ assert_equal(d1, d2)
204
+ assert_operator(d1, :<, OraDate.new(2004,03,15,18,55,35))
205
+ assert_operator(d1, :<, OraDate.new(2003,04,15,18,55,35))
206
+ assert_operator(d1, :<, OraDate.new(2003,03,16,18,55,35))
207
+ assert_operator(d1, :<, OraDate.new(2003,03,15,19,55,35))
208
+ assert_operator(d1, :<, OraDate.new(2003,03,15,18,56,35))
209
+ assert_operator(d1, :<, OraDate.new(2003,03,15,18,55,36))
210
+
211
+ assert_operator(OraDate.new(2002,03,15,18,55,35), :<, d1)
212
+ assert_operator(OraDate.new(2003,02,15,18,55,35), :<, d1)
213
+ assert_operator(OraDate.new(2003,03,14,18,55,35), :<, d1)
214
+ assert_operator(OraDate.new(2003,03,15,17,55,35), :<, d1)
215
+ assert_operator(OraDate.new(2003,03,15,18,54,35), :<, d1)
216
+ assert_operator(OraDate.new(2003,03,15,18,55,34), :<, d1)
217
+ end
218
+
219
+ def test_to_time
220
+ year, month, day, hour, minute, second = [2003,03,15,18,55,35]
221
+ dt = OraDate.new(year, month, day, hour, minute, second)
222
+ tm = Time.local(year, month, day, hour, minute, second)
223
+ assert_equal(tm, dt.to_time)
224
+
225
+ # year, month, day, hour, minute, second = [1900,1,1,0,0,0]
226
+ # dt = OraDate.new(year, month, day, hour, minute, second)
227
+ # assert_exception(RangeError) { dt.to_time }
228
+ end
229
+
230
+ def test_to_date
231
+ [[2003,03,15], [1900,1,1], [-4712, 1, 1]].each do |year, month, day|
232
+ odt = OraDate.new(year, month, day)
233
+ rdt = Date.new(year, month, day)
234
+ assert_equal(rdt, odt.to_date)
235
+ end
236
+ end
237
+
238
+ def test_dup
239
+ [[2003,03,15], [1900,1,1], [-4712, 1, 1]].each do |year, month, day|
240
+ dt = OraDate.new(year, month, day)
241
+ assert_equal(dt, dt.dup)
242
+ assert_equal(dt, dt.clone)
243
+ end
244
+ end
245
+
246
+ def test_marshal
247
+ [[2003,03,15], [1900,1,1], [-4712, 1, 1]].each do |year, month, day|
248
+ dt = OraDate.new(year, month, day)
249
+ assert_equal(dt, Marshal.load(Marshal.dump(dt)))
250
+ end
251
+ end
252
+
253
+ def teardown
254
+ @conn.logoff
255
+ end
256
+ end
@@ -0,0 +1,655 @@
1
+ # Low-level API
2
+ require 'oci8'
3
+ require 'test/unit'
4
+ require File.dirname(__FILE__) + '/config'
5
+ require 'yaml'
6
+ require 'bigdecimal'
7
+ require 'rational'
8
+
9
+ class TestOraNumber < Test::Unit::TestCase
10
+
11
+ LARGE_RANGE_VALUES = [
12
+ "12345678901234567890123456789012345678",
13
+ "1234567890123456789012345678901234567",
14
+ "1234567890123456789012345678901234567.8",
15
+ "12.345678901234567890123456789012345678",
16
+ "1.2345678901234567890123456789012345678",
17
+ "1.234567890123456789012345678901234567",
18
+ "0.0000000000000000000000000000000000001",
19
+ "0.000000000000000000000000000000000001",
20
+ "0",
21
+ "2147483647", # max of 32 bit signed value
22
+ "2147483648", # max of 32 bit signed value + 1
23
+ "-2147483648", # min of 32 bit signed value
24
+ "-2147483649", # min of 32 bit signed value - 1
25
+ "9223372036854775807", # max of 64 bit signed value
26
+ "9223372036854775808", # max of 64 bit signed value + 1
27
+ "-9223372036854775808", # min of 64 bit signed value
28
+ "-9223372036854775809", # min of 64 bit signed value - 1
29
+ "-12345678901234567890123456789012345678",
30
+ "-1234567890123456789012345678901234567",
31
+ "-123456789012345678901234567890123456",
32
+ "-1234567890123456789012345678901234567.8",
33
+ "-12.345678901234567890123456789012345678",
34
+ "-1.2345678901234567890123456789012345678",
35
+ "-0.0000000000000000000000000000000000001",
36
+ "-0.000000000000000000000000000000000001",
37
+ "-0.00000000000000000000000000000000001",
38
+ "1",
39
+ "20",
40
+ "300",
41
+ "-1",
42
+ "-20",
43
+ "-300",
44
+ "1.123",
45
+ "12.123",
46
+ "123.123",
47
+ "1.1",
48
+ "1.12",
49
+ "1.123",
50
+ "-1.123",
51
+ "-12.123",
52
+ "-123.123",
53
+ "-1.1",
54
+ "-1.12",
55
+ "-1.123",
56
+ ]
57
+
58
+ SMALL_RANGE_VALUES = [
59
+ "10",
60
+ "3",
61
+ "3.14159265358979323846", # PI
62
+ "2",
63
+ "1.57079632679489661923", # PI/2
64
+ "0.5",
65
+ "0.0000000001",
66
+ "0",
67
+ "-0.0000000001",
68
+ "-0.5",
69
+ "-1.57079632679489661923", # -PI/2
70
+ "-2",
71
+ "-3.14159265358979323846", # -PI
72
+ "-3",
73
+ "-10",
74
+ ]
75
+
76
+ def compare_with_float(values, rettype, proc1, proc2 = nil)
77
+ proc2 = proc1 if proc2.nil?
78
+ values.each do |x|
79
+ expected_val = proc1.call(x.to_f)
80
+ actual_val = proc2.call(OraNumber.new(x))
81
+ assert_kind_of(rettype, actual_val)
82
+ delta = [expected_val.abs * 1.0e-12, 1.0e-14].max
83
+ assert_in_delta(expected_val, actual_val, delta, x)
84
+ end
85
+ end
86
+
87
+ def compare_with_float2(values, proc_args, proc1, proc2 = nil)
88
+ proc2 = proc1 if proc2.nil?
89
+ values.each do |x|
90
+ proc_args.each do |y|
91
+ expected_val = proc1.call(x.to_f, y)
92
+ actual_val = proc2.call(OraNumber.new(x), y)
93
+ begin
94
+ delta = [expected_val.abs * 1.0e-12, 1.0e-14].max
95
+ rescue
96
+ puts '-----------'
97
+ p x
98
+ p y
99
+ p expected_val
100
+ puts '-----------'
101
+ raise $!
102
+ end
103
+ # explicity convert actual_val to a Float to prevent
104
+ # SEGV in OCINumberSub() if the Oracle client vesion
105
+ # is less than 10.2.0.4.
106
+ if defined? ::MiniTest and OCI8.oracle_client_version < OCI8::OracleVersion.new('10.2.0.4')
107
+ actual_val = actual_val.to_f
108
+ end
109
+ assert_in_delta(expected_val, actual_val, delta, x)
110
+ end
111
+ end
112
+ end
113
+
114
+ def test_in_bind
115
+ conn = get_oci8_connection
116
+ begin
117
+ conn.exec("alter session set nls_numeric_characters = '.,'")
118
+ cursor = conn.parse("BEGIN :out := TO_CHAR(:in); END;")
119
+ cursor.bind_param(:out, nil, String, 40)
120
+ cursor.bind_param(:in, OraNumber)
121
+ LARGE_RANGE_VALUES.each do |val|
122
+ cursor[:in] = OraNumber.new(val)
123
+ cursor.exec
124
+ # convert 0.0001 and -0.0001 to .0001 and -.0001 respectively
125
+ val = $1+'.'+$2 if /(-?)0\.(.*)/ =~ val
126
+ assert_equal(val, cursor[:out])
127
+ end
128
+ ensure
129
+ conn.logoff
130
+ end
131
+ end
132
+
133
+ def test_out_bind
134
+ conn = get_oci8_connection
135
+ begin
136
+ conn.exec("alter session set nls_numeric_characters = '.,'")
137
+ cursor = conn.parse("BEGIN :out := TO_NUMBER(:in); END;")
138
+ cursor.bind_param(:out, OraNumber)
139
+ cursor.bind_param(:in, nil, String, 40)
140
+ LARGE_RANGE_VALUES.each do |val|
141
+ cursor[:in] = val
142
+ cursor.exec
143
+ assert_equal(OraNumber.new(val), cursor[:out])
144
+ end
145
+ ensure
146
+ conn.logoff
147
+ end
148
+ end
149
+
150
+ def test_dup
151
+ LARGE_RANGE_VALUES.each do |x|
152
+ n = OraNumber.new(x)
153
+ assert_equal(n, n.dup)
154
+ assert_equal(n, n.clone)
155
+ end
156
+ end
157
+
158
+ def test_marshal
159
+ LARGE_RANGE_VALUES.each do |x|
160
+ n = OraNumber.new(x)
161
+ assert_equal(n, Marshal.load(Marshal.dump(n)))
162
+ end
163
+ end
164
+
165
+ def test_yaml
166
+ LARGE_RANGE_VALUES.each do |x|
167
+ n = OraNumber.new(x)
168
+ assert_equal(n, YAML.load(YAML.dump(n)))
169
+ end
170
+ end
171
+
172
+ # OCI8::Math.acos(x) -> ocinumber
173
+ def test_math_acos
174
+ test_values = []
175
+ -1.0.step(1.0, 0.01) do |n|
176
+ test_values << n
177
+ end
178
+ compare_with_float(test_values, OraNumber,
179
+ Proc.new {|n| Math::acos(n)},
180
+ Proc.new {|n| OCI8::Math::acos(n)})
181
+ end
182
+
183
+ # OCI8::Math.asin(x) -> ocinumber
184
+ def test_math_asin
185
+ test_values = []
186
+ -1.0.step(1.0, 0.01) do |n|
187
+ test_values << n
188
+ end
189
+ compare_with_float(test_values, OraNumber,
190
+ Proc.new {|n| Math::asin(n)},
191
+ Proc.new {|n| OCI8::Math::asin(n)})
192
+ end
193
+
194
+ # OCI8::Math.atan(x) -> ocinumber
195
+ def test_math_atan
196
+ compare_with_float(SMALL_RANGE_VALUES, OraNumber,
197
+ Proc.new {|n| Math::atan(n)},
198
+ Proc.new {|n| OCI8::Math::atan(n)})
199
+ end
200
+
201
+ # OCI8::Math.atan2(y, x) -> ocinumber
202
+ def test_math_atan2
203
+ compare_with_float2(SMALL_RANGE_VALUES, SMALL_RANGE_VALUES,
204
+ Proc.new {|x, y| Math::atan2(x, y.to_f)},
205
+ Proc.new {|x, y| OCI8::Math::atan2(x, y.to_f)})
206
+ compare_with_float2(SMALL_RANGE_VALUES, SMALL_RANGE_VALUES,
207
+ Proc.new {|x, y| Math::atan2(y.to_f, x)},
208
+ Proc.new {|x, y| OCI8::Math::atan2(y.to_f, x)})
209
+ end
210
+
211
+ # OCI8::Math.cos(x) -> ocinumber
212
+ def test_math_cos
213
+ compare_with_float(SMALL_RANGE_VALUES, OraNumber,
214
+ Proc.new {|n| Math::cos(n)},
215
+ Proc.new {|n| OCI8::Math::cos(n)})
216
+ end
217
+
218
+ # OCI8::Math.cosh(x) -> ocinumber
219
+ def test_math_cosh
220
+ compare_with_float(SMALL_RANGE_VALUES, OraNumber,
221
+ Proc.new {|n| Math::cosh(n)},
222
+ Proc.new {|n| OCI8::Math::cosh(n)})
223
+ end
224
+
225
+ # OCI8::Math.exp(x) -> ocinumber
226
+ def test_exp
227
+ compare_with_float(SMALL_RANGE_VALUES, OraNumber,
228
+ Proc.new {|n| Math::exp(n)},
229
+ Proc.new {|n| OCI8::Math::exp(n)})
230
+ end
231
+
232
+ # OCI8::Math.log(numeric) -> ocinumber
233
+ # OCI8::Math.log(numeric, base_num) -> ocinumber
234
+ def test_log
235
+ test_values = LARGE_RANGE_VALUES.reject do |x|
236
+ # reject minus and zero values
237
+ x[0,1] == '-' || x == '0'
238
+ end
239
+ compare_with_float(test_values, OraNumber,
240
+ Proc.new {|n| Math::log(n)},
241
+ Proc.new {|n| OCI8::Math::log(n)})
242
+ compare_with_float(test_values, OraNumber,
243
+ Proc.new {|n| Math::log(n)/Math::log(3)},
244
+ Proc.new {|n| OCI8::Math::log(n, 3)})
245
+ end
246
+
247
+ # OCI8::Math.log10(numeric) -> ocinumber
248
+ def test_log10
249
+ test_values = LARGE_RANGE_VALUES.reject do |x|
250
+ # reject minus and zero values
251
+ x[0,1] == '-' || x == '0'
252
+ end
253
+ compare_with_float(test_values, OraNumber,
254
+ Proc.new {|n| Math::log10(n)},
255
+ Proc.new {|n| OCI8::Math::log10(n)})
256
+ end
257
+
258
+ # OCI8::Math.sin(x) -> ocinumber
259
+ def test_math_sin
260
+ compare_with_float(SMALL_RANGE_VALUES, OraNumber,
261
+ Proc.new {|n| Math::sin(n)},
262
+ Proc.new {|n| OCI8::Math::sin(n)})
263
+ end
264
+
265
+ # OCI8::Math.sinh(x) -> ocinumber
266
+ def test_math_sinh
267
+ compare_with_float(SMALL_RANGE_VALUES, OraNumber,
268
+ Proc.new {|n| Math::sinh(n)},
269
+ Proc.new {|n| OCI8::Math::sinh(n)})
270
+ end
271
+
272
+ # OCI8::Math.sqrt(numeric) -> ocinumber
273
+ def test_sqrt
274
+ test_values = LARGE_RANGE_VALUES.reject do |x|
275
+ # reject minus values
276
+ x[0,1] == '-'
277
+ end
278
+ compare_with_float(test_values, OraNumber,
279
+ Proc.new {|n| Math::sqrt(n)},
280
+ Proc.new {|n| OCI8::Math::sqrt(n)})
281
+ end
282
+
283
+ # OCI8::Math.tan(x) -> ocinumber
284
+ def test_math_tan
285
+ test_values = SMALL_RANGE_VALUES.reject do |x|
286
+ # reject PI/2 and -PI/2.
287
+ # Those values are +inf and -info
288
+ radian = x.to_f
289
+ (radian.abs - Math::PI/2).abs < 0.000001
290
+ end
291
+ compare_with_float(test_values, OraNumber,
292
+ Proc.new {|n| Math::tan(n)},
293
+ Proc.new {|n| OCI8::Math::tan(n)})
294
+ end
295
+
296
+ # OCI8::Math.tanh() -> ocinumber
297
+ def test_math_tanh
298
+ compare_with_float(SMALL_RANGE_VALUES, OraNumber,
299
+ Proc.new {|n| Math::tanh(n)},
300
+ Proc.new {|n| OCI8::Math::tanh(n)})
301
+ end
302
+
303
+ # onum % other -> onum
304
+ # def test_mod
305
+
306
+ # onum * other -> onum
307
+ def test_mul
308
+ compare_with_float2(SMALL_RANGE_VALUES, SMALL_RANGE_VALUES,
309
+ Proc.new {|x, y| x * y.to_f})
310
+ compare_with_float2(SMALL_RANGE_VALUES, SMALL_RANGE_VALUES,
311
+ Proc.new {|x, y| y.to_f * x})
312
+ end
313
+
314
+ # onum ** other -> onum
315
+ def test_pow
316
+ base_values = SMALL_RANGE_VALUES.reject do |x|
317
+ # reject minus and zero values
318
+ x[0,1] == '-' || x == '0'
319
+ end
320
+ compare_with_float2(base_values, SMALL_RANGE_VALUES,
321
+ Proc.new {|x, y| x ** y.to_f})
322
+ compare_with_float2(SMALL_RANGE_VALUES, base_values,
323
+ Proc.new {|x, y| y.to_f ** x})
324
+ end
325
+
326
+ # onum + other -> onum
327
+ def test_add
328
+ compare_with_float2(SMALL_RANGE_VALUES, SMALL_RANGE_VALUES,
329
+ Proc.new {|x, y| x + y.to_f})
330
+ compare_with_float2(SMALL_RANGE_VALUES, SMALL_RANGE_VALUES,
331
+ Proc.new {|x, y| y.to_f + x})
332
+ end
333
+
334
+ # onum - other -> onum
335
+ def test_minus
336
+ compare_with_float2(SMALL_RANGE_VALUES, SMALL_RANGE_VALUES,
337
+ Proc.new {|x, y| x - y.to_f})
338
+ compare_with_float2(SMALL_RANGE_VALUES, SMALL_RANGE_VALUES,
339
+ Proc.new {|x, y| y.to_f - x})
340
+ end
341
+
342
+ # -ocinumber -> ocinumber
343
+ def test_uminus
344
+ compare_with_float(LARGE_RANGE_VALUES, OraNumber, Proc.new {|n| -n})
345
+ end
346
+
347
+ # onum / other -> onum
348
+ # TODO: test_div
349
+
350
+ # onum <=> other -> -1, 0, +1
351
+ def test_cmp
352
+ assert_equal(-1, 1 <=> OraNumber(2))
353
+ assert_equal(-1, 1.0 <=> OraNumber(2))
354
+ assert_equal(-1, BigDecimal("1") <=> OraNumber(2))
355
+ assert_equal(-1, Rational(1) <=> OraNumber(2))
356
+ assert_equal(0, 2 <=> OraNumber(2))
357
+ assert_equal(0, 2.0 <=> OraNumber(2))
358
+ assert_equal(0, BigDecimal("2") <=> OraNumber(2))
359
+ assert_equal(0, Rational(2) <=> OraNumber(2))
360
+ assert_equal(1, 3 <=> OraNumber(2))
361
+ assert_equal(1, 3.0 <=> OraNumber(2))
362
+ assert_equal(1, BigDecimal("3") <=> OraNumber(2))
363
+ assert_equal(1, Rational(3) <=> OraNumber(2))
364
+ end
365
+
366
+ # onum.abs -> ocinumber
367
+ def test_abs
368
+ compare_with_float(LARGE_RANGE_VALUES, OraNumber, Proc.new {|n| n.abs})
369
+ end
370
+
371
+ # onum.ceil -> integer
372
+ def test_ceil
373
+ compare_with_float(LARGE_RANGE_VALUES, Integer, Proc.new {|n| n.ceil})
374
+ end
375
+
376
+ # onum.floor -> integer
377
+ def test_floor
378
+ compare_with_float(LARGE_RANGE_VALUES, Integer, Proc.new {|n| n.floor})
379
+ end
380
+
381
+ # onum.round -> integer
382
+ # onum.round(decplace) -> onum
383
+ def test_round
384
+ compare_with_float(LARGE_RANGE_VALUES, Integer, Proc.new {|n| n.round})
385
+ compare_with_float(LARGE_RANGE_VALUES, OraNumber,
386
+ Proc.new {|n| (n * 10).round * 0.1},
387
+ Proc.new {|n| n.round(1)})
388
+ compare_with_float(LARGE_RANGE_VALUES, OraNumber,
389
+ Proc.new {|n| (n * 100).round * 0.01},
390
+ Proc.new {|n| n.round(2)})
391
+ compare_with_float(LARGE_RANGE_VALUES, OraNumber,
392
+ Proc.new {|n| (n * 0.1).round * 10},
393
+ Proc.new {|n| n.round(-1)})
394
+ end
395
+
396
+ # onum.round_prec(digits) -> ocinumber
397
+ def test_round_prec
398
+ if OCI8::oracle_client_version >= OCI8::ORAVER_8_1
399
+ # Oracle 8.1 client or upper
400
+ compare_with_float2(LARGE_RANGE_VALUES, [1, 2, 3, 5, 10, 20],
401
+ Proc.new {|x, y|
402
+ return 0.0 if x == 0.0
403
+ factor = 10 ** (Math::log10(x.abs).to_i - y + 1)
404
+ (x / factor).round * factor
405
+ },
406
+ Proc.new {|x, y| x.round_prec(y)})
407
+ else
408
+ # Oracle 8.0 client
409
+ assert_raise NoMethodError do
410
+ OraNumber.new(1).round_prec(1)
411
+ end
412
+ end
413
+ end
414
+
415
+ # onum.shift(fixnum) -> ocinumber
416
+ def test_shift
417
+ if OCI8::oracle_client_version >= OCI8::ORAVER_8_1
418
+ # Oracle 8.1 client or upper
419
+ compare_with_float2(LARGE_RANGE_VALUES, [-5, -4, -3, -1, 0, 1, 2, 3, 4, 5],
420
+ Proc.new {|x, y| x * (10 ** y)},
421
+ Proc.new {|x, y| x.shift(y)})
422
+ else
423
+ # Oracle 8.0 client
424
+ assert_raise NoMethodError do
425
+ OraNumber.new(1).shift(1)
426
+ end
427
+ end
428
+ end
429
+
430
+ # onum.to_char(fmt = nil, nls_params = nil) -> string
431
+ def test_to_char
432
+ onum = OraNumber.new(123.45)
433
+ assert_equal(' 123.4500', onum.to_char('99999.9999'))
434
+ assert_equal(' 0123.4500', onum.to_char('90000.0009'))
435
+ assert_equal(' 00123.4500', onum.to_char('00000.0000'))
436
+ assert_equal('123.45', onum.to_char('FM99999.9999'))
437
+ assert_equal('0123.450', onum.to_char('FM90000.0009'))
438
+ assert_equal('00123.4500', onum.to_char('FM00000.0000'))
439
+ assert_equal(' -123.4500',(-onum).to_char('99999.9999'))
440
+ assert_equal(' -0123.4500',(-onum).to_char('90000.0009'))
441
+ assert_equal('-00123.4500',(-onum).to_char('00000.0000'))
442
+ assert_equal('-123.45', (-onum).to_char('FM99999.9999'))
443
+ assert_equal('-0123.450', (-onum).to_char('FM90000.0009'))
444
+ assert_equal('-00123.4500',(-onum).to_char('FM00000.0000'))
445
+ assert_equal(' 0,123.4500', onum.to_char('0G000D0000', "NLS_NUMERIC_CHARACTERS = '.,'"))
446
+ assert_equal(' 0.123,4500', onum.to_char('0G000D0000', "NLS_NUMERIC_CHARACTERS = ',.'"))
447
+ assert_equal('Ducat123.45', onum.to_char('FML9999.999', "NLS_CURRENCY = 'Ducat'"))
448
+ end
449
+
450
+ # onum.to_f -> float
451
+ def test_to_f
452
+ LARGE_RANGE_VALUES.each do |x|
453
+ expected_val = x.to_f
454
+ actual_val = OraNumber.new(x).to_f
455
+ delta = [expected_val.abs * 1.0e-12, 1.0e-14].max
456
+ assert_in_delta(expected_val, actual_val, delta, x)
457
+ end
458
+ end
459
+
460
+ # onum.to_i -> integer
461
+ def test_to_i
462
+ LARGE_RANGE_VALUES.each do |x|
463
+ expected_val = x.to_i
464
+ actual_val = OraNumber.new(x).to_i
465
+ assert_equal(expected_val, actual_val, x)
466
+ end
467
+ end
468
+
469
+ # onum.to_s -> string
470
+ def test_to_s
471
+ LARGE_RANGE_VALUES.each do |x|
472
+ expected_val = x
473
+ actual_val = OraNumber.new(x).to_s
474
+ assert_equal(expected_val, actual_val, x)
475
+ end
476
+
477
+ conn = get_oci8_connection()
478
+ begin
479
+ cursor = conn.parse('select to_number(:1) from dual')
480
+ cursor.define(1, OraNumber)
481
+ cursor.bind_param(1, nil, String, 200)
482
+ [
483
+ "100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", # 1E125
484
+ "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", # 1E124
485
+ "234567890234567890234567890234567890000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
486
+ "23456789023456789023456789023456789000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
487
+ "2345678902345678902345678902345678900",
488
+ "234567890234567890234567890234567890",
489
+ "23456789023456789023456789023456789",
490
+ "2345678902345678902345678902345678.9",
491
+ "234567890234567890234567890234567.89",
492
+ "23.456789023456789023456789023456789",
493
+ "2.3456789023456789023456789023456789",
494
+ "0.23456789023456789023456789023456789", # 2.34..snip..E-1
495
+ "0.023456789023456789023456789023456789", # 2.34..snip..E-2
496
+ "0.0023456789023456789023456789023456789", # 2.34..snip..E-3
497
+ "0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000023456789023456789023456789023456789", # 2.34..snip..E-130
498
+ "0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", # 1E-129
499
+ "0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", # 1E-130
500
+ ].each do |str|
501
+ cursor[1] = str
502
+ cursor.exec
503
+ onum = cursor.fetch[0]
504
+ assert_equal(str, onum.to_s, "test data: " + str)
505
+
506
+ str = '-' + str
507
+ cursor[1] = str
508
+ cursor.exec
509
+ onum = cursor.fetch[0]
510
+ assert_equal(str, onum.to_s, "test data: " + str)
511
+ end
512
+ ensure
513
+ conn.logoff
514
+ end
515
+ end
516
+
517
+ # onum.truncate -> integer
518
+ # onum.truncate(decplace) -> ocinumber
519
+ # TODO: test_truncate
520
+
521
+ # onum.zero? -> true or false
522
+ def test_zero_p
523
+ LARGE_RANGE_VALUES.each do |x|
524
+ expected_val = x.to_f.zero?
525
+ actual_val = OraNumber.new(x).zero?
526
+ assert_equal(expected_val, actual_val, x)
527
+ end
528
+ end
529
+
530
+ def test_new_from_string
531
+ conn = get_oci8_connection()
532
+ begin
533
+ cursor = conn.parse('select to_number(:1) from dual')
534
+ cursor.define(1, OraNumber)
535
+ cursor.bind_param(1, nil, String, 200)
536
+ [
537
+ "100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", # 1E125
538
+ "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", # 1E124
539
+ "234567890234567890234567890234567890000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
540
+ "23456789023456789023456789023456789000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
541
+ "2345678902345678902345678902345678900",
542
+ "234567890234567890234567890234567890",
543
+ "23456789023456789023456789023456789",
544
+ "2345678902345678902345678902345678.9",
545
+ "234567890234567890234567890234567.89",
546
+ "23.456789023456789023456789023456789",
547
+ "2.3456789023456789023456789023456789",
548
+ "0.23456789023456789023456789023456789", # 2.34..snip..E-1
549
+ "0.023456789023456789023456789023456789", # 2.34..snip..E-2
550
+ "0.0023456789023456789023456789023456789", # 2.34..snip..E-3
551
+ "0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000023456789023456789023456789023456789", # 2.34..snip..E-130
552
+ "0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", # 1E-129
553
+ "0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", # 1E-130
554
+
555
+ # leading spaces
556
+ " 123",
557
+ # trailing spaces
558
+ "123 ",
559
+ "123.456 ",
560
+ "123E10 ",
561
+ "123.456E10 ",
562
+ # scientific notation
563
+ "1234567890000e-9",
564
+ "123456789000e-8",
565
+ "123456789e-5",
566
+ "12345.6789e-1",
567
+ "12.3456789e+1",
568
+ "0.0000123456789E7",
569
+ # round to zero
570
+ "1E-131",
571
+ # overflow
572
+ "1E126",
573
+ "10E125",
574
+ "100000000000E115",
575
+ "1E+126",
576
+ # invalid number
577
+ "",
578
+ " ",
579
+ "abc",
580
+ "1E10a",
581
+ "11E10a",
582
+ "1.1.1",
583
+ # round down
584
+ "444444444444444444444444444444444444444444444444440000",
585
+ "44444444444444444444444444444444444444444444444444000",
586
+ "44444444444444444444444444.444444444444444444444444",
587
+ "4444444444444444444444444.4444444444444444444444444",
588
+ "0.000000044444444444444444444444444444444444444444444444444",
589
+ "0.00000044444444444444444444444444444444444444444444444444",
590
+ # round up
591
+ "555555555555555555555555555555555555555555555555550000",
592
+ "55555555555555555555555555555555555555555555555555000",
593
+ "55555555555555555555555555.555555555555555555555555",
594
+ "5555555555555555555555555.5555555555555555555555555",
595
+ "0.000000055555555555555555555555555555555555555555555555555",
596
+ "0.00000055555555555555555555555555555555555555555555555555",
597
+ "999999999999999999999999999999999999999999999999990000",
598
+ "99999999999999999999999999999999999999999999999999000",
599
+ "99999999999999999999999999.999999999999999999999999",
600
+ "9999999999999999999999999.9999999999999999999999999",
601
+ "0.000000099999999999999999999999999999999999999999999999999",
602
+ "0.00000099999999999999999999999999999999999999999999999999",
603
+ # overflow by round up
604
+ "999999999999999999999999999999999999999999999999999999999900000000000000000000000000000000000000000000000000000000000000000000",
605
+ ].each do |str|
606
+ run_test_new_from_string(cursor, str)
607
+ run_test_new_from_string(cursor, '-' + str)
608
+ end
609
+ ensure
610
+ conn.logoff
611
+ end
612
+ end
613
+
614
+ def run_test_new_from_string(cursor, str)
615
+ cursor[1] = str
616
+ onum = nil
617
+ begin
618
+ cursor.exec
619
+ onum = cursor.fetch[0]
620
+ rescue OCIError => oraerr
621
+ begin
622
+ OraNumber.new(str)
623
+ flunk("exception expected but none was thrown. test data: " + str)
624
+ rescue
625
+ assert_equal(oraerr.to_s, $!.to_s, "test data: " + str)
626
+ end
627
+ end
628
+ if onum
629
+ assert_equal(onum.dump, OraNumber.new(str).dump, "test data: " + str)
630
+ end
631
+ end
632
+
633
+ def test_new_from_bigdecimal
634
+ ["+Infinity", "-Infinity", "NaN"].each do |n|
635
+ assert_raise TypeError do
636
+ OraNumber.new(BigDecimal.new(n))
637
+ end
638
+ end
639
+
640
+ LARGE_RANGE_VALUES.each do |val|
641
+ assert_equal(val, OraNumber.new(BigDecimal.new(val)).to_s)
642
+ end
643
+ end
644
+
645
+ def test_new_from_rational
646
+ [
647
+ [Rational(1, 2), "0.5"],
648
+ [Rational(3, 5), "0.6"],
649
+ [Rational(10, 3), "3.33333333333333333333333333333333333333"],
650
+ [Rational(20, 3), "6.66666666666666666666666666666666666667"],
651
+ ].each do |ary|
652
+ assert_equal(ary[1], OraNumber.new(ary[0]).to_s)
653
+ end
654
+ end
655
+ end