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
//! Display quality tests — verify clean subtraction rendering and LaTeX output.
//!
//! These tests ensure that the Display impl never produces ugly patterns like
//! `+ -` and that negative terms are rendered with proper subtraction syntax.
use symplex::prelude::*;
// ═══════════════════════════════════════════════════════════════════════════
// 1. x + (-3) displays as "x - 3"
// ═══════════════════════════════════════════════════════════════════════════
#[test]
fn display_sub_neg_int() {
let ctx = Context::new();
symplex::syms!(ctx; x);
let expr = &x + ctx.int(-3);
let s = format!("{expr}");
assert!(s.contains("- 3"), "expected 'x - 3' pattern, got: {s}");
assert!(!s.contains("+ -"), "must not contain '+ -', got: {s}");
}
// ═══════════════════════════════════════════════════════════════════════════
// 2. x + (-1/2) displays as "x - 1/2"
// ═══════════════════════════════════════════════════════════════════════════
#[test]
fn display_sub_neg_frac() {
let ctx = Context::new();
symplex::syms!(ctx; x);
let expr = &x + ctx.rational(-1, 2);
let s = format!("{expr}");
assert!(s.contains("- 1/2"), "expected '- 1/2' pattern, got: {s}");
assert!(!s.contains("+ -"), "must not contain '+ -', got: {s}");
}
// ═══════════════════════════════════════════════════════════════════════════
// 3. x - y displays as "x - y" (no "+ -")
// ═══════════════════════════════════════════════════════════════════════════
#[test]
fn display_sub_neg_symbol() {
let ctx = Context::new();
symplex::syms!(ctx; x, y);
let expr = &x - &y;
let s = format!("{expr}");
assert!(s.contains("- y"), "expected '- y' pattern, got: {s}");
assert!(!s.contains("+ -"), "must not contain '+ -', got: {s}");
}
// ═══════════════════════════════════════════════════════════════════════════
// 4. -3 + x displays correctly (canonical ordering should give "x - 3")
// ═══════════════════════════════════════════════════════════════════════════
#[test]
fn display_leading_neg_int() {
let ctx = Context::new();
symplex::syms!(ctx; x);
let expr = ctx.int(-3) + &x;
let s = format!("{expr}");
assert!(!s.contains("+ -"), "must not contain '+ -', got: {s}");
// The display should have a subtraction for the negative constant
assert!(
s.contains("- 3"),
"expected '- 3' somewhere in output, got: {s}"
);
}
// ═══════════════════════════════════════════════════════════════════════════
// 5. (-x) + (-y) displays as "-x - y"
// ═══════════════════════════════════════════════════════════════════════════
#[test]
fn display_double_neg() {
let ctx = Context::new();
symplex::syms!(ctx; x, y);
let expr = -&x + (-&y);
let s = format!("{expr}");
assert!(!s.contains("+ -"), "must not contain '+ -', got: {s}");
// Should have a leading minus and a subtraction
assert!(
s.contains('-'),
"expected at least one minus sign, got: {s}"
);
}
// ═══════════════════════════════════════════════════════════════════════════
// 6. x + (-2*y) displays as "x - 2*y"
// ═══════════════════════════════════════════════════════════════════════════
#[test]
fn display_neg_mul_coeff() {
let ctx = Context::new();
symplex::syms!(ctx; x, y);
let expr = &x + ctx.int(-2) * &y;
let s = format!("{expr}");
assert!(!s.contains("+ -"), "must not contain '+ -', got: {s}");
assert!(
s.contains("- 2*y") || s.contains("- 2y"),
"expected subtraction of 2*y term, got: {s}"
);
}
// ═══════════════════════════════════════════════════════════════════════════
// 7. Build 10 expressions with negative terms — NONE contain "+ -"
// ═══════════════════════════════════════════════════════════════════════════
#[test]
fn display_no_plus_minus_anywhere() {
let ctx = Context::new();
symplex::syms!(ctx; x, y, z);
let exprs: Vec<Ex> = vec![
// 1: x + (-3)
&x + ctx.int(-3),
// 2: x - y
&x - &y,
// 3: x + (-1/2)
&x + ctx.rational(-1, 2),
// 4: (-x) + (-y)
-&x + (-&y),
// 5: x + (-2*y)
&x + ctx.int(-2) * &y,
// 6: (-3) + x + (-y)
ctx.int(-3) + &x + (-&y),
// 7: x + y + (-z)
&x + &y - &z,
// 8: (-x) + y
-&x + &y,
// 9: x + (-5/3)
&x + ctx.rational(-5, 3),
// 10: x + (-1)*y + (-2)*z
&x + ctx.int(-1) * &y + ctx.int(-2) * &z,
];
for (i, e) in exprs.iter().enumerate() {
let s = format!("{e}");
assert!(
!s.contains("+ -"),
"expression {} contains '+ -': {s}",
i + 1
);
}
}
// ═══════════════════════════════════════════════════════════════════════════
// 8. -1 * x displays as "-x", not "-1*x"
// ═══════════════════════════════════════════════════════════════════════════
#[test]
fn display_mul_neg_one_invisible() {
let ctx = Context::new();
symplex::syms!(ctx; x);
let expr = ctx.int(-1) * &x;
let s = format!("{expr}");
assert_eq!(s, "-x", "expected '-x', got: {s}");
}
// ═══════════════════════════════════════════════════════════════════════════
// 9. (-3/4)*x + y displays cleanly (no "+ -")
// ═══════════════════════════════════════════════════════════════════════════
#[test]
fn display_neg_fraction_coeff() {
let ctx = Context::new();
symplex::syms!(ctx; x, y);
let expr = ctx.rational(-3, 4) * &x + &y;
let s = format!("{expr}");
assert!(!s.contains("+ -"), "must not contain '+ -', got: {s}");
// y should appear without a negative prefix, and the fraction term
// should use subtraction syntax
assert!(s.contains('y'), "expected 'y' in output, got: {s}");
}
// ═══════════════════════════════════════════════════════════════════════════
// 10. Matrix::to_latex() produces valid LaTeX with bmatrix environment
// ═══════════════════════════════════════════════════════════════════════════
#[test]
fn matrix_to_latex_works() {
let ctx = Context::new();
let m = matrix![ctx, [1, 2], [3, 4]];
let latex = m.to_latex();
assert!(
latex.contains("\\begin{bmatrix}"),
"expected \\begin{{bmatrix}} in LaTeX output, got: {latex}"
);
assert!(
latex.contains("\\end{bmatrix}"),
"expected \\end{{bmatrix}} in LaTeX output, got: {latex}"
);
}