ruby-decimal 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,32 @@
1
+ require File.dirname(__FILE__) + '/helper.rb'
2
+
3
+
4
+
5
+ class TestMultithreading < Test::Unit::TestCase
6
+
7
+ def setup
8
+ initialize_context
9
+ end
10
+
11
+ def test_concurrent_precision
12
+ threads = []
13
+ for prec in (5..100)
14
+ threads << Thread.new(prec) do |p|
15
+ n = 10000/(p/3)
16
+ n_fails = 0
17
+ Decimal.local_context do
18
+ Decimal.context.precision = p
19
+ n.times do
20
+ t = (Decimal(1)/Decimal(3)).to_s
21
+ n_fails += 1 if (t.size!=(p+2)) || (Decimal.context.precision!=p)
22
+ end
23
+ end
24
+ Thread.current[:n_fails] = n_fails
25
+ end
26
+ end
27
+ total_fails = 0
28
+ threads.each{|thr| thr.join; total_fails += thr[:n_fails]}
29
+ assert total_fails==0,"Context precision different per thread"
30
+ end
31
+
32
+ end
@@ -0,0 +1,49 @@
1
+ require File.dirname(__FILE__) + '/helper.rb'
2
+
3
+
4
+ class TestRound < Test::Unit::TestCase
5
+
6
+
7
+ def setup
8
+ initialize_context
9
+ end
10
+
11
+
12
+ def test_round
13
+
14
+ assert_equal(101, Decimal('100.5').round)
15
+ assert Decimal('100.5').round.kind_of?(Integer)
16
+ assert_equal 100, Decimal('100.4999999999').round
17
+ assert_equal(-101, Decimal('-100.5').round)
18
+ assert_equal(-100, Decimal('-100.4999999999').round)
19
+
20
+ assert_equal 101, Decimal('100.5').round(:places=>0)
21
+ assert Decimal('100.5').round(:places=>0).kind_of?(Decimal)
22
+ assert_equal 101, Decimal('100.5').round(0)
23
+ assert Decimal('100.5').round(0).kind_of?(Decimal)
24
+
25
+ assert_equal Decimal('123.12'), Decimal('123.123').round(2)
26
+ assert_equal Decimal('123'), Decimal('123.123').round(0)
27
+ assert_equal Decimal('120'), Decimal('123.123').round(-1)
28
+ assert_equal Decimal('120'), Decimal('123.123').round(:precision=>2)
29
+ assert_equal Decimal('123.12'), Decimal('123.123').round(:precision=>5)
30
+
31
+ assert_equal 100, Decimal('100.5').round(:rounding=>:half_even)
32
+ assert_equal 101, Decimal('100.5000001').round(:rounding=>:half_even)
33
+ assert_equal 102, Decimal('101.5').round(:rounding=>:half_even)
34
+ assert_equal 101, Decimal('101.4999999999').round(:rounding=>:half_even)
35
+
36
+ assert_equal 101, Decimal('100.0001').ceil
37
+ assert_equal(-100, Decimal('-100.0001').ceil)
38
+ assert_equal(-100, Decimal('-100.9999').ceil)
39
+ assert_equal 100, Decimal('100.9999').floor
40
+ assert_equal(-101, Decimal('-100.9999').floor)
41
+ assert_equal(-101, Decimal('-100.0001').floor)
42
+
43
+ assert_equal 100, Decimal('100.99999').truncate
44
+ assert_equal(-100, Decimal('-100.99999').truncate)
45
+
46
+ end
47
+
48
+
49
+ end
@@ -0,0 +1,104 @@
1
+ require File.dirname(__FILE__) + '/helper.rb'
2
+
3
+ class TestToInt < Test::Unit::TestCase
4
+
5
+
6
+ def setup
7
+ initialize_context
8
+ end
9
+
10
+ def test_to_i
11
+
12
+ assert_same 0, Decimal('0.0').to_i
13
+ assert_same 123, Decimal('0.0123000E4').to_i
14
+ assert 1234567890.eql?(Decimal('123456789E1').to_i)
15
+ assert 1234567890.eql?(Decimal('123456789000E-2').to_i)
16
+ assert_same(-123, Decimal('-0.0123000E4').to_i)
17
+ assert(-1234567890.eql?(Decimal('-123456789E1').to_i))
18
+ assert(-1234567890.eql?(Decimal('-123456789000E-2').to_i))
19
+ assert_raise(Decimal::Error) { Decimal.infinity.to_i }
20
+ assert_nil Decimal.nan.to_i
21
+ assert_same(1, Decimal('1.9').to_i)
22
+ assert_same(-1, Decimal('-1.9').to_i)
23
+
24
+ end
25
+
26
+ def test_to_integral_value
27
+
28
+ assert_equal Decimal('0'), Decimal('0.0').to_integral_value
29
+ assert_equal Decimal('123'), Decimal('0.0123000E4').to_integral_value
30
+ assert_equal Decimal('1234567890'), Decimal('123456789E1').to_integral_value
31
+ assert_equal Decimal('1234567890'), Decimal('123456789000E-2').to_integral_value
32
+ assert_equal Decimal('0'), Decimal('-0.0').to_integral_value
33
+ assert_equal Decimal('-123'), Decimal('-0.0123000E4').to_integral_value
34
+ assert_equal Decimal('-1234567890'), Decimal('-123456789E1').to_integral_value
35
+ assert_equal Decimal('-1234567890'), Decimal('-123456789000E-2').to_integral_value
36
+ Decimal.context.rounding = :half_up
37
+ assert_equal Decimal('2'), Decimal('1.9').to_integral_value
38
+ assert_equal Decimal('-2'), Decimal('-1.9').to_integral_value
39
+ Decimal.context.rounding = :down
40
+ assert_equal Decimal('1'), Decimal('1.9').to_integral_value
41
+ assert_equal Decimal('-1'), Decimal('-1.9').to_integral_value
42
+ assert Decimal.nan.to_integral_value.nan?
43
+ assert_equal Decimal.infinity, Decimal.infinity.to_integral_value
44
+
45
+ end
46
+
47
+ def test_to_integral_exact
48
+
49
+ Decimal.context.regard_flags Decimal::Rounded
50
+ Decimal.context.traps[Decimal::Rounded] = false
51
+ Decimal.context.regard_flags Decimal::Rounded
52
+ Decimal.context.traps[Decimal::Inexact] = true
53
+ assert_equal Decimal('0'), Decimal('0').to_integral_exact
54
+ assert !Decimal.context.flags[Decimal::Rounded]
55
+ assert_equal Decimal('0'), Decimal('0.0').to_integral_exact
56
+ assert !Decimal.context.flags[Decimal::Rounded]
57
+ assert_equal Decimal('123'), Decimal('123').to_integral_exact
58
+ assert !Decimal.context.flags[Decimal::Rounded]
59
+ assert_equal Decimal('123'), Decimal('0.0123000E4').to_integral_exact
60
+ assert Decimal.context.flags[Decimal::Rounded]
61
+ Decimal.context.flags[Decimal::Rounded] = false
62
+ assert_equal Decimal('1234567890'), Decimal('123456789E1').to_integral_exact
63
+ assert !Decimal.context.flags[Decimal::Rounded]
64
+ assert_equal Decimal('1234567890'), Decimal('123456789000E-2').to_integral_exact
65
+ assert Decimal.context.flags[Decimal::Rounded]
66
+ Decimal.context.flags[Decimal::Rounded] = false
67
+ assert_equal Decimal('0'), Decimal('-0.0').to_integral_exact
68
+ assert !Decimal.context.flags[Decimal::Rounded]
69
+ assert_equal Decimal('-123'), Decimal('-0.0123000E4').to_integral_exact
70
+ assert Decimal.context.flags[Decimal::Rounded]
71
+ Decimal.context.flags[Decimal::Rounded] = false
72
+ assert_equal Decimal('-1234567890'), Decimal('-123456789E1').to_integral_exact
73
+ assert !Decimal.context.flags[Decimal::Rounded]
74
+ assert_equal Decimal('-1234567890'), Decimal('-123456789000E-2').to_integral_exact
75
+ assert Decimal.context.flags[Decimal::Rounded]
76
+ Decimal.context.flags[Decimal::Rounded] = false
77
+ assert_raise(Decimal::Inexact) { Decimal('1.9').to_integral_exact }
78
+ assert Decimal.context.flags[Decimal::Rounded]
79
+ Decimal.context.flags[Decimal::Rounded] = false
80
+ assert_raise(Decimal::Inexact) { Decimal('-1.9').to_integral_exact }
81
+ assert Decimal.nan.to_integral_exact.nan?
82
+ assert_equal Decimal.infinity, Decimal.infinity.to_integral_exact
83
+ assert Decimal.context.flags[Decimal::Rounded]
84
+ Decimal.context.flags[Decimal::Rounded] = false
85
+ Decimal.context.traps[Decimal::Inexact] = false
86
+ Decimal.context.rounding = :half_up
87
+ assert_equal Decimal('2'), Decimal('1.9').to_integral_exact
88
+ assert Decimal.context.flags[Decimal::Inexact]
89
+ Decimal.context.flags[Decimal::Inexact] = false
90
+ assert_equal Decimal('-2'), Decimal('-1.9').to_integral_exact
91
+ assert Decimal.context.flags[Decimal::Inexact]
92
+ Decimal.context.flags[Decimal::Inexact] = false
93
+ Decimal.context.rounding = :down
94
+ assert_equal Decimal('1'), Decimal('1.9').to_integral_exact
95
+ assert Decimal.context.flags[Decimal::Inexact]
96
+ Decimal.context.flags[Decimal::Inexact] = false
97
+ assert_equal Decimal('-1'), Decimal('-1.9').to_integral_exact
98
+ assert Decimal.nan.to_integral_exact.nan?
99
+ assert_equal Decimal.infinity, Decimal.infinity.to_integral_exact
100
+
101
+ end
102
+
103
+
104
+ end
@@ -0,0 +1,36 @@
1
+ require File.dirname(__FILE__) + '/helper.rb'
2
+
3
+ class TestToRF < Test::Unit::TestCase
4
+
5
+
6
+ def setup
7
+ initialize_context
8
+ end
9
+
10
+ def test_to_r
11
+ [
12
+ [ '0', 0, 1 ],
13
+ [ '1', 1, 1 ],
14
+ [ '-1', -1, 1 ],
15
+ [ '1234567.1234567', 12345671234567, 10000000 ],
16
+ [ '-1234567.1234567', -12345671234567, 10000000 ],
17
+ [ '0.200', 2, 10 ],
18
+ [ '-0.200', -2, 10 ]
19
+ ].each do |n, num, den|
20
+ r = Rational(num,den)
21
+ d = Decimal(n)
22
+ assert d.to_r.is_a?(Rational)
23
+ assert_equal r, d.to_r
24
+ end
25
+ end
26
+
27
+ def test_to_f
28
+ ['0.1', '-0.1', '0.0', '1234567.1234567', '-1234567.1234567', '1.234E7', '1.234E-7'].each do |n|
29
+ f = Float(n)
30
+ d = Decimal(n)
31
+ assert d.to_f.is_a?(Float)
32
+ assert_equal f, d.to_f
33
+ end
34
+ end
35
+
36
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-decimal
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Javier Goizueta
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-19 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bones
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.1.1
24
+ version:
25
+ description: Ruby Decimal Type
26
+ email: javier@goizueta.info
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - History.txt
33
+ - License.txt
34
+ - README.txt
35
+ files:
36
+ - History.txt
37
+ - License.txt
38
+ - Manifest.txt
39
+ - README.txt
40
+ - Rakefile
41
+ - lib/decimal.rb
42
+ - lib/decimal/decimal.rb
43
+ - lib/decimal/support.rb
44
+ - lib/decimal/version.rb
45
+ - setup.rb
46
+ - tasks/ann.rake
47
+ - tasks/bones.rake
48
+ - tasks/gem.rake
49
+ - tasks/git.rake
50
+ - tasks/manifest.rake
51
+ - tasks/notes.rake
52
+ - tasks/post_load.rake
53
+ - tasks/rdoc.rake
54
+ - tasks/rubyforge.rake
55
+ - tasks/setup.rb
56
+ - tasks/spec.rake
57
+ - tasks/svn.rake
58
+ - tasks/test.rake
59
+ - test/all_tests.rb
60
+ - test/helper.rb
61
+ - test/test_basic.rb
62
+ - test/test_coercion.rb
63
+ - test/test_comparisons.rb
64
+ - test/test_dectest.rb
65
+ - test/test_define_conversions.rb
66
+ - test/test_exact.rb
67
+ - test/test_flags.rb
68
+ - test/test_multithreading.rb
69
+ - test/test_round.rb
70
+ - test/test_to_int.rb
71
+ - test/test_to_rf.rb
72
+ has_rdoc: true
73
+ homepage: http://ruby-decimal.rubyforge.org
74
+ licenses: []
75
+
76
+ post_install_message:
77
+ rdoc_options:
78
+ - --main
79
+ - README.txt
80
+ - --title
81
+ - Ruby Decimal Documentation
82
+ - --opname
83
+ - index.html
84
+ - --line-numbers
85
+ - --inline-source
86
+ - --main
87
+ - README.txt
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: "0"
95
+ version:
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: "0"
101
+ version:
102
+ requirements: []
103
+
104
+ rubyforge_project: ruby-decimal
105
+ rubygems_version: 1.3.3
106
+ signing_key:
107
+ specification_version: 3
108
+ summary: Ruby Decimal Type
109
+ test_files:
110
+ - test/test_basic.rb
111
+ - test/test_coercion.rb
112
+ - test/test_comparisons.rb
113
+ - test/test_dectest.rb
114
+ - test/test_define_conversions.rb
115
+ - test/test_exact.rb
116
+ - test/test_flags.rb
117
+ - test/test_multithreading.rb
118
+ - test/test_round.rb
119
+ - test/test_to_int.rb
120
+ - test/test_to_rf.rb