-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_liveledger.c
More file actions
1794 lines (1377 loc) · 65 KB
/
test_liveledger.c
File metadata and controls
1794 lines (1377 loc) · 65 KB
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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// test_liveledger.c - Comprehensive Unit Tests for LiveLedger
// Compile with: cl /O2 /W3 /TC test_liveledger.c sheet.c console.c charts.c /Fe:test_liveledger.exe /link user32.lib
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include <windows.h>
// Include the headers
#include "sheet.h"
#include "console.h"
#include "constants.h"
// Test framework macros
#define TEST_ASSERT(condition, msg) do { \
tests_run++; \
if (!(condition)) { \
printf(" FAIL: %s (line %d)\n", msg, __LINE__); \
tests_failed++; \
} else { \
tests_passed++; \
} \
} while(0)
#define TEST_ASSERT_EQ_DOUBLE(expected, actual, tolerance, msg) do { \
tests_run++; \
if (fabs((expected) - (actual)) > (tolerance)) { \
printf(" FAIL: %s - expected %f, got %f (line %d)\n", msg, (expected), (actual), __LINE__); \
tests_failed++; \
} else { \
tests_passed++; \
} \
} while(0)
#define TEST_ASSERT_EQ_INT(expected, actual, msg) do { \
tests_run++; \
if ((expected) != (actual)) { \
printf(" FAIL: %s - expected %d, got %d (line %d)\n", msg, (expected), (actual), __LINE__); \
tests_failed++; \
} else { \
tests_passed++; \
} \
} while(0)
#define TEST_ASSERT_EQ_STR(expected, actual, msg) do { \
tests_run++; \
if ((expected) == NULL && (actual) == NULL) { \
tests_passed++; \
} else if ((expected) == NULL || (actual) == NULL || strcmp((expected), (actual)) != 0) { \
printf(" FAIL: %s - expected '%s', got '%s' (line %d)\n", msg, (expected) ? (expected) : "NULL", (actual) ? (actual) : "NULL", __LINE__); \
tests_failed++; \
} else { \
tests_passed++; \
} \
} while(0)
#define TEST_SECTION(name) printf("\n=== Testing %s ===\n", name)
// Global test counters
static int tests_run = 0;
static int tests_passed = 0;
static int tests_failed = 0;
// ============================================================================
// SHEET OPERATIONS TESTS
// ============================================================================
void test_sheet_creation(void) {
TEST_SECTION("Sheet Creation");
// Test basic sheet creation
Sheet* sheet = sheet_new(100, 26);
TEST_ASSERT(sheet != NULL, "sheet_new should return non-NULL");
TEST_ASSERT_EQ_INT(100, sheet->rows, "Sheet should have 100 rows");
TEST_ASSERT_EQ_INT(26, sheet->cols, "Sheet should have 26 columns");
TEST_ASSERT(sheet->cells != NULL, "Cells array should be allocated");
TEST_ASSERT(sheet->col_widths != NULL, "Column widths array should be allocated");
TEST_ASSERT(sheet->row_heights != NULL, "Row heights array should be allocated");
TEST_ASSERT(sheet->name != NULL, "Sheet name should be set");
TEST_ASSERT_EQ_STR("Sheet1", sheet->name, "Default sheet name should be 'Sheet1'");
// Test default column widths
for (int i = 0; i < 26; i++) {
TEST_ASSERT_EQ_INT(DEFAULT_COLUMN_WIDTH, sheet->col_widths[i], "Default column width should be DEFAULT_COLUMN_WIDTH");
}
// Test default row heights
for (int i = 0; i < 100; i++) {
TEST_ASSERT_EQ_INT(1, sheet->row_heights[i], "Default row height should be 1");
}
sheet_free(sheet);
// Test larger sheet creation
Sheet* large_sheet = sheet_new(1000, 100);
TEST_ASSERT(large_sheet != NULL, "Large sheet should be created");
TEST_ASSERT_EQ_INT(1000, large_sheet->rows, "Large sheet should have 1000 rows");
TEST_ASSERT_EQ_INT(100, large_sheet->cols, "Large sheet should have 100 columns");
sheet_free(large_sheet);
// Test minimum sheet
Sheet* small_sheet = sheet_new(1, 1);
TEST_ASSERT(small_sheet != NULL, "Minimum 1x1 sheet should be created");
sheet_free(small_sheet);
}
void test_sheet_get_cell(void) {
TEST_SECTION("Sheet Get Cell");
Sheet* sheet = sheet_new(100, 26);
// Test getting cell that doesn't exist yet
Cell* cell = sheet_get_cell(sheet, 0, 0);
TEST_ASSERT(cell == NULL, "Cell should be NULL before creation");
// Test out-of-bounds access
cell = sheet_get_cell(sheet, -1, 0);
TEST_ASSERT(cell == NULL, "Negative row should return NULL");
cell = sheet_get_cell(sheet, 0, -1);
TEST_ASSERT(cell == NULL, "Negative column should return NULL");
cell = sheet_get_cell(sheet, 100, 0);
TEST_ASSERT(cell == NULL, "Row >= rows should return NULL");
cell = sheet_get_cell(sheet, 0, 26);
TEST_ASSERT(cell == NULL, "Column >= cols should return NULL");
sheet_free(sheet);
}
void test_sheet_get_or_create_cell(void) {
TEST_SECTION("Sheet Get or Create Cell");
Sheet* sheet = sheet_new(100, 26);
// Test creating cell
Cell* cell = sheet_get_or_create_cell(sheet, 5, 5);
TEST_ASSERT(cell != NULL, "Cell should be created");
TEST_ASSERT_EQ_INT(5, cell->row, "Cell row should be 5");
TEST_ASSERT_EQ_INT(5, cell->col, "Cell column should be 5");
TEST_ASSERT_EQ_INT(CELL_EMPTY, cell->type, "New cell should be CELL_EMPTY");
// Test getting same cell again
Cell* same_cell = sheet_get_or_create_cell(sheet, 5, 5);
TEST_ASSERT(same_cell == cell, "Should return same cell pointer");
// Test out-of-bounds
Cell* oob_cell = sheet_get_or_create_cell(sheet, 100, 0);
TEST_ASSERT(oob_cell == NULL, "Out-of-bounds should return NULL");
sheet_free(sheet);
}
// ============================================================================
// CELL OPERATIONS TESTS
// ============================================================================
void test_cell_creation(void) {
TEST_SECTION("Cell Creation");
Cell* cell = cell_new(10, 20);
TEST_ASSERT(cell != NULL, "cell_new should return non-NULL");
TEST_ASSERT_EQ_INT(10, cell->row, "Cell row should be 10");
TEST_ASSERT_EQ_INT(20, cell->col, "Cell column should be 20");
TEST_ASSERT_EQ_INT(CELL_EMPTY, cell->type, "New cell should be CELL_EMPTY");
TEST_ASSERT_EQ_INT(10, cell->width, "Default cell width should be 10");
TEST_ASSERT_EQ_INT(2, cell->precision, "Default precision should be 2");
TEST_ASSERT_EQ_INT(2, cell->align, "Default alignment should be 2 (right)");
TEST_ASSERT_EQ_INT(FORMAT_GENERAL, cell->format, "Default format should be FORMAT_GENERAL");
TEST_ASSERT_EQ_INT(-1, cell->text_color, "Default text color should be -1");
TEST_ASSERT_EQ_INT(-1, cell->background_color, "Default background color should be -1");
cell_free(cell);
}
void test_cell_set_number(void) {
TEST_SECTION("Cell Set Number");
Sheet* sheet = sheet_new(100, 26);
// Test setting a number
sheet_set_number(sheet, 0, 0, 42.5);
Cell* cell = sheet_get_cell(sheet, 0, 0);
TEST_ASSERT(cell != NULL, "Cell should exist after setting number");
TEST_ASSERT_EQ_INT(CELL_NUMBER, cell->type, "Cell type should be CELL_NUMBER");
TEST_ASSERT_EQ_DOUBLE(42.5, cell->data.number, 0.0001, "Cell value should be 42.5");
// Test setting zero
sheet_set_number(sheet, 1, 0, 0.0);
cell = sheet_get_cell(sheet, 1, 0);
TEST_ASSERT_EQ_INT(CELL_NUMBER, cell->type, "Cell type should be CELL_NUMBER for zero");
TEST_ASSERT_EQ_DOUBLE(0.0, cell->data.number, 0.0001, "Cell value should be 0.0");
// Test setting negative number
sheet_set_number(sheet, 2, 0, -123.456);
cell = sheet_get_cell(sheet, 2, 0);
TEST_ASSERT_EQ_DOUBLE(-123.456, cell->data.number, 0.0001, "Cell value should be -123.456");
// Test setting very large number
sheet_set_number(sheet, 3, 0, 1e15);
cell = sheet_get_cell(sheet, 3, 0);
TEST_ASSERT_EQ_DOUBLE(1e15, cell->data.number, 1e9, "Cell value should be 1e15");
// Test setting very small number
sheet_set_number(sheet, 4, 0, 1e-10);
cell = sheet_get_cell(sheet, 4, 0);
TEST_ASSERT_EQ_DOUBLE(1e-10, cell->data.number, 1e-15, "Cell value should be 1e-10");
sheet_free(sheet);
}
void test_cell_set_string(void) {
TEST_SECTION("Cell Set String");
Sheet* sheet = sheet_new(100, 26);
// Test setting a string
sheet_set_string(sheet, 0, 0, "Hello World");
Cell* cell = sheet_get_cell(sheet, 0, 0);
TEST_ASSERT(cell != NULL, "Cell should exist after setting string");
TEST_ASSERT_EQ_INT(CELL_STRING, cell->type, "Cell type should be CELL_STRING");
TEST_ASSERT_EQ_STR("Hello World", cell->data.string, "Cell string should be 'Hello World'");
TEST_ASSERT_EQ_INT(0, cell->align, "String cells should be left-aligned");
// Test setting empty string
sheet_set_string(sheet, 1, 0, "");
cell = sheet_get_cell(sheet, 1, 0);
TEST_ASSERT_EQ_INT(CELL_STRING, cell->type, "Cell type should be CELL_STRING for empty");
TEST_ASSERT_EQ_STR("", cell->data.string, "Cell string should be empty");
// Test setting string with special characters
sheet_set_string(sheet, 2, 0, "Test, with \"quotes\" and \nnewline");
cell = sheet_get_cell(sheet, 2, 0);
TEST_ASSERT_EQ_STR("Test, with \"quotes\" and \nnewline", cell->data.string, "String with special chars");
// Test overwriting number with string
sheet_set_number(sheet, 3, 0, 100.0);
sheet_set_string(sheet, 3, 0, "Now a string");
cell = sheet_get_cell(sheet, 3, 0);
TEST_ASSERT_EQ_INT(CELL_STRING, cell->type, "Cell type should change to CELL_STRING");
TEST_ASSERT_EQ_STR("Now a string", cell->data.string, "Cell should now be a string");
sheet_free(sheet);
}
void test_cell_set_formula(void) {
TEST_SECTION("Cell Set Formula");
Sheet* sheet = sheet_new(100, 26);
// Set up some values for formulas to reference
sheet_set_number(sheet, 0, 0, 10.0); // A1 = 10
sheet_set_number(sheet, 0, 1, 20.0); // B1 = 20
// Test setting a formula
sheet_set_formula(sheet, 0, 2, "=A1+B1");
Cell* cell = sheet_get_cell(sheet, 0, 2);
TEST_ASSERT(cell != NULL, "Cell should exist after setting formula");
TEST_ASSERT_EQ_INT(CELL_FORMULA, cell->type, "Cell type should be CELL_FORMULA");
TEST_ASSERT_EQ_STR("=A1+B1", cell->data.formula.expression, "Formula expression should be stored");
// Recalculate and check result
sheet_recalculate(sheet);
TEST_ASSERT_EQ_INT(ERROR_NONE, cell->data.formula.error, "Formula should have no error");
TEST_ASSERT_EQ_DOUBLE(30.0, cell->data.formula.cached_value, 0.0001, "Formula result should be 30");
sheet_free(sheet);
}
void test_cell_clear(void) {
TEST_SECTION("Cell Clear");
Sheet* sheet = sheet_new(100, 26);
// Set a number and clear it
sheet_set_number(sheet, 0, 0, 42.0);
sheet_clear_cell(sheet, 0, 0);
Cell* cell = sheet_get_cell(sheet, 0, 0);
TEST_ASSERT_EQ_INT(CELL_EMPTY, cell->type, "Cell should be CELL_EMPTY after clearing");
// Set a string and clear it
sheet_set_string(sheet, 1, 0, "Test");
sheet_clear_cell(sheet, 1, 0);
cell = sheet_get_cell(sheet, 1, 0);
TEST_ASSERT_EQ_INT(CELL_EMPTY, cell->type, "String cell should be CELL_EMPTY after clearing");
// Set a formula and clear it
sheet_set_formula(sheet, 2, 0, "=1+1");
sheet_clear_cell(sheet, 2, 0);
cell = sheet_get_cell(sheet, 2, 0);
TEST_ASSERT_EQ_INT(CELL_EMPTY, cell->type, "Formula cell should be CELL_EMPTY after clearing");
// Clear non-existent cell (should not crash)
sheet_clear_cell(sheet, 50, 50);
sheet_free(sheet);
}
// ============================================================================
// CELL REFERENCE TESTS
// ============================================================================
void test_cell_reference_parsing(void) {
TEST_SECTION("Cell Reference Parsing");
int row, col;
// Test simple references
TEST_ASSERT(parse_cell_reference("A1", &row, &col), "Should parse A1");
TEST_ASSERT_EQ_INT(0, row, "A1 row should be 0");
TEST_ASSERT_EQ_INT(0, col, "A1 col should be 0");
TEST_ASSERT(parse_cell_reference("B1", &row, &col), "Should parse B1");
TEST_ASSERT_EQ_INT(0, row, "B1 row should be 0");
TEST_ASSERT_EQ_INT(1, col, "B1 col should be 1");
TEST_ASSERT(parse_cell_reference("Z1", &row, &col), "Should parse Z1");
TEST_ASSERT_EQ_INT(0, row, "Z1 row should be 0");
TEST_ASSERT_EQ_INT(25, col, "Z1 col should be 25");
// Test two-letter columns
TEST_ASSERT(parse_cell_reference("AA1", &row, &col), "Should parse AA1");
TEST_ASSERT_EQ_INT(0, row, "AA1 row should be 0");
TEST_ASSERT_EQ_INT(26, col, "AA1 col should be 26");
TEST_ASSERT(parse_cell_reference("AB1", &row, &col), "Should parse AB1");
TEST_ASSERT_EQ_INT(0, row, "AB1 row should be 0");
TEST_ASSERT_EQ_INT(27, col, "AB1 col should be 27");
// Test larger row numbers
TEST_ASSERT(parse_cell_reference("A100", &row, &col), "Should parse A100");
TEST_ASSERT_EQ_INT(99, row, "A100 row should be 99");
TEST_ASSERT_EQ_INT(0, col, "A100 col should be 0");
TEST_ASSERT(parse_cell_reference("C999", &row, &col), "Should parse C999");
TEST_ASSERT_EQ_INT(998, row, "C999 row should be 998");
TEST_ASSERT_EQ_INT(2, col, "C999 col should be 2");
// Test with whitespace
TEST_ASSERT(parse_cell_reference(" A1", &row, &col), "Should parse with leading whitespace");
TEST_ASSERT_EQ_INT(0, row, "Row should be 0");
TEST_ASSERT_EQ_INT(0, col, "Col should be 0");
// Test invalid references
TEST_ASSERT(!parse_cell_reference("1A", &row, &col), "Should not parse 1A");
TEST_ASSERT(!parse_cell_reference("A", &row, &col), "Should not parse A (no number)");
TEST_ASSERT(!parse_cell_reference("1", &row, &col), "Should not parse 1 (no letter)");
TEST_ASSERT(!parse_cell_reference("", &row, &col), "Should not parse empty string");
TEST_ASSERT(!parse_cell_reference("A1B", &row, &col), "Should not parse A1B (trailing garbage)");
}
void test_cell_reference_to_string(void) {
TEST_SECTION("Cell Reference to String");
char buffer[16];
cell_reference_to_string(0, 0, buffer, sizeof(buffer));
TEST_ASSERT_EQ_STR("A1", buffer, "Row 0, Col 0 should be A1");
cell_reference_to_string(0, 1, buffer, sizeof(buffer));
TEST_ASSERT_EQ_STR("B1", buffer, "Row 0, Col 1 should be B1");
cell_reference_to_string(0, 25, buffer, sizeof(buffer));
TEST_ASSERT_EQ_STR("Z1", buffer, "Row 0, Col 25 should be Z1");
cell_reference_to_string(0, 26, buffer, sizeof(buffer));
TEST_ASSERT_EQ_STR("AA1", buffer, "Row 0, Col 26 should be AA1");
cell_reference_to_string(0, 27, buffer, sizeof(buffer));
TEST_ASSERT_EQ_STR("AB1", buffer, "Row 0, Col 27 should be AB1");
cell_reference_to_string(99, 0, buffer, sizeof(buffer));
TEST_ASSERT_EQ_STR("A100", buffer, "Row 99, Col 0 should be A100");
cell_reference_to_string(999, 51, buffer, sizeof(buffer));
TEST_ASSERT_EQ_STR("AZ1000", buffer, "Row 999, Col 51 should be AZ1000");
}
// ============================================================================
// FORMULA EVALUATION TESTS
// ============================================================================
void test_basic_arithmetic(void) {
TEST_SECTION("Basic Arithmetic");
Sheet* sheet = sheet_new(100, 26);
// Addition
sheet_set_formula(sheet, 0, 0, "=1+2");
sheet_recalculate(sheet);
Cell* cell = sheet_get_cell(sheet, 0, 0);
TEST_ASSERT_EQ_DOUBLE(3.0, cell->data.formula.cached_value, 0.0001, "1+2 should be 3");
// Subtraction
sheet_set_formula(sheet, 1, 0, "=10-7");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 1, 0);
TEST_ASSERT_EQ_DOUBLE(3.0, cell->data.formula.cached_value, 0.0001, "10-7 should be 3");
// Multiplication
sheet_set_formula(sheet, 2, 0, "=3*4");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 2, 0);
TEST_ASSERT_EQ_DOUBLE(12.0, cell->data.formula.cached_value, 0.0001, "3*4 should be 12");
// Division
sheet_set_formula(sheet, 3, 0, "=20/4");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 3, 0);
TEST_ASSERT_EQ_DOUBLE(5.0, cell->data.formula.cached_value, 0.0001, "20/4 should be 5");
// Complex expression
sheet_set_formula(sheet, 4, 0, "=1+2*3");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 4, 0);
TEST_ASSERT_EQ_DOUBLE(7.0, cell->data.formula.cached_value, 0.0001, "1+2*3 should be 7");
// Parentheses
sheet_set_formula(sheet, 5, 0, "=(1+2)*3");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 5, 0);
TEST_ASSERT_EQ_DOUBLE(9.0, cell->data.formula.cached_value, 0.0001, "(1+2)*3 should be 9");
// Negative numbers
sheet_set_formula(sheet, 6, 0, "=5+-3");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 6, 0);
TEST_ASSERT_EQ_DOUBLE(2.0, cell->data.formula.cached_value, 0.0001, "5+-3 should be 2");
// Decimals
sheet_set_formula(sheet, 7, 0, "=1.5+2.5");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 7, 0);
TEST_ASSERT_EQ_DOUBLE(4.0, cell->data.formula.cached_value, 0.0001, "1.5+2.5 should be 4");
sheet_free(sheet);
}
void test_cell_references_in_formulas(void) {
TEST_SECTION("Cell References in Formulas");
Sheet* sheet = sheet_new(100, 26);
// Set up values
sheet_set_number(sheet, 0, 0, 10.0); // A1 = 10
sheet_set_number(sheet, 0, 1, 20.0); // B1 = 20
sheet_set_number(sheet, 1, 0, 5.0); // A2 = 5
// Simple reference
sheet_set_formula(sheet, 2, 0, "=A1");
sheet_recalculate(sheet);
Cell* cell = sheet_get_cell(sheet, 2, 0);
TEST_ASSERT_EQ_DOUBLE(10.0, cell->data.formula.cached_value, 0.0001, "=A1 should be 10");
// Reference in addition
sheet_set_formula(sheet, 2, 1, "=A1+B1");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 2, 1);
TEST_ASSERT_EQ_DOUBLE(30.0, cell->data.formula.cached_value, 0.0001, "=A1+B1 should be 30");
// Reference with constant
sheet_set_formula(sheet, 2, 2, "=A1*2");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 2, 2);
TEST_ASSERT_EQ_DOUBLE(20.0, cell->data.formula.cached_value, 0.0001, "=A1*2 should be 20");
// Multiple references
sheet_set_formula(sheet, 2, 3, "=A1+B1+A2");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 2, 3);
TEST_ASSERT_EQ_DOUBLE(35.0, cell->data.formula.cached_value, 0.0001, "=A1+B1+A2 should be 35");
// Reference to empty cell (should be 0)
sheet_set_formula(sheet, 3, 0, "=Z99");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 3, 0);
TEST_ASSERT_EQ_DOUBLE(0.0, cell->data.formula.cached_value, 0.0001, "Reference to empty cell should be 0");
// Chain of references
sheet_set_formula(sheet, 4, 0, "=A3"); // A5 = A3 = A1 = 10
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 4, 0);
TEST_ASSERT_EQ_DOUBLE(10.0, cell->data.formula.cached_value, 0.0001, "Chain reference should work");
sheet_free(sheet);
}
void test_division_by_zero(void) {
TEST_SECTION("Division by Zero");
Sheet* sheet = sheet_new(100, 26);
sheet_set_formula(sheet, 0, 0, "=1/0");
sheet_recalculate(sheet);
Cell* cell = sheet_get_cell(sheet, 0, 0);
TEST_ASSERT_EQ_INT(ERROR_DIV_ZERO, cell->data.formula.error, "Division by zero should set ERROR_DIV_ZERO");
// Division by cell containing zero
sheet_set_number(sheet, 1, 0, 0.0);
sheet_set_formula(sheet, 1, 1, "=10/A2");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 1, 1);
TEST_ASSERT_EQ_INT(ERROR_DIV_ZERO, cell->data.formula.error, "Division by zero cell should set ERROR_DIV_ZERO");
sheet_free(sheet);
}
void test_sum_function(void) {
TEST_SECTION("SUM Function");
Sheet* sheet = sheet_new(100, 26);
// Set up data
sheet_set_number(sheet, 0, 0, 1.0); // A1
sheet_set_number(sheet, 1, 0, 2.0); // A2
sheet_set_number(sheet, 2, 0, 3.0); // A3
sheet_set_number(sheet, 3, 0, 4.0); // A4
sheet_set_number(sheet, 4, 0, 5.0); // A5
// SUM of vertical range
sheet_set_formula(sheet, 5, 0, "=SUM(A1:A5)");
sheet_recalculate(sheet);
Cell* cell = sheet_get_cell(sheet, 5, 0);
TEST_ASSERT_EQ_INT(ERROR_NONE, cell->data.formula.error, "SUM should have no error");
TEST_ASSERT_EQ_DOUBLE(15.0, cell->data.formula.cached_value, 0.0001, "SUM(A1:A5) should be 15");
// SUM of horizontal range
sheet_set_number(sheet, 0, 1, 10.0); // B1
sheet_set_number(sheet, 0, 2, 20.0); // C1
sheet_set_number(sheet, 0, 3, 30.0); // D1
sheet_set_formula(sheet, 0, 4, "=SUM(B1:D1)");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 0, 4);
TEST_ASSERT_EQ_DOUBLE(60.0, cell->data.formula.cached_value, 0.0001, "SUM(B1:D1) should be 60");
// SUM with negative numbers
sheet_set_number(sheet, 6, 0, -5.0);
sheet_set_number(sheet, 7, 0, 15.0);
sheet_set_formula(sheet, 8, 0, "=SUM(A7:A8)");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 8, 0);
TEST_ASSERT_EQ_DOUBLE(10.0, cell->data.formula.cached_value, 0.0001, "SUM with negative should be 10");
// SUM of single cell
sheet_set_formula(sheet, 9, 0, "=SUM(A1:A1)");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 9, 0);
TEST_ASSERT_EQ_DOUBLE(1.0, cell->data.formula.cached_value, 0.0001, "SUM of single cell should be 1");
// SUM with empty cells (should treat as 0)
sheet_set_formula(sheet, 10, 0, "=SUM(Z1:Z5)");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 10, 0);
TEST_ASSERT_EQ_DOUBLE(0.0, cell->data.formula.cached_value, 0.0001, "SUM of empty cells should be 0");
sheet_free(sheet);
}
void test_avg_function(void) {
TEST_SECTION("AVG Function");
Sheet* sheet = sheet_new(100, 26);
sheet_set_number(sheet, 0, 0, 10.0);
sheet_set_number(sheet, 1, 0, 20.0);
sheet_set_number(sheet, 2, 0, 30.0);
sheet_set_formula(sheet, 3, 0, "=AVG(A1:A3)");
sheet_recalculate(sheet);
Cell* cell = sheet_get_cell(sheet, 3, 0);
TEST_ASSERT_EQ_DOUBLE(20.0, cell->data.formula.cached_value, 0.0001, "AVG(A1:A3) should be 20");
// Single value
sheet_set_formula(sheet, 4, 0, "=AVG(A1:A1)");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 4, 0);
TEST_ASSERT_EQ_DOUBLE(10.0, cell->data.formula.cached_value, 0.0001, "AVG of single value should be that value");
sheet_free(sheet);
}
void test_max_min_functions(void) {
TEST_SECTION("MAX and MIN Functions");
Sheet* sheet = sheet_new(100, 26);
sheet_set_number(sheet, 0, 0, 5.0);
sheet_set_number(sheet, 1, 0, 10.0);
sheet_set_number(sheet, 2, 0, 3.0);
sheet_set_number(sheet, 3, 0, 8.0);
// MAX
sheet_set_formula(sheet, 4, 0, "=MAX(A1:A4)");
sheet_recalculate(sheet);
Cell* cell = sheet_get_cell(sheet, 4, 0);
TEST_ASSERT_EQ_DOUBLE(10.0, cell->data.formula.cached_value, 0.0001, "MAX(A1:A4) should be 10");
// MIN
sheet_set_formula(sheet, 5, 0, "=MIN(A1:A4)");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 5, 0);
TEST_ASSERT_EQ_DOUBLE(3.0, cell->data.formula.cached_value, 0.0001, "MIN(A1:A4) should be 3");
// With negative numbers
sheet_set_number(sheet, 6, 0, -5.0);
sheet_set_number(sheet, 7, 0, -2.0);
sheet_set_formula(sheet, 8, 0, "=MAX(A7:A8)");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 8, 0);
TEST_ASSERT_EQ_DOUBLE(-2.0, cell->data.formula.cached_value, 0.0001, "MAX of negatives should be -2");
sheet_set_formula(sheet, 9, 0, "=MIN(A7:A8)");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 9, 0);
TEST_ASSERT_EQ_DOUBLE(-5.0, cell->data.formula.cached_value, 0.0001, "MIN of negatives should be -5");
sheet_free(sheet);
}
void test_median_function(void) {
TEST_SECTION("MEDIAN Function");
Sheet* sheet = sheet_new(100, 26);
// Odd number of values
sheet_set_number(sheet, 0, 0, 1.0);
sheet_set_number(sheet, 1, 0, 2.0);
sheet_set_number(sheet, 2, 0, 3.0);
sheet_set_number(sheet, 3, 0, 4.0);
sheet_set_number(sheet, 4, 0, 5.0);
sheet_set_formula(sheet, 5, 0, "=MEDIAN(A1:A5)");
sheet_recalculate(sheet);
Cell* cell = sheet_get_cell(sheet, 5, 0);
TEST_ASSERT_EQ_DOUBLE(3.0, cell->data.formula.cached_value, 0.0001, "MEDIAN of 1,2,3,4,5 should be 3");
// Even number of values
sheet_set_number(sheet, 0, 1, 1.0);
sheet_set_number(sheet, 1, 1, 2.0);
sheet_set_number(sheet, 2, 1, 3.0);
sheet_set_number(sheet, 3, 1, 4.0);
sheet_set_formula(sheet, 5, 1, "=MEDIAN(B1:B4)");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 5, 1);
TEST_ASSERT_EQ_DOUBLE(2.5, cell->data.formula.cached_value, 0.0001, "MEDIAN of 1,2,3,4 should be 2.5");
// Unsorted values
sheet_set_number(sheet, 0, 2, 5.0);
sheet_set_number(sheet, 1, 2, 1.0);
sheet_set_number(sheet, 2, 2, 3.0);
sheet_set_formula(sheet, 5, 2, "=MEDIAN(C1:C3)");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 5, 2);
TEST_ASSERT_EQ_DOUBLE(3.0, cell->data.formula.cached_value, 0.0001, "MEDIAN of unsorted 5,1,3 should be 3");
sheet_free(sheet);
}
void test_mode_function(void) {
TEST_SECTION("MODE Function");
Sheet* sheet = sheet_new(100, 26);
// Values with clear mode
sheet_set_number(sheet, 0, 0, 1.0);
sheet_set_number(sheet, 1, 0, 2.0);
sheet_set_number(sheet, 2, 0, 2.0);
sheet_set_number(sheet, 3, 0, 3.0);
sheet_set_number(sheet, 4, 0, 2.0);
sheet_set_formula(sheet, 5, 0, "=MODE(A1:A5)");
sheet_recalculate(sheet);
Cell* cell = sheet_get_cell(sheet, 5, 0);
TEST_ASSERT_EQ_DOUBLE(2.0, cell->data.formula.cached_value, 0.0001, "MODE should be 2");
sheet_free(sheet);
}
void test_if_function(void) {
TEST_SECTION("IF Function");
Sheet* sheet = sheet_new(100, 26);
sheet_set_number(sheet, 0, 0, 10.0); // A1 = 10
sheet_set_number(sheet, 0, 1, 5.0); // B1 = 5
// Numeric IF - true condition
sheet_set_formula(sheet, 1, 0, "=IF(A1>B1, 100, 200)");
sheet_recalculate(sheet);
Cell* cell = sheet_get_cell(sheet, 1, 0);
TEST_ASSERT_EQ_DOUBLE(100.0, cell->data.formula.cached_value, 0.0001, "IF(10>5) should return true value");
// Numeric IF - false condition
sheet_set_formula(sheet, 1, 1, "=IF(A1<B1, 100, 200)");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 1, 1);
TEST_ASSERT_EQ_DOUBLE(200.0, cell->data.formula.cached_value, 0.0001, "IF(10<5) should return false value");
// IF with equals
sheet_set_formula(sheet, 2, 0, "=IF(A1=10, 1, 0)");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 2, 0);
TEST_ASSERT_EQ_DOUBLE(1.0, cell->data.formula.cached_value, 0.0001, "IF(A1=10) should be true");
// IF with not equals
sheet_set_formula(sheet, 2, 1, "=IF(A1<>10, 1, 0)");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 2, 1);
TEST_ASSERT_EQ_DOUBLE(0.0, cell->data.formula.cached_value, 0.0001, "IF(A1<>10) should be false");
// IF with >= and <=
sheet_set_formula(sheet, 3, 0, "=IF(A1>=10, 1, 0)");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 3, 0);
TEST_ASSERT_EQ_DOUBLE(1.0, cell->data.formula.cached_value, 0.0001, "IF(A1>=10) should be true");
sheet_set_formula(sheet, 3, 1, "=IF(A1<=10, 1, 0)");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 3, 1);
TEST_ASSERT_EQ_DOUBLE(1.0, cell->data.formula.cached_value, 0.0001, "IF(A1<=10) should be true");
// String IF results
sheet_set_formula(sheet, 4, 0, "=IF(A1>5, \"High\", \"Low\")");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 4, 0);
TEST_ASSERT(cell->data.formula.is_string_result, "IF with string result should set is_string_result");
TEST_ASSERT_EQ_STR("High", cell->data.formula.cached_string, "IF should return 'High'");
sheet_free(sheet);
}
void test_power_function(void) {
TEST_SECTION("POWER Function");
Sheet* sheet = sheet_new(100, 26);
sheet_set_formula(sheet, 0, 0, "=POWER(2, 3)");
sheet_recalculate(sheet);
Cell* cell = sheet_get_cell(sheet, 0, 0);
TEST_ASSERT_EQ_DOUBLE(8.0, cell->data.formula.cached_value, 0.0001, "POWER(2,3) should be 8");
sheet_set_formula(sheet, 1, 0, "=POWER(10, 2)");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 1, 0);
TEST_ASSERT_EQ_DOUBLE(100.0, cell->data.formula.cached_value, 0.0001, "POWER(10,2) should be 100");
// Square root
sheet_set_formula(sheet, 2, 0, "=POWER(16, 0.5)");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 2, 0);
TEST_ASSERT_EQ_DOUBLE(4.0, cell->data.formula.cached_value, 0.0001, "POWER(16,0.5) should be 4");
// Power of 0
sheet_set_formula(sheet, 3, 0, "=POWER(5, 0)");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 3, 0);
TEST_ASSERT_EQ_DOUBLE(1.0, cell->data.formula.cached_value, 0.0001, "POWER(5,0) should be 1");
// Cell reference
sheet_set_number(sheet, 4, 0, 3.0);
sheet_set_number(sheet, 4, 1, 4.0);
sheet_set_formula(sheet, 4, 2, "=POWER(A5, B5)");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 4, 2);
TEST_ASSERT_EQ_DOUBLE(81.0, cell->data.formula.cached_value, 0.0001, "POWER(3,4) should be 81");
sheet_free(sheet);
}
void test_xlookup_function(void) {
TEST_SECTION("XLOOKUP Function");
Sheet* sheet = sheet_new(100, 26);
// Set up lookup table
sheet_set_string(sheet, 0, 0, "Apple"); // A1
sheet_set_number(sheet, 0, 1, 0.5); // B1
sheet_set_number(sheet, 0, 2, 100); // C1
sheet_set_string(sheet, 1, 0, "Orange"); // A2
sheet_set_number(sheet, 1, 1, 0.75); // B2
sheet_set_number(sheet, 1, 2, 85); // C2
sheet_set_string(sheet, 2, 0, "Banana"); // A3
sheet_set_number(sheet, 2, 1, 0.3); // B3
sheet_set_number(sheet, 2, 2, 120); // C3
// String lookup - find price
sheet_set_formula(sheet, 5, 0, "=XLOOKUP(\"Orange\", A1:A3, B1:B3, 0)");
sheet_recalculate(sheet);
Cell* cell = sheet_get_cell(sheet, 5, 0);
TEST_ASSERT_EQ_INT(ERROR_NONE, cell->data.formula.error, "XLOOKUP should succeed");
TEST_ASSERT_EQ_DOUBLE(0.75, cell->data.formula.cached_value, 0.0001, "XLOOKUP for Orange price should be 0.75");
// String lookup - find stock
sheet_set_formula(sheet, 5, 1, "=XLOOKUP(\"Banana\", A1:A3, C1:C3, 0)");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 5, 1);
TEST_ASSERT_EQ_DOUBLE(120.0, cell->data.formula.cached_value, 0.0001, "XLOOKUP for Banana stock should be 120");
// Lookup not found
sheet_set_formula(sheet, 5, 2, "=XLOOKUP(\"Grape\", A1:A3, B1:B3, 0)");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 5, 2);
TEST_ASSERT_EQ_INT(ERROR_NA, cell->data.formula.error, "XLOOKUP for missing item should return #N/A");
// Numeric lookup
sheet_set_number(sheet, 10, 0, 1); // A11
sheet_set_number(sheet, 10, 1, 100); // B11
sheet_set_number(sheet, 11, 0, 2); // A12
sheet_set_number(sheet, 11, 1, 200); // B12
sheet_set_number(sheet, 12, 0, 3); // A13
sheet_set_number(sheet, 12, 1, 300); // B13
sheet_set_formula(sheet, 13, 0, "=XLOOKUP(2, A11:A13, B11:B13, 0)");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 13, 0);
TEST_ASSERT_EQ_DOUBLE(200.0, cell->data.formula.cached_value, 0.0001, "Numeric XLOOKUP should find 200");
sheet_free(sheet);
}
void test_nested_functions(void) {
TEST_SECTION("Nested Functions");
Sheet* sheet = sheet_new(100, 26);
sheet_set_number(sheet, 0, 0, 1.0);
sheet_set_number(sheet, 1, 0, 2.0);
sheet_set_number(sheet, 2, 0, 3.0);
// SUM inside expression
sheet_set_formula(sheet, 3, 0, "=SUM(A1:A3)*2");
sheet_recalculate(sheet);
Cell* cell = sheet_get_cell(sheet, 3, 0);
TEST_ASSERT_EQ_DOUBLE(12.0, cell->data.formula.cached_value, 0.0001, "SUM*2 should be 12");
// Division of functions
sheet_set_formula(sheet, 4, 0, "=SUM(A1:A3)/MAX(A1:A3)");
sheet_recalculate(sheet);
cell = sheet_get_cell(sheet, 4, 0);
TEST_ASSERT_EQ_DOUBLE(2.0, cell->data.formula.cached_value, 0.0001, "SUM/MAX should be 2");
sheet_free(sheet);
}
// ============================================================================
// FORMATTING TESTS
// ============================================================================
void test_percentage_format(void) {
TEST_SECTION("Percentage Format");
Sheet* sheet = sheet_new(100, 26);
sheet_set_number(sheet, 0, 0, 0.5);
Cell* cell = sheet_get_cell(sheet, 0, 0);
cell_set_format(cell, FORMAT_PERCENTAGE, 0);
char* display = format_cell_value(cell);
TEST_ASSERT(strstr(display, "50") != NULL, "0.5 as percentage should contain '50'");
TEST_ASSERT(strstr(display, "%") != NULL, "Percentage should contain '%' symbol");
sheet_set_number(sheet, 1, 0, 0.1234);
cell = sheet_get_cell(sheet, 1, 0);
cell_set_format(cell, FORMAT_PERCENTAGE, 0);
display = format_cell_value(cell);
TEST_ASSERT(strstr(display, "12") != NULL, "0.1234 as percentage should contain '12'");
sheet_set_number(sheet, 2, 0, 1.5);
cell = sheet_get_cell(sheet, 2, 0);
cell_set_format(cell, FORMAT_PERCENTAGE, 0);
display = format_cell_value(cell);
TEST_ASSERT(strstr(display, "150") != NULL, "1.5 as percentage should contain '150'");
sheet_free(sheet);
}
void test_currency_format(void) {
TEST_SECTION("Currency Format");
Sheet* sheet = sheet_new(100, 26);
sheet_set_number(sheet, 0, 0, 1234.56);
Cell* cell = sheet_get_cell(sheet, 0, 0);
cell_set_format(cell, FORMAT_CURRENCY, 0);
char* display = format_cell_value(cell);
TEST_ASSERT(strstr(display, "$") != NULL, "Currency should contain '$'");
TEST_ASSERT(strstr(display, "1234") != NULL, "Currency should contain the value");
// Negative currency
sheet_set_number(sheet, 1, 0, -500.00);
cell = sheet_get_cell(sheet, 1, 0);
cell_set_format(cell, FORMAT_CURRENCY, 0);
display = format_cell_value(cell);
TEST_ASSERT(strstr(display, "-") != NULL, "Negative currency should show minus");
TEST_ASSERT(strstr(display, "500") != NULL, "Negative currency should show value");
sheet_free(sheet);
}
void test_date_formats(void) {
TEST_SECTION("Date Formats");
Sheet* sheet = sheet_new(100, 26);
// Excel serial date for a known date (e.g., 44927 = 12/25/2022)
// Note: Excel dates start from 1/1/1900
double test_date = 44927.0; // This should be around Dec 25, 2022
sheet_set_number(sheet, 0, 0, test_date);
Cell* cell = sheet_get_cell(sheet, 0, 0);
// Test MM/DD/YYYY format
cell_set_format(cell, FORMAT_DATE, DATE_STYLE_MM_DD_YYYY);
char* display = format_cell_value(cell);
TEST_ASSERT(display != NULL, "Date display should not be NULL");
TEST_ASSERT(strlen(display) > 0, "Date display should not be empty");
TEST_ASSERT(strchr(display, '/') != NULL || strchr(display, '-') != NULL, "Date should contain separator");
// Test DD/MM/YYYY format
cell_set_format(cell, FORMAT_DATE, DATE_STYLE_DD_MM_YYYY);
display = format_cell_value(cell);
TEST_ASSERT(display != NULL, "DD/MM/YYYY format should work");
// Test YYYY-MM-DD format
cell_set_format(cell, FORMAT_DATE, DATE_STYLE_YYYY_MM_DD);
display = format_cell_value(cell);
TEST_ASSERT(display != NULL, "YYYY-MM-DD format should work");
// Test long format
cell_set_format(cell, FORMAT_DATE, DATE_STYLE_MON_DD_YYYY);
display = format_cell_value(cell);
TEST_ASSERT(display != NULL, "Long date format should work");
sheet_free(sheet);
}
void test_time_formats(void) {
TEST_SECTION("Time Formats");
Sheet* sheet = sheet_new(100, 26);
// Time fraction: 0.5 = 12:00 PM (noon)
sheet_set_number(sheet, 0, 0, 0.5);
Cell* cell = sheet_get_cell(sheet, 0, 0);
// Test 12-hour format
cell_set_format(cell, FORMAT_TIME, TIME_STYLE_12HR);
char* display = format_cell_value(cell);
TEST_ASSERT(display != NULL, "Time display should not be NULL");
TEST_ASSERT(strstr(display, "12") != NULL || strstr(display, ":") != NULL, "Time should show hours");
// Test 24-hour format
cell_set_format(cell, FORMAT_TIME, TIME_STYLE_24HR);
display = format_cell_value(cell);
TEST_ASSERT(display != NULL, "24-hour format should work");
// Test with seconds
cell_set_format(cell, FORMAT_TIME, TIME_STYLE_SECONDS);
display = format_cell_value(cell);
TEST_ASSERT(display != NULL, "Time with seconds should work");
// Test 6:00 PM (0.75)
sheet_set_number(sheet, 1, 0, 0.75);
cell = sheet_get_cell(sheet, 1, 0);
cell_set_format(cell, FORMAT_TIME, TIME_STYLE_12HR);
display = format_cell_value(cell);
TEST_ASSERT(strstr(display, "6") != NULL || strstr(display, "PM") != NULL, "0.75 should be 6 PM");
sheet_free(sheet);
}
void test_cell_colors(void) {
TEST_SECTION("Cell Colors");
Sheet* sheet = sheet_new(100, 26);
Cell* cell = sheet_get_or_create_cell(sheet, 0, 0);
// Test default colors
TEST_ASSERT_EQ_INT(-1, cell->text_color, "Default text color should be -1");
TEST_ASSERT_EQ_INT(-1, cell->background_color, "Default background color should be -1");
// Set text color
cell_set_text_color(cell, COLOR_RED);
TEST_ASSERT_EQ_INT(COLOR_RED, cell->text_color, "Text color should be red");
// Set background color
cell_set_background_color(cell, COLOR_YELLOW);
TEST_ASSERT_EQ_INT(COLOR_YELLOW, cell->background_color, "Background color should be yellow");