ucon 0.3.3rc2__py3-none-any.whl → 0.3.5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tests/ucon/__init__.py +3 -0
- tests/ucon/test_algebra.py +239 -0
- tests/ucon/test_core.py +607 -362
- tests/ucon/test_quantity.py +370 -0
- tests/ucon/test_units.py +7 -3
- ucon/__init__.py +9 -3
- ucon/algebra.py +216 -0
- ucon/core.py +703 -286
- ucon/quantity.py +196 -0
- ucon/units.py +5 -2
- {ucon-0.3.3rc2.dist-info → ucon-0.3.5.dist-info}/METADATA +49 -37
- ucon-0.3.5.dist-info/RECORD +16 -0
- {ucon-0.3.3rc2.dist-info → ucon-0.3.5.dist-info}/WHEEL +1 -1
- ucon-0.3.5.dist-info/licenses/LICENSE +202 -0
- ucon-0.3.5.dist-info/licenses/NOTICE +28 -0
- tests/ucon/test_dimension.py +0 -206
- tests/ucon/test_unit.py +0 -143
- ucon/dimension.py +0 -172
- ucon/unit.py +0 -92
- ucon-0.3.3rc2.dist-info/RECORD +0 -15
- ucon-0.3.3rc2.dist-info/licenses/LICENSE +0 -21
- {ucon-0.3.3rc2.dist-info → ucon-0.3.5.dist-info}/top_level.txt +0 -0
tests/ucon/__init__.py
CHANGED
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
# © 2025 The Radiativity Company
|
|
2
|
+
# Licensed under the Apache License, Version 2.0
|
|
3
|
+
# See the LICENSE file for details.
|
|
4
|
+
|
|
5
|
+
import math
|
|
6
|
+
from unittest import TestCase
|
|
7
|
+
|
|
8
|
+
from ucon.algebra import Exponent, Vector
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class TestVector(TestCase):
|
|
12
|
+
|
|
13
|
+
def test_vector_iteration_and_length(self):
|
|
14
|
+
v = Vector(1, 0, 0, 0, 0, 0, 0)
|
|
15
|
+
self.assertEqual(tuple(v), (1, 0, 0, 0, 0, 0, 0))
|
|
16
|
+
self.assertEqual(len(v), 7) # always 7 components
|
|
17
|
+
|
|
18
|
+
def test_vector_addition(self):
|
|
19
|
+
v1 = Vector(1, 0, 0, 0, 0, 0, 0)
|
|
20
|
+
v2 = Vector(0, 2, 0, 0, 0, 0, 0)
|
|
21
|
+
result = v1 + v2
|
|
22
|
+
self.assertEqual(result, Vector(1, 2, 0, 0, 0, 0, 0))
|
|
23
|
+
|
|
24
|
+
def test_vector_subtraction(self):
|
|
25
|
+
v1 = Vector(2, 1, 0, 0, 0, 0, 0)
|
|
26
|
+
v2 = Vector(1, 1, 0, 0, 0, 0, 0)
|
|
27
|
+
self.assertEqual(v1 - v2, Vector(1, 0, 0, 0, 0, 0, 0))
|
|
28
|
+
|
|
29
|
+
def test_vector_scalar_multiplication_by_integer(self):
|
|
30
|
+
v = Vector(1, -2, 0, 0, 0, 0, 3)
|
|
31
|
+
scaled = v * 2
|
|
32
|
+
self.assertEqual(scaled, Vector(2, -4, 0, 0, 0, 0, 6))
|
|
33
|
+
self.assertEqual(v, Vector(1, -2, 0, 0, 0, 0, 3)) # original unchanged
|
|
34
|
+
|
|
35
|
+
def test_vector_scalar_multiplication_by_float(self):
|
|
36
|
+
v = Vector(0, 1, 0, 0, 0, 0, 0)
|
|
37
|
+
scaled = v * 0.5
|
|
38
|
+
self.assertEqual(scaled, Vector(0.0, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0))
|
|
39
|
+
|
|
40
|
+
def test_vector_scalar_multiplication_by_zero(self):
|
|
41
|
+
v = Vector(1, 2, 3, 4, 5, 6, 7)
|
|
42
|
+
zeroed = v * 0
|
|
43
|
+
self.assertEqual(zeroed, Vector(0, 0, 0, 0, 0, 0, 0))
|
|
44
|
+
|
|
45
|
+
def test_vector_equality_and_hash(self):
|
|
46
|
+
v1 = Vector(1, 0, 0, 0, 0, 0, 0)
|
|
47
|
+
v2 = Vector(1, 0, 0, 0, 0, 0, 0)
|
|
48
|
+
v3 = Vector(0, 1, 0, 0, 0, 0, 0)
|
|
49
|
+
self.assertTrue(v1 == v2)
|
|
50
|
+
self.assertFalse(v1 == v3)
|
|
51
|
+
self.assertEqual(hash(v1), hash(v2))
|
|
52
|
+
self.assertNotEqual(hash(v1), hash(v3))
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class TestVectorEdgeCases(TestCase):
|
|
56
|
+
|
|
57
|
+
def test_zero_vector_equality_and_additivity(self):
|
|
58
|
+
zero = Vector()
|
|
59
|
+
self.assertEqual(zero, Vector(0, 0, 0, 0, 0, 0, 0))
|
|
60
|
+
# Adding or subtracting zero should yield same vector
|
|
61
|
+
v = Vector(1, 2, 3, 4, 5, 6, 7)
|
|
62
|
+
self.assertEqual(v + zero, v)
|
|
63
|
+
self.assertEqual(v - zero, v)
|
|
64
|
+
|
|
65
|
+
def test_vector_with_negative_exponents(self):
|
|
66
|
+
v1 = Vector(1, -2, 3, 0, 0, 0, 0)
|
|
67
|
+
v2 = Vector(-1, 2, -3, 0, 0, 0, 0)
|
|
68
|
+
result = v1 + v2
|
|
69
|
+
self.assertEqual(result, Vector(0, 0, 0, 0, 0, 0, 0))
|
|
70
|
+
self.assertEqual(v1 - v1, Vector()) # perfect cancellation
|
|
71
|
+
|
|
72
|
+
def test_vector_equality_with_non_vector(self):
|
|
73
|
+
v = Vector()
|
|
74
|
+
with self.assertRaises(AssertionError):
|
|
75
|
+
v == "not a vector"
|
|
76
|
+
with self.assertRaises(AssertionError):
|
|
77
|
+
v == None
|
|
78
|
+
|
|
79
|
+
def test_hash_consistency_for_equal_vectors(self):
|
|
80
|
+
v1 = Vector(1, 0, 0, 0, 0, 0, 0)
|
|
81
|
+
v2 = Vector(1, 0, 0, 0, 0, 0, 0)
|
|
82
|
+
self.assertEqual(hash(v1), hash(v2))
|
|
83
|
+
self.assertEqual(len({v1, v2}), 1)
|
|
84
|
+
|
|
85
|
+
def test_iter_length_order_consistency(self):
|
|
86
|
+
v = Vector(1, 2, 3, 4, 5, 6, 7)
|
|
87
|
+
components = list(v)
|
|
88
|
+
self.assertEqual(len(components), len(v))
|
|
89
|
+
# Ensure order of iteration is fixed (T→L→M→I→Θ→J→N)
|
|
90
|
+
self.assertEqual(components, [1, 2, 3, 4, 5, 6, 7])
|
|
91
|
+
|
|
92
|
+
def test_vector_arithmetic_does_not_mutate_operands(self):
|
|
93
|
+
v1 = Vector(1, 0, 0, 0, 0, 0, 0)
|
|
94
|
+
v2 = Vector(0, 1, 0, 0, 0, 0, 0)
|
|
95
|
+
_ = v1 + v2
|
|
96
|
+
self.assertEqual(v1, Vector(1, 0, 0, 0, 0, 0, 0))
|
|
97
|
+
self.assertEqual(v2, Vector(0, 1, 0, 0, 0, 0, 0))
|
|
98
|
+
|
|
99
|
+
def test_invalid_addition_type_raises(self):
|
|
100
|
+
v = Vector(1, 0, 0, 0, 0, 0, 0)
|
|
101
|
+
with self.assertRaises(TypeError):
|
|
102
|
+
_ = v + "length"
|
|
103
|
+
with self.assertRaises(TypeError):
|
|
104
|
+
_ = v - 5
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
class TestExponent(TestCase):
|
|
108
|
+
|
|
109
|
+
thousand = Exponent(10, 3)
|
|
110
|
+
thousandth = Exponent(10, -3)
|
|
111
|
+
kibibyte = Exponent(2, 10)
|
|
112
|
+
mebibyte = Exponent(2, 20)
|
|
113
|
+
|
|
114
|
+
def test___init__(self):
|
|
115
|
+
with self.assertRaises(ValueError):
|
|
116
|
+
Exponent(5, 3) # no support for base 5 logarithms
|
|
117
|
+
|
|
118
|
+
def test_parts(self):
|
|
119
|
+
self.assertEqual((10, 3), self.thousand.parts())
|
|
120
|
+
self.assertEqual((10, -3), self.thousandth.parts())
|
|
121
|
+
|
|
122
|
+
def test_evaluated_property(self):
|
|
123
|
+
self.assertEqual(1000, self.thousand.evaluated)
|
|
124
|
+
self.assertAlmostEqual(0.001, self.thousandth.evaluated)
|
|
125
|
+
self.assertEqual(1024, self.kibibyte.evaluated)
|
|
126
|
+
self.assertEqual(1048576, self.mebibyte.evaluated)
|
|
127
|
+
|
|
128
|
+
def test___truediv__(self):
|
|
129
|
+
# same base returns a new Exponent
|
|
130
|
+
ratio = self.thousand / self.thousandth
|
|
131
|
+
self.assertIsInstance(ratio, Exponent)
|
|
132
|
+
self.assertEqual(ratio.base, 10)
|
|
133
|
+
self.assertEqual(ratio.power, 6)
|
|
134
|
+
self.assertEqual(ratio.evaluated, 1_000_000)
|
|
135
|
+
|
|
136
|
+
# different base returns numeric float
|
|
137
|
+
val = self.thousand / self.kibibyte
|
|
138
|
+
self.assertIsInstance(val, float)
|
|
139
|
+
self.assertAlmostEqual(1000 / 1024, val)
|
|
140
|
+
|
|
141
|
+
def test___mul__(self):
|
|
142
|
+
product = self.kibibyte * self.mebibyte
|
|
143
|
+
self.assertIsInstance(product, Exponent)
|
|
144
|
+
self.assertEqual(product.base, 2)
|
|
145
|
+
self.assertEqual(product.power, 30)
|
|
146
|
+
self.assertEqual(product.evaluated, 2**30)
|
|
147
|
+
|
|
148
|
+
# cross-base multiplication returns numeric
|
|
149
|
+
val = self.kibibyte * self.thousand
|
|
150
|
+
self.assertIsInstance(val, float)
|
|
151
|
+
self.assertAlmostEqual(1024 * 1000, val)
|
|
152
|
+
|
|
153
|
+
def test___hash__(self):
|
|
154
|
+
a = Exponent(10, 3)
|
|
155
|
+
b = Exponent(10, 3)
|
|
156
|
+
self.assertEqual(hash(a), hash(b))
|
|
157
|
+
self.assertEqual(len({a, b}), 1) # both should hash to same value
|
|
158
|
+
|
|
159
|
+
def test___float__(self):
|
|
160
|
+
self.assertEqual(float(self.thousand), 1000.0)
|
|
161
|
+
|
|
162
|
+
def test___int__(self):
|
|
163
|
+
self.assertEqual(int(self.thousand), 1000)
|
|
164
|
+
|
|
165
|
+
def test_comparisons(self):
|
|
166
|
+
self.assertTrue(self.thousand > self.thousandth)
|
|
167
|
+
self.assertTrue(self.thousandth < self.thousand)
|
|
168
|
+
self.assertTrue(self.kibibyte < self.mebibyte)
|
|
169
|
+
self.assertTrue(self.kibibyte == Exponent(2, 10))
|
|
170
|
+
|
|
171
|
+
with self.assertRaises(TypeError):
|
|
172
|
+
_ = self.thousand == 1000 # comparison to non-Exponent
|
|
173
|
+
|
|
174
|
+
def test___repr__(self):
|
|
175
|
+
self.assertIn("Exponent", repr(Exponent(10, -3)))
|
|
176
|
+
|
|
177
|
+
def test___str__(self):
|
|
178
|
+
self.assertEqual(str(self.thousand), '10^3')
|
|
179
|
+
self.assertEqual(str(self.thousandth), '10^-3')
|
|
180
|
+
|
|
181
|
+
def test_to_base(self):
|
|
182
|
+
e = Exponent(2, 10)
|
|
183
|
+
converted = e.to_base(10)
|
|
184
|
+
self.assertIsInstance(converted, Exponent)
|
|
185
|
+
self.assertEqual(converted.base, 10)
|
|
186
|
+
self.assertAlmostEqual(converted.power, math.log10(1024), places=10)
|
|
187
|
+
|
|
188
|
+
with self.assertRaises(ValueError):
|
|
189
|
+
e.to_base(5)
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
class TestExponentEdgeCases(TestCase):
|
|
193
|
+
|
|
194
|
+
def test_extreme_powers(self):
|
|
195
|
+
e = Exponent(10, 308)
|
|
196
|
+
self.assertTrue(math.isfinite(e.evaluated))
|
|
197
|
+
e_small = Exponent(10, -308)
|
|
198
|
+
self.assertGreater(e.evaluated, e_small.evaluated)
|
|
199
|
+
|
|
200
|
+
def test_precision_rounding_in_hash(self):
|
|
201
|
+
a = Exponent(10, 6)
|
|
202
|
+
b = Exponent(10, 6 + 1e-16)
|
|
203
|
+
# rounding in hash avoids floating drift
|
|
204
|
+
self.assertEqual(hash(a), hash(b))
|
|
205
|
+
|
|
206
|
+
def test_negative_and_zero_power(self):
|
|
207
|
+
e0 = Exponent(10, 0)
|
|
208
|
+
e_neg = Exponent(10, -1)
|
|
209
|
+
self.assertEqual(e0.evaluated, 1.0)
|
|
210
|
+
self.assertEqual(e_neg.evaluated, 0.1)
|
|
211
|
+
self.assertLess(e_neg, e0)
|
|
212
|
+
|
|
213
|
+
def test_valid_exponent_evaluates_correctly(self):
|
|
214
|
+
base, power = 10, 3
|
|
215
|
+
e = Exponent(base, power)
|
|
216
|
+
self.assertEqual(e.evaluated, 1000)
|
|
217
|
+
self.assertEqual(e.parts(), (base, power))
|
|
218
|
+
self.assertEqual(f'{base}^{power}', str(e))
|
|
219
|
+
self.assertEqual(f'Exponent(base={base}, power={power})', repr(e))
|
|
220
|
+
|
|
221
|
+
def test_invalid_base_raises_value_error(self):
|
|
222
|
+
with self.assertRaises(ValueError):
|
|
223
|
+
Exponent(5, 2)
|
|
224
|
+
|
|
225
|
+
def test_exponent_comparisons(self):
|
|
226
|
+
e1 = Exponent(10, 2)
|
|
227
|
+
e2 = Exponent(10, 3)
|
|
228
|
+
self.assertTrue(e1 < e2)
|
|
229
|
+
self.assertTrue(e2 > e1)
|
|
230
|
+
self.assertFalse(e1 == e2)
|
|
231
|
+
|
|
232
|
+
def test_division_returns_exponent(self):
|
|
233
|
+
e1 = Exponent(10, 3)
|
|
234
|
+
e2 = Exponent(10, 2)
|
|
235
|
+
self.assertEqual(e1 / e2, Exponent(10, 1))
|
|
236
|
+
|
|
237
|
+
def test_equality_with_different_type(self):
|
|
238
|
+
with self.assertRaises(TypeError):
|
|
239
|
+
Exponent(10, 2) == "10^2"
|