constants 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +21 -0
- data/README +125 -0
- data/Rakefile +171 -0
- data/lib/constants.rb +5 -0
- data/lib/constants/constant.rb +94 -0
- data/lib/constants/constant_library.rb +299 -0
- data/lib/constants/libraries/element.rb +214 -0
- data/lib/constants/libraries/particle.rb +83 -0
- data/lib/constants/libraries/physical.rb +297 -0
- data/lib/constants/library.rb +133 -0
- data/lib/constants/stash.rb +94 -0
- data/lib/constants/uncertainty.rb +8 -0
- data/test/constants/constant_library_test.rb +304 -0
- data/test/constants/constant_test.rb +77 -0
- data/test/constants/libraries/element_test.rb +207 -0
- data/test/constants/libraries/particle_test.rb +43 -0
- data/test/constants/libraries/physical_test.rb +32 -0
- data/test/constants/library_test.rb +125 -0
- data/test/constants/stash_test.rb +99 -0
- data/test/constants_test_helper.rb +51 -0
- data/test/constants_test_suite.rb +3 -0
- data/test/readme_doc_test.rb +58 -0
- metadata +84 -0
@@ -0,0 +1,77 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '../constants_test_helper.rb')
|
2
|
+
require 'constants/constant'
|
3
|
+
|
4
|
+
class ConstantTest < Test::Unit::TestCase
|
5
|
+
include Constants
|
6
|
+
|
7
|
+
#
|
8
|
+
# parse test
|
9
|
+
#
|
10
|
+
|
11
|
+
def test_parse_documentation
|
12
|
+
assert_equal [1.0, 0.2], Constant.parse("1.0(2)").to_a
|
13
|
+
assert_equal [1.0078250321, 1/2500000000], Constant.parse("1.007 825 032 1(4)").to_a
|
14
|
+
assert_equal [6.62606896, nil], Constant.parse("6.626 068 96").to_a
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_parse
|
18
|
+
assert_equal [1.0, nil], Constant.parse("1.0").to_a
|
19
|
+
assert_equal [1.0, 0], Constant.parse("1.0(0)").to_a
|
20
|
+
assert_equal [1.0, 0.1], Constant.parse("1.0(1)").to_a
|
21
|
+
assert_equal [100, 1], Constant.parse("100(1)").to_a
|
22
|
+
assert_equal [100, 11], Constant.parse("100(11)").to_a
|
23
|
+
assert_equal [1234.54789, 0.00011], Constant.parse("1234.54789(11)").to_a
|
24
|
+
assert_equal [1234.54789, 0.00011], Constant.parse("1234.547 89 (11)").to_a
|
25
|
+
assert_equal [1000, 100], Constant.parse("1.0(1)e3").to_a
|
26
|
+
assert_equal [0.001, 0.0001], Constant.parse("1.0(1)e-3").to_a
|
27
|
+
end
|
28
|
+
|
29
|
+
#
|
30
|
+
# == test
|
31
|
+
#
|
32
|
+
|
33
|
+
def test_equal_compares_Numerics_with_value
|
34
|
+
c = Constant.new(1.23)
|
35
|
+
assert c == 1.23
|
36
|
+
assert c != 1.24
|
37
|
+
|
38
|
+
assert_equal 1.23, c
|
39
|
+
assert_not_equal 1.24, c
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_equal_compares_non_Numerics_directly
|
43
|
+
c1 = Constant.new(1.23)
|
44
|
+
c2 = Constant.new(1.23)
|
45
|
+
c3 = Constant.new(1.24)
|
46
|
+
|
47
|
+
assert c1 == c2
|
48
|
+
assert c1 != c3
|
49
|
+
end
|
50
|
+
|
51
|
+
#
|
52
|
+
# <=> test
|
53
|
+
#
|
54
|
+
|
55
|
+
def test_compare_compares_on_value
|
56
|
+
c1 = Constant.new(1.23)
|
57
|
+
c2 = Constant.new(1.23)
|
58
|
+
c3 = Constant.new(1.24)
|
59
|
+
|
60
|
+
assert_equal 0, c1 <=> c2
|
61
|
+
assert_equal -1, c1 <=> c3
|
62
|
+
assert_equal 1, c3 <=> c1
|
63
|
+
end
|
64
|
+
|
65
|
+
#
|
66
|
+
# to_a test
|
67
|
+
#
|
68
|
+
|
69
|
+
def test_to_a_returns_value_uncertainty_array
|
70
|
+
c = Constant.new(1.23)
|
71
|
+
assert_equal [1.23, nil], c.to_a
|
72
|
+
|
73
|
+
c = Constant.new(1.23, nil, 0.03)
|
74
|
+
assert_equal [1.23, 0.03], c.to_a
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
@@ -0,0 +1,207 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '../../constants_test_helper.rb')
|
2
|
+
require 'constants/libraries/element'
|
3
|
+
|
4
|
+
class ElementTest < Test::Unit::TestCase
|
5
|
+
include Constants::Libraries
|
6
|
+
|
7
|
+
#
|
8
|
+
# documentation test
|
9
|
+
#
|
10
|
+
|
11
|
+
def test_documentation
|
12
|
+
e = Element::He
|
13
|
+
assert_equal "Helium", e.name
|
14
|
+
assert_equal "He", e.symbol
|
15
|
+
assert_equal 2, e.atomic_number
|
16
|
+
assert_equal 4.0026032497, e.mass
|
17
|
+
assert_equal [3, 4], e.isotopes
|
18
|
+
assert_equal [0.000137, 99.999863], e.abundances
|
19
|
+
end
|
20
|
+
|
21
|
+
#
|
22
|
+
# initialize test
|
23
|
+
#
|
24
|
+
|
25
|
+
def test_initialize
|
26
|
+
c = Element::C
|
27
|
+
|
28
|
+
assert_equal "C", c.symbol
|
29
|
+
assert_equal "Carbon", c.name
|
30
|
+
assert_equal 6, c.atomic_number
|
31
|
+
assert_equal [12, 13], c.isotopes
|
32
|
+
assert_equal [[12.0, 0], [13.0033548378, 0.000000001]], c.masses.collect {|m| m.to_a}
|
33
|
+
assert_equal [[98.93, 0.08], [1.07, 0.08]], c.abundances.collect {|m| m.to_a}
|
34
|
+
assert_equal 0, c.index_max_abundance
|
35
|
+
end
|
36
|
+
|
37
|
+
#
|
38
|
+
# lookup test
|
39
|
+
#
|
40
|
+
|
41
|
+
def test_lookup
|
42
|
+
c = Element::C
|
43
|
+
assert_equal c, Element['C']
|
44
|
+
assert_equal c, Element['Carbon']
|
45
|
+
assert_equal c, Element[6]
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_lookup_is_nil_for_undefined_elements
|
49
|
+
assert_nil Element['Q']
|
50
|
+
assert_nil Element["Madeupium"]
|
51
|
+
assert_nil Element[102]
|
52
|
+
end
|
53
|
+
|
54
|
+
#
|
55
|
+
# methods test
|
56
|
+
#
|
57
|
+
|
58
|
+
def test_has_isotope?
|
59
|
+
assert Element::C.has_isotope?(12)
|
60
|
+
assert Element::C.has_isotope?(13)
|
61
|
+
assert !Element::C.has_isotope?(8)
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_index_isotope
|
65
|
+
assert_equal 0, Element::C.index_isotope(12)
|
66
|
+
assert_equal 1, Element::C.index_isotope(13)
|
67
|
+
assert_equal nil, Element::C.index_isotope(8)
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_mass
|
71
|
+
assert_equal 12, Element::C.mass
|
72
|
+
assert_equal 12, Element::C.mass(12)
|
73
|
+
assert_equal 13.0033548378, Element::C.mass(13)
|
74
|
+
assert_equal nil, Element::C.mass(8)
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_abundance
|
78
|
+
assert_equal 98.93, Element::C.abundance
|
79
|
+
assert_equal 98.93, Element::C.abundance(12)
|
80
|
+
assert_equal 1.07, Element::C.abundance(13)
|
81
|
+
assert_equal nil, Element::C.abundance(8)
|
82
|
+
end
|
83
|
+
|
84
|
+
# vs the Proteome Commons Atom Reference, 2008-01-11
|
85
|
+
# http://www.proteomecommons.org/archive/1129086318745/docs/atom-reference.html
|
86
|
+
#
|
87
|
+
# The website states 'These values are taken from the NIST's list, http://physics.nist.gov'
|
88
|
+
def test_mass_values_vs_proteome_commons
|
89
|
+
str = %Q{
|
90
|
+
H 1.0078250321 0.999885
|
91
|
+
H2 2.014101778 1.15E-4
|
92
|
+
O 15.9949146221 0.9975700000000001
|
93
|
+
O17 16.9991315 3.7999999999999997E-4
|
94
|
+
O18 17.9991604 0.0020499999999999997
|
95
|
+
N14 14.0030740052 0.9963200000000001
|
96
|
+
N15 15.0001088984 0.00368
|
97
|
+
C12 12.0 0.9893000000000001
|
98
|
+
C13 13.0033548378 0.010700000000000001
|
99
|
+
P31 30.97376151 1.0
|
100
|
+
S32 31.97207069 0.9493
|
101
|
+
S33 32.9714585 0.0076
|
102
|
+
S34 33.96786683 0.0429
|
103
|
+
S36 35.96708088 2.0E-4}
|
104
|
+
|
105
|
+
atoms = str.split(/\n/)
|
106
|
+
atoms.each do |atom_str|
|
107
|
+
next if atom_str.empty?
|
108
|
+
|
109
|
+
name, mass, abundance = atom_str.split(/\s+/)
|
110
|
+
name =~ /(\w)(\d*)/
|
111
|
+
symbol = $1
|
112
|
+
isotope = $2.empty? ? nil : $2.to_i
|
113
|
+
mass = mass.to_f
|
114
|
+
abundance = abundance.to_f * 100
|
115
|
+
|
116
|
+
element = Element[symbol]
|
117
|
+
assert_not_nil element, atom_str
|
118
|
+
assert element.has_isotope?(isotope), atom_str unless isotope == nil
|
119
|
+
|
120
|
+
assert_in_delta mass, element.mass(isotope), delta_mass, atom_str
|
121
|
+
assert_in_delta abundance, element.abundance(isotope), delta_abundance, atom_str
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
# vs the Unimod Symbols and Mass Values, 2008-01-11
|
126
|
+
# http://www.unimod.org/masses.html
|
127
|
+
#
|
128
|
+
# The website states 'All mass values in Unimod are calculated
|
129
|
+
# from the IUPAC atomic weights and isotopic abundances
|
130
|
+
# tabulated by WebElements'
|
131
|
+
#
|
132
|
+
def test_mass_values_vs_unimod
|
133
|
+
str = %Q{
|
134
|
+
H Hydrogen 1.007825035 1.00794
|
135
|
+
2H Deuterium 2.014101779 2.014101779
|
136
|
+
Li Lithium 7.016003 6.941
|
137
|
+
C Carbon 12 12.0107
|
138
|
+
13C Carbon13 13.00335483 13.00335483
|
139
|
+
N Nitrogen 14.003074 14.0067
|
140
|
+
15N Nitrogen15 15.00010897 15.00010897
|
141
|
+
O Oxygen 15.99491463 15.9994
|
142
|
+
18O Oxygen18 17.9991603 17.9991603
|
143
|
+
F Fluorine 18.99840322 18.9984032
|
144
|
+
Na Sodium 22.9897677 22.98977
|
145
|
+
P Phosphorous 30.973762 30.973761
|
146
|
+
S Sulfur 31.9720707 32.065
|
147
|
+
Cl Chlorine 34.96885272 35.453
|
148
|
+
K Potassium 38.9637074 39.0983
|
149
|
+
Ca Calcium 39.9625906 40.078
|
150
|
+
Fe Iron 55.9349393 55.845
|
151
|
+
Ni Nickel 57.9353462 58.6934
|
152
|
+
Cu Copper 62.9295989 63.546
|
153
|
+
Zn Zinc 63.9291448 65.409
|
154
|
+
Br Bromine 78.9183361 79.904
|
155
|
+
Se Selenium 79.9165196 78.96
|
156
|
+
Mo Molybdenum 97.9054073 95.94
|
157
|
+
Ag Silver 106.905092 107.8682
|
158
|
+
I Iodine 126.904473 126.90447
|
159
|
+
Au Gold 196.966543 196.96655
|
160
|
+
Hg Mercury 201.970617 200.59}
|
161
|
+
|
162
|
+
atoms = str.split(/\n/)
|
163
|
+
atoms.each do |atom_str|
|
164
|
+
next if atom_str.empty?
|
165
|
+
|
166
|
+
symbol, name, monoisotopic, average = atom_str.split(/\s+/)
|
167
|
+
symbol =~ /(\d*)(\w+)/
|
168
|
+
isotope = $1.empty? ? nil : $1.to_i
|
169
|
+
symbol = $2
|
170
|
+
monoisotopic = monoisotopic.to_f
|
171
|
+
average = average.to_f
|
172
|
+
|
173
|
+
element = Element[symbol]
|
174
|
+
assert_not_nil element, atom_str
|
175
|
+
assert element.has_isotope?(isotope), atom_str unless isotope == nil
|
176
|
+
|
177
|
+
assert_in_delta monoisotopic, element.mass(isotope), delta_mass, atom_str
|
178
|
+
# TODO -- check average mass
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
# vs the VG Analytical Organic Mass Spectrometry reference, reference date unknown (prior to 2005)
|
183
|
+
# the data from the data sheet was copied manually to doc/VG Analytical DataSheet.txt
|
184
|
+
def test_mass_values_vs_vg_analytical
|
185
|
+
str = %Q{
|
186
|
+
H 1.0078250 1.00794
|
187
|
+
C 12 12.011
|
188
|
+
N 14.0030740 14.0067
|
189
|
+
O 15.9949146 15.9994
|
190
|
+
S 31.9720718 32.06}
|
191
|
+
|
192
|
+
atoms = str.split(/\n/)
|
193
|
+
atoms.each do |atom_str|
|
194
|
+
next if atom_str.empty?
|
195
|
+
|
196
|
+
symbol, monoisotopic, average = atom_str.split(/\s+/)
|
197
|
+
monoisotopic = monoisotopic.to_f
|
198
|
+
average = average.to_f
|
199
|
+
|
200
|
+
element = Element[symbol]
|
201
|
+
assert_not_nil element, atom_str
|
202
|
+
assert_in_delta monoisotopic, element.mass, delta_mass, atom_str
|
203
|
+
# TODO -- check average mass
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '../../constants_test_helper.rb')
|
2
|
+
require 'constants/libraries/particle'
|
3
|
+
|
4
|
+
class ParticleTest < Test::Unit::TestCase
|
5
|
+
include Constants::Libraries
|
6
|
+
|
7
|
+
#
|
8
|
+
# initialize test
|
9
|
+
#
|
10
|
+
|
11
|
+
def test_initialize
|
12
|
+
c = Particle::CHARM
|
13
|
+
|
14
|
+
assert_equal "Charm", c.name
|
15
|
+
assert_equal "Fermion", c.family
|
16
|
+
assert_equal "Quark", c.group
|
17
|
+
assert_equal "Second", c.generation
|
18
|
+
assert_equal 2.0/3, c.charge
|
19
|
+
assert_equal 0.5, c.spin
|
20
|
+
|
21
|
+
t = Particle::ANTITAU
|
22
|
+
|
23
|
+
assert_equal "Anti-Tau", t.name
|
24
|
+
assert_equal "Fermion", t.family
|
25
|
+
assert_equal "Lepton", t.group
|
26
|
+
assert_equal "Third", t.generation
|
27
|
+
assert_equal -1, t.charge
|
28
|
+
assert_equal 0.5, t.spin
|
29
|
+
end
|
30
|
+
|
31
|
+
#
|
32
|
+
# lookup test
|
33
|
+
#
|
34
|
+
|
35
|
+
def test_lookup
|
36
|
+
c = Particle::CHARM
|
37
|
+
assert_equal c, Particle['Charm']
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_lookup_is_nil_for_undefined_particles
|
41
|
+
assert_nil Particle['Blop']
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '../../constants_test_helper.rb')
|
2
|
+
require 'constants/libraries/physical'
|
3
|
+
|
4
|
+
class PhysicalTest < Test::Unit::TestCase
|
5
|
+
include Constants::Libraries
|
6
|
+
|
7
|
+
#
|
8
|
+
# initialize test
|
9
|
+
#
|
10
|
+
|
11
|
+
def test_initialize
|
12
|
+
c = Physical::SPEED_OF_LIGHT_IN_VACUUM
|
13
|
+
|
14
|
+
assert_equal "speed of light in vacuum", c.name
|
15
|
+
assert_equal 299792458, c.value
|
16
|
+
assert_equal 0, c.uncertainty
|
17
|
+
assert_equal Unit.new("m/s"), c.unit
|
18
|
+
end
|
19
|
+
|
20
|
+
#
|
21
|
+
# lookup test
|
22
|
+
#
|
23
|
+
|
24
|
+
def test_lookup
|
25
|
+
c = Physical::SPEED_OF_LIGHT_IN_VACUUM
|
26
|
+
assert_equal c, Physical["speed of light in vacuum"]
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_lookup_is_nil_for_undefined_constants
|
30
|
+
assert_nil Physical["made up blah in a blah"]
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,125 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '../constants_test_helper.rb')
|
2
|
+
require 'constants/library'
|
3
|
+
|
4
|
+
class Constants::LibraryTest < Test::Unit::TestCase
|
5
|
+
include Constants
|
6
|
+
|
7
|
+
#
|
8
|
+
# documentation test
|
9
|
+
#
|
10
|
+
|
11
|
+
module Color
|
12
|
+
RED = 'red'
|
13
|
+
GREEN = 'green'
|
14
|
+
BLUE = 'blue'
|
15
|
+
GREY = 'grey'
|
16
|
+
|
17
|
+
include Constants::Library
|
18
|
+
library.index_by('name') {|c| c }
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_documentation
|
22
|
+
###
|
23
|
+
assert_equal({
|
24
|
+
'red' => Color::RED,
|
25
|
+
'blue' => Color::BLUE,
|
26
|
+
'green' => Color::GREEN,
|
27
|
+
'grey' => Color::GREY},
|
28
|
+
Color.index('name'))
|
29
|
+
|
30
|
+
assert_equal Color::RED, Color['red']
|
31
|
+
|
32
|
+
###
|
33
|
+
Color.library.index_by_attribute 'length'
|
34
|
+
const_ordered_assert_equal({
|
35
|
+
3 => Color::RED,
|
36
|
+
4 => [Color::BLUE, Color::GREY],
|
37
|
+
5 => Color::GREEN},
|
38
|
+
Color.index('length'))
|
39
|
+
|
40
|
+
const_ordered_assert_equal [Color::BLUE, Color::GREY], Color[4]
|
41
|
+
|
42
|
+
###
|
43
|
+
Color.library.collect('gstar') {|c| c =~ /^g/ ? c : nil }
|
44
|
+
const_ordered_assert_equal [Color::GREEN, Color::GREY], Color.collection('gstar')
|
45
|
+
|
46
|
+
Color.library.collect_attribute 'length'
|
47
|
+
const_ordered_assert_equal [3,5,4,4], Color.collection('length')
|
48
|
+
|
49
|
+
###
|
50
|
+
Color.library.add('yellow')
|
51
|
+
const_ordered_assert_equal({
|
52
|
+
3 => Color::RED,
|
53
|
+
4 => [Color::BLUE, Color::GREY],
|
54
|
+
5 => Color::GREEN,
|
55
|
+
6 => 'yellow'},
|
56
|
+
Color.index('length'))
|
57
|
+
|
58
|
+
Color.module_eval %Q{
|
59
|
+
ORANGE = 'orange'
|
60
|
+
reset_library
|
61
|
+
}
|
62
|
+
|
63
|
+
const_ordered_assert_equal({
|
64
|
+
3 => Color::RED,
|
65
|
+
4 => [Color::BLUE, Color::GREY],
|
66
|
+
5 => Color::GREEN,
|
67
|
+
6 => Color::ORANGE},
|
68
|
+
Color.index('length'))
|
69
|
+
end
|
70
|
+
|
71
|
+
#
|
72
|
+
# extend test
|
73
|
+
#
|
74
|
+
|
75
|
+
module ExtendModule
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_extend_initializes_library
|
79
|
+
ExtendModule.extend Library
|
80
|
+
assert ExtendModule.respond_to?(:library)
|
81
|
+
assert ExtendModule.library.kind_of?(ConstantLibrary)
|
82
|
+
end
|
83
|
+
|
84
|
+
#
|
85
|
+
# include test
|
86
|
+
#
|
87
|
+
|
88
|
+
module IncludeModule
|
89
|
+
include Constants::Library
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_include_initializes_library
|
93
|
+
assert IncludeModule.respond_to?(:library)
|
94
|
+
assert IncludeModule.library.kind_of?(ConstantLibrary)
|
95
|
+
end
|
96
|
+
|
97
|
+
#
|
98
|
+
# benchmark tests
|
99
|
+
#
|
100
|
+
|
101
|
+
module BenchmarkModule
|
102
|
+
include Constants::Library
|
103
|
+
A = 'A'
|
104
|
+
|
105
|
+
library.index_by("name") {|value| value}
|
106
|
+
reset_library
|
107
|
+
end
|
108
|
+
|
109
|
+
def test_access_speed
|
110
|
+
benchmark_test(24) do |x|
|
111
|
+
n = 100
|
112
|
+
x.report("#{n}k BenchmarkModule::A") do
|
113
|
+
(n*10**3).times { BenchmarkModule::A }
|
114
|
+
end
|
115
|
+
|
116
|
+
x.report("#{n}k ['A']") do
|
117
|
+
(n*10**3).times { BenchmarkModule['A'] }
|
118
|
+
end
|
119
|
+
|
120
|
+
x.report("#{n}k index('name')['A']") do
|
121
|
+
(n*10**3).times { BenchmarkModule.index('name')['A'] }
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|