summaryrefslogtreecommitdiff
path: root/macros/luatex/latex/lua-physical/test/testDimension.lua
blob: 44d2840322eb8752fc1c500d2e5e801a598d0c16 (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
--[[
This file contains the unit tests for the physical.Dimension class.

Copyright (c) 2020 Thomas Jenni

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 D = physical.Dimension

local L = D("L")
local M = D("M")
local T = D("T")
local I = D("I")
local K = D("K")
local N = D("N")
local J = D("J")


TestDimension = {} --class


-- Dimension.new(o=nil)
function TestDimension:testEmptyConstructor()
   local d = D()
   lu.assertTrue( d:iszero() )
end

function TestDimension:testConstructorByString()
   local d = D("Dimensionless")
   lu.assertTrue( d:iszero() )
end

function TestDimension:testCopyConstructor()
   local d1, d2

   d1 = D("Energy")
   lu.assertEquals( d1, M*L^2/T^2 )

   d2 = D(d1)
   lu.assertEquals( d2, M*L^2/T^2 )
end




function TestDimension:testDefine()
   local i1 = D("Force")^4
   local i2 = D.define("Insanity",i1)
   local i3 = D("Insanity")
   lu.assertEquals( i1, L^4 * M^4 * T^-8)
   lu.assertEquals( i2, L^4 * M^4 * T^-8)
   lu.assertEquals( i3, L^4 * M^4 * T^-8)
end

function TestDimension:testToString()
   local d = D("Force")
   lu.assertEquals( tostring(d), "[Force]" )

   local d = D(L^-1 * T * I^2 * K^3 * N^4 * J^5)
   lu.assertEquals( tostring(d), "[L]^-1 [T] [I]^2 [K]^3 [N]^4 [J]^5" )
end

function TestDimension:testEqual()
   local d1 = D("Energy")
   local d2 = D("Force")
   local d3 = D("Torque")

   lu.assertEquals( d1==d2, false)
   lu.assertEquals( d1==d3, true)
end

function TestDimension:testMultiply()
   local d1 = D("Force")
   local d2 = D("Energy")

   local d3 = d1 * d2
   local d4 = d2 * d1

   lu.assertEquals( d3, L^3 * M^2 * T^-4)
   lu.assertEquals( d3, d4)
end

function TestDimension:testDivide()
   local d1 = D("Force")
   local d2 = D("Energy")

   lu.assertEquals( d1, {1,1,-2,0,0,0,0,0,0})
   lu.assertEquals( d2, {2,1,-2,0,0,0,0,0,0})
   
   local d3 = d1 / d2
   local d4 = d2 / d1

   lu.assertEquals( d3, {-1,0,0,0,0,0,0,0,0})
   lu.assertNotEquals( d3, d4)
end

function TestDimension:testPow()
   local d = D("Length")^3
   lu.assertEquals( d, L^3)
end

function TestDimension:isequal()
   local d = D("Length")^3
   lu.assertTrue( d == L^3)

   local d = D("Force")
   lu.assertTrue( d == L * M / T^2)
end


-- Test Dimension Definitions

function TestDimension:testLength()
   lu.assertEquals(D("Length"), L)
   lu.assertEquals(D("L"), L)
end

function TestDimension:testMass()
   lu.assertEquals(D("Mass"), M)
   lu.assertEquals(D("M"), M)
end

function TestDimension:testTime()
   lu.assertEquals(D("Time"), T)
   lu.assertEquals(D("T"), T)
end

function TestDimension:testCreateByNameForce()
   lu.assertTrue( D("Force") == M*L/T^2 )
end

function TestDimension:testArea()
   lu.assertEquals(D("Area"), L^2)
end

function TestDimension:testVolume()
   lu.assertEquals(D("Volume"), L^3)
end

function TestDimension:testFrequency()
   lu.assertEquals(D("Frequency"), T^-1)
end

function TestDimension:testFrequency()
   lu.assertEquals(D("Frequency"), T^-1)
end

function TestDimension:testDensity()
   lu.assertEquals(D("Density"), M / D("Volume"))
end

function TestDimension:testVelocity()
   lu.assertEquals(D("Velocity"), L / T)
end

function TestDimension:testAcceleration()
   lu.assertEquals(D("Acceleration"), D("Velocity") / T)
end

function TestDimension:testForce()
   lu.assertEquals(D("Force"), M * D("Acceleration"))
end

function TestDimension:testEnergy()
   lu.assertEquals(D("Energy"),  D("Force") * L)
end

function TestDimension:testPower()
   lu.assertEquals(D("Power"), D("Energy") / T)
end

function TestDimension:testPower()
   lu.assertEquals(D("Power"), D("Energy") / T)
end

function TestDimension:testTorque()
   lu.assertEquals(D("Energy"), D("Torque"))
end

function TestDimension:testTorque()
   lu.assertEquals(D("Pressure"), D("Force") / D("Area"))
end

function TestDimension:testImpulse()
   lu.assertEquals(D("Impulse"), M * D("Velocity"))
end

function TestDimension:testSpecificAbsorbedDose()
   lu.assertEquals(D("Absorbed Dose"), D("Energy") / M)
end

function TestDimension:testHeatCapacity()
   lu.assertEquals(D("Heat Capacity"), D("Energy") / K)
end

function TestDimension:testSpecificHeatCapacity()
   lu.assertEquals(D("Specific Heat Capacity"), D("Energy") / (M * K) )
end

function TestDimension:testAngularMomentum()
   lu.assertEquals(D("Angular Momentum"), L * D("Impulse") )
end

function TestDimension:testAngularMomentofInertia()
   lu.assertEquals(D("Moment of Inertia"), D("Torque") * T^2 )
end

function TestDimension:testEntropy()
   lu.assertEquals(D("Entropy"), D("Energy") / K )
end

function TestDimension:testThermalConductivity()
   lu.assertEquals(D("Thermal Conductivity"), D("Power") / (L*K) )
end

function TestDimension:testElectricCharge()
   lu.assertEquals(D("Electric Charge"), D("Electric Current") * T )
end

function TestDimension:testElectricPermittivity()
   lu.assertEquals(D("Electric Permittivity"), D("Electric Charge")^2 / ( D("Force") * D("Area") ) )
end

function TestDimension:testElectricFieldStrength()
   lu.assertEquals(D("Electric Field Strength"), D("Force") / D("Electric Charge") )
end

function TestDimension:testElectricPotential()
   lu.assertEquals(D("Electric Potential"), D("Energy") / D("Electric Charge") )
end

function TestDimension:testElectricResistance()
   lu.assertEquals(D("Electric Resistance"), D("Electric Potential") / D("Electric Current") )
end

function TestDimension:testElectricConductance()
   lu.assertEquals(D("Electric Conductance"), 1 / D("Electric Resistance") )
end

function TestDimension:testElectricCapacitance()
   lu.assertEquals(D("Electric Capacitance"), D("Electric Charge") / D("Electric Potential")  )
end

function TestDimension:testElectricInductance()
   lu.assertEquals(D("Inductance"), D("Electric Potential") * T / D("Electric Current")  )
end

function TestDimension:testMagneticFluxDensity()
   lu.assertEquals(D("Magnetic Flux Density"), D("Force") / (D("Electric Charge") * D("Velocity"))  )
end

function TestDimension:testMagneticFlux()
   lu.assertEquals(D("Magnetic Flux"), D("Magnetic Flux Density") * D("Area")  )
end

function TestDimension:testMagneticPermeability()
   lu.assertEquals(D("Magnetic Permeability"), D("Magnetic Flux Density") * L / D("Electric Current")  )
end

function TestDimension:testMagneticFieldStrength()
   lu.assertEquals(D("Magnetic Field Strength"), D("Magnetic Flux Density") / D("Magnetic Permeability")  )
end

function TestDimension:testIntensity()
   lu.assertEquals(D("Intensity"), D("Power") / D("Area")  )
end

function TestDimension:testReactionRate()
   lu.assertEquals(D("Reaction Rate"), N / (T * D("Volume"))  )
end

function TestDimension:testCatalyticActivity()
   lu.assertEquals(D("Catalytic Activity"), N / T  )
end

function TestDimension:testChemicalPotential()
   lu.assertEquals(D("Chemical Potential"), D("Energy") / N  )
end

function TestDimension:testMolarConcentration()
   lu.assertEquals(D("Molar Concentration"), N / D("Volume")  )
end

function TestDimension:testMolarHeatCapacity()
   lu.assertEquals(D("Molar Heat Capacity"), D("Energy") / (K * N)  )
end

function TestDimension:testIlluminance()
   lu.assertEquals(D("Illuminance"), J / D("Area")  )
end

return TestDimension