flow-lang-value 0.1.0-rc.5

The Flow runtime value model: data values, their equality, order and text, and the operations std's built-in types declare.
Documentation
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
//! `Int` and `Decimal` (`data.md`, *Int* and *Decimal*).

mod common;

use common::*;
use flow_value::{BigInt, DIVISION_DIGITS, Decimal, Fault, Int, Value};

// --- Int ---------------------------------------------------------------------

#[test]
fn an_integer_literal_denotes_the_exact_number_its_digits_spell() {
    assert_eq!(int("42").to_string(), "42");
    assert_eq!(int("-42").to_string(), "-42");
    assert_eq!(int("007").to_string(), "7");
    assert_eq!(int("-0").to_string(), "0");
    let huge = "123456789012345678901234567890123456789012345678901234567890";
    assert_eq!(int(huge).to_string(), huge);
}

#[test]
fn only_the_decoded_literal_spelling_parses_as_an_integer() {
    for refused in [
        "", "-", "+5", "1_000", " 5", "5 ", "0x10", "1.5", "--5", "5-",
    ] {
        assert_eq!(Int::parse(refused), None, "{refused:?} should be refused");
    }
}

#[test]
fn integer_arithmetic_is_exact_with_no_upper_or_lower_bound() {
    let max = int("9223372036854775807");
    assert_eq!(max.add(&Int::from(1)), int("9223372036854775808"));
    assert_eq!(
        int("-9223372036854775808").sub(&Int::from(1)),
        int("-9223372036854775809")
    );
    assert_eq!(max.mul(&max), int("85070591730234615847396907784232501249"));
    assert_eq!(Int::from(40).add(&Int::from(2)), Int::from(42));
    assert_eq!(Int::from(40).sub(&Int::from(42)), Int::from(-2));
    assert_eq!(Int::from(6).mul(&Int::from(-7)), Int::from(-42));
    assert_eq!(Int::from(42).neg(), Int::from(-42));
    assert_eq!(Int::from(-42).neg(), Int::from(42));
}

#[test]
fn integer_division_rounds_toward_negative_infinity() {
    let div = |a: i64, b: i64| Int::from(a).div(&Int::from(b)).expect("nonzero divisor");
    assert_eq!(div(7, 2), Int::from(3));
    assert_eq!(div(-7, 2), Int::from(-4));
    assert_eq!(div(7, -2), Int::from(-4));
    assert_eq!(div(-7, -2), Int::from(3));
    assert_eq!(div(6, 3), Int::from(2));
    assert_eq!(div(-6, 3), Int::from(-2));
}

#[test]
fn the_remainder_is_zero_or_carries_the_divisors_sign() {
    let rem = |a: i64, b: i64| Int::from(a).rem(&Int::from(b)).expect("nonzero divisor");
    assert_eq!(rem(7, 2), Int::from(1));
    assert_eq!(rem(-7, 2), Int::from(1));
    assert_eq!(rem(7, -2), Int::from(-1));
    assert_eq!(rem(-7, -2), Int::from(-1));
    assert_eq!(rem(6, -3), Int::from(0));
}

#[test]
fn a_zero_divisor_faults_for_div_and_remainder_alike() {
    assert_eq!(Int::from(7).div(&Int::from(0)), Err(Fault::DivisionByZero));
    assert_eq!(Int::from(7).rem(&Int::from(0)), Err(Fault::DivisionByZero));
    assert_eq!(Int::from(0).div(&Int::from(0)), Err(Fault::DivisionByZero));
}

#[test]
fn integers_order_by_value() {
    assert!(Int::from(-3) < Int::from(2));
    assert!(int("100000000000000000000") > int("99999999999999999999"));
    assert!(int("-100000000000000000000") < int("-99999999999999999999"));
}

#[test]
fn an_integer_reports_its_sign_and_zero() {
    assert!(Int::from(0).is_zero());
    assert!(!Int::from(1).is_zero());
    assert!(Int::from(-1).is_negative());
    assert!(!Int::from(0).is_negative());
    assert!(!Int::from(1).is_negative());
}

#[test]
fn an_integer_is_a_position_only_when_it_is_non_negative_and_reachable() {
    assert_eq!(Int::from(0).to_index(), Some(0));
    assert_eq!(Int::from(5).to_index(), Some(5));
    assert_eq!(Int::from(-1).to_index(), None);
    assert_eq!(int("1000000000000000000000000").to_index(), None);
}

#[test]
fn an_integer_exposes_its_exact_number_to_an_encoder() {
    assert_eq!(int("-12").as_bigint(), &BigInt::from(-12));
    assert_eq!(Int::from(BigInt::from(9)), Int::from(9));
    assert_eq!(Int::from_count(3), Int::from(3));
    assert_eq!(format!("{:?}", Int::from(-3)), "-3");
}

// --- Decimal -----------------------------------------------------------------

#[test]
fn a_decimal_literal_denotes_exactly_the_number_its_digits_spell() {
    assert_eq!(dec("4.2").to_string(), "4.2");
    assert_eq!(dec("-4.2").to_string(), "-4.2");
    assert_eq!(dec("0.001").to_string(), "0.001");
    assert_eq!(dec("12").to_string(), "12");
    assert_eq!(dec("12.0").to_string(), "12");
    let long = "3.1415926535897932384626433832795028841971693993751";
    assert_eq!(dec(long).to_string(), long);
}

#[test]
fn only_the_decoded_literal_spelling_parses_as_a_decimal() {
    for refused in [
        "", ".5", "5.", "-.5", "+1.5", "1e5", "1.5e2", "1_0.5", "1.2.3", "1.-5", "1. 5", "-", "abc",
    ] {
        assert_eq!(
            Decimal::parse(refused),
            None,
            "{refused:?} should be refused"
        );
    }
}

#[test]
fn decimals_are_normalized_so_a_value_is_its_number_not_its_digits() {
    assert_eq!(dec("1.50"), dec("1.5"));
    assert_eq!(dec("1.50").to_string(), "1.5");
    assert_eq!(dec("100").coefficient(), &BigInt::from(1));
    assert_eq!(dec("100").exponent(), 2);
    assert_eq!(dec("1.50").coefficient(), &BigInt::from(15));
    assert_eq!(dec("1.50").exponent(), -1);
    assert!(same(&decimal_value("1.50"), &decimal_value("1.5")));
}

#[test]
fn zero_has_one_value_with_no_sign() {
    let zero = dec("0");
    assert_eq!(dec("-0.000"), zero);
    assert_eq!(dec("0.0").exponent(), 0);
    assert_eq!(dec("5").sub(&dec("5")).unwrap(), zero);
    assert_eq!(zero.neg(), zero);
    assert_eq!(zero.neg().to_string(), "0");
    assert!(zero.is_zero());
    assert!(!dec("0.1").is_zero());
    assert_eq!(Decimal::new(BigInt::from(0), 7).unwrap().exponent(), 0);
}

#[test]
fn decimal_addition_subtraction_and_multiplication_are_exact() {
    assert_eq!(dec("0.1").add(&dec("0.2")).unwrap(), dec("0.3"));
    assert_eq!(dec("19.99").mul(&dec("3.0")).unwrap(), dec("59.97"));
    assert_eq!(dec("1.5").sub(&dec("0.25")).unwrap(), dec("1.25"));
    assert_eq!(dec("100").add(&dec("0.001")).unwrap(), dec("100.001"));
    assert_eq!(dec("0.001").add(&dec("100")).unwrap(), dec("100.001"));
    assert_eq!(dec("2.5").mul(&dec("0.4")).unwrap(), dec("1"));
    assert_eq!(dec("-1.5").mul(&dec("2")).unwrap(), dec("-3"));
    assert_eq!(dec("1.5").neg(), dec("-1.5"));
}

#[test]
fn a_quotient_with_a_finite_expansion_is_exact_however_long() {
    assert_eq!(dec("1.0").div(&dec("2.0")).unwrap(), dec("0.5"));
    assert_eq!(dec("1").div(&dec("8")).unwrap(), dec("0.125"));
    assert_eq!(dec("3").div(&dec("0.5")).unwrap(), dec("6"));
    assert_eq!(dec("1").div(&dec("0.04")).unwrap(), dec("25"));
    assert_eq!(dec("12").div(&dec("3")).unwrap(), dec("4"));
    assert_eq!(dec("-7").div(&dec("2")).unwrap(), dec("-3.5"));
    assert_eq!(dec("7").div(&dec("-2")).unwrap(), dec("-3.5"));
    assert_eq!(dec("-7").div(&dec("-2")).unwrap(), dec("3.5"));
    assert_eq!(dec("0").div(&dec("3")).unwrap(), dec("0"));
    assert_eq!(dec("6").div(&dec("15")).unwrap(), dec("0.4"));
    // 1 / 2^120 has 120 digits after the point, far more than 34, and is
    // still exact because its expansion is finite.
    let power = Decimal::new(BigInt::from(2).pow(120), 0).unwrap();
    let expected = Decimal::new(BigInt::from(5).pow(120), -120).unwrap();
    assert_eq!(dec("1").div(&power).unwrap(), expected);
    // Likewise for powers of five, whose exact quotient needs powers of two.
    let five = Decimal::new(BigInt::from(5).pow(60), 0).unwrap();
    let expected = Decimal::new(BigInt::from(2).pow(60), -60).unwrap();
    assert_eq!(dec("1").div(&five).unwrap(), expected);
}

#[test]
fn any_other_quotient_rounds_to_thirty_four_significant_digits() {
    assert_eq!(DIVISION_DIGITS, 34);
    assert_eq!(
        dec("1.0").div(&dec("3.0")).unwrap().to_string(),
        "0.3333333333333333333333333333333333"
    );
    assert_eq!(
        dec("2").div(&dec("3")).unwrap().to_string(),
        "0.6666666666666666666666666666666667"
    );
    assert_eq!(
        dec("-2").div(&dec("3")).unwrap().to_string(),
        "-0.6666666666666666666666666666666667"
    );
    assert_eq!(
        dec("1").div(&dec("7")).unwrap().to_string(),
        "0.1428571428571428571428571428571429"
    );
    assert_eq!(
        dec("10").div(&dec("3")).unwrap().to_string(),
        "3.333333333333333333333333333333333"
    );
    assert_eq!(
        dec("100000").div(&dec("3")).unwrap().to_string(),
        "33333.33333333333333333333333333333"
    );
    assert_eq!(
        dec("1").div(&dec("30000")).unwrap().to_string(),
        "0.00003333333333333333333333333333333333"
    );
    // A dividend whose leading digits are below the divisor's (8/9) needs
    // one more place of scale than one whose leading digits are not (7/3).
    assert_eq!(
        dec("7").div(&dec("3")).unwrap().to_string(),
        "2.333333333333333333333333333333333"
    );
    assert_eq!(
        dec("8").div(&dec("9")).unwrap().to_string(),
        "0.8888888888888888888888888888888889"
    );
    // A dividend far longer than the divisor scales the divisor up instead.
    assert_eq!(
        dec("10000000000000000000000000000000000000001")
            .div(&dec("3"))
            .unwrap()
            .to_string(),
        "3333333333333333333333333333333333000000"
    );
    assert_eq!(
        dec("-10000000000000000000000000000000000000001")
            .div(&dec("7"))
            .unwrap()
            .to_string(),
        "-1428571428571428571428571428571429000000"
    );
    assert_eq!(
        dec("1").div(&dec("9")).unwrap().to_string(),
        "0.1111111111111111111111111111111111"
    );
}

#[test]
fn rounding_up_carries_into_a_new_leading_digit_and_normalizes() {
    // Just below one, by far less than the 34th digit: rounding carries all
    // the way, and the result is the normalized 1.
    let almost = Decimal::new(BigInt::from(10).pow(34) - 1, 0).unwrap();
    let above = almost
        .add(&dec("0.000000000000000000000000000000000001"))
        .unwrap();
    assert_eq!(almost.div(&above).unwrap(), dec("1"));
    assert_eq!(almost.div(&above).unwrap().exponent(), 0);
}

#[test]
fn a_quotient_whose_first_thirty_five_digits_are_a_power_of_ten_keeps_thirty_four() {
    // (3·10^34 + 2) / (3·10^34) is 1.000…0006…, with the sixth in the 36th
    // significant digit: 34 digits round it away, 35 would keep a final 1.
    let numerator = Decimal::new(BigInt::from(3) * BigInt::from(10).pow(34) + 2, 0).unwrap();
    let denominator = Decimal::new(BigInt::from(3), 34).unwrap();
    assert_eq!(numerator.div(&denominator).unwrap(), dec("1"));
}

#[test]
fn a_zero_divisor_faults_for_decimal_division() {
    assert_eq!(dec("1").div(&dec("0")), Err(Fault::DivisionByZero));
    assert_eq!(dec("0").div(&dec("0.0")), Err(Fault::DivisionByZero));
}

#[test]
fn a_scale_beyond_a_sixty_four_bit_exponent_is_past_the_limit() {
    let top = Decimal::new(BigInt::from(1), i64::MAX).unwrap();
    let bottom = Decimal::new(BigInt::from(1), i64::MIN).unwrap();
    assert_eq!(Decimal::new(BigInt::from(10), i64::MAX), Err(Fault::Limit));
    assert_eq!(top.mul(&dec("10")), Err(Fault::Limit));
    assert_eq!(bottom.mul(&dec("0.1")), Err(Fault::Limit));
    let five_top = Decimal::new(BigInt::from(5), i64::MAX).unwrap();
    assert_eq!(five_top.add(&five_top), Err(Fault::Limit));
    assert_eq!(five_top.sub(&five_top.neg()), Err(Fault::Limit));
    assert_eq!(bottom.div(&dec("10")), Err(Fault::Limit));
    assert_eq!(bottom.div(&dec("2")), Err(Fault::Limit));
    assert_eq!(bottom.div(&dec("3")), Err(Fault::Limit));
    assert_eq!(top.div(&dec("0.1")), Err(Fault::Limit));
}

#[test]
fn decimals_order_by_value() {
    let ascending = [
        "-1000", "-2.5", "-2.25", "-1", "-0.001", "0", "0.001", "0.01", "0.5", "1", "1.25", "1.5",
        "2", "10", "11", "100.5", "1000",
    ];
    for (at, left) in ascending.iter().enumerate() {
        for (other, right) in ascending.iter().enumerate() {
            assert_eq!(
                dec(left).cmp(&dec(right)),
                at.cmp(&other),
                "{left} against {right}"
            );
            assert_eq!(
                dec(left).partial_cmp(&dec(right)),
                Some(at.cmp(&other)),
                "{left} against {right}"
            );
        }
    }
}

#[test]
fn ordering_far_apart_decimals_never_builds_the_power_of_ten_between_them() {
    let huge = Decimal::new(BigInt::from(1), i64::MAX - 1).unwrap();
    let tiny = Decimal::new(BigInt::from(1), i64::MIN + 1).unwrap();
    assert!(huge > tiny);
    assert!(huge.neg() < tiny.neg());
    assert!(tiny > dec("0"));
}

#[test]
fn floor_rounds_toward_negative_infinity_to_an_int() {
    assert_eq!(dec("2.5").floor(), Int::from(2));
    assert_eq!(dec("-2.5").floor(), Int::from(-3));
    assert_eq!(dec("3").floor(), Int::from(3));
    assert_eq!(dec("-3").floor(), Int::from(-3));
    assert_eq!(dec("1200").floor(), Int::from(1200));
    assert_eq!(dec("12.75").floor(), Int::from(12));
    assert_eq!(dec("-12.75").floor(), Int::from(-13));
    assert_eq!(dec("0.5").floor(), Int::from(0));
    assert_eq!(dec("-0.5").floor(), Int::from(-1));
    assert_eq!(dec("0.05").floor(), Int::from(0));
    assert_eq!(dec("-0.05").floor(), Int::from(-1));
    assert_eq!(dec("0").floor(), Int::from(0));
    let tiny = Decimal::new(BigInt::from(-7), -1000).unwrap();
    assert_eq!(tiny.floor(), Int::from(-1));
    let tiny = Decimal::new(BigInt::from(7), -1000).unwrap();
    assert_eq!(tiny.floor(), Int::from(0));
}

#[test]
fn an_integer_counts_the_bits_of_its_magnitude() {
    assert_eq!(Int::from(0).bits(), 0);
    assert_eq!(Int::from(1).bits(), 1);
    assert_eq!(Int::from(-1).bits(), 1);
    assert_eq!(Int::from(255).bits(), 8);
    assert_eq!(Int::from(-256).bits(), 9);
    assert_eq!(int(&"9".repeat(40)).bits(), 133);
}

#[test]
fn a_floor_within_a_bit_bound_is_the_floor_and_beyond_it_is_none() {
    assert_eq!(dec("255.9").floor_within(8), Some(Int::from(255)));
    assert_eq!(dec("256").floor_within(8), None);
    assert_eq!(dec("-255.5").floor_within(8), None, "the floor is -256");
    assert_eq!(dec("-254.5").floor_within(8), Some(Int::from(-255)));
    assert_eq!(dec("0.5").floor_within(0), Some(Int::from(0)));
    assert_eq!(dec("-0.5").floor_within(0), None);
    assert_eq!(dec("999").floor_within(10), Some(Int::from(999)));
    // Four digits before the point take at least ten bits; three may not.
    assert_eq!(dec("1000").floor_within(9), None);
    assert_eq!(dec("1000").floor_within(10), Some(Int::from(1000)));
    // A number with more digits than the bound has bits is refused before
    // its floor is built, however large its exponent.
    let vast = Decimal::new(BigInt::from(3), i64::MAX).unwrap();
    assert_eq!(vast.floor_within(4096), None);
    assert_eq!(
        Decimal::new(BigInt::from(1), 4).unwrap().floor_within(4),
        None
    );
    assert_eq!(
        Decimal::new(BigInt::from(1), 4).unwrap().floor_within(14),
        Some(Int::from(10_000))
    );
}

#[test]
fn an_integer_converts_to_the_same_number_as_a_decimal() {
    assert_eq!(Decimal::from(&Int::from(1500)), dec("1500"));
    assert_eq!(Decimal::from(&Int::from(1500)).exponent(), 2);
    assert_eq!(Decimal::from(&Int::from(-7)), dec("-7"));
    assert_eq!(Decimal::from(&Int::from(0)), dec("0"));
}

#[test]
fn a_decimals_text_is_plain_notation_with_no_exponent() {
    assert_eq!(
        Decimal::new(BigInt::from(15), 3).unwrap().to_string(),
        "15000"
    );
    assert_eq!(
        Decimal::new(BigInt::from(-15), -1).unwrap().to_string(),
        "-1.5"
    );
    assert_eq!(
        Decimal::new(BigInt::from(15), -2).unwrap().to_string(),
        "0.15"
    );
    assert_eq!(
        Decimal::new(BigInt::from(15), -4).unwrap().to_string(),
        "0.0015"
    );
    assert_eq!(
        Decimal::new(BigInt::from(-15), -4).unwrap().to_string(),
        "-0.0015"
    );
    assert_eq!(
        Decimal::new(BigInt::from(123), -1).unwrap().to_string(),
        "12.3"
    );
    assert_eq!(format!("{:?}", dec("2.50")), "2.5");
}

#[test]
fn adding_zero_never_builds_the_power_of_ten_between_the_two_scales() {
    let tiny = Decimal::new(BigInt::from(-7), i64::MIN + 1).unwrap();
    let huge = Decimal::new(BigInt::from(3), i64::MAX - 1).unwrap();
    for far in [&tiny, &huge] {
        assert_eq!(&dec("0").add(far).unwrap(), far);
        assert_eq!(&far.add(&dec("0")).unwrap(), far);
        assert_eq!(dec("0").sub(far).unwrap(), far.neg());
        assert_eq!(&far.sub(&dec("0")).unwrap(), far);
    }
}

#[test]
fn a_decimal_measures_its_text_without_writing_it() {
    for spelling in [
        "0", "1", "-1", "15000", "-1.5", "0.15", "0.0015", "-0.0015", "12.3",
    ] {
        let number = dec(spelling);
        assert_eq!(
            number.text_len(),
            u128::try_from(spelling.len()).unwrap(),
            "{spelling}"
        );
    }
    let number = |coefficient: i64, exponent: i64| {
        Decimal::new(BigInt::from(coefficient), exponent).unwrap()
    };
    for (coefficient, exponent) in [(15, 3), (-15, -1), (15, -2), (123, -1), (-123, -3), (7, 0)] {
        let it = number(coefficient, exponent);
        assert_eq!(
            it.text_len(),
            u128::try_from(it.to_string().len()).unwrap(),
            "{it}"
        );
    }
    // Nine quintillion zeros after a one, and before a minus and a one.
    assert_eq!(
        number(1, i64::MAX).text_len(),
        1 + u128::from(i64::MAX.unsigned_abs())
    );
    assert_eq!(
        number(-1, i64::MIN).text_len(),
        3 + u128::from(i64::MIN.unsigned_abs())
    );
}

#[test]
fn int_and_decimal_values_never_compare_with_each_other() {
    assert_eq!(
        flow_value::compare(&Value::int(1), &decimal_value("1")),
        Err(flow_value::DataError::Mismatch)
    );
}

#[test]
fn faults_describe_themselves() {
    assert_eq!(Fault::DivisionByZero.to_string(), "division by zero");
    assert_eq!(
        Fault::Limit.to_string(),
        "the result's exponent does not fit in 64 bits"
    );
}

#[test]
fn decimals_with_long_coefficients_order_and_measure_by_value() {
    // Coefficients past 64 bits, at exponents that put their leading digits
    // at one place, at neighbouring places, and far apart.
    let long = |digits: &str, exponent: i64| {
        Decimal::new(digits.parse::<BigInt>().expect("digits"), exponent).expect("a decimal")
    };
    let ascending = [
        long("-123456789012345678901234567891", 0),
        long("-123456789012345678901234567890001", -3),
        long("-99999999999999999999999", 0),
        long("12345678901234567890123456789", -40),
        long("1000000000000000000000001", -2),
        long("99999999999999999999999", 0),
        long("123456789012345678901234567890000001", -6),
        long("1234567890123456789012345678901", -1),
        long("12345678901234567890123456789013", -2),
        long("1", 30),
    ];
    for (at, left) in ascending.iter().enumerate() {
        for (other, right) in ascending.iter().enumerate() {
            assert_eq!(left.cmp(right), at.cmp(&other), "{left} against {right}");
        }
        assert_eq!(
            left.text_len(),
            u128::try_from(left.to_string().len()).expect("a short text"),
            "{left}"
        );
    }
    // Normalizing strips trailing zeros past nineteen at a time, and counts
    // them all, from a coefficient longer than 64 bits.
    let zeros = long(&format!("7{}", "0".repeat(45)), -5);
    assert_eq!(zeros.coefficient(), &BigInt::from(7));
    assert_eq!(zeros.exponent(), 40);
    // Every power of ten counts one more digit than the one below it.
    for places in 15..25_u32 {
        let power = Decimal::new(BigInt::from(10).pow(places), 0).expect("a power of ten");
        assert_eq!(power.text_len(), u128::from(places) + 1, "10^{places}");
        let below = Decimal::new(BigInt::from(10).pow(places) - 1, 0).expect("nines");
        assert_eq!(below.text_len(), u128::from(places), "10^{places} - 1");
    }
}