numerals 0.0.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +149 -5
- data/lib/numerals/conversions/bigdecimal.rb +209 -9
- data/lib/numerals/conversions/context_conversion.rb +40 -0
- data/lib/numerals/conversions/float.rb +106 -71
- data/lib/numerals/conversions/flt.rb +115 -44
- data/lib/numerals/conversions/integer.rb +32 -3
- data/lib/numerals/conversions/rational.rb +27 -3
- data/lib/numerals/conversions.rb +74 -33
- data/lib/numerals/digits.rb +8 -5
- data/lib/numerals/format/base_scaler.rb +160 -0
- data/lib/numerals/format/exp_setter.rb +218 -0
- data/lib/numerals/format/format.rb +257 -0
- data/lib/numerals/format/input.rb +140 -0
- data/lib/numerals/format/mode.rb +157 -0
- data/lib/numerals/format/notation.rb +51 -0
- data/lib/numerals/format/notations/html.rb +53 -0
- data/lib/numerals/format/notations/latex.rb +48 -0
- data/lib/numerals/format/notations/text.rb +141 -0
- data/lib/numerals/format/output.rb +167 -0
- data/lib/numerals/format/symbols.rb +565 -0
- data/lib/numerals/format/text_parts.rb +35 -0
- data/lib/numerals/format.rb +25 -0
- data/lib/numerals/formatting_aspect.rb +36 -0
- data/lib/numerals/numeral.rb +34 -21
- data/lib/numerals/repeat_detector.rb +99 -0
- data/lib/numerals/rounding.rb +340 -181
- data/lib/numerals/version.rb +1 -1
- data/lib/numerals.rb +4 -2
- data/numerals.gemspec +1 -1
- data/test/test_base_scaler.rb +189 -0
- data/test/test_big_conversions.rb +105 -0
- data/test/test_digits_definition.rb +23 -28
- data/test/test_exp_setter.rb +732 -0
- data/test/test_float_conversions.rb +48 -30
- data/test/test_flt_conversions.rb +476 -80
- data/test/test_format.rb +124 -0
- data/test/test_format_input.rb +226 -0
- data/test/test_format_mode.rb +124 -0
- data/test/test_format_output.rb +789 -0
- data/test/test_integer_conversions.rb +22 -22
- data/test/test_numeral.rb +35 -0
- data/test/test_rational_conversions.rb +28 -28
- data/test/test_repeat_detector.rb +72 -0
- data/test/test_rounding.rb +158 -0
- data/test/test_symbols.rb +32 -0
- metadata +38 -5
- data/lib/numerals/formatting/digits_definition.rb +0 -75
data/test/test_format.rb
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__),'helper.rb'))
|
2
|
+
include Numerals
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
class TestFormat < Test::Unit::TestCase # < Minitest::Test
|
6
|
+
|
7
|
+
def test_mutated_copy
|
8
|
+
f1 = Format[Rounding[precision: 3, base: 2]]
|
9
|
+
|
10
|
+
f2 = f1.set(rounding: :short)
|
11
|
+
assert_equal Rounding[precision: 3, base: 2], f1.rounding
|
12
|
+
assert_equal Rounding[:short, base: 2], f2.rounding
|
13
|
+
|
14
|
+
f2 = f1.set_rounding(:short)
|
15
|
+
assert_equal Rounding[precision: 3, base: 2], f1.rounding
|
16
|
+
assert_equal Rounding[:short, base: 2], f2.rounding
|
17
|
+
|
18
|
+
f2 = f1.set(rounding: { precision: 4 })
|
19
|
+
assert_equal Rounding[precision: 3, base: 2], f1.rounding
|
20
|
+
assert_equal Rounding[precision: 4, base: 2], f2.rounding
|
21
|
+
|
22
|
+
f2 = f1.set_rounding(precision: 4)
|
23
|
+
assert_equal Rounding[precision: 3, base: 2], f1.rounding
|
24
|
+
assert_equal Rounding[precision: 4, base: 2], f2.rounding
|
25
|
+
|
26
|
+
f2 = f1.set(rounding: Rounding[:short])
|
27
|
+
assert_equal Rounding[precision: 3, base: 2], f1.rounding
|
28
|
+
assert_equal Rounding[:short, base: 10], f2.rounding
|
29
|
+
|
30
|
+
f2 = f1.set(Rounding[:short])
|
31
|
+
assert_equal Rounding[precision: 3, base: 2], f1.rounding
|
32
|
+
assert_equal Rounding[:short, base: 10], f2.rounding
|
33
|
+
|
34
|
+
f2 = f1.set_rounding(Rounding[:short])
|
35
|
+
assert_equal Rounding[precision: 3, base: 2], f1.rounding
|
36
|
+
assert_equal Rounding[:short, base: 10], f2.rounding
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_set_base
|
40
|
+
f = Format[base: 2, rounding: :short]
|
41
|
+
assert_equal 2, f.base
|
42
|
+
assert_equal Rounding[:short, base: 2], f.rounding
|
43
|
+
f.set_base! 5
|
44
|
+
assert_equal 5, f.base
|
45
|
+
assert_equal Rounding[:short, base: 5], f.rounding
|
46
|
+
f = f[base: 6]
|
47
|
+
assert_equal 6, f.base
|
48
|
+
assert_equal Rounding[:short, base: 6], f.rounding
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_aspects
|
52
|
+
f = Format[exact_input: true, rounding: Rounding[precision: 5]]
|
53
|
+
assert_equal true, f.exact_input
|
54
|
+
assert_equal Rounding[precision: 5], f.rounding
|
55
|
+
assert_equal Format::Mode[], f.mode
|
56
|
+
|
57
|
+
f = Format[:exact_input, Rounding[precision: 5]]
|
58
|
+
assert_equal true, f.exact_input
|
59
|
+
assert_equal Rounding[precision: 5], f.rounding
|
60
|
+
assert_equal Format::Mode[], f.mode
|
61
|
+
|
62
|
+
f = Format[Rounding[precision: 5], exact_input: false]
|
63
|
+
assert_equal false, f.exact_input
|
64
|
+
assert_equal Rounding[precision: 5], f.rounding
|
65
|
+
assert_equal Format::Mode[], f.mode
|
66
|
+
|
67
|
+
f = Format[Rounding[precision: 5], mode: :fixed]
|
68
|
+
assert_equal false, f.exact_input
|
69
|
+
assert_equal Rounding[precision: 5], f.rounding
|
70
|
+
assert_equal Format::Mode[:fixed], f.mode
|
71
|
+
|
72
|
+
f = f[exact_input: true]
|
73
|
+
assert_equal true, f.exact_input
|
74
|
+
assert_equal Rounding[precision: 5], f.rounding
|
75
|
+
assert_equal Format::Mode[:fixed], f.mode
|
76
|
+
|
77
|
+
f = f[rounding: { precision: 6 }]
|
78
|
+
assert_equal true, f.exact_input
|
79
|
+
assert_equal Rounding[precision: 6], f.rounding
|
80
|
+
assert_equal Format::Mode[:fixed], f.mode
|
81
|
+
|
82
|
+
f = f.set_exact_input(false)
|
83
|
+
assert_equal false, f.exact_input
|
84
|
+
assert_equal Rounding[precision: 6], f.rounding
|
85
|
+
assert_equal Format::Mode[:fixed], f.mode
|
86
|
+
|
87
|
+
f = f.set_rounding(precision: 10)
|
88
|
+
assert_equal false, f.exact_input
|
89
|
+
assert_equal Rounding[precision: 10], f.rounding
|
90
|
+
assert_equal Format::Mode[:fixed], f.mode
|
91
|
+
|
92
|
+
f = f.set_rounding(precision: 10).set_mode(:engineering)
|
93
|
+
assert_equal false, f.exact_input
|
94
|
+
assert_equal Rounding[precision: 10], f.rounding
|
95
|
+
assert_equal Format::Mode[:engineering], f.mode
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_redefine_aspects
|
99
|
+
f = Format[Format::Mode[max_leading: 10]]
|
100
|
+
f2 = f[Format::Mode[:scientific]]
|
101
|
+
assert_equal :scientific, f2.mode.mode
|
102
|
+
assert_equal Format[].mode.max_leading, f2.mode.max_leading
|
103
|
+
f2 = f[mode: :scientific]
|
104
|
+
assert_equal :scientific, f2.mode.mode
|
105
|
+
assert_equal 10, f2.mode.max_leading
|
106
|
+
|
107
|
+
f = Format[Rounding[base: 2]]
|
108
|
+
f2 = f[Rounding[:short]]
|
109
|
+
assert_equal :short, f2.rounding.precision
|
110
|
+
assert_equal 10, f2.rounding.base
|
111
|
+
f2 = f[rounding: :short]
|
112
|
+
assert_equal :short, f2.rounding.precision
|
113
|
+
assert_equal 2, f2.rounding.base
|
114
|
+
|
115
|
+
f = Format[Format::Symbols[plus: 'PLUS']]
|
116
|
+
f2 = f[Format::Symbols[nan: 'Not a Number']]
|
117
|
+
assert_equal 'Not a Number', f2.symbols.nan
|
118
|
+
assert_equal '+', f2.symbols.plus
|
119
|
+
f2 = f[symbols: { nan: 'Not a Number' }]
|
120
|
+
assert_equal 'Not a Number', f2.symbols.nan
|
121
|
+
assert_equal 'PLUS', f2.symbols.plus
|
122
|
+
end
|
123
|
+
|
124
|
+
end
|
@@ -0,0 +1,226 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__),'helper.rb'))
|
2
|
+
include Numerals
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
class TestFormatInput < Test::Unit::TestCase # < Minitest::Test
|
6
|
+
|
7
|
+
def assert_same_flt(x, y)
|
8
|
+
assert_equal x.class, y.class, x.to_s
|
9
|
+
assert_equal x.split, y.split, x.to_s
|
10
|
+
end
|
11
|
+
|
12
|
+
def assert_same_float(x, y)
|
13
|
+
assert_equal x.class, y.class, x.to_s
|
14
|
+
assert_equal Float.context.split(x), Float.context.split(y), x.to_s
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_read_special_float
|
18
|
+
f = Format[]
|
19
|
+
assert f.read('NaN', type: Float).nan?
|
20
|
+
assert f.read('NAN', type: Float).nan?
|
21
|
+
assert_raise(RuntimeError) { f[case_sensitive: true].read('NAN', type: Float) }
|
22
|
+
assert_equal Float::INFINITY, f.read('Infinity', type: Float)
|
23
|
+
assert_equal Float::INFINITY, f.read('+Infinity', type: Float)
|
24
|
+
assert_equal Float::INFINITY, f.read('+INFINITY', type: Float)
|
25
|
+
assert_equal -Float::INFINITY, f.read('-Infinity', type: Float)
|
26
|
+
assert_equal -Float::INFINITY, f.read('-INFINITY', type: Float)
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_read_float
|
30
|
+
f = Format[]
|
31
|
+
assert_same_float 1.0, f.read('1', type: Float)
|
32
|
+
assert_same_float 1.2345e-23, f.read('1.2345e-23', type: Float)
|
33
|
+
assert_same_float -1.2345e-23, f.read('-1.2345e-23', type: Float)
|
34
|
+
assert_same_float 1.2345e-23, f.read('1.2345E-23', type: Float)
|
35
|
+
assert_same_float 1.2345e23, f.read('1.2345E23', type: Float)
|
36
|
+
assert_same_float 1.2345e23, f.read('1.2345E+23', type: Float)
|
37
|
+
assert_same_float -1/3.0 - 123456, f.read('-123,456.<3>', type: Float)
|
38
|
+
assert_same_float 4/3.0, f.read('+1.<3>', type: Float)
|
39
|
+
assert_same_float 4/3.0, f.read('+1.333...', type: Float)
|
40
|
+
assert_same_float 0.1, f.read('0.1', type: Float)
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_read_rational
|
44
|
+
f = Format[]
|
45
|
+
assert_equal Rational(1,1), f.read('1.0', type: Rational)
|
46
|
+
assert_equal Rational(-370369,3), f.read('-123,456.<3>', type: Rational)
|
47
|
+
assert_equal Rational(4,3), f.read('+1.<3>', type: Rational)
|
48
|
+
assert_equal Rational(1,10), f.read('0.1', type: Rational)
|
49
|
+
assert_equal Rational(41000000000,333), f.read('1,23123123...', type: Rational)
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_read_decnum
|
53
|
+
f = Format[]
|
54
|
+
Flt::DecNum.context(precision: 20) do
|
55
|
+
assert_same_flt Flt::DecNum('123123123.12312312312'), f.read('1,23123123...', type: Flt::DecNum)
|
56
|
+
assert_same_flt Flt::DecNum('-123456.33333333333333'), f.read('-123,456.<3>', type: Flt::DecNum)
|
57
|
+
assert_same_flt Flt::DecNum('1.3333333333333333333'), f.read('+1.<3>', type: Flt::DecNum)
|
58
|
+
assert_same_flt Flt::DecNum('0.1'), f.read('0.1', type: Flt::DecNum)
|
59
|
+
assert_same_flt Flt::DecNum('0.10000000000000000000'), f[:exact_input].read('0.1', type: Flt::DecNum)
|
60
|
+
assert_same_flt Flt::DecNum('0.10000000000000000000'), f[:exact_input].read('0.1000', type: Flt::DecNum)
|
61
|
+
assert_same_flt Flt::DecNum('0.1'), f[:short].read('0.1000', type: Flt::DecNum)
|
62
|
+
assert_same_flt Flt::DecNum('0.1000'), f[:free].read('0.1000', type: Flt::DecNum)
|
63
|
+
end
|
64
|
+
context = Flt::DecNum.context(precision: 19)
|
65
|
+
assert_same_flt Flt::DecNum('123123123.1231231231'), f.read('1,23123123...', type: context)
|
66
|
+
assert_same_flt Flt::DecNum('-123456.3333333333333'), f.read('-123,456.<3>', type: context)
|
67
|
+
assert_same_flt Flt::DecNum('1.333333333333333333'), f.read('+1.<3>', type: context)
|
68
|
+
assert_same_flt Flt::DecNum('0.1'), f.read('0.1', type: context)
|
69
|
+
assert_same_flt Flt::DecNum('0.1000000000000000000'), f[:exact_input].read('0.1', type: context)
|
70
|
+
assert_same_flt Flt::DecNum('0.1000000000000000000'), f[:exact_input].read('0.1000', type: context)
|
71
|
+
assert_same_flt Flt::DecNum('0.1'), f[:short].read('0.1000', type: context)
|
72
|
+
assert_same_flt Flt::DecNum('0.1000'), f[:free].read('0.1000', type: context)
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_read_binnum
|
76
|
+
f = Format[]
|
77
|
+
Flt::BinNum.context(Flt::BinNum::IEEEDoubleContext) do
|
78
|
+
assert_same_flt Flt::BinNum('1', :short), f.read('1', type: Flt::BinNum)
|
79
|
+
assert_same_flt Flt::BinNum('1', :free), f[:free].read('1', type: Flt::BinNum)
|
80
|
+
assert_same_flt Flt::BinNum('1.00', :free), f[:free].read('1.00', type: Flt::BinNum)
|
81
|
+
assert_same_flt Flt::BinNum('1.2345E-23', :short), f.read('1.2345E-23', type: Flt::BinNum)
|
82
|
+
assert_same_flt Flt::BinNum('-123456.333333333333333', :fixed), f.read('-123,456.<3>', type: Flt::BinNum)
|
83
|
+
assert_same_flt Flt::BinNum('1.33333333333333333333', :fixed), f.read('+1.<3>', type: Flt::BinNum)
|
84
|
+
assert_same_flt Flt::BinNum('0.1', :short), f.read('0.1', type: Flt::BinNum)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_read_hexbin
|
89
|
+
context = Flt::BinNum::IEEEDoubleContext
|
90
|
+
Flt::BinNum.context(context) do
|
91
|
+
f = Format[:free, :hexbin]
|
92
|
+
x = Flt::BinNum('0.1', :fixed)
|
93
|
+
assert_same_flt x, f.read("1.999999999999Ap-4", type: Flt::BinNum)
|
94
|
+
assert_same_flt -x, f.read("-1.999999999999Ap-4", type: Flt::BinNum)
|
95
|
+
assert_same_flt x, f.read("19.99999999999Ap-8", type: Flt::BinNum)
|
96
|
+
%w(1.52d02c7e14af6p+76 1.52d02c7e14af7p+76 1.0066666666666p+6 1.0066666666667p+6).each do |txt|
|
97
|
+
x = Flt::BinNum("0x#{txt}", :fixed)
|
98
|
+
assert_same_flt x, f.read(txt, type: Flt::BinNum)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_read_equidistant_flt
|
104
|
+
# In IEEEDoubleContext
|
105
|
+
# 1E23 is equidistant from 2 Floats: lo & hi
|
106
|
+
# one or the other will be chosen based on the rounding mode
|
107
|
+
|
108
|
+
context = Flt::BinNum::IEEEDoubleContext
|
109
|
+
|
110
|
+
lo = hi = nil
|
111
|
+
Flt::BinNum.context(context) do
|
112
|
+
lo = Flt::BinNum('0x1.52d02c7e14af6p+76', :fixed) # 9.999999999999999E22
|
113
|
+
hi = Flt::BinNum('0x1.52d02c7e14af7p+76', :fixed) # 1.0000000000000001E23
|
114
|
+
end
|
115
|
+
|
116
|
+
f = Format[:exact_input]
|
117
|
+
|
118
|
+
# define rounding mode with :type_options
|
119
|
+
x = f.read('1E23', type: context, type_options: { input_rounding: :half_even })
|
120
|
+
assert_same_flt lo, x
|
121
|
+
x = f.read('1E23', type: context, type_options: { input_rounding: :half_down })
|
122
|
+
assert_equal lo, x
|
123
|
+
x = f.read('1E23', type: context, type_options: { input_rounding: :half_up })
|
124
|
+
assert_equal hi, x
|
125
|
+
|
126
|
+
x = f.read('-1E23', type: context, type_options: { input_rounding: :half_even })
|
127
|
+
assert_equal -lo, x
|
128
|
+
x = f.read('-1E23', type: context, type_options: { input_rounding: :half_down })
|
129
|
+
assert_equal -lo, x
|
130
|
+
x = f.read('-1E23', type: context, type_options: { input_rounding: :half_up })
|
131
|
+
assert_equal -hi, x
|
132
|
+
|
133
|
+
# define rounding mode with Format's Rounding
|
134
|
+
x = f[:half_even].read('1E23', type: context)
|
135
|
+
assert_same_flt lo, x
|
136
|
+
x = f[:half_down].read('1E23', type: context)
|
137
|
+
assert_equal lo, x
|
138
|
+
x = f[:half_up].read('1E23', type: context)
|
139
|
+
assert_equal hi, x
|
140
|
+
|
141
|
+
x = f[:half_even].read('-1E23', type: context)
|
142
|
+
assert_equal -lo, x
|
143
|
+
x = f[:half_down].read('-1E23', type: context)
|
144
|
+
assert_equal -lo, x
|
145
|
+
x = f[:half_up].read('-1E23', type: context)
|
146
|
+
assert_equal -hi, x
|
147
|
+
|
148
|
+
# define rounding mode with Format's input_rounding
|
149
|
+
x = f[input_rounding: :half_even].read('1E23', type: context)
|
150
|
+
assert_same_flt lo, x
|
151
|
+
x = f[input_rounding: :half_down].read('1E23', type: context)
|
152
|
+
assert_equal lo, x
|
153
|
+
x = f[input_rounding: :half_up].read('1E23', type: context)
|
154
|
+
assert_equal hi, x
|
155
|
+
|
156
|
+
x = f[input_rounding: :half_even].read('-1E23', type: context)
|
157
|
+
assert_equal -lo, x
|
158
|
+
x = f[input_rounding: :half_down].read('-1E23', type: context)
|
159
|
+
assert_equal -lo, x
|
160
|
+
x = f[input_rounding: :half_up].read('-1E23', type: context)
|
161
|
+
assert_equal -hi, x
|
162
|
+
end
|
163
|
+
|
164
|
+
def test_read_single_nearest
|
165
|
+
# In IEEEDoubleContext
|
166
|
+
# 64.1 between the floats lo, hi, but is closer to lo
|
167
|
+
# So there's a closet Float that should be chosen for rounding
|
168
|
+
|
169
|
+
context = Flt::BinNum::IEEEDoubleContext
|
170
|
+
|
171
|
+
lo = hi = nil
|
172
|
+
Flt::BinNum.context(context) do
|
173
|
+
lo = Flt::BinNum('0x1.0066666666666p+6', :fixed) # this is nearer to the 64.1 Float
|
174
|
+
hi = Flt::BinNum('0x1.0066666666667p+6', :fixed)
|
175
|
+
end
|
176
|
+
|
177
|
+
f = Format[:exact_input]
|
178
|
+
|
179
|
+
# define rounding mode with type_optoins
|
180
|
+
x = f.read('64.1', type: context, type_options: { input_rounding: :half_even })
|
181
|
+
assert_equal lo, x
|
182
|
+
x = f.read('64.1', type: context, type_options: { input_rounding: :half_down })
|
183
|
+
assert_equal lo, x
|
184
|
+
x = f.read('64.1', type: context, type_options: { input_rounding: :half_up })
|
185
|
+
assert_equal lo, x
|
186
|
+
|
187
|
+
x = f.read('-64.1', type: context, type_options: { input_rounding: :half_even })
|
188
|
+
assert_equal -lo, x
|
189
|
+
x = f.read('-64.1', type: context, type_options: { input_rounding: :half_even })
|
190
|
+
assert_equal -lo, x
|
191
|
+
x = f.read('-64.1', type: context, type_options: { input_rounding: :half_up })
|
192
|
+
assert_equal -lo, x
|
193
|
+
|
194
|
+
# define rounding mode with Format's Rounding
|
195
|
+
x = f[:half_even].read('64.1', type: context)
|
196
|
+
assert_equal lo, x
|
197
|
+
x = f[:half_down].read('64.1', type: context)
|
198
|
+
assert_equal lo, x
|
199
|
+
x = f[:half_up].read('64.1', type: context)
|
200
|
+
assert_equal lo, x
|
201
|
+
|
202
|
+
x = f[:half_even].read('-64.1', type: context)
|
203
|
+
assert_equal -lo, x
|
204
|
+
x = f[:half_down].read('-64.1', type: context)
|
205
|
+
assert_equal -lo, x
|
206
|
+
x = f[:half_up].read('-64.1', type: context)
|
207
|
+
assert_equal -lo, x
|
208
|
+
|
209
|
+
# define rounding mode with Format's input_rounding
|
210
|
+
x = f[input_rounding: :half_even].read('64.1', type: context)
|
211
|
+
assert_equal lo, x
|
212
|
+
x = f[input_rounding: :half_down].read('64.1', type: context)
|
213
|
+
assert_equal lo, x
|
214
|
+
x = f[input_rounding: :half_up].read('64.1', type: context)
|
215
|
+
assert_equal lo, x
|
216
|
+
|
217
|
+
x = f[input_rounding: :half_even].read('-64.1', type: context)
|
218
|
+
assert_equal -lo, x
|
219
|
+
x = f[input_rounding: :half_down].read('-64.1', type: context)
|
220
|
+
assert_equal -lo, x
|
221
|
+
x = f[input_rounding: :half_up].read('-64.1', type: context)
|
222
|
+
assert_equal -lo, x
|
223
|
+
|
224
|
+
end
|
225
|
+
|
226
|
+
end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__),'helper.rb'))
|
2
|
+
|
3
|
+
require 'numerals/rounding'
|
4
|
+
include Numerals
|
5
|
+
|
6
|
+
class TestFormatMode < Test::Unit::TestCase # < Minitest::Test
|
7
|
+
|
8
|
+
def test_format_mode_constructor
|
9
|
+
mode = Format::Mode[:scientific]
|
10
|
+
assert mode.scientific?
|
11
|
+
refute mode.engineering?
|
12
|
+
refute mode.fixed?
|
13
|
+
refute mode.general?
|
14
|
+
assert_equal :scientific, mode.mode
|
15
|
+
assert_equal 1, mode.sci_int_digits
|
16
|
+
assert_equal 1, mode.base_scale
|
17
|
+
assert_equal Format::Mode[:scientific], Format::Mode[:sci]
|
18
|
+
assert_equal Format::Mode[:scientific, sci_int_digits: 2], Format::Mode[:sci, sci_int_digits: 2]
|
19
|
+
assert_equal Format::Mode[:scientific, sci_int_digits: :engineering], Format::Mode[:sci, sci_int_digits: :engineering]
|
20
|
+
assert_equal Format::Mode[:scientific, sci_int_digits: :eng], Format::Mode[:sci, sci_int_digits: :eng]
|
21
|
+
|
22
|
+
mode = Format::Mode[:scientific, sci_int_digits: 0]
|
23
|
+
assert mode.scientific?
|
24
|
+
refute mode.engineering?
|
25
|
+
refute mode.fixed?
|
26
|
+
refute mode.general?
|
27
|
+
assert_equal :scientific, mode.mode
|
28
|
+
assert_equal 0, mode.sci_int_digits
|
29
|
+
assert_equal 1, mode.base_scale
|
30
|
+
|
31
|
+
mode = Format::Mode[:scientific, base_scale: 4]
|
32
|
+
assert mode.scientific?
|
33
|
+
refute mode.engineering?
|
34
|
+
refute mode.fixed?
|
35
|
+
refute mode.general?
|
36
|
+
assert_equal :scientific, mode.mode
|
37
|
+
assert_equal 1, mode.sci_int_digits
|
38
|
+
assert_equal 4, mode.base_scale
|
39
|
+
|
40
|
+
mode = Format::Mode[:engineering]
|
41
|
+
assert mode.engineering?
|
42
|
+
assert mode.scientific?
|
43
|
+
refute mode.fixed?
|
44
|
+
refute mode.general?
|
45
|
+
assert_equal :engineering, mode.sci_int_digits
|
46
|
+
assert_equal :scientific, mode.mode
|
47
|
+
assert_equal 1, mode.base_scale
|
48
|
+
assert_equal Format::Mode[:engineering], Format::Mode[:eng]
|
49
|
+
assert_equal Format::Mode[:engineering], Format::Mode[:scientific, sci_int_digits: :engineering]
|
50
|
+
assert_equal Format::Mode[:engineering], Format::Mode[:scientific, sci_int_digits: :eng]
|
51
|
+
|
52
|
+
mode = Format::Mode[:fixed]
|
53
|
+
refute mode.scientific?
|
54
|
+
refute mode.engineering?
|
55
|
+
assert mode.fixed?
|
56
|
+
refute mode.general?
|
57
|
+
assert_equal :fixed, mode.mode
|
58
|
+
assert_equal 1, mode.base_scale
|
59
|
+
assert_equal Format::Mode[:fixed], Format::Mode[:fix]
|
60
|
+
assert_equal Format::Mode[:fixed, base_scale: 4], Format::Mode[:fix, base_scale: 4]
|
61
|
+
|
62
|
+
mode = Format::Mode[:general]
|
63
|
+
refute mode.scientific?
|
64
|
+
refute mode.engineering?
|
65
|
+
refute mode.fixed?
|
66
|
+
assert mode.general?
|
67
|
+
assert_equal :general, mode.mode
|
68
|
+
assert_equal 1, mode.sci_int_digits
|
69
|
+
assert_equal Format::Mode::DEFAULTS[:max_leading], mode.max_leading
|
70
|
+
assert_equal Format::Mode::DEFAULTS[:max_trailing], mode.max_trailing
|
71
|
+
assert_equal 1, mode.base_scale
|
72
|
+
assert_equal Format::Mode[:general], Format::Mode[]
|
73
|
+
assert_equal Format::Mode[:general], Format::Mode[:gen]
|
74
|
+
assert_equal Format::Mode[:general, sci_int_digits: 0], Format::Mode[:gen, sci_int_digits: 0]
|
75
|
+
assert_equal Format::Mode[:general, sci_int_digits: :engineering], Format::Mode[:gen, sci_int_digits: :engineering]
|
76
|
+
assert_equal Format::Mode[:general, sci_int_digits: :eng], Format::Mode[:gen, sci_int_digits: :eng]
|
77
|
+
|
78
|
+
mode = Format::Mode[:general, max_leading: 8, max_trailing: 2, base_scale: 2, sci_int_digits: 0]
|
79
|
+
refute mode.scientific?
|
80
|
+
refute mode.engineering?
|
81
|
+
refute mode.fixed?
|
82
|
+
assert mode.general?
|
83
|
+
assert_equal :general, mode.mode
|
84
|
+
assert_equal 0, mode.sci_int_digits
|
85
|
+
assert_equal 8, mode.max_leading
|
86
|
+
assert_equal 2, mode.max_trailing
|
87
|
+
assert_equal 2, mode.base_scale
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_copy
|
91
|
+
m1 = Format::Mode[:fixed]
|
92
|
+
m2 = Format::Mode[m1]
|
93
|
+
assert_equal :fixed, m2.mode
|
94
|
+
m2.mode = :scientific
|
95
|
+
assert_equal :fixed, m1.mode
|
96
|
+
assert_equal :scientific, m2.mode
|
97
|
+
|
98
|
+
m2 = m1[:scientific]
|
99
|
+
assert_equal :fixed, m1.mode
|
100
|
+
assert_equal :scientific, m2.mode
|
101
|
+
|
102
|
+
m1 = Format::Mode[:scientific, sci_int_digits: 0]
|
103
|
+
m2 = m1[sci_int_digits: 2]
|
104
|
+
assert_equal 0, m1.sci_int_digits
|
105
|
+
assert_equal 2, m2.sci_int_digits
|
106
|
+
assert_equal :scientific, m1.mode
|
107
|
+
assert_equal :scientific, m2.mode
|
108
|
+
|
109
|
+
m1 = Format::Mode[:scientific, sci_int_digits: 0]
|
110
|
+
m2 = m1.set(sci_int_digits: 2)
|
111
|
+
assert_equal 0, m1.sci_int_digits
|
112
|
+
assert_equal 2, m2.sci_int_digits
|
113
|
+
assert_equal :scientific, m1.mode
|
114
|
+
assert_equal :scientific, m2.mode
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_mutators
|
118
|
+
m1 = Format::Mode[:fixed]
|
119
|
+
m2 = m1.set!(:scientific)
|
120
|
+
assert_equal :scientific, m1.mode
|
121
|
+
assert_equal :scientific, m2.mode
|
122
|
+
assert_equal m1.object_id, m2.object_id
|
123
|
+
end
|
124
|
+
end
|