File: SpreadsheetFormulaTest.cpp

package info (click to toggle)
labplot 2.12.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 78,500 kB
  • sloc: cpp: 241,048; ansic: 6,324; python: 915; xml: 400; yacc: 237; sh: 221; awk: 35; makefile: 11
file content (561 lines) | stat: -rw-r--r-- 20,138 bytes parent folder | download | duplicates (2)
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
/*
	File                 : SpreadsheetFormulaTest.cpp
	Project              : LabPlot
	Description          : Tests for formula in spreadsheets
	--------------------------------------------------------------------
	SPDX-FileCopyrightText: 2022 Stefan Gerlach <stefan.gerlach@uni.kn>
	SPDX-FileCopyrightText: 2023-2025 Alexander Semke <alexander.semke@web.de>

	SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "SpreadsheetFormulaTest.h"
#include "backend/lib/macros.h"
#include "backend/spreadsheet/Spreadsheet.h"
#include "frontend/spreadsheet/SpreadsheetView.h"

#include <QClipboard>

#define INIT_SPREADSHEET                                                                                                                                       \
	Spreadsheet sheet(QStringLiteral("test 2 cols"), false);                                                                                                   \
	const int cols = 2;                                                                                                                                        \
	const int rows = 100;                                                                                                                                      \
                                                                                                                                                               \
	sheet.setColumnCount(cols);                                                                                                                                \
	sheet.setRowCount(rows);                                                                                                                                   \
                                                                                                                                                               \
	SpreadsheetView view(&sheet, false);                                                                                                                       \
	view.selectColumn(0);                                                                                                                                      \
	view.fillWithRowNumbers();                                                                                                                                 \
                                                                                                                                                               \
	QStringList variableNames;                                                                                                                                 \
	variableNames << QLatin1String("x");                                                                                                                       \
	QVector<Column*> variableColumns;                                                                                                                          \
	variableColumns << sheet.column(0);                                                                                                                        \
	sheet.column(1)->setFormulaVariableColumn(sheet.column(0));

#define INIT_SPREADSHEET2                                                                                                                                      \
	Spreadsheet sheet(QStringLiteral("test 3 cols"), false);                                                                                                   \
	const int cols = 3;                                                                                                                                        \
	const int rows = 100;                                                                                                                                      \
                                                                                                                                                               \
	sheet.setColumnCount(cols);                                                                                                                                \
	sheet.setRowCount(rows);                                                                                                                                   \
                                                                                                                                                               \
	SpreadsheetView view(&sheet, false);                                                                                                                       \
	view.selectColumn(0);                                                                                                                                      \
	view.fillWithRowNumbers();                                                                                                                                 \
	for (int i = 0; i < rows; i++)                                                                                                                             \
		sheet.column(1)->setValueAt(i, 1.);                                                                                                                    \
                                                                                                                                                               \
	QStringList variableNames;                                                                                                                                 \
	variableNames << QStringLiteral("x") << QStringLiteral("y");                                                                                               \
	QVector<Column*> variableColumns;                                                                                                                          \
	variableColumns << sheet.column(0) << sheet.column(1);                                                                                                     \
	sheet.column(2)->setFormulaVariableColumn(sheet.column(0));                                                                                                \
	sheet.column(2)->setFormulaVariableColumn(sheet.column(1));

//**********************************************************
//********** Check different formulas **********************
//**********************************************************
/*!
   formula "1"
*/
void SpreadsheetFormulaTest::formula1() {
	INIT_SPREADSHEET

	sheet.column(1)->setFormula(QLatin1String("1"), variableNames, variableColumns, true);
	sheet.column(1)->updateFormula();

	// spreadsheet size
	QCOMPARE(sheet.columnCount(), cols);
	QCOMPARE(sheet.rowCount(), rows);

	// column modes
	QCOMPARE(sheet.column(0)->columnMode(), AbstractColumn::ColumnMode::Integer);
	QCOMPARE(sheet.column(1)->columnMode(), AbstractColumn::ColumnMode::Double);

	// values
	for (int i = 0; i < rows; i++) {
		QCOMPARE(sheet.column(0)->valueAt(i), i + 1);
		QCOMPARE(sheet.column(1)->valueAt(i), 1);
	}
}
/*!
   formula "x"
*/
void SpreadsheetFormulaTest::formulax() {
	INIT_SPREADSHEET

	sheet.column(1)->setFormula(QLatin1String("x"), variableNames, variableColumns, true);
	sheet.column(1)->updateFormula();

	// values
	for (int i = 0; i < rows; i++) {
		QCOMPARE(sheet.column(0)->valueAt(i), i + 1);
		QCOMPARE(sheet.column(1)->valueAt(i), i + 1);
	}
}
/*!
   formula "x + 1"
*/
void SpreadsheetFormulaTest::formulaxp1() {
	INIT_SPREADSHEET

	sheet.column(1)->setFormula(QLatin1String("x+1"), variableNames, variableColumns, true);
	sheet.column(1)->updateFormula();

	// values
	for (int i = 0; i < rows; i++) {
		QCOMPARE(sheet.column(0)->valueAt(i), i + 1);
		QCOMPARE(sheet.column(1)->valueAt(i), i + 2);
	}
}
//**********************************************************
//********** Check different cell() formulas ***************
//**********************************************************
/*!
   formula "cell(1, x)"
*/
void SpreadsheetFormulaTest::formulaCell1() {
	INIT_SPREADSHEET

	sheet.column(1)->setFormula(QLatin1String("cell(1; x)"), variableNames, variableColumns, true);
	sheet.column(1)->updateFormula();

	// values
	for (int i = 0; i < rows; i++) {
		QCOMPARE(sheet.column(0)->valueAt(i), i + 1);
		QCOMPARE(sheet.column(1)->valueAt(i), 1);
	}
}
/*!
   formula "cell(i, x)"
*/
void SpreadsheetFormulaTest::formulaCelli() {
	INIT_SPREADSHEET

	sheet.column(1)->setFormula(QLatin1String("cell(i; x)"), variableNames, variableColumns, true);
	sheet.column(1)->updateFormula();

	// values
	for (int i = 0; i < rows; i++) {
		QCOMPARE(sheet.column(0)->valueAt(i), i + 1);
		QCOMPARE(sheet.column(1)->valueAt(i), i + 1);
	}
}
/*!
   formula "cell(i + 1, x)"
*/
void SpreadsheetFormulaTest::formulaCellip1() {
	INIT_SPREADSHEET

	sheet.column(1)->setFormula(QLatin1String("cell(i + 1; x)"), variableNames, variableColumns, true);
	sheet.column(1)->updateFormula();

	// values
	for (int i = 0; i < rows; i++) {
		QCOMPARE(sheet.column(0)->valueAt(i), i + 1);
		if (i < rows - 1)
			QCOMPARE(sheet.column(1)->valueAt(i), i + 2);
		else
			QCOMPARE(sheet.column(1)->valueAt(i), NAN);
	}
}
/*!
   formula "cell(i - 1, x)"
*/
void SpreadsheetFormulaTest::formulaCellim1() {
	INIT_SPREADSHEET

	sheet.column(1)->setFormula(QLatin1String("cell(i - 1; x)"), variableNames, variableColumns, true);
	sheet.column(1)->updateFormula();

	// values
	for (int i = 0; i < rows; i++) {
		QCOMPARE(sheet.column(0)->valueAt(i), i + 1);
		if (i > 0)
			QCOMPARE(sheet.column(1)->valueAt(i), i);
		else
			QCOMPARE(sheet.column(1)->valueAt(i), NAN);
	}
}
/*!
   formula "cell(2*i, x)"
*/
void SpreadsheetFormulaTest::formulaCell2i() {
	INIT_SPREADSHEET

	sheet.column(1)->setFormula(QLatin1String("cell(2*i; x)"), variableNames, variableColumns, true);
	sheet.column(1)->updateFormula();

	// values
	for (int i = 0; i < rows; i++) {
		QCOMPARE(sheet.column(0)->valueAt(i), i + 1);
		if (i < rows / 2)
			QCOMPARE(sheet.column(1)->valueAt(i), 2 * (i + 1));
		else
			QCOMPARE(sheet.column(1)->valueAt(i), NAN);
	}
}
/*!
   formula "cell(i+1, x) - cell(i-1, x)"
*/
void SpreadsheetFormulaTest::formulaCellip1im1() {
	INIT_SPREADSHEET

	sheet.column(1)->setFormula(QLatin1String("cell(i+1; x) - cell(i-1; x)"), variableNames, variableColumns, true);
	sheet.column(1)->updateFormula();

	// values
	for (int i = 0; i < rows; i++) {
		QCOMPARE(sheet.column(0)->valueAt(i), i + 1);
		if (i > 0 && i < rows - 1)
			QCOMPARE(sheet.column(1)->valueAt(i), 2);
		else
			QCOMPARE(sheet.column(1)->valueAt(i), NAN);
	}
}
/*!
   formula "sqrt(cell(i+1, x))"
*/
void SpreadsheetFormulaTest::formulaCellsqrtip1() {
	INIT_SPREADSHEET

	sheet.column(1)->setFormula(QLatin1String("sqrt(cell(i+1; x))"), variableNames, variableColumns, true);
	sheet.column(1)->updateFormula();

	// values
	for (int i = 0; i < rows; i++) {
		QCOMPARE(sheet.column(0)->valueAt(i), i + 1);
		if (i < rows - 1)
			QCOMPARE(sheet.column(1)->valueAt(i), std::sqrt(i + 2));
		else
			QCOMPARE(sheet.column(1)->valueAt(i), NAN);
	}
}

/*!
   formula "cell(1, 2*x)"
*/
void SpreadsheetFormulaTest::formulaCell1_2x() {
	INIT_SPREADSHEET

	sheet.column(1)->setFormula(QLatin1String("2*cell(1; x)"), variableNames, variableColumns, true);
	sheet.column(1)->updateFormula();

	// values
	for (int i = 0; i < rows; i++) {
		QCOMPARE(sheet.column(0)->valueAt(i), i + 1);
		QCOMPARE(sheet.column(1)->valueAt(i), 2);
	}
}
/*!
   formula "cell(i, 2*x)"
*/
void SpreadsheetFormulaTest::formulaCelli_2x() {
	INIT_SPREADSHEET

	sheet.column(1)->setFormula(QLatin1String("cell(i; x) * 2"), variableNames, variableColumns, true);
	sheet.column(1)->updateFormula();

	// values
	for (int i = 0; i < rows; i++) {
		QCOMPARE(sheet.column(0)->valueAt(i), i + 1);
		QCOMPARE(sheet.column(1)->valueAt(i), 2 * (i + 1));
	}
}
/*!
   formula "cell(1, x+x)"
*/
void SpreadsheetFormulaTest::formulaCelli_xpx() {
	INIT_SPREADSHEET

	sheet.column(1)->setFormula(QLatin1String("cell(i; x) * 2"), variableNames, variableColumns, true);
	sheet.column(1)->updateFormula();

	// values
	for (int i = 0; i < rows; i++) {
		QCOMPARE(sheet.column(0)->valueAt(i), i + 1);
		QCOMPARE(sheet.column(1)->valueAt(i), 2 * (i + 1));
	}
}
/*!
   formula "cell(i, x + 2*x)"
*/
void SpreadsheetFormulaTest::formulaCelli_xp2x() {
	INIT_SPREADSHEET

	sheet.column(1)->setFormula(QLatin1String("3 * cell(i; x)"), variableNames, variableColumns, true);
	sheet.column(1)->updateFormula();

	// values
	for (int i = 0; i < rows; i++) {
		QCOMPARE(sheet.column(0)->valueAt(i), i + 1);
		QCOMPARE(sheet.column(1)->valueAt(i), 3 * (i + 1));
	}
}
/*!
   formula "cell(i, sqrt(x))"
*/
void SpreadsheetFormulaTest::formulaCelli_sqrtx() {
	INIT_SPREADSHEET

	sheet.column(1)->setFormula(QLatin1String("sqrt(cell(i; x))"), variableNames, variableColumns, true);
	sheet.column(1)->updateFormula();

	// values
	for (int i = 0; i < rows; i++) {
		QCOMPARE(sheet.column(0)->valueAt(i), i + 1);
		QCOMPARE(sheet.column(1)->valueAt(i), std::sqrt(i + 1));
	}
}
/*!
   formula "cell(i, x+y)"
*/
void SpreadsheetFormulaTest::formulaCelli_xpy() {
	INIT_SPREADSHEET2

	sheet.column(2)->setFormula(QLatin1String("cell(i; x) + cell(i; y)"), variableNames, variableColumns, true);
	sheet.column(2)->updateFormula();

	// values
	for (int i = 0; i < rows; i++) {
		QCOMPARE(sheet.column(0)->valueAt(i), i + 1);
		QCOMPARE(sheet.column(1)->valueAt(i), 1);
		QCOMPARE(sheet.column(2)->valueAt(i), i + 2);
	}
}

/*!
   formula "cell(2*i, x+y)"
*/
void SpreadsheetFormulaTest::formulaCell2i_xpy() {
	INIT_SPREADSHEET2

	sheet.column(2)->setFormula(QLatin1String("cell(2*i; x) + cell(2*i; y)"), variableNames, variableColumns, true);
	sheet.column(2)->updateFormula();

	// values
	for (int i = 0; i < rows; i++) {
		QCOMPARE(sheet.column(0)->valueAt(i), i + 1);
		QCOMPARE(sheet.column(1)->valueAt(i), 1);
		if (i < rows / 2)
			QCOMPARE(sheet.column(2)->valueAt(i), sheet.column(0)->valueAt(2 * i + 1) + sheet.column(1)->valueAt(2 * i + 1));
		else
			QCOMPARE(sheet.column(2)->valueAt(i), NAN);
	}
}
/*!
   formula "cell(i, 2*x) + cell (i, 2*y)"
*/
void SpreadsheetFormulaTest::formulaCelli_2xpCelli_2y() {
	INIT_SPREADSHEET2

	sheet.column(2)->setFormula(QLatin1String("2*cell(i; x) + cell(i; y) * 2"), variableNames, variableColumns, true);
	sheet.column(2)->updateFormula();

	// values
	for (int i = 0; i < rows; i++) {
		QCOMPARE(sheet.column(0)->valueAt(i), i + 1);
		QCOMPARE(sheet.column(1)->valueAt(i), 1);
		QCOMPARE(sheet.column(2)->valueAt(i), 2 * sheet.column(0)->valueAt(i) + 2 * sheet.column(1)->valueAt(i));
	}
}

///////////////////////// check group separator problem /////////////////////

void SpreadsheetFormulaTest::formulaLocale() {
	Spreadsheet sheet(QStringLiteral("test"), false);
	const int cols = 2;
	const int rows = 3;
	const QVector<double> xData{13000, 14000, 15000};

	sheet.setColumnCount(cols);
	sheet.setRowCount(rows);
	auto* col0{sheet.column(0)};
	col0->replaceValues(0, xData);

	SpreadsheetView view(&sheet, false);
	view.selectColumn(0);

	QStringList variableNames;
	variableNames << QLatin1String("x");
	QVector<Column*> variableColumns;
	variableColumns << sheet.column(0);
	sheet.column(1)->setFormulaVariableColumn(sheet.column(0));

	sheet.column(1)->setFormula(QLatin1String("mean(x)"), variableNames, variableColumns, true);
	sheet.column(1)->updateFormula();

	// values
	for (int i = 0; i < rows; i++)
		QCOMPARE(sheet.column(1)->valueAt(i), sheet.column(0)->valueAt(1));
}

///////////////////////// more methods /////////////////////

/*!
   formula "ma(x)"
*/
void SpreadsheetFormulaTest::formulama() {
	INIT_SPREADSHEET

	sheet.column(1)->setFormula(QLatin1String("ma(x)"), variableNames, variableColumns, true);
	sheet.column(1)->updateFormula();

	// values
	for (int i = 0; i < rows; i++) {
		QCOMPARE(sheet.column(0)->valueAt(i), i + 1);
		if (i > 0)
			QCOMPARE(sheet.column(1)->valueAt(i), i + .5);
		else
			QCOMPARE(sheet.column(1)->valueAt(i), NAN);
	}
}
/*!
   formula "mr(x)"
*/
void SpreadsheetFormulaTest::formulamr() {
	INIT_SPREADSHEET

	sheet.column(1)->setFormula(QLatin1String("mr(x)"), variableNames, variableColumns, true);
	sheet.column(1)->updateFormula();

	// values
	for (int i = 0; i < rows; i++) {
		QCOMPARE(sheet.column(0)->valueAt(i), i + 1);
		if (i > 0)
			QCOMPARE(sheet.column(1)->valueAt(i), 1);
		else
			QCOMPARE(sheet.column(1)->valueAt(i), NAN);
	}
}
/*!
   formula "sma(n, x)"
*/
void SpreadsheetFormulaTest::formulasma() {
	INIT_SPREADSHEET

	sheet.column(1)->setFormula(QLatin1String("sma(4; x)"), variableNames, variableColumns, true);
	sheet.column(1)->updateFormula();

	// values
	const int N = 4;
	for (int i = 0; i < rows; i++) {
		QCOMPARE(sheet.column(0)->valueAt(i), i + 1);
		double value = 0.;
		for (int index = std::max(0, i - N + 1); index <= i; index++)
			value += sheet.column(0)->valueAt(index);
		QCOMPARE(sheet.column(1)->valueAt(i), value / N);
	}
}
/*!
   formula "smr(n, x)"
*/
void SpreadsheetFormulaTest::formulasmr() {
	INIT_SPREADSHEET

	sheet.column(1)->setFormula(QLatin1String("smr(4; x)"), variableNames, variableColumns, true);
	sheet.column(1)->updateFormula();

	// values
	QCOMPARE(sheet.column(1)->valueAt(0), 0);
	QCOMPARE(sheet.column(1)->valueAt(1), 1);
	QCOMPARE(sheet.column(1)->valueAt(2), 2);
	for (int i = 3; i < rows; i++) {
		QCOMPARE(sheet.column(0)->valueAt(i), i + 1);
		QCOMPARE(sheet.column(1)->valueAt(i), 3);
	}
}

// ##############################################################################
// ######### check updates of columns defined via a formula on changes ##########
// ##############################################################################
void SpreadsheetFormulaTest::formulaUpdateAfterCellChange() {
	INIT_SPREADSHEET

	auto* col1 = sheet.column(0);
	auto* col2 = sheet.column(1);
	col2->setFormula(QLatin1String("x"), variableNames, variableColumns, true);
	col2->updateFormula();

	// values
	for (int i = 0; i < rows; i++) {
		QCOMPARE(col1->valueAt(i), i + 1);
		QCOMPARE(col2->valueAt(i), i + 1);
	}

	// modify the first cell in the first column and check the updated values
	col1->setIntegerAt(0, 5);

	// check the first row
	QCOMPARE(col1->valueAt(0), 5);
	QCOMPARE(col2->valueAt(0), 5);

	// check the remaining rows
	for (int i = 1; i < rows; i++) {
		QCOMPARE(col1->valueAt(i), i + 1);
		QCOMPARE(col2->valueAt(i), i + 1);
	}
}

void SpreadsheetFormulaTest::formulaUpdateAfterPaste() {
	INIT_SPREADSHEET

	auto* col1 = sheet.column(0);
	auto* col2 = sheet.column(1);
	col2->setFormula(QLatin1String("x"), variableNames, variableColumns, true);
	col2->updateFormula();

	// values
	for (int i = 0; i < rows; i++) {
		QCOMPARE(col1->valueAt(i), i + 1);
		QCOMPARE(col2->valueAt(i), i + 1);
	}

	// paste three values into the first column and check the updated values
	const QString str = QStringLiteral("10\n20\n30");
	QApplication::clipboard()->setText(str);
	SpreadsheetView viewToPaste(&sheet, false);
	viewToPaste.pasteIntoSelection();

	// check the first three rows
	QCOMPARE(col1->valueAt(0), 10);
	QCOMPARE(col2->valueAt(0), 10);
	QCOMPARE(col1->valueAt(1), 20);
	QCOMPARE(col2->valueAt(1), 20);
	QCOMPARE(col1->valueAt(2), 30);
	QCOMPARE(col2->valueAt(2), 30);

	// check the remaining rows
	for (int i = 3; i < rows; i++) {
		QCOMPARE(col1->valueAt(i), i + 1);
		QCOMPARE(col2->valueAt(i), i + 1);
	}
}

void SpreadsheetFormulaTest::formulaUpdateAfterRowRemoval() {
	INIT_SPREADSHEET2

	sheet.column(2)->setFormula(QLatin1String("x + y"), variableNames, variableColumns, true);
	sheet.column(2)->updateFormula();

	// check the initial values
	for (int i = 0; i < sheet.rowCount(); i++) {
		QCOMPARE(sheet.column(0)->valueAt(i), i + 1);
		QCOMPARE(sheet.column(1)->valueAt(i), 1);
		QCOMPARE(sheet.column(2)->valueAt(i), i + 2);
	}

	// select the rows from 10 to 15 and delete them
	view.setCellsSelected(9, 0, 14, 2, true);
	view.removeSelectedRows();

	// re-check the values in the calculated column
	QCOMPARE(sheet.rowCount(), 94);
	for (int i = 0; i < sheet.rowCount(); i++)
		QCOMPARE(sheet.column(2)->valueAt(i), sheet.column(0)->valueAt(i) + 1);
}

QTEST_MAIN(SpreadsheetFormulaTest)