Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
mat_latex_path_test.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
37#include <gtest/gtest.h>
38#include <sstream>
39#include <vector>
40#include <mat_latex.H>
41
42using namespace Aleph;
43
44//============================================================================
45// Simple Matrix for Testing mat_latex.H
46//============================================================================
47
49{
50 std::vector<std::vector<int>> data;
51 long rows_, cols_;
52
53public:
54 using Entry_Type = int;
55
56 SimpleMatrix(long r, long c) : rows_(r), cols_(c)
57 {
58 data.resize(r);
59 for (long i = 0; i < r; ++i)
60 data[i].resize(c, 0);
61 }
62
63 int & operator()(long i, long j) { return data[i][j]; }
64 int operator()(long i, long j) const { return data[i][j]; }
65
66 long rows() const { return rows_; }
67 long cols() const { return cols_; }
68};
69
70//============================================================================
71// mat_latex.H Tests
72//============================================================================
73
74class MatLatexTest : public ::testing::Test
75{
76protected:
77 void SetUp() override
78 {
79 // 3x3 matrix with values 1-9
80 mat = std::make_unique<SimpleMatrix>(3, 3);
81 int val = 1;
82 for (long i = 0; i < 3; ++i)
83 for (long j = 0; j < 3; ++j)
84 (*mat)(i, j) = val++;
85 }
86
87 std::unique_ptr<SimpleMatrix> mat;
88};
89
90// Custom formatters for testing
92{
93 std::string operator()(SimpleMatrix &, long i) const
94 {
95 return "R" + std::to_string(i);
96 }
97};
98
100{
101 std::string operator()(SimpleMatrix &, long j) const
102 {
103 return "C" + std::to_string(j);
104 }
105};
106
108{
109 std::string operator()(SimpleMatrix & m, long i, long j) const
110 {
111 return std::to_string(m(i, j));
112 }
113};
114
116{
117 std::ostringstream out;
118
120 *mat, 3, 3, out);
121
122 std::string result = out.str();
123
124 // Check for LaTeX tabular environment
125 EXPECT_TRUE(result.find("\\begin{tabular}") != std::string::npos);
126 EXPECT_TRUE(result.find("\\end{tabular}") != std::string::npos);
127 EXPECT_TRUE(result.find("\\hline") != std::string::npos);
128}
129
131{
132 std::ostringstream out;
133
135 *mat, 3, 3, out);
136
137 std::string result = out.str();
138
139 EXPECT_TRUE(result.find("C0") != std::string::npos);
140 EXPECT_TRUE(result.find("C1") != std::string::npos);
141 EXPECT_TRUE(result.find("C2") != std::string::npos);
142}
143
145{
146 std::ostringstream out;
147
149 *mat, 3, 3, out);
150
151 std::string result = out.str();
152
153 EXPECT_TRUE(result.find("R0") != std::string::npos);
154 EXPECT_TRUE(result.find("R1") != std::string::npos);
155 EXPECT_TRUE(result.find("R2") != std::string::npos);
156}
157
159{
160 std::ostringstream out;
161
163 *mat, 3, 3, out);
164
165 std::string result = out.str();
166
167 // Check all values 1-9 are present
168 for (int i = 1; i <= 9; ++i)
169 EXPECT_TRUE(result.find(std::to_string(i)) != std::string::npos)
170 << "Value " << i << " not found in output";
171}
172
174{
175 std::ostringstream out;
176
178 *mat, 3, 3, out, "PREFIX_START\n", "SUFFIX_END\n");
179
180 std::string result = out.str();
181
182 EXPECT_TRUE(result.find("PREFIX_START") != std::string::npos);
183 EXPECT_TRUE(result.find("SUFFIX_END") != std::string::npos);
184
185 // Prefix should be at start
186 EXPECT_EQ(result.find("PREFIX_START"), 0u);
187}
188
190{
192 single_row(0, 0) = 10;
193 single_row(0, 1) = 20;
194 single_row(0, 2) = 30;
195
196 std::ostringstream out;
198 single_row, 1, 3, out);
199
200 std::string result = out.str();
201
202 EXPECT_TRUE(result.find("10") != std::string::npos);
203 EXPECT_TRUE(result.find("20") != std::string::npos);
204 EXPECT_TRUE(result.find("30") != std::string::npos);
205}
206
208{
210 single_col(0, 0) = 100;
211 single_col(1, 0) = 200;
212 single_col(2, 0) = 300;
213
214 std::ostringstream out;
216 single_col, 3, 1, out);
217
218 std::string result = out.str();
219
220 EXPECT_TRUE(result.find("100") != std::string::npos);
221 EXPECT_TRUE(result.find("200") != std::string::npos);
222 EXPECT_TRUE(result.find("300") != std::string::npos);
223}
224
226{
227 std::ostringstream out;
228 mat_to_latex_simple(*mat, 3, 3, out);
229
230 std::string result = out.str();
231
232 // Should contain default integer indices
233 EXPECT_TRUE(result.find("0") != std::string::npos);
234 EXPECT_TRUE(result.find("1") != std::string::npos);
235 EXPECT_TRUE(result.find("2") != std::string::npos);
236}
237
239{
240 std::ostringstream out;
242 *mat, 3, 3, out, "", "");
243
244 std::string result = out.str();
245
246 // Should start with \begin
247 EXPECT_TRUE(result.find("\\begin") == 0u);
248}
249
251{
252 std::ostringstream out;
254 *mat, 3, 3, out);
255
256 std::string result = out.str();
257
258 // Count & separators (should have 2 per data row * 3 rows + header row)
259 size_t ampersand_count = 0;
260 for (char c : result)
261 if (c == '&')
263
264 // Each row has (cols - 1) & + 1 for row header = cols &
265 // Total: (header row: cols-1) + (data rows: cols * n)
266 // 3x3: header has 2 &, each data row has 3 & (including row header sep)
268}
269
270//============================================================================
271// Edge Cases
272//============================================================================
273
274class MatLatexEdgeCaseTest : public ::testing::Test {};
275
277{
278 SimpleMatrix large(10, 10);
279 for (long i = 0; i < 10; ++i)
280 for (long j = 0; j < 10; ++j)
281 large(i, j) = static_cast<int>(i * 10 + j);
282
283 std::ostringstream out;
284 mat_to_latex_simple(large, 10, 10, out);
285
286 std::string result = out.str();
287
288 EXPECT_TRUE(result.find("\\begin{tabular}") != std::string::npos);
289 EXPECT_TRUE(result.find("99") != std::string::npos); // Last element
290}
291
293{
294 SimpleMatrix rect(2, 5);
295 for (long i = 0; i < 2; ++i)
296 for (long j = 0; j < 5; ++j)
297 rect(i, j) = static_cast<int>(i * 5 + j);
298
299 std::ostringstream out;
300 mat_to_latex_simple(rect, 2, 5, out);
301
302 std::string result = out.str();
303
304 // Should have 5 column widths
305 size_t count = 0;
306 size_t pos = 0;
307 while ((pos = result.find("p{1mm}", pos)) != std::string::npos)
308 {
309 ++count;
310 ++pos;
311 }
312 EXPECT_EQ(count, 5u);
313}
314
316{
317 SimpleMatrix single(1, 1);
318 single(0, 0) = 42;
319
320 std::ostringstream out;
322
323 std::string result = out.str();
324
325 EXPECT_TRUE(result.find("42") != std::string::npos);
326 EXPECT_TRUE(result.find("\\begin{tabular}") != std::string::npos);
327}
328
330{
331 SimpleMatrix neg(2, 2);
332 neg(0, 0) = -5;
333 neg(0, 1) = -10;
334 neg(1, 0) = 0;
335 neg(1, 1) = -100;
336
337 std::ostringstream out;
339
340 std::string result = out.str();
341
342 EXPECT_TRUE(result.find("-5") != std::string::npos);
343 EXPECT_TRUE(result.find("-10") != std::string::npos);
344 EXPECT_TRUE(result.find("-100") != std::string::npos);
345}
346
347//============================================================================
348// Default Formatter Tests
349//============================================================================
350
351class DefaultFormatterTest : public ::testing::Test {};
352
354{
355 SimpleMatrix mat(3, 3);
357
358 EXPECT_EQ(fmt(mat, 0), "0");
359 EXPECT_EQ(fmt(mat, 5), "5");
360 EXPECT_EQ(fmt(mat, 100), "100");
361}
362
364{
365 SimpleMatrix mat(3, 3);
367
368 EXPECT_EQ(fmt(mat, 0), "0");
369 EXPECT_EQ(fmt(mat, 3), "3");
370 EXPECT_EQ(fmt(mat, 99), "99");
371}
372
374{
375 SimpleMatrix mat(3, 3);
376 mat(1, 2) = 42;
377
379
380 EXPECT_EQ(fmt(mat, 1, 2), "42");
381}
382
384{
385 SimpleMatrix mat(2, 2);
386 mat(0, 0) = -999;
387
389
390 EXPECT_EQ(fmt(mat, 0, 0), "-999");
391}
392
393//============================================================================
394// Custom Formatter Tests
395//============================================================================
396
397class CustomFormatterTest : public ::testing::Test {};
398
400{
401 std::string operator()(SimpleMatrix &, long i) const
402 {
403 return "\\textbf{" + std::to_string(i) + "}";
404 }
405};
406
408{
409 std::string operator()(SimpleMatrix &, long j) const
410 {
411 return "\\textit{" + std::to_string(j) + "}";
412 }
413};
414
416{
417 std::string operator()(SimpleMatrix & m, long i, long j) const
418 {
419 int val = m(i, j);
420 if (val == std::numeric_limits<int>::max())
421 return "\\infty";
422 return std::to_string(val);
423 }
424};
425
427{
428 SimpleMatrix mat(2, 2);
429 mat(0, 0) = 1;
430 mat(0, 1) = 2;
431 mat(1, 0) = 3;
432 mat(1, 1) = 4;
433
434 std::ostringstream out;
436 mat, 2, 2, out);
437
438 std::string result = out.str();
439
440 EXPECT_TRUE(result.find("\\textbf{0}") != std::string::npos);
441 EXPECT_TRUE(result.find("\\textbf{1}") != std::string::npos);
442}
443
445{
446 SimpleMatrix mat(2, 2);
447
448 std::ostringstream out;
450 mat, 2, 2, out);
451
452 std::string result = out.str();
453
454 EXPECT_TRUE(result.find("\\textit{0}") != std::string::npos);
455 EXPECT_TRUE(result.find("\\textit{1}") != std::string::npos);
456}
457
459{
460 SimpleMatrix mat(2, 2);
461 mat(0, 0) = 0;
462 mat(0, 1) = std::numeric_limits<int>::max();
463 mat(1, 0) = std::numeric_limits<int>::max();
464 mat(1, 1) = 5;
465
466 std::ostringstream out;
468 mat, 2, 2, out);
469
470 std::string result = out.str();
471
472 EXPECT_TRUE(result.find("\\infty") != std::string::npos);
473 EXPECT_TRUE(result.find("5") != std::string::npos);
474}
475
476//============================================================================
477// Main
478//============================================================================
479
480int main(int argc, char **argv)
481{
482 ::testing::InitGoogleTest(&argc, argv);
483 return RUN_ALL_TESTS();
484}
int main()
std::unique_ptr< SimpleMatrix > mat
void SetUp() override
int & operator()(long i, long j)
int operator()(long i, long j) const
std::vector< std::vector< int > > data
SimpleMatrix(long r, long c)
Matrix to LaTeX table conversion utilities.
TEST_F(MatLatexTest, BasicOutput)
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
void mat_to_latex_simple(Mat &mat, long n, long m, std::ostream &out, const std::string &prefix="", const std::string &suffix="")
Generate LaTeX table with default formatters.
Definition mat_latex.H:275
DynList< T > maps(const C &c, Op op)
Classic map operation.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
Default column index formatter.
Definition mat_latex.H:233
Default entry formatter.
Definition mat_latex.H:250
Default row index formatter.
Definition mat_latex.H:217
std::string operator()(SimpleMatrix &, long i) const
std::string operator()(SimpleMatrix &m, long i, long j) const
std::string operator()(SimpleMatrix &, long j) const
std::string operator()(SimpleMatrix &, long j) const
std::string operator()(SimpleMatrix &m, long i, long j) const
std::string operator()(SimpleMatrix &, long i) const