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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
|
/**
* Copyright (C) 2019, 2021, 2022, 2024, 2025 Adrien Hopkins
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package sevenUnits.unit;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import sevenUnits.utils.NameSymbol;
import sevenUnits.utils.UncertainDouble;
/**
* A test for the {@link UnitDatabase} class. This is NOT part of this program's
* public API.
*
* @author Adrien Hopkins
* @since 2019-04-14
* @since v0.2.0
*/
class UnitDatabaseTest {
private static final class SimpleEntry<K, V> implements Map.Entry<K, V> {
private final K key;
private V value;
/**
*
* @since 2021-10-07
* @since v0.3.2
*/
public SimpleEntry(K key, V value) {
this.key = key;
this.value = value;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!(obj instanceof Map.Entry<?, ?>))
return false;
final Map.Entry<?, ?> other = (Map.Entry<?, ?>) obj;
return Objects.equals(this.key, other.getKey())
&& Objects.equals(this.value, other.getValue());
}
@Override
public K getKey() {
return this.key;
}
@Override
public V getValue() {
return this.value;
}
@Override
public int hashCode() {
return (this.key == null ? 0 : this.key.hashCode())
^ (this.value == null ? 0 : this.value.hashCode());
}
@Override
public V setValue(V value) {
final var oldValue = this.value;
this.value = value;
return oldValue;
}
}
// some linear units and one nonlinear
private static final Unit U = Metric.METRE;
private static final Unit V = Metric.KILOGRAM;
private static final Unit W = Metric.SECOND;
// used for testing expressions
// J = U^2 * V / W^2
private static final LinearUnit J = Metric.KILOGRAM
.times(Metric.METRE.toExponent(2))
.dividedBy(Metric.SECOND.toExponent(2));
private static final LinearUnit K = Metric.KELVIN;
private static final Unit NONLINEAR = Unit.fromConversionFunctions(
Metric.METRE.getBase(), o -> o + 1, o -> o - 1);
// make the prefix values prime so I can tell which multiplications were made
private static final UnitPrefix A = UnitPrefix.valueOf(2)
.withName(NameSymbol.ofName("A"));
private static final UnitPrefix B = UnitPrefix.valueOf(3)
.withName(NameSymbol.ofName("B"));
private static final UnitPrefix C = UnitPrefix.valueOf(5)
.withName(NameSymbol.ofName("C"));
private static final UnitPrefix AB = UnitPrefix.valueOf(7);
private static final UnitPrefix BC = UnitPrefix.valueOf(11);
/**
* Gets a map entry.
*
* @param <K> type of key
* @param <V> type of value
* @param key key in entry
* @param value value in entry
* @return entry
* @since 2021-10-07
* @since v0.3.2
*/
private static <K, V> Map.Entry<K, V> entry(K key, V value) {
return new SimpleEntry<>(key, value);
}
/**
* Loads the dimensionfile at src/test/resources/[path] to the database
* {@code loadTo}.
*
* @param loadTo database to load to
* @param path path of file to load
* @return exceptions returned by file loading
* @since 2021-10-04
* @since v0.3.2
*/
private static List<LoadingException> loadDimensionFile(UnitDatabase loadTo,
String path) {
try (final var testFile = UnitDatabaseTest.class
.getResourceAsStream(path)) {
return loadTo.loadDimensionsFromStream(testFile);
} catch (final IOException e) {
fail(e.getClass() + " occurred upon loading file \"" + path + "\".");
return Collections.emptyList();
}
}
/**
* Loads the unitfile at src/test/resources/[path] to the database
* {@code loadTo}.
*
* @param loadTo database to load to
* @param path path of file to load
* @return exceptions returned by file loading
* @since 2021-09-22
* @since v0.3.2
*/
private static List<LoadingException> loadUnitsFile(UnitDatabase loadTo,
String path) {
try (final var testFile = UnitDatabaseTest.class
.getResourceAsStream(path)) {
return loadTo.loadUnitsFromStream(testFile);
} catch (final IOException e) {
fail(e.getClass() + " occurred upon loading file \"" + path + "\".");
return Collections.emptyList();
}
}
private static final Stream<Arguments> testEvaluateExpressionInvalid() {
return Stream.of(Arguments.of("K^K"), Arguments.of("1 + K"));
}
private static final Stream<Arguments> testEvaluateExpressionValid() {
return Stream.of(
Arguments.of("J + (2 * 3) J + (20 / 4) J",
LinearUnitValue.of(J,
UncertainDouble.of(12, Math.sqrt(14.625)))),
Arguments.of("J - -1 * J",
LinearUnitValue.of(J, UncertainDouble.of(2, 1))),
Arguments.of("K^2",
LinearUnitValue.of(K.times(K), UncertainDouble.of(1, 0))));
}
/**
* Tests expressions that are valid to the parser, but semantically invalid
* (e.g. adding different dimensions)
*
* @param expression expression to test - should throw
* {@link IllegalArgumentException}
*/
@ParameterizedTest
@MethodSource
public void testEvaluateExpressionInvalid(String expression) {
final var database = new UnitDatabase();
database.addUnit("J", J);
database.addUnit("K", K);
database.addPrefix("A", A);
database.addPrefix("B", B);
database.addPrefix("C", C);
assertThrows(IllegalArgumentException.class,
() -> database.getUnitFromExpression(expression));
assertThrows(IllegalArgumentException.class,
() -> database.evaluateUnitExpression(expression));
}
/**
* A test for the {@link UnitDatabase#evaluateUnitExpression(String)}
* function. Simple because the expression parser has its own test.
*
* @since 2021-09-27
* @since v0.3.2
*/
@ParameterizedTest
@MethodSource
public void testEvaluateExpressionValid(String expression,
LinearUnitValue expected) {
final var database = new UnitDatabase();
database.addUnit("J", J);
database.addUnit("K", K);
database.addPrefix("A", A);
database.addPrefix("B", B);
database.addPrefix("C", C);
final var actual = database
.evaluateUnitExpression(expression);
assertEquals(expected, actual);
final var expectedU = expected.getUnit().times(expected.getValueExact());
final var actualU = database.getUnitFromExpression(expression);
assertEquals(expectedU, actualU);
}
/**
* Test for {@link UnitDatabase#getUnit}, {@link UnitDatabase#getLinearUnit}
* and {@link UnitDatabase#getLinearUnitValue}.
*
* @since 2021-10-07
* @since v0.3.2
*/
@Test
public void testGetUnit() {
final var database = new UnitDatabase();
database.addUnit("m", Metric.METRE);
database.addUnit("meter", Metric.METRE);
database.addUnit("metre", Metric.METRE);
database.addUnit("badname", Metric.METRE);
database.addUnit("K", Metric.KELVIN);
database.addUnit("degC", Metric.CELSIUS);
// ensure getUnit returns units, regardless of whether the name is one of
// the unit's names
assertEquals(Metric.METRE, database.getUnit("m"));
assertEquals(Metric.METRE, database.getUnit("metre"));
assertEquals(Metric.METRE, database.getUnit("meter"));
assertEquals(Metric.METRE, database.getUnit("badname"));
assertThrows(NoSuchElementException.class,
() -> database.getUnit("blabla"));
assertEquals(Metric.KELVIN, database.getLinearUnit("K"));
assertThrows(IllegalArgumentException.class,
() -> database.getLinearUnit("degC"));
assertEquals(Metric.KELVIN.times(373.15),
database.getLinearUnit("degC(100)"));
}
/**
* Confirms that operations that shouldn't function for infinite databases
* throw an {@code IllegalStateException}.
*
* @since 2019-05-03
* @since v0.3.0
*/
// @Test
// @Timeout(value = 1, unit = TimeUnit.SECONDS)
public void testInfiniteSetExceptions() {
// load units
final var infiniteDatabase = new UnitDatabase();
infiniteDatabase.addUnit("J", J);
infiniteDatabase.addUnit("K", K);
infiniteDatabase.addPrefix("A", A);
infiniteDatabase.addPrefix("B", B);
infiniteDatabase.addPrefix("C", C);
final var entrySet = infiniteDatabase.unitMap()
.entrySet();
final var keySet = infiniteDatabase.unitMap().keySet();
assertThrows(IllegalStateException.class, () -> entrySet.toArray());
assertThrows(IllegalStateException.class, () -> keySet.toArray());
}
/**
* A bunch of tests for invalid dimension files
*
* @param num which file to test
* @since 2021-10-04
* @since v0.3.2
*/
@ParameterizedTest
@ValueSource(ints = { 1, 2, 3 })
public void testLoadingInvalidDimensionFile(int num) {
final var database = new UnitDatabase();
database.addDimension("LENGTH", Metric.Dimensions.LENGTH);
database.addDimension("MASS", Metric.Dimensions.MASS);
database.addDimension("TIME", Metric.Dimensions.TIME);
final var filename = String.format("/test-dimensionfile-invalid%d.txt",
num);
final var errs = loadDimensionFile(database, filename);
assertFalse(errs.isEmpty(), "no error from invalid file " + filename);
final var e = errs.get(0).problem();
assertTrue(e instanceof IllegalArgumentException
|| e instanceof NoSuchElementException);
}
/**
* A bunch of tests for invalid unit files
*
* @param num which file to test
* @since 2021-09-27
* @since v0.3.2
*/
@ParameterizedTest
@ValueSource(ints = { 1, 2, 3, 4, 5 })
public void testLoadingInvalidUnitFile(int num) {
final var database = new UnitDatabase();
final var filename = String.format("/test-unitsfile-invalid%d.txt",
num);
final var errs = loadUnitsFile(database, filename);
assertFalse(errs.isEmpty(), "no error from invalid file " + filename);
final var e = errs.get(0).problem();
assertTrue(e instanceof IllegalArgumentException
|| e instanceof NoSuchElementException);
}
/**
* Tests loading a valid dimension-file with some derived dimensions.
*
* @since 2021-10-04
* @since v0.3.2
*/
@Test
public void testLoadingValidDimensions() {
final var database = new UnitDatabase();
database.addDimension("LENGTH", Metric.Dimensions.LENGTH);
database.addDimension("MASS", Metric.Dimensions.MASS);
database.addDimension("TIME", Metric.Dimensions.TIME);
loadDimensionFile(database, "/test-dimensionfile-valid1.txt");
assertEquals(Metric.Dimensions.ENERGY, database.getDimension("ENERGY"));
assertEquals(Metric.Dimensions.POWER, database.getDimension("POWER"));
}
/**
* Tests loading a valid unitfile with some prefixes and no units.
*
* @since 2021-09-22
* @since v0.3.2
*/
@Test
public void testLoadingValidPrefixes() {
final var database = new UnitDatabase();
loadUnitsFile(database, "/test-unitsfile-valid2.txt");
assertEquals(7, database.getPrefix("A").getMultiplier());
assertEquals(11, database.getPrefix("B").getMultiplier());
assertEquals(13, database.getPrefix("C").getMultiplier());
// test invalid prefixes
assertThrows(NoSuchElementException.class,
() -> database.getPrefix("N/A"));
}
/**
* Tests loading a valid unitfile with some units and preloaded prefixes
*
* @since 2021-09-22
* @since v0.3.2
*/
@Test
public void testLoadingValidUnits() {
final var database = new UnitDatabase();
database.addUnit("U", U);
database.addUnit("V", V);
database.addUnit("W", W);
database.addUnit("fj", J.times(5));
database.addUnit("ej", J.times(8));
database.addPrefix("A", A);
database.addPrefix("B", B);
database.addPrefix("C", C);
loadUnitsFile(database, "/test-unitsfile-valid1.txt");
final Unit expected1 = ((LinearUnit) U).withPrefix(A).withPrefix(B)
.withPrefix(C);
final var actual1 = database.getUnit("test1");
assertEquals(expected1, actual1);
final Unit expected2 = ((LinearUnit) W).withPrefix(B)
.times(((LinearUnit) V).withPrefix(C));
final var actual2 = database.getUnit("test2");
assertEquals(expected2, actual2);
final Unit expected3 = ((LinearUnit) U)
.times(A.getMultiplier() + C.getMultiplier() - B.getMultiplier());
final var actual3 = database.getUnit("test3");
assertEquals(expected3, actual3);
final var expected4 = UnitValue.of(U, 1);
final var actual4 = database
.evaluateUnitExpression("-5 * U + -3 * U + 12 * U - 3 * U")
.asUnitValue();
assertEquals(expected4, actual4);
assertTrue(System.err.toString().length() > 0);
}
/**
* Tests the iterator of the prefixless unit map. These tests are simple, as
* the unit map iterator is simple.
*
* @since 2021-10-07
* @since v0.3.2
*/
@Test
public void testPrefixedUnitMapIterator() {
final var database1 = new UnitDatabase();
database1.addUnit("U", U);
database1.addUnit("V", V);
database1.addUnit("W", W);
final var map1 = database1.unitMap();
final var keyIterator1 = map1.keySet().iterator();
final var entryIterator1 = map1.entrySet()
.iterator();
final Set<String> expectedKeys = Set.of("U", "V", "W");
final Set<String> actualKeys = new HashSet<>();
while (keyIterator1.hasNext()) {
actualKeys.add(keyIterator1.next());
}
assertEquals(expectedKeys, actualKeys);
assertEquals(expectedKeys, map1.keySet());
final Set<Map.Entry<String, Unit>> expectedEntries = Set.of(entry("U", U),
entry("V", V), entry("W", W));
final Set<Map.Entry<String, Unit>> actualEntries = new HashSet<>();
while (entryIterator1.hasNext()) {
actualEntries.add(entryIterator1.next());
}
assertEquals(expectedEntries, actualEntries);
assertEquals(expectedEntries, map1.entrySet());
}
/**
* Test that prefixes correctly apply to units.
*
* @since 2019-04-14
* @since v0.2.0
*/
@Test
public void testPrefixes() {
final var database = new UnitDatabase();
database.addUnit("U", U);
database.addUnit("V", V);
database.addUnit("W", W);
database.addPrefix("A", A);
database.addPrefix("B", B);
database.addPrefix("C", C);
// test the getPrefixesFromName method
final List<UnitPrefix> expected = Arrays.asList(C, B, A);
assertEquals(expected, database.getPrefixesFromName("ABCU"));
// get the product
final var abcuNonlinear = database.getUnit("ABCU");
assert abcuNonlinear instanceof LinearUnit;
final var abcu = (LinearUnit) abcuNonlinear;
assertEquals(A.getMultiplier() * B.getMultiplier() * C.getMultiplier(),
abcu.getConversionFactor(), 1e-15);
}
/**
* Tests the functionnalites of the prefixless unit map.
*
* <p>
* The map should be an auto-updating view of the units in the database.
* </p>
*
* @since 2019-04-14
* @since v0.2.0
*/
@Test
public void testPrefixlessUnitMap() {
final var database = new UnitDatabase();
final var prefixlessUnits = database
.unitMapPrefixless(true);
database.addUnit("U", U);
database.addUnit("V", V);
database.addUnit("W", W);
// this should work because the map should be an auto-updating view
assertTrue(prefixlessUnits.containsKey("U"));
assertFalse(prefixlessUnits.containsKey("Z"));
assertTrue(prefixlessUnits.containsValue(U));
assertFalse(prefixlessUnits.containsValue(NONLINEAR));
}
/**
* Tests that the database correctly stores and retrieves units, ignoring
* prefixes.
*
* @since 2019-04-14
* @since v0.2.0
*/
@Test
public void testPrefixlessUnits() {
final var database = new UnitDatabase();
database.addUnit("U", U);
database.addUnit("V", V);
database.addUnit("W", W);
assertTrue(database.containsUnitName("U"));
assertFalse(database.containsUnitName("Z"));
assertEquals(U, database.getUnit("U"));
assertThrows(NoSuchElementException.class, () -> database.getUnit("Z"));
}
@Test
public void testRemovableDuplicates() {
final Map<String, Unit> unitMap = new HashMap<>();
unitMap.put("meter", Metric.METRE);
unitMap.put("metre", Metric.METRE);
unitMap.put("m", Metric.METRE);
unitMap.put("second", Metric.SECOND);
assertTrue(UnitDatabase.isRemovableDuplicate(unitMap,
entry("m", Metric.METRE)));
assertTrue(UnitDatabase.isRemovableDuplicate(unitMap,
entry("meter", Metric.METRE)));
assertFalse(UnitDatabase.isRemovableDuplicate(unitMap,
entry("metre", Metric.METRE)));
assertFalse(UnitDatabase.isRemovableDuplicate(unitMap,
entry("second", Metric.SECOND)));
}
@Test
public void testToString() {
final var database = new UnitDatabase();
database.addUnit("J", J);
database.addUnit("K", J);
database.addPrefix("A", A);
database.addPrefix("B", B);
database.addPrefix("C", C);
if ("Unit Database with 1 units, 3 unit prefixes and 0 dimensions"
.equals(database.toString())) {
fail("Database counts by number of units, not number of unit names.");
}
assertEquals(
"Unit Database with 2 units, 3 unit prefixes and 0 dimensions",
database.toString());
}
/**
* Test that unit expressions return the correct value.
*
* @since 2019-04-14
* @since v0.2.0
*/
@Test
public void testUnitExpressions() {
// load units
final var database = new UnitDatabase();
database.addUnit("U", U);
database.addUnit("V", V);
database.addUnit("W", W);
database.addUnit("fj", J.times(5));
database.addUnit("ej", J.times(8));
database.addPrefix("A", A);
database.addPrefix("B", B);
database.addPrefix("C", C);
// first test - test prefixes and operations
final Unit expected1 = J.withPrefix(A).withPrefix(B).withPrefix(C)
.withPrefix(C);
final var actual1 = database.getUnitFromExpression("ABV * CU^2 / W / W");
assertEquals(expected1, actual1);
// second test - test addition and subtraction
final Unit expected2 = J.times(58);
final var actual2 = database.getUnitFromExpression("2 fj + 6 ej");
assertEquals(expected2, actual2);
// test incorrect expressions
assertThrows(IllegalArgumentException.class,
() -> database.getUnitFromExpression("U + V"));
assertThrows(IllegalArgumentException.class,
() -> database.getUnitFromExpression("U - V"));
}
/**
* Tests both the unit name iterator and the name-unit entry iterator
*
* @since 2019-04-14
* @since v0.2.0
*/
@Test
public void testUnitIterator() {
// load units
final var database = new UnitDatabase();
database.addUnit("J", J);
database.addUnit("K", K);
database.addPrefix("A", A);
database.addPrefix("B", B);
database.addPrefix("C", C);
final var NUM_UNITS = database.unitMapPrefixless(true).size();
final var NUM_PREFIXES = database.prefixMap(true).size();
final var nameIterator = database.unitMap().keySet()
.iterator();
final var entryIterator = database.unitMap()
.entrySet().iterator();
var expectedLength = 1;
var unitsWithThisLengthSoFar = 0;
// loop 1000 times
for (var i = 0; i < 1000; i++) {
// expected length of next
if (unitsWithThisLengthSoFar >= NUM_UNITS
* (int) Math.pow(NUM_PREFIXES, expectedLength - 1)) {
expectedLength++;
unitsWithThisLengthSoFar = 0;
}
// test that stuff is valid
final var nextName = nameIterator.next();
final var nextUnit = database.getUnit(nextName);
final var nextEntry = entryIterator.next();
assertEquals(expectedLength, nextName.length());
assertEquals(nextName, nextEntry.getKey());
assertEquals(nextUnit, nextEntry.getValue());
unitsWithThisLengthSoFar++;
}
// test toString for consistency
final var entryIteratorString = entryIterator.toString();
for (var i = 0; i < 3; i++) {
assertEquals(entryIteratorString, entryIterator.toString());
}
final var nameIteratorString = nameIterator.toString();
for (var i = 0; i < 3; i++) {
assertEquals(nameIteratorString, nameIterator.toString());
}
}
/**
* Determine, given a unit name that could mean multiple things, which
* meaning is chosen.
* <p>
* For example, "ABCU" could mean "A-B-C-U", "AB-C-U", or "A-BC-U". In this
* case, "AB-C-U" is the correct choice.
* </p>
*
* @since 2019-04-14
* @since v0.2.0
*/
@Test
public void testUnitPrefixCombinations() {
// load units
final var database = new UnitDatabase();
database.addUnit("J", J);
database.addPrefix("A", A);
database.addPrefix("B", B);
database.addPrefix("C", C);
database.addPrefix("AB", AB);
database.addPrefix("BC", BC);
// test 1 - AB-C-J vs A-BC-J vs A-B-C-J
final Unit expected1 = J.withPrefix(AB).withPrefix(C);
final var actual1 = database.getUnit("ABCJ");
assertEquals(expected1, actual1);
// test 2 - ABC-J vs AB-CJ vs AB-C-J
database.addUnit("CJ", J.times(13));
database.addPrefix("ABC", UnitPrefix.valueOf(17));
final Unit expected2 = J.times(17);
final var actual2 = database.getUnit("ABCJ");
assertEquals(expected2, actual2);
}
/**
* Tests the ability to create, read, and delete unit sets.
*
* @since 2025-04-30
* @since v1.0.0
*/
@Test
void testUnitSetsInvalid() {
final List<LinearUnit> units = List.of(Metric.SECOND,
BritishImperial.Length.INCH);
final var database = new UnitDatabase();
assertThrows(IllegalArgumentException.class,
() -> database.addUnitSet("badtest", units));
assertThrows(NoSuchElementException.class,
() -> database.getUnitSet("badtest"));
database.addUnit("ft", BritishImperial.Length.FOOT);
database.addUnit("s", Metric.SECOND);
database.addUnit("dC", Metric.CELSIUS);
database.addUnit("K", Metric.KELVIN);
assertThrows(IllegalArgumentException.class,
() -> database.getUnitSetFromExpression("K; dC"),
"getUnitSetFromExpression allowed nonlinear unit.");
assertThrows(IllegalArgumentException.class,
() -> database.getUnitSetFromExpression("ft; s"),
"getUnitSetFromExpression allowed units of different dimension.");
}
/**
* Tests the ability to create, read, and delete unit sets.
*
* @since 2025-04-30
* @since v1.0.0
*/
@Test
void testUnitSetsValid() {
final List<LinearUnit> units = List.of(BritishImperial.Length.FOOT,
BritishImperial.Length.INCH);
final var database = new UnitDatabase();
database.addUnitSet("ftintest", units);
assertEquals(units, database.getUnitSet("ftintest"));
database.addUnit("ft", BritishImperial.Length.FOOT);
database.addUnit("in", BritishImperial.Length.INCH);
assertEquals(units, database.getUnitSetFromExpression("ft; in"));
}
}
|