stick 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. data/CHANGES +7 -0
  2. data/COPYING +344 -0
  3. data/README +110 -0
  4. data/lib/stick/constants.rb +3 -0
  5. data/lib/stick/constants/cgs.rb +151 -0
  6. data/lib/stick/constants/mks.rb +158 -0
  7. data/lib/stick/constants/number.rb +33 -0
  8. data/lib/stick/constants/typeless_cgs.rb +141 -0
  9. data/lib/stick/constants/typeless_mks.rb +142 -0
  10. data/lib/stick/currency.rb +8 -0
  11. data/lib/stick/mapcar.rb +61 -0
  12. data/lib/stick/matrix.rb +1022 -0
  13. data/lib/stick/quaternion.rb +562 -0
  14. data/lib/stick/times.rb +441 -0
  15. data/lib/stick/units.rb +112 -0
  16. data/lib/stick/units/base.rb +980 -0
  17. data/lib/stick/units/currency.rb +159 -0
  18. data/lib/stick/units/data/binary/base.rb +4 -0
  19. data/lib/stick/units/data/cex.rb +5 -0
  20. data/lib/stick/units/data/currency-default.rb +5 -0
  21. data/lib/stick/units/data/currency-standard.rb +2 -0
  22. data/lib/stick/units/data/currency/base.rb +89 -0
  23. data/lib/stick/units/data/iec.rb +5 -0
  24. data/lib/stick/units/data/iec_binary/base.rb +6 -0
  25. data/lib/stick/units/data/si.rb +7 -0
  26. data/lib/stick/units/data/si/base.rb +9 -0
  27. data/lib/stick/units/data/si/derived.rb +26 -0
  28. data/lib/stick/units/data/si/extra.rb +22 -0
  29. data/lib/stick/units/data/uk.rb +10 -0
  30. data/lib/stick/units/data/uk/base.rb +22 -0
  31. data/lib/stick/units/data/units-default.rb +11 -0
  32. data/lib/stick/units/data/units-standard.rb +5 -0
  33. data/lib/stick/units/data/us.rb +10 -0
  34. data/lib/stick/units/data/us/base.rb +23 -0
  35. data/lib/stick/units/data/xmethods.rb +5 -0
  36. data/lib/stick/units/data/xmethods/cached.rb +84 -0
  37. data/lib/stick/units/data/xmethods/mapping.rb +87 -0
  38. data/lib/stick/units/loaders.rb +98 -0
  39. data/lib/stick/units/units.rb +109 -0
  40. data/meta/MANIFEST +76 -0
  41. data/meta/ROLLRC +2 -0
  42. data/meta/icli.yaml +16 -0
  43. data/meta/project.yaml +18 -0
  44. data/task/clobber/package +10 -0
  45. data/task/publish +57 -0
  46. data/task/release +10 -0
  47. data/task/setup +1616 -0
  48. data/task/test +25 -0
  49. data/test/spec_matrix.rb +342 -0
  50. data/test/test_currency.rb +26 -0
  51. data/test/test_matrix.rb +359 -0
  52. data/test/test_units.rb +205 -0
  53. data/work/TODO +20 -0
  54. data/work/bytes.rb +231 -0
  55. data/work/multipliers.rb +195 -0
  56. metadata +138 -0
@@ -0,0 +1,205 @@
1
+ $:.unshift(File.dirname(__FILE__) + '../lib')
2
+
3
+ require 'test/unit'
4
+ require 'stick/units'
5
+
6
+ class TestUnits < Test::Unit::TestCase
7
+
8
+ def assert_in_unit_delta(v1, v2, d)
9
+ assert( v2 * (1 - d) <= v1 && v1 <= v2 * (1 + d), "<#{v1}> expected but was\n<#{v2}>" )
10
+ end
11
+
12
+ include ::Stick
13
+
14
+ def test_bit_and_bytes
15
+ assert_equal( 65.bit/s, 1.bit/s + 8.bytes/s )
16
+ assert_equal( 0.125.byte/s, ((1.bit/s).to(byte/s)) )
17
+ # PETER : Why 0?
18
+ # assert_equal( 0, ((1.bit/s).to(byte/s)) )
19
+ end
20
+
21
+ def miles_to_feet
22
+ assert_equal( 5000.feet, 1.mile.to(feet) )
23
+ end
24
+
25
+ def test_acre_to_yards_squared
26
+ assert_equal( 4840.sq_yd, 1.acre.to(yd**2) )
27
+ # PETER : This obviously fails because yards and square yards are not the same.
28
+ # assert_equal( 4840.yd, 1.acre.to(yd**2) )
29
+ end
30
+
31
+ def test_acre_to_sq_yd
32
+ assert_equal( 4840.sq_yd, 1.acre.to(sq_yd) )
33
+ end
34
+
35
+ def test_gallon_to_liter
36
+ assert_in_unit_delta( 3.785411784.L, 1.gallon.to(L), 1e-15 )
37
+ # PETER : A gallon expressed in liters is not a liter.
38
+ # assert_equal( 1.L, 1.gallon.to(L) )
39
+ end
40
+
41
+ def test_lb_to_kg
42
+ assert_equal( 0.45359237.kg, 1.lb.to(kg) )
43
+ end
44
+
45
+ def test_m_s_to_m_s
46
+ assert_equal( 1.m.s, 1.m.s.to(m.s) )
47
+ end
48
+
49
+ def test_sq_mi_to_km_squared
50
+ assert_in_unit_delta( 2.589988110336.to_value(km**2), 1.sq_mi.to(km**2), 1e-15 )
51
+ # PETER : See comment in #test_bandwidth.
52
+ # assert_equal( 1.to(km**2), 1.sq_mi.to(km**2) )
53
+ end
54
+
55
+ def test_mile_to_km
56
+ assert_in_unit_delta( 1.609344.km, 1.mile.to(km), 1e-15 )
57
+ # PETER : These are floats, they don't compare.
58
+ # assert_equal( 1.609344.km, 1.mile.to(km) )
59
+ end
60
+
61
+ def test_with_unit_convertor_uk
62
+ with_unit_converter(:uk) {
63
+ assert_equal( 112.lb, 1.cwt.to(lb) )
64
+ }
65
+ assert_equal( 112.lb(:uk), 1.cwt(:uk).to(lb(:uk)) )
66
+ # PETER : That would be a very small hundredweight
67
+ # assert_equal( 1.lb(:uk), 1.cwt(:uk).to(lb(:uk)) )
68
+ end
69
+
70
+ def test_with_unit_convertor_us
71
+ with_unit_converter(:us) {
72
+ assert_equal( 100.lb, 1.cwt.to(lb) )
73
+ }
74
+ assert_equal( 100.lb(:us), 1.cwt(:us).to(lb(:us)) )
75
+ # PETER : That would be a very small hundredweight
76
+ # assert_equal( 1.lb(:us), 1.cwt(:us).to(lb(:us)) )
77
+ end
78
+
79
+ def test_current_lb
80
+ assert_equal( lb, Converter.current.lb )
81
+ # PETER : compares values and units again.
82
+ # assert_equal( 1.lb, Converter.current.lb )
83
+ end
84
+
85
+ def test_cwt
86
+ assert_equal( 1.12.cwt(:us), 1.cwt(:uk).to(cwt(:us)) )
87
+ assert_equal( 1.cwt(:uk), 1.12.cwt(:us).to(cwt(:uk)) )
88
+ end
89
+
90
+ def test_unit_convertor_cwt
91
+ with_unit_converter(:uk) {
92
+ assert_equal( 1.cwt, 1.cwt.to(cwt(:us)) )
93
+ assert_equal( 1.cwt(:us), 1.cwt(:us).to(cwt) )
94
+ # PETER : This is not correct. The conversion does
95
+ # not change the magnitude of the thing, i.e.,
96
+ # something that weighs a hundredweight UK style does
97
+ # not weigh a hundredweight US style even when its
98
+ # weight us expressed in US style hundredweights
99
+ # (if that makes sense.)
100
+ # Also, this converter has no effect because the
101
+ # converter is specified everywhere. So it is really
102
+ # the same as the above.
103
+ # assert_equal( 1.cwt(:us), 1.cwt(:uk).to(cwt(:us)) )
104
+ # assert_equal( 1.cwt(:uk), 1.cwt(:us).to(cwt(:uk)) )
105
+ }
106
+ end
107
+
108
+ #def test_registered_convertors
109
+ # assert_equal( [:cex, :default, :uk, :us], Converter.registered_converters.sort )
110
+ #end
111
+
112
+ def test_spaceship
113
+ assert_nil( 1.m <=> 1.L )
114
+ # PETER : meter and liter are like apples and oranges.
115
+ # returns nil instead.
116
+ # assert_equal( 0, (1.m <=> 1.L) )
117
+ assert_equal( 1, (1.m <=> 1.cm) )
118
+ end
119
+
120
+ def test_bandwidth
121
+ assert_equal( 1024.to_value(kB / s), ((1.MB / s).to(kB / s)) )
122
+ with_unit_converter(:binary_iec_base) {
123
+ assert_equal( 1000.to_value(kB / s), ((1.MB / s).to(kB / s)) )
124
+ }
125
+ # PETER : The #to method does conversion from one unit
126
+ # to another compatible unit. Although it could make
127
+ # sense to convert a unitless number to a unit
128
+ # that is essentially unitless too (like kB/MB), there
129
+ # are better ways to do the same, and the below still
130
+ # wouldn't work because 1 is unitless and 1.kB/s is not.
131
+ # I've added a to_value method for this.
132
+ # assert_equal( 1.to(kB / s), ((1.MB / s).to(kB / s)) )
133
+ # with_unit_converter(:binary_iec_base) {
134
+ # assert_equal( 1.to(kB / s), ((1.MB / s).to(kB / s)) )
135
+ # }
136
+ end
137
+
138
+ def test_to_unit
139
+ assert_equal( m / s, "m / s".to_unit )
140
+ # PETER : m/s and 1 m/s are not the same thing.
141
+ # assert_equal( 1.to(m / s), "m / s".to_unit )
142
+ end
143
+
144
+ def test_to_value
145
+ assert_equal( 1.m / s, "1 m / s".to_value )
146
+ # PETER : See remark in #test_bandwidth.
147
+ # assert_equal( 1.to(m / s), "1 m / s".to_value )
148
+ end
149
+
150
+ def test_simplify
151
+ # PETER : Don't see how this could be zero. Don't know
152
+ # exactly what you want to test though...
153
+ # assert_equal( 0, "1 m / cm L".to_value.simplify )
154
+ end
155
+
156
+ def test_float
157
+ assert_equal( 100.0, "1 m / cm".to_value.to_f )
158
+ end
159
+
160
+ def test_m_to_str_cm
161
+ assert_equal( 100.cm, 1.m.to("cm") )
162
+ end
163
+
164
+ def test_m_plus_str_cm
165
+ assert_equal( 105.cm, 1.m + "5cm" )
166
+ end
167
+
168
+ def test_plus
169
+ assert_equal( 1.05.m, 1.m + 5.cm )
170
+ end
171
+
172
+ def test
173
+ assert_equal( 105.m, 5.cm + 1.m )
174
+ end
175
+
176
+ # PETER : This compares a value (quantity + unit) to a unit. This could
177
+ # be allowed by saying that the unit of meter and 1 meter is the
178
+ # same, but personally I'd like to keep the distinction.
179
+ # def test_
180
+ # assert_equal( 101.cm, cm * m )
181
+ # end
182
+
183
+ # def test_
184
+ # assert_equal( 101.cm, cm * "m" )
185
+ # end
186
+
187
+ def test_value
188
+ assert_equal(-5.mm, "-5mm".to_value )
189
+ # PETER : -5mm is not the same as -5
190
+ # assert_equal(-5, "-5mm".to_value )
191
+ end
192
+
193
+ def test_value_abs
194
+ assert_equal(5.mm, "-5mm".to_value.abs )
195
+ # PETER : 5mm is not the same as 5
196
+ # assert_equal(5, "-5mm".to_value.abs )
197
+ end
198
+
199
+ def test_infinite?
200
+ # PETER : I'm not sure what the point of this is, but in
201
+ # any case, a Value does not have an infinite? method
202
+ # assert_equal( false, ("5.0mm".to_value / 1).infinite? )
203
+ end
204
+
205
+ end
data/work/TODO ADDED
@@ -0,0 +1,20 @@
1
+ = To Do List -- Stick
2
+
3
+ == Current
4
+
5
+ * times.rb mainly serves as a reminder that it would be nice if the units
6
+ system could have an extra set of conversions for handling calander nomencalture.
7
+ Of course times.rb can be used too, it's just not really inteded to be used
8
+ alonde side units.rb.
9
+
10
+ == Pre-Stick List
11
+
12
+ file://lib/facets/bytes.rb
13
+ * TODO Currently kilo, mega, etc. are all powers of two and not ten,
14
+ which technically isn't corrent even though it is common usage. (30)
15
+ * TODO The in_* notation is weak. If a better nomentclature is thought
16
+ of then consider changing this. (33)
17
+
18
+ file://lib/facets/times.rb
19
+ * TODO Extra Add in_* methods, like in_days, in_hours, etc. (35)
20
+
data/work/bytes.rb ADDED
@@ -0,0 +1,231 @@
1
+ # = bytes.rb
2
+ #
3
+ # == Copyright (c) 2005 Rich Kilmer, Thomas Sawyer
4
+ #
5
+ # Ruby License
6
+ #
7
+ # This module is free software. You may use, modify, and/or redistribute this
8
+ # software under the same terms as Ruby.
9
+ #
10
+ # This program is distributed in the hope that it will be useful, but WITHOUT
11
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12
+ # FOR A PARTICULAR PURPOSE.
13
+ #
14
+ # == Special Thanks
15
+ #
16
+ # Special thanks to Richard Kilmer for the orignal work.
17
+ # This library is based on the original library bytes.rb
18
+ # Copyright (c) 2004 by Rich Kilmer.
19
+ #
20
+ # Also thanks to Alexander Kellett for suggesting it be
21
+ # included in Facets.
22
+ #
23
+ # == Authors and Contributors
24
+ #
25
+ # * Rich Kilmer
26
+ # * Thomas Sawyer
27
+ #
28
+ # == Developer Notes
29
+ #
30
+ # TODO Currently kilo, mega, etc. are all powers of two and not ten,
31
+ # which technically isn't corrent even though it is common usage.
32
+ #
33
+ # TODO The in_* notation is weak. If a better nomentclature is thought
34
+ # of then consider changing this.
35
+
36
+ # Author:: Rich Kilmer, Thomas Sawyer
37
+ # Copyright:: Copyright (c) 2005 Thomas Sawyer, Rich Kilmer
38
+ # License:: Ruby License
39
+
40
+ # = Binary Multipliers
41
+ #
42
+ # Additional methods for Numeric class to make working with
43
+ # bits and bytes easier. Bits are used as the base value and
44
+ # these methods can be used to convert between different
45
+ # magnitudes.
46
+ #
47
+ # == Synopisis
48
+ #
49
+ # 1.byte #=> 8
50
+ # 2.bytes #=> 16
51
+ # 1.kilobit #=> 1024
52
+ # 1.kilobyte #=> 8192
53
+ #
54
+ # Use the in_* methods to perform the inverse operations.
55
+ #
56
+ # 8192.in_kilobytes #=> 1
57
+ # 1024.in_kilobits #=> 1
58
+ #
59
+
60
+ class Numeric
61
+
62
+ def bit ; self ; end
63
+ def bits ; self ; end
64
+ def byte ; self * 8 ; end
65
+ def bytes ; self * 8 ; end
66
+
67
+ [ 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa' ].each_with_index do |m, i|
68
+ j = i + 1
69
+ class_eval %{
70
+ def #{m}bit ; self * #{1024**j} ; end
71
+ def #{m}byte ; self * #{1024**j*8} ; end
72
+ def in_#{m}bits ; self / #{1024**j} ; end
73
+ def in_#{m}bytes ; self / #{1024**j*8} ; end
74
+ alias_method :#{m}bits, :#{m}bit
75
+ alias_method :#{m}bytes, :#{m}byte
76
+ }
77
+ end
78
+
79
+ [ 'kibi', 'mebi', 'gibi', 'tebi', 'pebi', 'exbi' ].each_with_index do |m, i|
80
+ j = i + 1
81
+ class_eval %{
82
+ def #{m}bit ; self * #{1024**j} ; end
83
+ def #{m}byte ; self * #{1024**j*8} ; end
84
+ def in_#{m}bits ; self / #{1024**j} ; end
85
+ def in_#{m}bytes ; self / #{1024**j*8} ; end
86
+ alias_method :#{m}bits, :#{m}bit
87
+ alias_method :#{m}bytes, :#{m}byte
88
+ }
89
+ end
90
+
91
+ # Formated string of bits proportial to size.
92
+ #
93
+ # 1024.bits_to_s #=> "1.00 kb"
94
+ # 1048576.bits_to_s #=> "1.00 mb"
95
+ # 1073741824.bits_to_s #=> "1.00 gb"
96
+ # 1099511627776.bits_to_s #=> "1.00 tb"
97
+ #
98
+ # Takes a format string to adjust output.
99
+ #
100
+ # 1024.bits_to_s('%.0f') #=> "1 kb"
101
+ #
102
+ def strfbits(fmt='%.2f')
103
+ case
104
+ when self < 1024
105
+ "#{self} bits"
106
+ when self < 1024**2
107
+ "#{fmt % (self.to_f / 1024)} kb"
108
+ when self < 1024**3
109
+ "#{fmt % (self.to_f / 1024**2)} mb"
110
+ when self < 1024**4
111
+ "#{fmt % (self.to_f / 1024**3)} gb"
112
+ when self < 1024**5
113
+ "#{fmt % (self.to_f / 1024**4)} tb"
114
+ else
115
+ "#{self} bits"
116
+ end
117
+ end
118
+
119
+ # Formated string of bytes proportial to size.
120
+ #
121
+ # 1024.bytes_to_s #=> "1.00 KB"
122
+ # 1048576.bytes_to_s #=> "1.00 MB"
123
+ # 1073741824.bytes_to_s #=> "1.00 GB"
124
+ # 1099511627776.bytes_to_s #=> "1.00 TB"
125
+ #
126
+ # Takes a format string to adjust output.
127
+ #
128
+ # 1024.bytes_to_s('%.0f') #=> "1 KB"
129
+ #
130
+ def strfbytes(fmt='%.2f')
131
+ case
132
+ when self < 1024
133
+ "#{self} bytes"
134
+ when self < 1024**2
135
+ "#{fmt % (self.to_f / 1024)} KB"
136
+ when self < 1024**3
137
+ "#{fmt % (self.to_f / 1024**2)} MB"
138
+ when self < 1024**4
139
+ "#{fmt % (self.to_f / 1024**3)} GB"
140
+ when self < 1024**5
141
+ "#{fmt % (self.to_f / 1024**4)} TB"
142
+ else
143
+ "#{self} bytes"
144
+ end
145
+ end
146
+
147
+ # deprecated
148
+ alias_method :octet_units, :strfbytes
149
+
150
+ end
151
+
152
+
153
+
154
+ # _____ _
155
+ # |_ _|__ ___| |_
156
+ # | |/ _ \/ __| __|
157
+ # | | __/\__ \ |_
158
+ # |_|\___||___/\__|
159
+ #
160
+
161
+ =begin testing
162
+
163
+ require 'test/unit'
164
+
165
+ class TC_Numeric < Test::Unit::TestCase
166
+
167
+ # bits
168
+
169
+ def test_bits
170
+ assert_equal( 8, 8.bits )
171
+ end
172
+
173
+ def test_kilobits
174
+ assert_equal( 1024**1, 1.kilobit )
175
+ end
176
+
177
+ def test_megabits
178
+ assert_equal( 1024**2, 1.megabit )
179
+ end
180
+
181
+ def test_gigabits
182
+ assert_equal( 1024**3, 1.gigabit )
183
+ end
184
+
185
+ def test_terabits
186
+ assert_equal( 1024**4, 1.terabit )
187
+ end
188
+
189
+ # bytes
190
+
191
+ def test_bytes
192
+ assert_equal( 8192, 1024.bytes )
193
+ end
194
+
195
+ def test_kilobytes
196
+ assert_equal( 1024**1*8, 1.kilobyte )
197
+ end
198
+
199
+ def test_megabytes
200
+ assert_equal( 1024**2*8, 1.megabyte )
201
+ end
202
+
203
+ def test_gigabytes
204
+ assert_equal( 1024**3*8, 1.gigabyte )
205
+ end
206
+
207
+ def test_terabytes
208
+ assert_equal( 1024**4*8, 1.terabyte )
209
+ end
210
+
211
+ # bits_to_s
212
+
213
+ def test_strfbits
214
+ assert_equal( "1.00 kb", 1024.strfbits )
215
+ assert_equal( "1.00 mb", 1048576.strfbits )
216
+ assert_equal( "1.00 gb", 1073741824.strfbits )
217
+ assert_equal( "1.00 tb", 1099511627776.strfbits )
218
+ end
219
+
220
+ # bytes_to_s
221
+
222
+ def test_strfbytes
223
+ assert_equal( "1.00 KB", 1024.strfbytes )
224
+ assert_equal( "1.00 MB", 1048576.strfbytes )
225
+ assert_equal( "1.00 GB", 1073741824.strfbytes )
226
+ assert_equal( "1.00 TB", 1099511627776.strfbytes )
227
+ end
228
+
229
+ end
230
+
231
+ =end