summaryrefslogtreecommitdiff
path: root/macros/luatex/latex/lua-physical/test/testData.lua
blob: 9df3c0e1c94b471d852f53f4a1fc4fd049c53cb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
--[[
This file contains the unit tests for the physical.Data object.

Copyright (c) 2017 Thomas Jenni (tjenni@me.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]--

local lu = require("luaunit")

package.path = "../?.lua;" .. package.path

local physical = require("physical")

local Data = physical.Data
local D = physical.Dimension
local Q = physical.Quantitiy
local N = physical.Number

local N = physical.Number
N.compact = true

function dump(o)
   if type(o) == 'table' then
      local s = '{ '
      for k,v in pairs(o) do
         if type(k) ~= 'number' then k = '"'..k..'"' end
         if getmetatable(v) == physical.Unit then
            s = s .. '['..k..'] = ' .. tostring(v) .. ','
         else
           s = s .. '['..k..'] = ' .. dump(v) .. ','
         end
      end
      return s .. '}\n'
   else
      return tostring(o)
   end
end


local function contains(table, val)
   for i=1,#table do
      if table[i] == val then 
         return true
      end
   end
   return false
end

local function count(table)
   local n = 0
   for k,v in pairs(table) do 
      n = n + 1
   end
   return n
end


TestData = {}


-- Test astronomical data

function TestData:testAstronomicalBodies()
   local bodies = Data.Astronomical()
   lu.assertTrue( contains(bodies,"Sun") )
   lu.assertTrue( contains(bodies,"Earth") )
   lu.assertTrue( contains(bodies,"Ceres") )
end

function TestData:testAstronomicalKeys()
   local keys = Data.Astronomical("Earth")

   lu.assertTrue( keys["Mass"] ~= nil )
   lu.assertTrue( keys["EquatorialRadius"] ~= nil )
end

function TestData:testSun()
   local m_s = N(1.9884e30, 2e26) * _kg
   lu.assertEquals( tostring(Data.Astronomical("Sun","Mass")), tostring(m_s) )
end

function TestData:testMercury()
   local m = (N(2.2031870799e13, 8.6e5) * _m^3 * _s^(-2)  / _Gc ):to()
   
   lu.assertEquals( tostring(Data.Astronomical("Mercury","Mass")), tostring(m) )
end

function TestData:testVenus()
   local m = Data.Astronomical("Sun","Mass") / N(4.08523719e5,8e-3)
   lu.assertEquals( tostring(Data.Astronomical("Venus","Mass")), tostring(m) )
end

function TestData:testEarth()
   local m = N("5.97220(60)e24") * _kg
   lu.assertEquals( tostring(Data.Astronomical("Earth","Mass")), tostring(m) )
end

function TestData:testMoon()
   local m = Data.Astronomical("Earth","Mass") * N(1.23000371e-2,4e-10)
   lu.assertEquals( tostring(Data.Astronomical("Moon","Mass")), tostring(m) )
end

function TestData:testMars()
   local m = Data.Astronomical("Sun","Mass") / N(3.09870359e6,2e-2)

   lu.assertEquals( tostring(Data.Astronomical("Mars","Mass")), tostring(m) )
end

function TestData:testJupiter()
   local m = Data.Astronomical("Sun","Mass") / N(1.047348644e3,1.7e-5)

   lu.assertEquals(tostring(m), tostring(Data.Astronomical("Jupiter","Mass")) )
end

function TestData:testSaturn()
   local m = Data.Astronomical("Sun","Mass") / N(3.4979018e3,1e-4)

   lu.assertEquals(tostring(m), tostring(Data.Astronomical("Saturn","Mass")) )
end

function TestData:testUranus()
   local m = Data.Astronomical("Sun","Mass") / N(2.290298e4,3e-2)

   lu.assertEquals(tostring(m), tostring(Data.Astronomical("Uranus","Mass")) )
end

function TestData:testNeptune()
   local m = Data.Astronomical("Sun","Mass") / N(1.941226e4,3e-2)

   lu.assertEquals(tostring(m), tostring(Data.Astronomical("Neptune","Mass")) )
end

function TestData:testPluto()
   local m = Data.Astronomical("Sun","Mass") / N(1.36566e8,2.8e4)

   lu.assertEquals(tostring(m), tostring(Data.Astronomical("Pluto","Mass")) )
end

function TestData:testEris()
   local m = Data.Astronomical("Sun","Mass") / N(1.191e8,1.4e6)
   lu.assertEquals(tostring(m), tostring(Data.Astronomical("Eris","Mass")) )
end

function TestData:testCeres()
   local m = N(4.72e-10,3e-12) * Data.Astronomical("Sun","Mass")
   lu.assertEquals(tostring(m), tostring(Data.Astronomical("Ceres","Mass")) )
end

function TestData:testPallas()
   local m = N(1.03e-10,3e-12) * Data.Astronomical("Sun","Mass")

   lu.assertEquals(tostring(m), tostring(Data.Astronomical("Pallas","Mass")) )
end

function TestData:testVesta()
   local m = N(1.35e-10,3e-12) * Data.Astronomical("Sun","Mass")

   lu.assertEquals(tostring(m), tostring(Data.Astronomical("Vesta","Mass")) )
end



-- Test isotope data
function TestQuantity.isotopeGetByAZError()
   local Z = Data.Isotope({56,55},"Z")
end
function TestData:testIsotopeGetByAZ()
   lu.assertEquals( Data.Isotope({4,2},"A"), 4 )
   lu.assertEquals( Data.Isotope({4,2},"Z"), 2 )

   lu.assertTrue( (28667 * _keV):isclose(Data.Isotope({3,3},"MassExcess"),1e-9) )

   lu.assertEquals( Data.Isotope({56,25},"A"), 56 )
   lu.assertEquals( Data.Isotope({56,25},"Z"), 25 )
  
   lu.assertTrue( (-2267 * _keV):isclose(Data.Isotope({3,3},"BindingEnergyPerNucleon"),1e-9) )

   lu.assertError( TestQuantity.isotopeGetByAZError )
end

function TestData:testIsotopeGetAllKeys()
   local row = Data.Isotope({4,2})
   lu.assertTrue( row["MassExcess"] ~= nil )
end


function TestData:testIsotopeGetByIsotopeName()
   lu.assertEquals( Data.Isotope("Helium-5","A"), 5 )

   
   lu.assertEquals( Data.Isotope("Helium-5","Z"), 2 )
   
   lu.assertEquals( Data.Isotope("Lithium5","A"), 5 )
   lu.assertEquals( Data.Isotope("Lithium5","Z"), 3 )

   lu.assertEquals( Data.Isotope("5He","A"), 5 )
   lu.assertEquals( Data.Isotope("5He","Z"), 2 )
end

function TestData:testIsotopeGetByElementName()
   local list = Data.Isotope("Fe","symbol")

   lu.assertEquals( count(list), 31 )
   lu.assertTrue( list["47Fe"] ~= nil )
   lu.assertTrue( list["56Fe"] ~= nil )
   lu.assertTrue( list["66Fe"] ~= nil )

   --print(dump(Data.Isotope("He",{"DecayModes","DecayModesIntensity"})))
end

function TestData:testIsotopeGetAll()
   local isotopes = Data.Isotope()

   lu.assertTrue( contains(isotopes,"6He") )
   lu.assertTrue( contains(isotopes,"55K") )
   lu.assertTrue( contains(isotopes,"209Rn") )
   lu.assertTrue( contains(isotopes,"295Ei") )
end


return TestData