-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTests.c
More file actions
1044 lines (823 loc) · 27.3 KB
/
Tests.c
File metadata and controls
1044 lines (823 loc) · 27.3 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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include "Graph.h"
#include "Graph.c"
#include "Parser.c"
#include "Functions.c"
#include "display.c"
// Define NUM_CELLS before including Graph.h and Graph.c
int NUM_CELLS = 100;
// Mock variable for the external hasCycle
int hasCycle = 0;
// Test helper function to print a linked list
void printList(Cell *head)
{
Cell *current = head;
printf("[");
while (current != NULL)
{
printf("%d", current->cell);
current = current->next;
if (current != NULL)
printf(", ");
}
printf("]\n");
}
// Test helper to compare two linked lists
int listsEqual(Cell *list1, Cell *list2)
{
while (list1 != NULL && list2 != NULL)
{
if (list1->cell != list2->cell)
return 0;
list1 = list1->next;
list2 = list2->next;
}
return (list1 == NULL && list2 == NULL);
}
// Test helper to create a list with specified values
Cell *createTestList(int values[], int size)
{
Cell *head = NULL;
for (int i = 0; i < size; i++) // Changed loop to keep the order
{
head = Addedge(values[i], head);
}
return head;
}
// Test utility functions
void test_utility_functions()
{
printf("Testing utility functions...\n");
// Test min2 function
assert(min2(5, 10) == 5);
assert(min2(10, 5) == 5);
assert(min2(-5, 5) == -5);
// Test max2 function
assert(max2(5, 10) == 10);
assert(max2(10, 5) == 10);
assert(max2(-5, 5) == 5);
// Test arithmetic_eval2 function
assert(arithmetic_eval2(5, 3, '+') == 8);
assert(arithmetic_eval2(5, 3, '-') == 2);
assert(arithmetic_eval2(5, 3, '*') == 15);
assert(arithmetic_eval2(6, 3, '/') == 2);
printf("Utility functions tests passed!\n");
}
// Test AddFormula function
void test_AddFormula()
{
printf("Testing AddFormula function...\n");
Graph *graph = CreateGraph();
Formula formulaArray[NUM_CELLS];
// Test constant formula
AddFormula(graph, 0, 42, 0, 0, formulaArray);
assert(formulaArray[0].op_type == 0);
assert(formulaArray[0].op_info1 == 42);
assert(formulaArray[0].op_info2 == -1);
// Test cell + constant formula
AddFormula(graph, 1, 0, 10, 1, formulaArray);
assert(formulaArray[1].op_type == 1);
assert(formulaArray[1].op_info1 == 0);
assert(formulaArray[1].op_info2 == 10);
// Test cell + cell formula
AddFormula(graph, 2, 0, 1, 5, formulaArray);
assert(formulaArray[2].op_type == 5);
assert(formulaArray[2].op_info1 == 0);
assert(formulaArray[2].op_info2 == 1);
printf("AddFormula tests passed!\n");
// Clean up
FreeGraph(graph);
}
// Test graph creation and cell operations
void test_graph_creation_and_cell_ops()
{
printf("Testing graph creation and cell operations...\n");
// Test CreateGraph
Graph *graph = CreateGraph();
assert(graph != NULL);
assert(graph->adjLists_head != NULL);
// Test Addcell
Cell *cell1 = Addcell(1);
assert(cell1 != NULL);
assert(cell1->cell == 1);
assert(cell1->next == NULL);
// Test Addedge
Cell *head = NULL;
head = Addedge(1, head);
assert(head != NULL);
assert(head->cell == 1);
// Add another edge
Cell *oldHead = head;
head = Addedge(2, head);
assert(head != NULL);
// In the new implementation, Addedge adds to the end of the list
// So head should still point to node with cell 1
assert(head->cell == 1);
assert(head->next->cell == 2);
// Try adding duplicate - should not change list
Cell *before = head;
head = Addedge(2, head);
assert(listsEqual(head, before)); // Should be the same list
// Test Deletecell
head = Deletecell(2, head);
assert(head != NULL);
assert(head->cell == 1);
assert(head->next == NULL);
// Delete the remaining cell
head = Deletecell(1, head);
assert(head == NULL);
// Test deleting from empty list
head = Deletecell(1, head);
assert(head == NULL);
free(cell1);
FreeGraph(graph);
printf("Graph creation and cell operations tests passed!\n");
}
// Test topological sort and cycle detection with the new range-aware implementation
void test_topological_sort()
{
printf("Testing topological sort and cycle detection...\n");
Graph *graph = CreateGraph();
int COLS = 10; // 10x10 grid
// Create a simple DAG: 0 -> 1 -> 2
graph->adjLists_head[0] = Addedge(1, graph->adjLists_head[0]);
graph->adjLists_head[1] = Addedge(2, graph->adjLists_head[1]);
int size;
hasCycle = 0;
int *result = topoSortFromCell(graph, 0, &size, &hasCycle, COLS);
assert(result != NULL);
assert(hasCycle == 0);
assert(size == 3);
assert(result[0] == 0);
assert(result[1] == 1);
assert(result[2] == 2);
free(result);
// Create a cycle: 0 -> 1 -> 2 -> 0
graph->adjLists_head[2] = Addedge(0, graph->adjLists_head[2]);
hasCycle = 0;
result = topoSortFromCell(graph, 0, &size, &hasCycle, COLS);
assert(result == NULL);
assert(hasCycle == 1);
// Clean up and create a new graph
FreeGraph(graph);
graph = CreateGraph();
// Test range dependency
AddRangeToGraph(graph, 0, 3, 5);
graph->adjLists_head[5] = Addedge(6, graph->adjLists_head[5]);
hasCycle = 0;
result = topoSortFromCell(graph, 0, &size, &hasCycle, COLS);
assert(result != NULL);
assert(hasCycle == 0);
int found5 = -1, found6 = -1;
for (int i = 0; i < size; i++)
{
if (result[i] == 5)
found5 = i;
if (result[i] == 6)
found6 = i;
}
assert(found5 != -1 && found6 != -1);
assert(found5 < found6); // 5 must come before 6
free(result);
// Test disconnected components
FreeGraph(graph);
graph = CreateGraph();
graph->adjLists_head[0] = Addedge(1, graph->adjLists_head[0]);
graph->adjLists_head[2] = Addedge(3, graph->adjLists_head[2]); // Separate component
hasCycle = 0;
result = topoSortFromCell(graph, 0, &size, &hasCycle, COLS);
assert(result != NULL);
assert(hasCycle == 0);
assert(size == 2);
assert(result[0] == 0);
assert(result[1] == 1);
free(result);
// Test a larger DAG
FreeGraph(graph);
graph = CreateGraph();
graph->adjLists_head[0] = Addedge(1, graph->adjLists_head[0]);
graph->adjLists_head[1] = Addedge(2, graph->adjLists_head[1]);
graph->adjLists_head[2] = Addedge(3, graph->adjLists_head[2]);
graph->adjLists_head[3] = Addedge(4, graph->adjLists_head[3]);
graph->adjLists_head[4] = Addedge(5, graph->adjLists_head[4]);
hasCycle = 0;
result = topoSortFromCell(graph, 0, &size, &hasCycle, COLS);
assert(result != NULL);
assert(hasCycle == 0);
assert(size == 6);
for (int i = 0; i < 6; i++)
{
assert(result[i] == i);
}
free(result);
// Test multiple start points
FreeGraph(graph);
graph = CreateGraph();
graph->adjLists_head[0] = Addedge(2, graph->adjLists_head[0]);
graph->adjLists_head[1] = Addedge(2, graph->adjLists_head[1]);
graph->adjLists_head[2] = Addedge(3, graph->adjLists_head[2]);
hasCycle = 0;
result = topoSortFromCell(graph, 0, &size, &hasCycle, COLS);
assert(result != NULL);
assert(hasCycle == 0);
assert(size == 3);
free(result);
// Test self-loop cycle detection
FreeGraph(graph);
graph = CreateGraph();
graph->adjLists_head[0] = Addedge(0, graph->adjLists_head[0]);
hasCycle = 0;
result = topoSortFromCell(graph, 0, &size, &hasCycle, COLS);
assert(result == NULL);
assert(hasCycle == 1);
printf("All topological sort and cycle detection tests passed!\n");
FreeGraph(graph);
}
// Test formula edge operations with the new range handling
void test_formula_edge_operations()
{
printf("Testing formula edge operations...\n");
Graph *graph = CreateGraph();
Formula formulaArray[NUM_CELLS];
int COLS = 10; // 10x10 grid
// Test cell + constant formula (op_type 1)
formulaArray[4].op_type = 1;
formulaArray[4].op_info1 = 1;
formulaArray[4].op_info2 = 10;
Addedge_formula(graph, 4, COLS, formulaArray);
// Cell 1 should have an edge to cell 4
assert(graph->adjLists_head[1] != NULL);
assert(graph->adjLists_head[1]->cell == 4);
// Test cell + cell formula (op_type 5)
formulaArray[5].op_type = 5;
formulaArray[5].op_info1 = 1;
formulaArray[5].op_info2 = 2;
Addedge_formula(graph, 5, COLS, formulaArray);
// Cell 1 should have edges to cells 4 and 5
assert(graph->adjLists_head[1] != NULL);
Cell *current = graph->adjLists_head[1];
int found4 = 0, found5 = 0;
while (current)
{
if (current->cell == 4)
found4 = 1;
if (current->cell == 5)
found5 = 1;
current = current->next;
}
assert(found4 && found5);
// Cell 2 should have an edge to cell 5
assert(graph->adjLists_head[2] != NULL);
assert(graph->adjLists_head[2]->cell == 5);
// Test range formula (op_type 9 - MIN)
// Range covering cells 0, 1, 10, 11 (2x2 grid starting at 0)
formulaArray[6].op_type = 9;
formulaArray[6].op_info1 = 0; // Start cell
formulaArray[6].op_info2 = 11; // End cell
Addedge_formula(graph, 6, COLS, formulaArray);
// We should now have a range entry instead of direct edges
Range *range = graph->ranges_head;
assert(range != NULL);
assert(range->startCell == 0);
assert(range->endCell == 11);
assert(range->dependentCell == 6);
// Test Deleteedge
Deleteedge(graph, 4, COLS, formulaArray);
// Cell 1 should no longer have an edge to cell 4
current = graph->adjLists_head[1];
found4 = 0;
while (current)
{
if (current->cell == 4)
found4 = 1;
current = current->next;
}
assert(!found4);
// Test deleting range formula
Deleteedge(graph, 6, COLS, formulaArray);
// Range should be gone
assert(graph->ranges_head == NULL);
printf("Formula edge operations tests passed!\n");
FreeGraph(graph);
}
// Test Recalc function with the new implementation
void test_Recalc()
{
printf("Testing Recalc function...\n");
int *values = (int *)calloc(NUM_CELLS, sizeof(int));
int COLS = 10; // 10x10 grid
Graph *graph = CreateGraph();
Formula formulaArray[NUM_CELLS];
// --- BASIC DEPENDENCY CHAIN ---
// Cell 0 = 5 (constant)
// Cell 1 = 10 (constant)
// Cell 2 = Cell 0 + Cell 1 (5 + 10 = 15)
// Cell 3 = Cell 2 * 2 (15 * 2 = 30)
AddFormula(graph, 0, 5, 0, 0, formulaArray);
AddFormula(graph, 1, 10, 0, 0, formulaArray);
AddFormula(graph, 2, 0, 1, 5, formulaArray);
AddFormula(graph, 3, 2, 2, 3, formulaArray);
values[0] = 5;
values[1] = 10;
Addedge_formula(graph, 2, COLS, formulaArray);
Addedge_formula(graph, 3, COLS, formulaArray);
hasCycle = 0;
Recalc(graph, COLS, values, 0, formulaArray);
assert(values[2] == 15);
assert(values[3] == 30);
// --- MODIFY VALUE & RECALCULATE ---
AddFormula(graph, 0, 7, 0, 0, formulaArray);
values[0] = 7; // Directly updating since it's a constant
Recalc(graph, COLS, values, 0, formulaArray);
assert(values[2] == 17);
assert(values[3] == 34);
// --- MULTIPLICATION CHAIN ---
// Cell 4 = Cell 3 * 3 (34 * 3 = 102)
// Cell 5 = Cell 4 * 2 (102 * 2 = 204)
AddFormula(graph, 4, 3, 3, 3, formulaArray);
AddFormula(graph, 5, 4, 2, 3, formulaArray);
Addedge_formula(graph, 4, COLS, formulaArray);
Addedge_formula(graph, 5, COLS, formulaArray);
Recalc(graph, COLS, values, 3, formulaArray);
assert(values[4] == 102);
assert(values[5] == 204);
// --- TEST RANGE SUM ---
// Cells 10, 11, 20, 21 sum into cell 30
AddFormula(graph, 10, 25, 0, 0, formulaArray);
AddFormula(graph, 11, 15, 0, 0, formulaArray);
AddFormula(graph, 20, 10, 0, 0, formulaArray);
AddFormula(graph, 21, 50, 0, 0, formulaArray);
values[10] = 25;
values[11] = 15;
values[20] = 10;
values[21] = 50;
AddFormula(graph, 30, 10, 21, 12, formulaArray); // SUM(10:21)
Addedge_formula(graph, 30, COLS, formulaArray);
Recalc(graph, COLS, values, 10, formulaArray);
assert(values[30] == 100);
// --- COMPLEX DEPENDENCY ---
// Cell 40 = Cell 30 - Cell 10 (100 - 25 = 75)
// Cell 50 = Cell 40 / 5 (75 / 5 = 15)
AddFormula(graph, 40, 30, 10, 6, formulaArray); // Subtraction
AddFormula(graph, 50, 40, 5, 4, formulaArray); // Division
Addedge_formula(graph, 40, COLS, formulaArray);
Addedge_formula(graph, 50, COLS, formulaArray);
Recalc(graph, COLS, values, 30, formulaArray);
assert(values[40] == 75);
assert(values[50] == 15);
// --- CYCLE DETECTION ---
// Creating a cycle: 60 -> 61 -> 62 -> 60
AddFormula(graph, 60, 61, 0, 5, formulaArray);
AddFormula(graph, 61, 62, 0, 5, formulaArray);
AddFormula(graph, 62, 60, 0, 5, formulaArray);
Addedge_formula(graph, 60, COLS, formulaArray);
Addedge_formula(graph, 61, COLS, formulaArray);
Addedge_formula(graph, 62, COLS, formulaArray);
hasCycle = 0;
int prevCycle = hasCycle;
Recalc(graph, COLS, values, 60, formulaArray);
assert(hasCycle == 1 && prevCycle == 0); // Should detect cycle
printf("Recalc function tests passed!\n");
// Clean up
free(values);
FreeGraph(graph);
}
// Test cell_parser function
void test_cell_parser()
{
printf("Testing cell_parser function...\n");
Graph *graph = CreateGraph();
int C = 26; // Columns A-Z
int R = 100; // Rows 1-100
// Test valid cell references
assert(cell_parser("A1", C, R, 0, 1, graph) == 0);
assert(cell_parser("Z100", C, R, 0, 3, graph) == 2599);
// Test invalid cell references
assert(cell_parser("AA1", C, R, 0, 2, graph) == -1);
assert(cell_parser("A101", C, R, 0, 3, graph) == -1);
assert(cell_parser("1A", C, R, 0, 1, graph) == -1);
printf("cell_parser tests passed!\n");
free(graph->adjLists_head);
free(graph);
}
// Test valuefunc function
void test_valuefunc()
{
printf("Testing valuefunc function...\n");
Graph *graph = CreateGraph();
Formula formulaArray[NUM_CELLS];
int *arr = (int *)calloc(NUM_CELLS, sizeof(int));
int C = 26; // Columns A-Z
int R = 100; // Rows 1-100
// Test constant assignment
char input1[] = "A1=42";
valuefunc(input1, C, R, 2, 5, arr, graph, formulaArray);
assert(arr[0] == 42);
// Test cell reference
char input2[] = "B1=A1";
valuefunc(input2, C, R, 2, 5, arr, graph, formulaArray);
assert(arr[1] == 42);
// Test negative value
char input3[] = "C1=-10";
valuefunc(input3, C, R, 2, 6, arr, graph, formulaArray);
assert(arr[2] == -10);
printf("valuefunc tests passed!\n");
free(arr);
free(graph->adjLists_head);
free(graph);
}
// Test arth_op function
void test_arth_op()
{
printf("Testing arth_op function...\n");
Graph *graph = CreateGraph();
Formula formulaArray[NUM_CELLS];
int *arr = (int *)calloc(NUM_CELLS, sizeof(int));
int C = 26; // Columns A-Z
int R = 100; // Rows 1-100
// Set up initial values
arr[0] = 10; // A1 = 10
arr[1] = 5; // B1 = 5
// Test addition
char input1[] = "C1=A1+B1";
arth_op(input1, C, R, 2, 8, arr, graph, formulaArray);
assert(arr[2] == 15);
// Test subtraction
char input2[] = "D1=A1-B1";
arth_op(input2, C, R, 2, 8, arr, graph, formulaArray);
assert(arr[3] == 5);
// Test multiplication
char input3[] = "E1=A1*B1";
arth_op(input3, C, R, 2, 8, arr, graph, formulaArray);
assert(arr[4] == 50);
// Test division
char input4[] = "F1=A1/B1";
arth_op(input4, C, R, 2, 8, arr, graph, formulaArray);
assert(arr[5] == 2);
printf("arth_op tests passed!\n");
free(arr);
free(graph->adjLists_head);
free(graph);
}
// Test min_func function
void test_min_func()
{
printf("Testing min_func function...\n");
Graph *graph = CreateGraph();
int *arr = (int *)calloc(NUM_CELLS, sizeof(int));
Formula formulaArray[NUM_CELLS];
int C = 3, R = 3;
// Initialize test data
arr[0] = 5; // A1
arr[1] = 2; // B1
arr[2] = 8; // C1
arr[3] = 1; // A2
arr[4] = 9; // B2
arr[5] = 3; // C2
arr[6] = 7; // A3
arr[7] = 4; // B3
arr[8] = 0; // C3 (initialize target cell)
char input[] = "C3=MIN(A1:B2)";
int pos_equalto = 2;
int pos_end = strlen(input);
min_func(input, C, R, pos_equalto, pos_end, arr, graph, formulaArray);
assert(arr[8] == 1); // C3 should have the minimum value from range A1:B2
printf("min_func tests passed!\n");
// Clean up
free(arr);
free(graph->adjLists_head);
free(graph);
}
// Test maxfunc function
void test_max_func()
{
printf("Testing max_func function...\n");
Graph *graph = CreateGraph();
int *arr = (int *)calloc(NUM_CELLS, sizeof(int));
Formula formulaArray[NUM_CELLS];
int C = 3, R = 3;
// Initialize test data
arr[0] = 5; // A1
arr[1] = 2; // B1
arr[2] = 8; // C1
arr[3] = 1; // A2
arr[4] = 9; // B2
arr[5] = 3; // C2
arr[6] = 7; // A3
arr[7] = 4; // B3
arr[8] = 0; // C3 (initialize target cell)
char input[] = "C3=MAX(A1:B2)";
int pos_equalto = 2;
int pos_end = strlen(input);
maxfunc(input, C, R, pos_equalto, pos_end, arr, graph, formulaArray);
assert(arr[8] == 9); // C3 should have the maximum value from range A1:B2
printf("max_func tests passed!\n");
// Clean up
free(arr);
free(graph->adjLists_head);
free(graph);
}
// Test avg_func function
void test_avg_func()
{
printf("Testing avg_func function...\n");
Graph *graph = CreateGraph();
int *arr = (int *)calloc(NUM_CELLS, sizeof(int));
Formula formulaArray[NUM_CELLS];
int C = 3, R = 3;
// Initialize test data
arr[0] = 1; // A1
arr[1] = 2; // B1
arr[2] = 3; // C1
arr[3] = 4; // A2
arr[4] = 5; // B2
arr[5] = 6; // C2
arr[6] = 7; // A3
arr[7] = 8; // B3
arr[8] = 0; // C3 (initialize target cell)
char input[] = "C3=AVG(A1:B2)";
int pos_equalto = 2;
int pos_end = strlen(input);
avg_func(input, C, R, pos_equalto, pos_end, arr, graph, formulaArray);
assert(arr[8] == 3); // C3 should have the average value from range A1:B2 (1+2+4+5)/4 = 3
printf("avg_func tests passed!\n");
// Clean up
free(arr);
free(graph->adjLists_head);
free(graph);
}
// Test sum_func function
void test_sum_func()
{
printf("Testing sum_func function...\n");
Graph *graph = CreateGraph();
int *arr = (int *)calloc(NUM_CELLS, sizeof(int));
Formula formulaArray[NUM_CELLS];
// Initialize test data
arr[0] = 1; // A1
arr[1] = 2; // B1
arr[2] = 3; // C1
arr[3] = 4; // A2
arr[4] = 5; // B2
arr[5] = 6; // C2
arr[6] = 7; // A3
arr[7] = 8; // B3
arr[8] = 9; // C3
char input[] = "C3=SUM(A1:B2)"; // Avoids self-cycle
int C = 3, R = 3;
int pos_equalto = 2;
int pos_end = strlen(input);
sum_func(input, C, R, pos_equalto, pos_end, arr, graph, formulaArray);
assert(arr[8] == 12); // C3 should have the sum of values from range A1:B2
printf("sum_func tests passed!\n");
// Clean up
free(arr);
free(graph->adjLists_head);
free(graph);
}
// Test stdev_func function
void test_stdev_func()
{
printf("Testing stdev_func function...\n");
Graph *graph = CreateGraph();
int *arr = (int *)calloc(NUM_CELLS, sizeof(int));
Formula formulaArray[NUM_CELLS];
int C = 3, R = 3;
// Initialize test data
arr[0] = 2; // A1
arr[1] = 4; // B1
arr[2] = 4; // C1
arr[3] = 4; // A2
arr[4] = 5; // B2
arr[5] = 5; // C2
arr[6] = 7; // A3
arr[7] = 9; // B3
arr[8] = 0; // C3 (initialize target cell)
char input[] = "C3=STDEV(A1:B2)";
int pos_equalto = 2;
int pos_end = strlen(input);
stdev_func(input, C, R, pos_equalto, pos_end, arr, graph, formulaArray);
assert(arr[8] == 1); // C3 should have approx std deviation of [2,4,4,5] rounded
printf("stdev_func tests passed!\n");
// Clean up
free(arr);
free(graph->adjLists_head);
free(graph);
}
// Test sleep_func function
void test_sleep_func()
{
printf("Testing sleep_func function...\n");
Graph *graph = CreateGraph();
int *arr = (int *)calloc(NUM_CELLS, sizeof(int));
Formula formulaArray[NUM_CELLS];
int C = 3, R = 3;
// Target cell is C3 (index 8)
char input[] = "C3=SLEEP(1)";
int pos_equalto = 2;
int pos_end = strlen(input);
sleep_func(input, C, R, pos_equalto, pos_end, arr, graph, formulaArray);
assert(arr[8] == 1); // C3 should have the value 1
// Test cell reference
arr[0] = 3; // A1 = 3
char input2[] = "B1=SLEEP(A1)";
sleep_func(input2, C, R, 2, strlen(input2), arr, graph, formulaArray);
assert(arr[1] == 3); // B1 should have the value from A1
printf("sleep_func tests passed!\n");
// Clean up
free(arr);
free(graph->adjLists_head);
free(graph);
}
// Test for scroller() function with 'w' command (scroll up)
void test_scroller_w_command()
{
printf("Testing scroller with 'w' command...\n");
Graph *graph = CreateGraph();
int *arr = (int *)calloc(100, sizeof(int)); // 10x10 grid for simplicity
int currx = 0, curry = 5;
int C = 10, R = 10;
// Test scrolling up when possible
char cmd1[] = "w";
scroller(cmd1, arr, &currx, &curry, C, R, graph);
assert(curry == 0); // Should scroll to top since curry < 10
// Test scrolling up when already at top
char cmd2[] = "w";
scroller(cmd2, arr, &currx, &curry, C, R, graph);
assert(curry == 0); // Should remain at top
// Test scrolling up from position > 10
curry = 15;
char cmd3[] = "w";
scroller(cmd3, arr, &currx, &curry, C, R, graph);
assert(curry == 5); // Should scroll up by 10
free(arr);
free(graph->adjLists_head);
free(graph);
printf("scroller 'w' command tests passed!\n");
}
// Test for scroller() function with 's' command (scroll down)
void test_scroller_s_command()
{
printf("Testing scroller with 's' command...\n");
Graph *graph = CreateGraph();
int *arr = (int *)calloc(400, sizeof(int)); // 20x20 grid
int currx = 0, curry = 0;
int C = 20, R = 20;
// Test scrolling down when possible
char cmd1[] = "s";
scroller(cmd1, arr, &currx, &curry, C, R, graph);
assert(curry == 10); // Should scroll down by 10
// Test scrolling down near bottom
curry = 5;
char cmd2[] = "s";
scroller(cmd2, arr, &currx, &curry, C, R, graph);
assert(curry == 10); // Should scroll down by 5
// Test scrolling down when at bottom
curry = 10;
char cmd3[] = "s";
scroller(cmd3, arr, &currx, &curry, C, R, graph);
assert(curry == 10); // Should remain at 10 since scrolling would go out of bounds
free(arr);
free(graph->adjLists_head);
free(graph);
printf("scroller 's' command tests passed!\n");
}
// Test for scroller() function with 'a' command (scroll left)
void test_scroller_a_command()
{
printf("Testing scroller with 'a' command...\n");
Graph *graph = CreateGraph();
int *arr = (int *)calloc(400, sizeof(int)); // 20x20 grid
int currx = 5, curry = 0;
int C = 20, R = 20;
// Test scrolling left when possible
char cmd1[] = "a";
scroller(cmd1, arr, &currx, &curry, C, R, graph);
assert(currx == 0); // Should scroll to leftmost since currx < 10
// Test scrolling left when already at leftmost
char cmd2[] = "a";
scroller(cmd2, arr, &currx, &curry, C, R, graph);
assert(currx == 0); // Should remain at leftmost
// Test scrolling left from position > 10
currx = 15;
char cmd3[] = "a";
scroller(cmd3, arr, &currx, &curry, C, R, graph);
assert(currx == 5); // Should scroll left by 10
free(arr);
free(graph->adjLists_head);
free(graph);
printf("scroller 'a' command tests passed!\n");
}
// Test for scroller() function with 'd' command (scroll right)
void test_scroller_d_command()
{
printf("Testing scroller with 'd' command...\n");
Graph *graph = CreateGraph();
int *arr = (int *)calloc(400, sizeof(int)); // 20x20 grid
int currx = 0, curry = 0;
int C = 20, R = 20;
// Test scrolling right when possible
char cmd1[] = "d";
scroller(cmd1, arr, &currx, &curry, C, R, graph);
assert(currx == 10); // Should scroll right by 10
// Test scrolling right with less than 10 columns remaining
char cmd2[] = "d";
scroller(cmd2, arr, &currx, &curry, C, R, graph);
assert(currx == 10); // Should remain at 10 since 10+10 = 20 (grid width)
// Test in a wider grid
C = 30;
char cmd3[] = "d";
scroller(cmd3, arr, &currx, &curry, C, R, graph);
assert(currx == 20); // Should scroll right by 10
free(arr);
free(graph->adjLists_head);
free(graph);
printf("scroller 'd' command tests passed!\n");
}
// Test for scroller() function with 'scroll_to' command
void test_scroller_scroll_to_command()
{
printf("Testing scroller with 'scroll_to' command...\n");
Graph *graph = CreateGraph();
int *arr = (int *)calloc(400, sizeof(int)); // 20x20 grid
int currx = 0, curry = 0;
int C = 20, R = 20;
char cmd1[] = "scroll_to A1";
scroller(cmd1, arr, &currx, &curry, C, R, graph);
// Test invalid scroll target (out of bounds)
char cmd2[] = "scroll_to Z99"; // Assuming this is out of bounds
int old_x = currx;
int old_y = curry;
scroller(cmd2, arr, &currx, &curry, C, R, graph);
// Test unrecognized command
char cmd3[] = "invalid";
scroller(cmd3, arr, &currx, &curry, C, R, graph);
free(arr);
free(graph->adjLists_head);
free(graph);
printf("scroller 'scroll_to' command tests passed!\n");
}
// Test for printer() function
void test_printer_function()
{
printf("Testing printer function...\n");
int C = 20, R = 20;
int *arr = (int *)calloc(C * R, sizeof(int));
// Initialize some test values
for (int i = 0; i < C * R; i++)
{
arr[i] = i;
}
// Set a few cells to INT_MIN to test error display
arr[5] = INT_MIN;
arr[25] = INT_MIN;
// Test printing from different starting positions
// We can't automatically verify the output, but we can verify it doesn't crash
// Print from top-left
printf("Printing from top-left (0,0):\n");
printer(0, 0, arr, C, R);
// Print from middle
printf("\nPrinting from middle (5,5):\n");
printer(5, 5, arr, C, R);
// Print near edge
printf("\nPrinting near edge (15,15):\n");
printer(15, 15, arr, C, R);