summaryrefslogtreecommitdiff
path: root/src/test/java/sevenUnitsGUI/PrefixRepetitionTest.java
blob: ead5f4afe52ebf6bf642eb20e3593e53bca40a42 (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
/**
 * Copyright (C) 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 sevenUnitsGUI;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static sevenUnitsGUI.DefaultPrefixRepetitionRule.COMPLEX_REPETITION;
import static sevenUnitsGUI.DefaultPrefixRepetitionRule.NO_REPETITION;
import static sevenUnitsGUI.DefaultPrefixRepetitionRule.NO_RESTRICTION;

import java.util.List;

import org.junit.jupiter.api.Test;

import sevenUnits.unit.Metric;

/**
 * Tests for the default prefix repetition rules.
 *
 * @since 2022-07-17
 * @since v0.4.0
 */
class PrefixRepetitionTest {
	/**
	 * Ensures that the complex repetition rule disallows invalid prefix lists.
	 *
	 * @since 2022-07-17
	 * @since v0.4.0
	 */
	@Test
	void testInvalidComplexRepetition() {
		assertFalse(COMPLEX_REPETITION.test(List.of(Metric.KILO, Metric.YOTTA)),
				"Complex repetition does not factor order of prefixes");
		assertFalse(COMPLEX_REPETITION.test(List.of(Metric.MEGA, Metric.KILO)),
				"\"kilomega\" allowed (should use \"giga\")");
		assertFalse(
				COMPLEX_REPETITION
						.test(List.of(Metric.YOTTA, Metric.MEGA, Metric.KILO)),
				"\"kilomega\" allowed after yotta (should use \"giga\")");
		assertFalse(COMPLEX_REPETITION.test(List.of(Metric.YOTTA, Metric.MILLI)),
				"Complex repetition does not factor direction of prefixes");
	}

	/**
	 * Tests the {@code NO_REPETITION} rule.
	 *
	 * @since 2022-07-17
	 * @since v0.4.0
	 */
	@Test
	void testNoRepetition() {
		assertTrue(NO_REPETITION.test(List.of(Metric.KILO)));
		assertFalse(NO_REPETITION.test(List.of(Metric.KILO, Metric.YOTTA)));
		assertTrue(NO_REPETITION.test(List.of(Metric.MILLI)));
		assertTrue(NO_REPETITION.test(List.of()), "Empty list yields false");
	}

	/**
	 * Tests the {@code NO_RESTRICTION} rule.
	 *
	 * @since 2022-07-17
	 * @since v0.4.0
	 */
	@Test
	void testNoRestriction() {
		assertTrue(NO_RESTRICTION.test(List.of(Metric.KILO)));
		assertTrue(NO_RESTRICTION.test(List.of(Metric.KILO, Metric.YOTTA)));
		assertTrue(NO_RESTRICTION.test(List.of(Metric.MILLI)));
		assertTrue(NO_RESTRICTION.test(List.of()));
	}

	/**
	 * Ensures that the complex repetition rule allows valid prefix lists.
	 *
	 * @since 2022-07-17
	 * @since v0.4.0
	 */
	@Test
	void testValidComplexRepetition() {
		// simple situations
		assertTrue(COMPLEX_REPETITION.test(List.of(Metric.KILO)),
				"Single prefix not allowed");
		assertTrue(COMPLEX_REPETITION.test(List.of()), "No prefixes not allowed");

		// valid complex repetition
		assertTrue(COMPLEX_REPETITION.test(List.of(Metric.YOTTA, Metric.KILO)),
				"Valid complex repetition (kiloyotta) not allowed");
		assertTrue(COMPLEX_REPETITION.test(List.of(Metric.KILO, Metric.DEKA)),
				"Valid complex repetition (dekakilo) not allowed");
		assertTrue(
				COMPLEX_REPETITION
						.test(List.of(Metric.YOTTA, Metric.KILO, Metric.DEKA)),
				"Valid complex repetition (dekakiloyotta) not allowed");
		assertTrue(
				COMPLEX_REPETITION.test(List.of(Metric.YOTTA, Metric.YOTTA,
						Metric.KILO, Metric.DEKA)),
				"Valid complex repetition (dekakiloyottayotta) not allowed");

		// valid with negative prefixes
		assertTrue(COMPLEX_REPETITION.test(List.of(Metric.YOCTO, Metric.MILLI)),
				"Valid complex repetition (milliyocto) not allowed");
		assertTrue(COMPLEX_REPETITION.test(List.of(Metric.MILLI, Metric.CENTI)),
				"Valid complex repetition (centimilli) not allowed");
		assertTrue(
				COMPLEX_REPETITION
						.test(List.of(Metric.YOCTO, Metric.MILLI, Metric.CENTI)),
				"Valid complex repetition (centimilliyocto) not allowed");
	}
}