mathypad 0.1.10

A smart TUI calculator that understands units and makes complex calculations simple.
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
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
//! Tests for expression parsing and evaluation

use super::*;
use crate::test_helpers::*;
use crate::units::Unit;

#[test]
fn test_basic_arithmetic() {
    // Basic operations
    assert_eq!(evaluate_test_expression("2 + 3"), Some("5".to_string()));
    assert_eq!(evaluate_test_expression("10 - 4"), Some("6".to_string()));
    assert_eq!(evaluate_test_expression("6 * 7"), Some("42".to_string()));
    assert_eq!(evaluate_test_expression("15 / 3"), Some("5".to_string()));

    // Order of operations
    assert_eq!(
        evaluate_test_expression("2 + 3 * 4"),
        Some("14".to_string())
    );
    assert_eq!(
        evaluate_test_expression("(2 + 3) * 4"),
        Some("20".to_string())
    );
    assert_eq!(
        evaluate_test_expression("10 - 2 * 3"),
        Some("4".to_string())
    );

    // Decimal numbers
    assert_eq!(evaluate_test_expression("1.5 + 2.5"), Some("4".to_string()));
    assert_eq!(
        evaluate_test_expression("3.14 * 2"),
        Some("6.28".to_string())
    );

    // Numbers with commas
    assert_eq!(
        evaluate_test_expression("1,000 + 500"),
        Some("1,500".to_string())
    );
    assert_eq!(
        evaluate_test_expression("1,234,567 / 1000"),
        Some("1,234.567".to_string())
    );
}

#[test]
fn test_inline_expressions() {
    // Test expressions within text
    assert_eq!(
        evaluate_test_expression("The result is 5 + 3"),
        Some("8".to_string())
    );
    assert_eq!(
        evaluate_test_expression("Cost: 100 * 12 dollars"),
        Some("1,200".to_string())
    );
    assert_eq!(
        evaluate_test_expression("Total (10 + 20) items"),
        Some("30".to_string())
    );
}

#[test]
fn test_complex_expressions() {
    // Complex arithmetic
    assert_eq!(
        evaluate_test_expression("(10 + 5) * 2 - 8 / 4"),
        Some("28".to_string())
    );
    assert_eq!(
        evaluate_test_expression("100 / (5 + 5) + 3 * 2"),
        Some("16".to_string())
    );

    // Large numbers with commas
    assert_eq!(
        evaluate_test_expression("1,000,000 + 500,000"),
        Some("1,500,000".to_string())
    );
    assert_eq!(
        evaluate_test_expression("2,500 * 1,000"),
        Some("2,500,000".to_string())
    );

    // Complex unit expressions
    assert_eq!(
        evaluate_test_expression("Transfer: 5 GiB/s * 10 minutes"),
        Some("3,000 GiB".to_string())
    );

    // Complex unit expressions with generic rate...
    assert_eq!(
        evaluate_test_expression("Transfer: 1 GiB/minute * 8 minutes"),
        Some("8 GiB".to_string())
    );

    // More generic rate tests
    assert_eq!(
        evaluate_test_expression("Backup speed: 500 MB/hour * 12 hours"),
        Some("6,000 MB".to_string())
    );
    assert_eq!(
        evaluate_test_expression("Download: 2.5 GiB/minute * 4 minutes"),
        Some("10 GiB".to_string())
    );
}

#[test]
fn test_edge_cases() {
    // Division by zero
    assert_eq!(evaluate_test_expression("5 / 0"), None);

    assert_eq!(evaluate_test_expression("5 +"), None);
    assert_eq!(evaluate_test_expression("* 5"), None);
    assert_eq!(evaluate_test_expression("((5)"), None);

    // Empty or invalid input
    assert_eq!(evaluate_test_expression(""), None);
    assert_eq!(evaluate_test_expression("hello world"), None);

    // Incompatible unit operations
    assert_eq!(evaluate_test_expression("5 GiB + 10 seconds"), None);
    assert_eq!(evaluate_test_expression("1 hour - 500 MB"), None);
}

#[test]
fn test_precision() {
    // Test decimal precision
    assert_eq!(
        evaluate_test_expression("1.234 + 2.567"),
        Some("3.801".to_string())
    );
    assert_eq!(
        evaluate_test_expression("10.5 / 3"),
        Some("3.5".to_string())
    );

    // Test with units requiring precision
    let result = evaluate_with_unit_info("1.5 GiB to MiB");
    assert!(result.is_some());
    let unit_val = result.unwrap();
    assert!((unit_val.value - 1536.0).abs() < 0.001);
}

#[test]
fn test_whitespace_handling() {
    // Various whitespace formats
    assert_eq!(evaluate_test_expression("5+3"), Some("8".to_string()));
    assert_eq!(
        evaluate_test_expression("  5  +  3  "),
        Some("8".to_string())
    );
    assert_eq!(evaluate_test_expression("5 * 3"), Some("15".to_string()));

    // Units with whitespace
    let result = evaluate_with_unit_info("1   GiB   to   KiB");
    assert!(result.is_some());
    let unit_val = result.unwrap();
    assert!((unit_val.value - 1048576.0).abs() < 0.001);
}

#[test]
fn test_line_references() {
    // Test parsing line references
    assert_eq!(parse_line_reference("line1"), Some(0));
    assert_eq!(parse_line_reference("line5"), Some(4));
    assert_eq!(parse_line_reference("line123"), Some(122));
    assert_eq!(parse_line_reference("Line1"), Some(0)); // Case insensitive
    assert_eq!(parse_line_reference("LINE1"), Some(0)); // Case insensitive

    // Test invalid line references
    assert_eq!(parse_line_reference("line0"), None); // Line numbers start at 1
    assert_eq!(parse_line_reference("line"), None); // No number
    assert_eq!(parse_line_reference("lineabc"), None); // Invalid number
    assert_eq!(parse_line_reference("myline1"), None); // Doesn't start with "line"
    assert_eq!(parse_line_reference("1line"), None); // Doesn't start with "line"

    // Test line reference resolution with context
    let previous_results = vec![
        Some("10 GiB".to_string()),
        Some("5".to_string()),
        None,
        Some("1,024 MiB".to_string()),
    ];

    // Test valid line references
    assert_eq!(
        evaluate_expression_with_context("line1 + 4 GiB", &previous_results, 4),
        Some("14 GiB".to_string())
    );
    assert_eq!(
        evaluate_expression_with_context("line2 * 3", &previous_results, 4),
        Some("15".to_string())
    );
    assert_eq!(
        evaluate_expression_with_context("line4 to GiB", &previous_results, 4),
        Some("1 GiB".to_string())
    );

    // Test circular reference prevention
    assert_eq!(
        evaluate_expression_with_context("line1 + 2", &previous_results, 0),
        None
    ); // Can't reference self
    assert_eq!(
        evaluate_expression_with_context("line5 + 2", &previous_results, 4),
        None
    ); // Can't reference future lines

    // Test reference to line with no result
    assert_eq!(
        evaluate_expression_with_context("line3 + 5", &previous_results, 4),
        None
    ); // Line 3 has no result

    // Test complex expressions with line references
    assert_eq!(
        evaluate_expression_with_context("(line1 + line4) / 2", &previous_results, 4),
        Some("5,632 MiB".to_string())
    );
    assert_eq!(
        evaluate_expression_with_context("line1 * line2 to MiB", &previous_results, 4),
        Some("51,200 MiB".to_string())
    );
}

#[test]
fn test_line_reference_parsing_edge_cases() {
    // Test result string parsing
    assert!(parse_result_string("10 GiB").is_some());
    assert!(parse_result_string("1,024").is_some());
    assert!(parse_result_string("42").is_some());
    assert!(parse_result_string("3.14 MiB/s").is_some());

    // Test invalid result strings
    assert!(parse_result_string("").is_none());
    assert!(parse_result_string("invalid").is_none());
    assert!(parse_result_string("GiB 10").is_none()); // Wrong order

    // Test line reference in tokenizer
    let tokens = tokenize_with_units("line1 + 5 GiB").unwrap();
    assert!(matches!(tokens[0], Token::LineReference(0)));
    assert!(matches!(tokens[1], Token::Plus));
    assert!(matches!(tokens[2], Token::NumberWithUnit(5.0, Unit::GiB)));
}

#[test]
fn test_variable_assignments() {
    use std::collections::HashMap;

    // Test simple variable assignment
    let variables = HashMap::new();
    let previous_results = vec![];
    let (result, assignment) =
        evaluate_with_variables("servers = 40", &variables, &previous_results, 0);
    assert_eq!(result, Some("40".to_string()));
    assert_eq!(assignment, Some(("servers".to_string(), "40".to_string())));

    // Test variable assignment with units
    let (result, assignment) =
        evaluate_with_variables("ram = 1 TiB", &variables, &previous_results, 0);
    assert_eq!(result, Some("1 TiB".to_string()));
    assert_eq!(assignment, Some(("ram".to_string(), "1 TiB".to_string())));

    // Test variable assignment with expression
    let (result, assignment) =
        evaluate_with_variables("total = 10 + 20", &variables, &previous_results, 0);
    assert_eq!(result, Some("30".to_string()));
    assert_eq!(assignment, Some(("total".to_string(), "30".to_string())));

    // Test variable assignment with unit expression
    let (result, assignment) = evaluate_with_variables(
        "storage = 2 GiB + 512 MiB",
        &variables,
        &previous_results,
        0,
    );
    assert_eq!(result, Some("2,560 MiB".to_string()));
    assert_eq!(
        assignment,
        Some(("storage".to_string(), "2,560 MiB".to_string()))
    );
}

#[test]
fn test_variable_references() {
    use std::collections::HashMap;

    // Set up variables
    let mut variables = HashMap::new();
    variables.insert("servers".to_string(), "40".to_string());
    variables.insert("ram".to_string(), "1 TiB".to_string());
    variables.insert("speed".to_string(), "100 MB/s".to_string());

    let previous_results = vec![];

    // Test simple variable reference
    let (result, assignment) = evaluate_with_variables("servers", &variables, &previous_results, 0);
    assert_eq!(result, Some("40".to_string()));
    assert_eq!(assignment, None);

    // Test variable reference with unit
    let (result, assignment) = evaluate_with_variables("ram", &variables, &previous_results, 0);
    assert_eq!(result, Some("1 TiB".to_string()));
    assert_eq!(assignment, None);

    // Test variable arithmetic
    let (result, assignment) =
        evaluate_with_variables("servers * 2", &variables, &previous_results, 0);
    assert_eq!(result, Some("80".to_string()));
    assert_eq!(assignment, None);

    // Test variable with unit arithmetic
    let (result, assignment) =
        evaluate_with_variables("ram + 512 GiB", &variables, &previous_results, 0);
    assert_eq!(result, Some("1,536 GiB".to_string()));
    assert_eq!(assignment, None);

    // Test two variables together
    let (result, assignment) =
        evaluate_with_variables("servers * ram", &variables, &previous_results, 0);
    assert_eq!(result, Some("40 TiB".to_string()));
    assert_eq!(assignment, None);
}

#[test]
fn test_multiline_variable_scenario() {
    use std::collections::HashMap;

    // Simulate the multiline notebook scenario: servers = 40, ram = 1 TiB, servers * ram
    let mut variables = HashMap::new();
    let mut previous_results = vec![];

    // Line 1: servers = 40
    let (result1, assignment1) =
        evaluate_with_variables("servers = 40", &variables, &previous_results, 0);
    assert_eq!(result1, Some("40".to_string()));
    assert_eq!(assignment1, Some(("servers".to_string(), "40".to_string())));

    // Store the variable assignment
    if let Some((var_name, var_value)) = assignment1 {
        variables.insert(var_name, var_value);
    }
    previous_results.push(result1);

    // Line 2: ram = 1 TiB
    let (result2, assignment2) =
        evaluate_with_variables("ram = 1 TiB", &variables, &previous_results, 1);
    assert_eq!(result2, Some("1 TiB".to_string()));
    assert_eq!(assignment2, Some(("ram".to_string(), "1 TiB".to_string())));

    // Store the variable assignment
    if let Some((var_name, var_value)) = assignment2 {
        variables.insert(var_name, var_value);
    }
    previous_results.push(result2);

    // Line 3: servers * ram
    let (result3, assignment3) =
        evaluate_with_variables("servers * ram", &variables, &previous_results, 2);
    assert_eq!(result3, Some("40 TiB".to_string()));
    assert_eq!(assignment3, None); // No assignment, just evaluation
}

#[test]
fn test_variable_with_line_references() {
    use std::collections::HashMap;

    let mut variables = HashMap::new();
    variables.insert("multiplier".to_string(), "3".to_string());

    // Simulate previous line results
    let previous_results = vec![Some("10 GiB".to_string()), Some("5".to_string())];

    // Test variable with line reference
    let (result, assignment) =
        evaluate_with_variables("line1 * multiplier", &variables, &previous_results, 2);
    assert_eq!(result, Some("30 GiB".to_string()));
    assert_eq!(assignment, None);

    // Test assigning line reference to variable
    let (result, assignment) =
        evaluate_with_variables("backup = line1 + 5 GiB", &variables, &previous_results, 2);
    assert_eq!(result, Some("15 GiB".to_string()));
    assert_eq!(
        assignment,
        Some(("backup".to_string(), "15 GiB".to_string()))
    );
}

#[test]
fn test_variable_conversions() {
    use std::collections::HashMap;

    let mut variables = HashMap::new();
    variables.insert("storage".to_string(), "1024 GiB".to_string());
    variables.insert("time".to_string(), "8 minutes".to_string());

    let previous_results = vec![];

    // Test variable conversion
    let (result, assignment) =
        evaluate_with_variables("storage to TB", &variables, &previous_results, 0);
    assert_eq!(result, Some("1.1 TB".to_string()));
    assert_eq!(assignment, None);

    // Test variable in complex conversion expression with generic rates
    let (result, assignment) =
        evaluate_with_variables("storage / time", &variables, &previous_results, 0);
    assert_eq!(result, Some("128 GiB/min".to_string())); // Creates generic rate
    assert_eq!(assignment, None);
}

#[test]
fn test_variable_edge_cases() {
    use std::collections::HashMap;

    let variables = HashMap::new();
    let previous_results = vec![];

    // Test undefined variable
    let (result, assignment) =
        evaluate_with_variables("undefined_var + 5", &variables, &previous_results, 0);
    assert_eq!(result, None);
    assert_eq!(assignment, None);

    // Test variable name conflicts with units - now parses as [Unit, Assign, Number]
    // which doesn't match assignment pattern, so evaluates "GiB" as standalone unit
    let (result, assignment) = evaluate_with_variables("GiB = 5", &variables, &previous_results, 0);
    assert_eq!(result, Some("1 GiB".to_string())); // Evaluates "GiB" as standalone unit
    assert_eq!(assignment, None); // No variable assignment

    // Test variable name conflicts with keywords - this actually parses as [To, Assign, Number(10)]
    // which doesn't match variable assignment pattern but does parse the number 10
    let (result, assignment) = evaluate_with_variables("to = 10", &variables, &previous_results, 0);
    assert_eq!(result, Some("10".to_string())); // Parses the "10" part
    assert_eq!(assignment, None); // No variable assignment

    // Test variable name conflicts with line references - now parses as [LineReference, Assign, Number]
    // which doesn't match assignment pattern, so evaluates "20" from the expression
    let (result, assignment) =
        evaluate_with_variables("line1 = 20", &variables, &previous_results, 0);
    assert_eq!(result, Some("20".to_string())); // Evaluates "20" from the expression
    assert_eq!(assignment, None); // No variable assignment
}

#[test]
fn test_complex_variable_expressions() {
    use std::collections::HashMap;

    let mut variables = HashMap::new();
    variables.insert("servers".to_string(), "10".to_string());
    variables.insert("ram_per_server".to_string(), "32 GiB".to_string());
    variables.insert("cpu_cores".to_string(), "8".to_string());
    variables.insert("disk_size".to_string(), "1 TiB".to_string());

    let previous_results = vec![];

    // Test complex variable expression
    let (result, assignment) = evaluate_with_variables(
        "total_ram = servers * ram_per_server",
        &variables,
        &previous_results,
        0,
    );
    assert_eq!(result, Some("320 GiB".to_string()));
    assert_eq!(
        assignment,
        Some(("total_ram".to_string(), "320 GiB".to_string()))
    );

    // Test expression with multiple variables and units
    let (result, assignment) = evaluate_with_variables(
        "(servers * disk_size) to GiB",
        &variables,
        &previous_results,
        0,
    );
    assert_eq!(result, Some("10,240 GiB".to_string()));
    assert_eq!(assignment, None);

    // Test complex arithmetic with variables
    let (result, assignment) = evaluate_with_variables(
        "servers * (ram_per_server + disk_size) to TiB",
        &variables,
        &previous_results,
        0,
    );
    assert_eq!(result, Some("10.312 TiB".to_string()));
    assert_eq!(assignment, None);
}

#[test]
fn test_user_multiline_scenario() {
    use std::collections::HashMap;

    // Test the specific user scenario: "memory = 40 GiB\ntime = 18 s\nmemory / time"
    let mut variables = HashMap::new();
    let mut previous_results = vec![];

    // Line 1: memory = 40 GiB
    let (result1, assignment1) =
        evaluate_with_variables("memory = 40 GiB", &variables, &previous_results, 0);
    assert_eq!(result1, Some("40 GiB".to_string()));
    assert_eq!(
        assignment1,
        Some(("memory".to_string(), "40 GiB".to_string()))
    );

    // Store the variable assignment
    if let Some((var_name, var_value)) = assignment1 {
        variables.insert(var_name, var_value);
    }
    previous_results.push(result1);

    // Line 2: time = 18 s
    let (result2, assignment2) =
        evaluate_with_variables("time = 18 s", &variables, &previous_results, 1);
    assert_eq!(result2, Some("18 s".to_string()));
    assert_eq!(assignment2, Some(("time".to_string(), "18 s".to_string())));

    // Store the variable assignment
    if let Some((var_name, var_value)) = assignment2 {
        variables.insert(var_name, var_value);
    }
    previous_results.push(result2);

    // Line 3: memory / time
    let (result3, assignment3) =
        evaluate_with_variables("memory / time", &variables, &previous_results, 2);
    assert_eq!(result3, Some("2.222 GiB/s".to_string()));
    assert_eq!(assignment3, None); // No assignment, just evaluation
}

#[test]
fn test_percentage_conversions() {
    // Test converting decimal to percentage
    assert_eq!(
        evaluate_test_expression("0.1 to %"),
        Some("10 %".to_string())
    );
    assert_eq!(
        evaluate_test_expression("0.25 to %"),
        Some("25 %".to_string())
    );
    assert_eq!(
        evaluate_test_expression("1 to %"),
        Some("100 %".to_string())
    );
    assert_eq!(
        evaluate_test_expression("1.5 to %"),
        Some("150 %".to_string())
    );

    // Test percentage parsing (just check it works)
    assert_eq!(evaluate_test_expression("50%"), Some("50 %".to_string()));

    // Test division result to percentage
    assert_eq!(
        evaluate_test_expression("1/10 to %"),
        Some("10 %".to_string())
    );
    assert_eq!(
        evaluate_test_expression("3/4 to %"),
        Some("75 %".to_string())
    );
    assert_eq!(
        evaluate_test_expression("1/3 to %"),
        Some("33.333 %".to_string())
    );
}

#[test]
fn test_percentage_of_operations() {
    // Test basic percentage of operations
    assert_eq!(evaluate_test_expression("10% of 50"), Some("5".to_string()));
    assert_eq!(
        evaluate_test_expression("25% of 100"),
        Some("25".to_string())
    );
    assert_eq!(
        evaluate_test_expression("50% of 200"),
        Some("100".to_string())
    );
    assert_eq!(
        evaluate_test_expression("150% of 40"),
        Some("60".to_string())
    );

    // Test percentage of values with units
    assert_eq!(
        evaluate_test_expression("20% of 100 GiB"),
        Some("20 GiB".to_string())
    );
    assert_eq!(
        evaluate_test_expression("75% of 8 hours"),
        Some("6 h".to_string())
    );
    assert_eq!(
        evaluate_test_expression("12.5% of 80 MB"),
        Some("10 MB".to_string())
    );

    // Test fractional percentages
    assert_eq!(
        evaluate_test_expression("0.5% of 1000"),
        Some("5".to_string())
    );
    assert_eq!(
        evaluate_test_expression("33.33% of 300"),
        Some("99.99".to_string())
    );
}

#[test]
fn test_percentage_with_variables() {
    use std::collections::HashMap;

    // Test percentage operations with variables
    let mut variables = HashMap::new();
    let mut previous_results = vec![];

    // Line 1: total = 100
    let (result1, assignment1) =
        evaluate_with_variables("total = 100", &variables, &previous_results, 0);
    assert_eq!(result1, Some("100".to_string()));
    assert_eq!(assignment1, Some(("total".to_string(), "100".to_string())));

    if let Some((var_name, var_value)) = assignment1 {
        variables.insert(var_name, var_value);
    }
    previous_results.push(result1);

    // Line 2: 15% of total
    let (result2, assignment2) =
        evaluate_with_variables("15% of total", &variables, &previous_results, 1);
    assert_eq!(result2, Some("15".to_string()));
    assert_eq!(assignment2, None);
}

#[test]
fn test_generic_rates_with_variables_and_references() {
    use std::collections::HashMap;

    // Test generic rates with variables
    let mut variables = HashMap::new();
    variables.insert("backup_rate".to_string(), "250 MB/hour".to_string());
    variables.insert("download_time".to_string(), "30 minutes".to_string());
    variables.insert("upload_rate".to_string(), "1 GiB/minute".to_string());

    let previous_results = vec![];

    // Test variable containing generic rate
    let (result, _) = evaluate_with_variables("backup_rate", &variables, &previous_results, 0);
    assert_eq!(result, Some("250 MB/h".to_string())); // Note: display shows "MB/h"

    // Test generic rate variable * time
    let (result, _) =
        evaluate_with_variables("backup_rate * 4 hours", &variables, &previous_results, 0);
    assert_eq!(result, Some("1,000 MB".to_string()));

    // Test generic rate variable * time variable (should fail - can't parse "30 minutes" as single variable)
    // This would require more complex parsing to work

    // Test with line references
    let previous_results = vec![
        Some("100 GiB/hour".to_string()),
        Some("2.5 hours".to_string()),
        Some("500 MB/minute".to_string()),
    ];

    // Test line reference with generic rate
    assert_eq!(
        evaluate_expression_with_context("line1 * 0.5 hours", &previous_results, 3),
        Some("50 GiB".to_string())
    );

    // Test multiple line references with generic rates
    assert_eq!(
        evaluate_expression_with_context("line3 * 6 seconds", &previous_results, 3),
        Some("50 MB".to_string())
    );

    // Test complex expression with line references
    // line1 is 100 GiB/hour, line3 is 500 MB/minute
    // (100 GiB/hour * 2 hours) + (500 MB/minute * 30 minutes)
    // = 200 GiB + 15,000 MB = 200 GiB + 15 GB ≈ 214.7 GiB ≈ 229,748 MB
    assert_eq!(
        evaluate_expression_with_context(
            "(line1 * 2 hours) + (line3 * 30 minutes)",
            &previous_results,
            3
        ),
        Some("229,748.365 MB".to_string())
    );
}

#[test]
fn test_generic_rates_real_world_scenarios() {
    // Data migration scenario
    assert_eq!(
        evaluate_test_expression("Migration: 50 GiB/hour * 8 hours"),
        Some("400 GiB".to_string())
    );

    // Bandwidth calculation
    assert_eq!(
        evaluate_test_expression("Monthly usage: 10 GB/day * 30 days"),
        Some("300 GB".to_string())
    );

    // Storage growth projection
    assert_eq!(
        evaluate_test_expression("Growth: 100 MB/day * 365 days to GiB"),
        Some("33.993 GiB".to_string())
    );

    // Video streaming data transfer calculation
    assert_eq!(
        evaluate_test_expression("Streaming: 25 Mb/minute * 120 minutes to GB"),
        Some("0.375 GB".to_string())
    );
}

#[test]
fn test_percentage_edge_cases() {
    // Test 0% and 100%
    assert_eq!(evaluate_test_expression("0% of 100"), Some("0".to_string()));
    assert_eq!(
        evaluate_test_expression("100% of 50"),
        Some("50".to_string())
    );

    // Test very small percentages
    assert_eq!(
        evaluate_test_expression("0.01% of 10000"),
        Some("1".to_string())
    );

    // Test very large percentages
    assert_eq!(
        evaluate_test_expression("1000% of 5"),
        Some("50".to_string())
    );

    // Test percentage parsing variations
    assert_eq!(
        evaluate_test_expression("25 % of 80"),
        Some("20".to_string())
    );
}