-
Notifications
You must be signed in to change notification settings - Fork 0
/
combinationUnit.test.ts
223 lines (183 loc) · 10.6 KB
/
combinationUnit.test.ts
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
import { expect, describe, test } from "@jest/globals";
import { BaseSIUnit } from "./baseSIUnit";
import { AnyUnitPower, CombinationUnit } from "./combinationUnit"
import { NoneUnit } from "./noneUnit"
import { Unit } from './unit'
import SimpleUnit from "./simpleUnit";
import { UnitShape, UnitShapeMap } from "./unitShape";
function getPower(name: string): number {
let i = name.indexOf('^')
return Number.parseInt(name.slice(i+1));
}
function combinatoricUnits(a: Readonly<Record<string, Unit<UnitShapeMap>>>, b?: Readonly<Record<string, Unit<UnitShapeMap>>>): Record<string, CombinationUnit<AnyUnitPower[]>> {
let comboUnits: Record<string, CombinationUnit<AnyUnitPower[]>> = {};
Object.entries(a).forEach(([shape1, unit1]) => {
Object.entries(b ? b : a).forEach(([shape2, unit2]) => {
comboUnits[`${shape1}${shape2}`] = new CombinationUnit([[unit1, 1], [unit2, 1]])
comboUnits[`${shape1}over${shape2}`] = new CombinationUnit([[unit1, 1], [unit2, -1]])
})
});
return comboUnits
}
const nameSplitter = " * "
const abbrSplitter = " "
describe("Creating Base and Combination Units", () => {
const exampleUnitShapes: ['A', 'D', 'B'] = ['A', 'D', 'B']
const exampleBaseUnits = exampleUnitShapes.reduce(
(map: Record<string, BaseSIUnit<UnitShapeMap>>, shape: string, index:number) => {
map[shape] = new BaseSIUnit(
UnitShape.FromBasisType(shape),
{
name: `Example Base ${shape} Unit`,
abbreviation: (index == 0 ? `EB${shape}U`: undefined)
}
)
return map;
},
{}
);
const exampleComboUnits: Record<string, CombinationUnit<AnyUnitPower[]>> = combinatoricUnits(exampleBaseUnits);
describe("Combination Units auto-generate abbreviations iff all components have abbreviations", () => {
Object.values(exampleComboUnits).forEach((comboUnit: CombinationUnit<AnyUnitPower[]>) => {
test(`with ${comboUnit.name}`, () => {
if(comboUnit.unitPowers.every(([unit, power]) => unit.abbreviation != undefined)) {
expect(comboUnit.abbreviation).toBeDefined()
} else {
expect(comboUnit.abbreviation).toBeUndefined()
}
})
})
})
describe("Combination Units never contain 0 powered units", () => {
Object.entries(exampleComboUnits).forEach(([, comboUnit]) => {
test(`with ${comboUnit.name}`, () => {
if (comboUnit.unitPowers.length == 0) {
expect(comboUnit.shape).toEqual(NoneUnit.shape)
} else {
comboUnit.unitPowers.forEach(([, power]) => { expect(power).not.toEqual(0) })
}
})
});
})
describe("Unit power list is always flat", () => {
// Create multi-level combo units
let exampleComboOfComboUnits: Record<string, CombinationUnit<AnyUnitPower[]>> = combinatoricUnits(exampleComboUnits);
// Check that UnitPowers are flattened
Object.values(exampleComboOfComboUnits).forEach((comboOfComboUnit) => {
test(`with ${comboOfComboUnit.name}`, () => {
comboOfComboUnit.unitPowers.forEach(([unit, power]) => {
expect(unit instanceof CombinationUnit).toBeFalsy()
})
})
})
})
// FIXME: Currently tests for stricter 'expected order', rather than only 'consistent order' (which would be correct)
describe("Name order is 'consistent'", () => {
[1, -1 /* Unit Power Sign */].forEach((sign) => {
describe((sign > 0) ? "Positive" : "Negative" + " powers are individually ordered alphabetically", () => {
Object.values(exampleComboUnits).forEach((comboUnit) => {
test(`with ${comboUnit.name}`, () => {
let filterFunc = (power: number) => power*sign > 0
let namesWithCurrentSign = comboUnit.name.split(nameSplitter).filter((name:string) => filterFunc(getPower(name)))
expect(namesWithCurrentSign).toEqual(namesWithCurrentSign.sort())
// Don't actually expect this to be true
// let abbrWithCurrentSign = comboUnit.abbreviation.split(abbrSplitter).filter((name:string) => filterFunc(getPower(name)))
// expect(abbrWithCurrentSign).toEqual(abbrWithCurrentSign.sort())
let unitPowersWithCurrentSign = comboUnit.unitPowers.filter(([_, power]) => filterFunc(power))
expect(unitPowersWithCurrentSign).toEqual(unitPowersWithCurrentSign.sort(([aUnit, aPower], [bUnit, bPower]) => aUnit.name.localeCompare(bUnit.name)))
})
})
})
});
describe("Positive powers are ordered before negative power", () => {
Object.values(exampleComboUnits).forEach((comboUnit) =>{
test(`with ${comboUnit.name}`, () => {
let namesWithPowers = comboUnit.name.split(nameSplitter)
let indexOfFirstNegativePoweredName = namesWithPowers.findIndex((nameWithPower) => getPower(nameWithPower) < 0)
if (indexOfFirstNegativePoweredName >= 0) {
namesWithPowers.slice(indexOfFirstNegativePoweredName).forEach((nameWithPowers) => {
expect(getPower(nameWithPowers)).toBeLessThan(0)
})
}
let indexOfFirstNegativePowerdUnitPower = comboUnit.unitPowers.findIndex((_, power) => power < 0)
if (indexOfFirstNegativePowerdUnitPower >= 0) {
comboUnit.unitPowers.slice(indexOfFirstNegativePowerdUnitPower).forEach(([unit, power]) => {
expect(power).toBeLessThan(0)
})
}
})
})
});
// Testing order of two units with the same names:
describe("Testing order of two units with the same names is by power", () => {
let duplicateExampleBaseUnits: Record<string, BaseSIUnit<UnitShapeMap>> = {};
Object.entries(exampleBaseUnits).forEach( ([shape, unit]) => {
duplicateExampleBaseUnits[shape] = new BaseSIUnit(UnitShape.FromBasisType(`${shape}2`), {name: unit.name, abbreviation: unit.abbreviation, otherNames: [`Duplicate of ${unit.name}`]})
});
let combinationOfDuplicateUnits: Record<string, CombinationUnit<AnyUnitPower[]>> = {};
exampleUnitShapes.forEach((shape) => {
combinationOfDuplicateUnits[shape] = new CombinationUnit(([[exampleBaseUnits[shape], 1], [duplicateExampleBaseUnits[shape], 2]]))
});
Object.values(combinationOfDuplicateUnits).forEach((comboDupUnit) => {
test(`with ${combinationOfDuplicateUnits.name}`, () => {
expect(comboDupUnit.unitPowers[0][1]).toBeLessThanOrEqual(comboDupUnit.unitPowers[1][1])
})
})
})
})
describe('Custom Names and abbreviations are remembered', () => {
const [unit1, power1] = [exampleBaseUnits[exampleUnitShapes[0]], 3]
const [unit2, power2] = [exampleBaseUnits[exampleUnitShapes[1]], 5]
const customName: string = "Custom Name"
let customNamedComboUnit = new CombinationUnit(
[[unit1, power1], [unit2, power2]],
{ name: customName }
);
let autoNamedComboUnit = new CombinationUnit([[unit1, power1], [unit2, power2]])
expect(customNamedComboUnit.name).toEqual(customName)
expect(autoNamedComboUnit.name).toEqual(customName)
expect(autoNamedComboUnit.name).toEqual(customNamedComboUnit.name)
// TODO: Test custom abbreviations
})
describe("Units conversions produce correct values", () => {
const exampleScaleFactors = [3, 12, -0.166666, 1000]
const shapeToScaleFactor: Record<(typeof exampleUnitShapes)[number], number> = exampleUnitShapes.reduce((map: Partial<Record<(typeof exampleUnitShapes)[number], number>>, shape, idx) => {
map[shape] = exampleScaleFactors[idx]
return map;
}, {}) as Record<(typeof exampleUnitShapes)[number], number>
const exampleRelativeUnits: Record<string, SimpleUnit<UnitShapeMap>> = exampleUnitShapes.reduce(
(map: Partial<Record<(typeof exampleUnitShapes)[number], SimpleUnit<UnitShapeMap>>>, shape) => {
let scaleFactor = shapeToScaleFactor[shape]
map[shape] = new SimpleUnit(UnitShape.FromBasisType(shape), scaleFactor, { name: `[${scaleFactor}${shape}]` });
return map;
},
{}
)
const exampleRelativeComboUnits: Record<string, CombinationUnit<AnyUnitPower[]>> = combinatoricUnits(exampleRelativeUnits);
const valuesToConvert = [1, 5, 10, -3, 12.513]
valuesToConvert.forEach((valueToConvert) => {
describe(`Testing with ${valueToConvert}`, () => {
test("Unit conversions between squared units", () => {
exampleUnitShapes.forEach((shape) => {
let squareKey = `${shape}${shape}`;
let relUnit = exampleRelativeComboUnits[squareKey]
let baseUnit = exampleComboUnits[squareKey]
let scaleFactorSquared = shapeToScaleFactor[shape]**2
expect(relUnit.convertTo(valueToConvert, baseUnit)).toBeCloseTo(valueToConvert * scaleFactorSquared)
expect(baseUnit.convertTo(valueToConvert, relUnit)).toBeCloseTo(valueToConvert / scaleFactorSquared)
})
})
test('Unit conversions fail iff they have different shapes', () => {
expect(() => exampleComboUnits.AA.convertTo(valueToConvert, exampleComboUnits.BB)).toThrow(TypeError)
expect(() => exampleComboUnits.AA.convertTo(valueToConvert, exampleRelativeComboUnits.AA)).not.toThrow(TypeError)
})
test('Unit Conversions to self are reflexive', () => {
let comboUnits = Object.values(exampleComboUnits).concat(Object.values(exampleRelativeComboUnits));
comboUnits.forEach((unit: CombinationUnit<AnyUnitPower[]>) => {
expect(unit.convertTo(valueToConvert, unit)).toBeCloseTo(valueToConvert)
})
})
})
})
})
})