-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.cpp
1438 lines (1177 loc) · 70.9 KB
/
tests.cpp
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
// tests.cpp
#include "crdt.hpp"
#include <cstdlib>
#include <iostream>
#include <string>
#include <unordered_map>
// Helper function to generate unique IDs (simulating UUIDs)
CrdtString generate_uuid() {
static uint64_t counter = 0;
return "uuid-" + std::to_string(++counter);
}
/// Simple assertion helper
void assert_true(bool condition, const CrdtString &message) {
if (!condition) {
std::cerr << "Assertion failed: " << message << std::endl;
exit(1);
}
}
int main() {
// Test Case: Basic Insert and Merge using insert_or_update
{
CRDT<CrdtString, CrdtString> node1(1);
CRDT<CrdtString, CrdtString> node2(2);
// Node1 inserts a record
CrdtString record_id = generate_uuid();
CrdtString form_id = generate_uuid();
CrdtVector<Change<CrdtString, CrdtString>> changes1;
node1.insert_or_update(record_id, changes1, std::make_pair("id", record_id), std::make_pair("form_id", form_id),
std::make_pair("tag", "Node1Tag"), std::make_pair("created_at", "2023-10-01T12:00:00Z"),
std::make_pair("created_by", "User1"));
// Node2 inserts the same record with different data
CrdtVector<Change<CrdtString, CrdtString>> changes2;
node2.insert_or_update(record_id, changes2, std::make_pair("id", record_id), std::make_pair("form_id", form_id),
std::make_pair("tag", "Node2Tag"), std::make_pair("created_at", "2023-10-01T12:05:00Z"),
std::make_pair("created_by", "User2"));
// Merge node2's changes into node1
node1.merge_changes(std::move(changes2));
// Merge node1's changes into node2
node2.merge_changes(std::move(changes1));
// Both nodes should resolve the conflict and have the same data
assert_true(node1.get_data() == node2.get_data(), "Basic Insert and Merge: Data mismatch");
assert_true(node1.get_data().at(record_id).fields.at("tag") == "Node2Tag",
"Basic Insert and Merge: Tag should be 'Node2Tag'");
assert_true(node1.get_data().at(record_id).fields.at("created_by") == "User2",
"Basic Insert and Merge: created_by should be 'User2'");
std::cout << "Test 'Basic Insert and Merge' passed." << std::endl;
}
// Test Case: Updates with Conflicts using insert_or_update
{
CRDT<CrdtString, CrdtString> node1(1);
CRDT<CrdtString, CrdtString> node2(2);
// Insert a shared record
CrdtString record_id = generate_uuid();
CrdtVector<Change<CrdtString, CrdtString>> changes_init1, changes_init2;
node1.insert_or_update(record_id, changes_init1, std::make_pair("id", record_id), std::make_pair("tag", "InitialTag"));
node2.insert_or_update(record_id, changes_init2, std::make_pair("id", record_id), std::make_pair("tag", "InitialTag"));
// Merge initial inserts
node1.merge_changes(std::move(changes_init2));
node2.merge_changes(std::move(changes_init1));
// Node1 updates 'tag'
CrdtVector<Change<CrdtString, CrdtString>> change_update1;
node1.insert_or_update(record_id, change_update1, std::make_pair("tag", "Node1UpdatedTag"));
// Node2 updates 'tag'
CrdtVector<Change<CrdtString, CrdtString>> change_update2;
node2.insert_or_update(record_id, change_update2, std::make_pair("tag", "Node2UpdatedTag"));
// Merge changes
node1.merge_changes(std::move(change_update2));
node2.merge_changes(std::move(change_update1));
// Conflict resolved based on site_id (Node2 has higher site_id)
assert_true(node1.get_data().at(record_id).fields.at("tag") == "Node2UpdatedTag",
"Updates with Conflicts: Tag resolution mismatch");
assert_true(node1.get_data() == node2.get_data(), "Updates with Conflicts: Data mismatch");
std::cout << "Test 'Updates with Conflicts' passed." << std::endl;
}
// Test Case: Delete and Merge using insert_or_update
{
CRDT<CrdtString, CrdtString> node1(1);
CRDT<CrdtString, CrdtString> node2(2);
// Insert and sync a record
CrdtString record_id = generate_uuid();
CrdtVector<Change<CrdtString, CrdtString>> changes_init;
node1.insert_or_update(record_id, changes_init, std::make_pair("id", record_id), std::make_pair("tag", "ToBeDeleted"));
// Merge to node2
node2.merge_changes(std::move(changes_init));
// Node1 deletes the record
CrdtVector<Change<CrdtString, CrdtString>> changes_delete;
node1.delete_record(record_id, changes_delete);
// Merge the deletion to node2
node2.merge_changes(std::move(changes_delete));
// Both nodes should reflect the deletion
assert_true(node1.get_data().at(record_id).fields.empty(), "Delete and Merge: Node1 should have empty fields");
assert_true(node2.get_data().at(record_id).fields.empty(), "Delete and Merge: Node2 should have empty fields");
assert_true(node1.get_data().at(record_id).column_versions.find("__deleted__") !=
node1.get_data().at(record_id).column_versions.end(),
"Delete and Merge: Node1 should have '__deleted__' column version");
assert_true(node2.get_data().at(record_id).column_versions.find("__deleted__") !=
node2.get_data().at(record_id).column_versions.end(),
"Delete and Merge: Node2 should have '__deleted__' column version");
std::cout << "Test 'Delete and Merge' passed." << std::endl;
}
// Test Case: Tombstone Handling using insert_or_update
{
CRDT<CrdtString, CrdtString> node1(1);
CRDT<CrdtString, CrdtString> node2(2);
// Insert a record and delete it on node1
CrdtString record_id = generate_uuid();
CrdtVector<Change<CrdtString, CrdtString>> changes_insert, changes_delete;
node1.insert_or_update(record_id, changes_insert, std::make_pair("id", record_id), std::make_pair("tag", "Temporary"));
node1.delete_record(record_id, changes_delete);
// Merge changes to node2
node2.merge_changes(std::move(changes_insert));
node2.merge_changes(std::move(changes_delete));
// Node2 tries to insert the same record
CrdtVector<Change<CrdtString, CrdtString>> changes_attempt_insert;
node2.insert_or_update(record_id, changes_attempt_insert, std::make_pair("id", record_id),
std::make_pair("tag", "Temporary"));
// Merge changes back to node1
node1.merge_changes(std::move(changes_attempt_insert));
// Node2 should respect the tombstone
assert_true(node2.get_data().at(record_id).fields.empty(), "Tombstone Handling: Node2 should have empty fields");
assert_true(node2.get_data().at(record_id).column_versions.find("__deleted__") !=
node2.get_data().at(record_id).column_versions.end(),
"Tombstone Handling: Node2 should have '__deleted__' column version");
std::cout << "Test 'Tombstone Handling' passed." << std::endl;
}
// Test Case: Conflict Resolution with site_id using insert_or_update
{
CRDT<CrdtString, CrdtString> node1(1);
CRDT<CrdtString, CrdtString> node2(2);
// Both nodes insert a record with the same id
CrdtString record_id = generate_uuid();
CrdtVector<Change<CrdtString, CrdtString>> changes1, changes2;
node1.insert_or_update(record_id, changes1, std::make_pair("id", record_id), std::make_pair("tag", "Node1Tag"));
node2.insert_or_update(record_id, changes2, std::make_pair("id", record_id), std::make_pair("tag", "Node2Tag"));
// Merge changes
node1.merge_changes(std::move(changes2));
node2.merge_changes(std::move(changes1));
// Both nodes update the 'tag' field multiple times
CrdtVector<Change<CrdtString, CrdtString>> changes_update1, changes_update2, changes_update3, changes_update4;
node1.insert_or_update(record_id, changes_update1, std::make_pair("tag", "Node1Tag1"));
node1.insert_or_update(record_id, changes_update2, std::make_pair("tag", "Node1Tag2"));
node2.insert_or_update(record_id, changes_update3, std::make_pair("tag", "Node2Tag1"));
node2.insert_or_update(record_id, changes_update4, std::make_pair("tag", "Node2Tag2"));
// Merge changes
node1.merge_changes(std::move(changes_update4));
node2.merge_changes(std::move(changes_update2));
node2.merge_changes(std::move(changes_update1));
node1.merge_changes(std::move(changes_update3));
// Since node2 has a higher site_id, its latest update should prevail
CrdtString expected_tag = "Node2Tag2";
assert_true(node1.get_data().at(record_id).fields.at("tag") == expected_tag, "Conflict Resolution: Tag resolution mismatch");
assert_true(node1.get_data() == node2.get_data(), "Conflict Resolution: Data mismatch");
std::cout << "Test 'Conflict Resolution with site_id' passed." << std::endl;
}
// Test Case: Logical Clock Update using insert_or_update
{
CRDT<CrdtString, CrdtString> node1(1);
CRDT<CrdtString, CrdtString> node2(2);
// Node1 inserts a record
CrdtString record_id = generate_uuid();
CrdtVector<Change<CrdtString, CrdtString>> changes_insert;
node1.insert_or_update(record_id, changes_insert, std::make_pair("id", record_id), std::make_pair("tag", "Node1Tag"));
// Node2 receives the change
node2.merge_changes(std::move(changes_insert));
// Node2's clock should be updated
assert_true(node2.get_clock().current_time() > 0, "Logical Clock Update: Node2 clock should be greater than 0");
assert_true(node2.get_clock().current_time() >= node1.get_clock().current_time(),
"Logical Clock Update: Node2 clock should be >= Node1 clock");
std::cout << "Test 'Logical Clock Update' passed." << std::endl;
}
// Test Case: Merge without Conflicts using insert_or_update
{
CRDT<CrdtString, CrdtString> node1(1);
CRDT<CrdtString, CrdtString> node2(2);
// Node1 inserts a record
CrdtString record_id1 = generate_uuid();
CrdtVector<Change<CrdtString, CrdtString>> changes1;
node1.insert_or_update(record_id1, changes1, std::make_pair("id", record_id1), std::make_pair("tag", "Node1Record"));
// Node2 inserts a different record
CrdtString record_id2 = generate_uuid();
CrdtVector<Change<CrdtString, CrdtString>> changes2;
node2.insert_or_update(record_id2, changes2, std::make_pair("id", record_id2), std::make_pair("tag", "Node2Record"));
// Merge changes
node1.merge_changes(std::move(changes2));
node2.merge_changes(std::move(changes1));
// Both nodes should have both records
assert_true(node1.get_data().find(record_id1) != node1.get_data().end(),
"Merge without Conflicts: Node1 should contain record_id1");
assert_true(node1.get_data().find(record_id2) != node1.get_data().end(),
"Merge without Conflicts: Node1 should contain record_id2");
assert_true(node2.get_data().find(record_id1) != node2.get_data().end(),
"Merge without Conflicts: Node2 should contain record_id1");
assert_true(node2.get_data().find(record_id2) != node2.get_data().end(),
"Merge without Conflicts: Node2 should contain record_id2");
assert_true(node1.get_data() == node2.get_data(), "Merge without Conflicts: Data mismatch between Node1 and Node2");
std::cout << "Test 'Merge without Conflicts' passed." << std::endl;
}
// Test Case: Multiple Merges using insert_or_update
{
CRDT<CrdtString, CrdtString> node1(1);
CRDT<CrdtString, CrdtString> node2(2);
// Node1 inserts a record
CrdtString record_id = generate_uuid();
CrdtVector<Change<CrdtString, CrdtString>> changes_init;
node1.insert_or_update(record_id, changes_init, std::make_pair("id", record_id), std::make_pair("tag", "InitialTag"));
// Merge to node2
node2.merge_changes(std::move(changes_init));
// Node2 updates the record
CrdtVector<Change<CrdtString, CrdtString>> changes_update2;
node2.insert_or_update(record_id, changes_update2, std::make_pair("tag", "UpdatedByNode2"));
// Node1 updates the record
CrdtVector<Change<CrdtString, CrdtString>> changes_update1;
node1.insert_or_update(record_id, changes_update1, std::make_pair("tag", "UpdatedByNode1"));
// Merge changes
node1.merge_changes(std::move(changes_update2));
node2.merge_changes(std::move(changes_update1));
// Since node2 has a higher site_id, its latest update should prevail
CrdtString expected_tag = "UpdatedByNode2";
assert_true(node1.get_data().at(record_id).fields.at("tag") == expected_tag, "Multiple Merges: Tag resolution mismatch");
assert_true(node1.get_data() == node2.get_data(), "Multiple Merges: Data mismatch between Node1 and Node2");
std::cout << "Test 'Multiple Merges' passed." << std::endl;
}
// Test Case: Inserting After Deletion using insert_or_update
{
CRDT<CrdtString, CrdtString> node1(1);
CRDT<CrdtString, CrdtString> node2(2);
// Node1 inserts and deletes a record
CrdtString record_id = generate_uuid();
CrdtVector<Change<CrdtString, CrdtString>> changes_insert, changes_delete;
node1.insert_or_update(record_id, changes_insert, std::make_pair("id", record_id), std::make_pair("tag", "Temporary"));
node1.delete_record(record_id, changes_delete);
// Merge deletion to node2
node2.merge_changes(std::move(changes_insert));
node2.merge_changes(std::move(changes_delete));
// Node2 tries to insert the same record
CrdtVector<Change<CrdtString, CrdtString>> changes_attempt_insert;
node2.insert_or_update(record_id, changes_attempt_insert, std::make_pair("id", record_id),
std::make_pair("tag", "Temporary"));
// Merge changes back to node1
node1.merge_changes(std::move(changes_attempt_insert));
// The deletion should prevail
assert_true(node1.get_data().at(record_id).fields.empty(), "Inserting After Deletion: Node1 should have empty fields");
assert_true(node2.get_data().at(record_id).fields.empty(), "Inserting After Deletion: Node2 should have empty fields");
assert_true(node1.get_data().at(record_id).column_versions.find("__deleted__") !=
node1.get_data().at(record_id).column_versions.end(),
"Inserting After Deletion: Node1 should have '__deleted__' column version");
assert_true(node2.get_data().at(record_id).column_versions.find("__deleted__") !=
node2.get_data().at(record_id).column_versions.end(),
"Inserting After Deletion: Node2 should have '__deleted__' column version");
std::cout << "Test 'Inserting After Deletion' passed." << std::endl;
}
// Test Case: Offline Changes Then Merge using insert_or_update
{
CRDT<CrdtString, CrdtString> node1(1);
CRDT<CrdtString, CrdtString> node2(2);
// Both nodes start with an empty state
// Node1 inserts a record
CrdtString record_id1 = generate_uuid();
CrdtVector<Change<CrdtString, CrdtString>> changes1;
node1.insert_or_update(record_id1, changes1, std::make_pair("id", record_id1), std::make_pair("tag", "Node1Tag"));
// Node2 is offline and inserts a different record
CrdtString record_id2 = generate_uuid();
CrdtVector<Change<CrdtString, CrdtString>> changes2;
node2.insert_or_update(record_id2, changes2, std::make_pair("id", record_id2), std::make_pair("tag", "Node2Tag"));
// Now, node2 comes online and merges changes from node1
uint64_t last_db_version_node2 = 0;
sync_nodes(node1, node2, last_db_version_node2);
// Similarly, node1 merges changes from node2
uint64_t last_db_version_node1 = 0;
sync_nodes(node2, node1, last_db_version_node1);
// Both nodes should now have both records
assert_true(node1.get_data().find(record_id1) != node1.get_data().end(),
"Offline Changes Then Merge: Node1 should contain record_id1");
assert_true(node1.get_data().find(record_id2) != node1.get_data().end(),
"Offline Changes Then Merge: Node1 should contain record_id2");
assert_true(node2.get_data().find(record_id1) != node2.get_data().end(),
"Offline Changes Then Merge: Node2 should contain record_id1");
assert_true(node2.get_data().find(record_id2) != node2.get_data().end(),
"Offline Changes Then Merge: Node2 should contain record_id2");
assert_true(node1.get_data() == node2.get_data(), "Offline Changes Then Merge: Data mismatch between Node1 and Node2");
std::cout << "Test 'Offline Changes Then Merge' passed." << std::endl;
}
// Test Case: Conflicting Updates with Different Last DB Versions using insert_or_update
{
CRDT<CrdtString, CrdtString> node1(1);
CRDT<CrdtString, CrdtString> node2(2);
// Both nodes insert the same record
CrdtString record_id = generate_uuid();
CrdtVector<Change<CrdtString, CrdtString>> changes_init1, changes_init2;
node1.insert_or_update(record_id, changes_init1, std::make_pair("id", record_id), std::make_pair("tag", "InitialTag"));
node2.insert_or_update(record_id, changes_init2, std::make_pair("id", record_id), std::make_pair("tag", "InitialTag"));
// Merge initial inserts
node1.merge_changes(std::move(changes_init2));
node2.merge_changes(std::move(changes_init1));
// Node1 updates 'tag' twice
CrdtVector<Change<CrdtString, CrdtString>> changes_node1_update1, changes_node1_update2;
node1.insert_or_update(record_id, changes_node1_update1, std::make_pair("tag", "Node1Tag1"));
node1.insert_or_update(record_id, changes_node1_update2, std::make_pair("tag", "Node1Tag2"));
// Node2 updates 'tag' once
CrdtVector<Change<CrdtString, CrdtString>> changes_node2_update1;
node2.insert_or_update(record_id, changes_node2_update1, std::make_pair("tag", "Node2Tag1"));
// Merge node1's changes into node2
node2.merge_changes(std::move(changes_node1_update1));
node2.merge_changes(std::move(changes_node1_update2));
// Merge node2's changes into node1
node1.merge_changes(std::move(changes_node2_update1));
// The 'tag' should reflect the latest update based on db_version and site_id Assuming node1 has a higher db_version due to
// two updates
CrdtString final_tag = "Node1Tag2";
assert_true(node1.get_data().at(record_id).fields.at("tag") == final_tag,
"Conflicting Updates: Final tag should be 'Node1Tag2'");
assert_true(node2.get_data().at(record_id).fields.at("tag") == final_tag,
"Conflicting Updates: Final tag should be 'Node1Tag2'");
assert_true(node1.get_data() == node2.get_data(), "Conflicting Updates: Data mismatch between Node1 and Node2");
std::cout << "Test 'Conflicting Updates with Different Last DB Versions' passed." << std::endl;
}
// Test Case: Atomic Sync Per Transaction using insert_or_update
{
CRDT<CrdtString, CrdtString> node1(1);
CRDT<CrdtString, CrdtString> node2(2);
// Node1 inserts a record
CrdtString record_id = generate_uuid();
CrdtVector<Change<CrdtString, CrdtString>> changes_node1;
node1.insert_or_update(record_id, changes_node1, std::make_pair("id", record_id), std::make_pair("tag", "InitialTag"));
// Sync immediately after the transaction
node2.merge_changes(std::move(changes_node1));
// Verify synchronization
assert_true(node2.get_data().find(record_id) != node2.get_data().end(),
"Atomic Sync: Node2 should contain the inserted record");
assert_true(node2.get_data().at(record_id).fields.at("tag") == "InitialTag", "Atomic Sync: Tag should be 'InitialTag'");
std::cout << "Test 'Atomic Sync Per Transaction' passed." << std::endl;
}
// Test Case: Concurrent Updates using insert_or_update
{
CRDT<CrdtString, CrdtString> node1(1);
CRDT<CrdtString, CrdtString> node2(2);
// Insert a record on node1
CrdtString record_id = generate_uuid();
CrdtVector<Change<CrdtString, CrdtString>> changes_insert;
node1.insert_or_update(record_id, changes_insert, std::make_pair("id", record_id), std::make_pair("tag", "InitialTag"));
// Merge to node2
node2.merge_changes(std::move(changes_insert));
// Concurrently update 'tag' on both nodes
CrdtVector<Change<CrdtString, CrdtString>> changes_update1, changes_update2;
node1.insert_or_update(record_id, changes_update1, std::make_pair("tag", "Node1TagUpdate"));
node2.insert_or_update(record_id, changes_update2, std::make_pair("tag", "Node2TagUpdate"));
// Merge changes
node1.merge_changes(std::move(changes_update2));
node2.merge_changes(std::move(changes_update1));
// Conflict resolution based on site_id (Node2 has higher site_id)
CrdtString expected_tag = "Node2TagUpdate";
assert_true(node1.get_data().at(record_id).fields.at("tag") == expected_tag,
"Concurrent Updates: Tag should be 'Node2TagUpdate'");
assert_true(node2.get_data().at(record_id).fields.at("tag") == expected_tag,
"Concurrent Updates: Tag should be 'Node2TagUpdate'");
std::cout << "Test 'Concurrent Updates' passed." << std::endl;
}
// Test Case: Get Changes Since After Loading with Merge Versions
{
// Initialize CRDT with pre-loaded changes
CrdtVector<Change<CrdtString, CrdtString>> changes;
CrdtNodeId node_id = 1;
CrdtString record_id = generate_uuid();
changes.emplace_back(Change<CrdtString, CrdtString>(record_id, "field1", "value1", 1, 1, node_id));
CRDT<CrdtString, CrdtString> crdt_loaded(node_id, std::move(changes));
// Make additional changes after loading
CrdtVector<Change<CrdtString, CrdtString>> changes_new;
crdt_loaded.insert_or_update(record_id, changes_new, std::make_pair("field2", "value2"));
// Retrieve changes since db_version 1
CrdtVector<Change<CrdtString, CrdtString>> retrieved_changes = crdt_loaded.get_changes_since(1);
// Should include only the new change
assert_true(retrieved_changes.size() == 1, "Get Changes Since: Should retrieve one new change");
assert_true(retrieved_changes[0].col_name.has_value() && retrieved_changes[0].col_name.value() == "field2",
"Get Changes Since: Retrieved change should be for 'field2'");
assert_true(retrieved_changes[0].value.has_value() && retrieved_changes[0].value.value() == "value2",
"Get Changes Since: Retrieved change 'field2' value mismatch");
std::cout << "Test 'Get Changes Since After Loading with Merge Versions' passed." << std::endl;
}
// Test Case: Prevent Reapplication of Changes Loaded via Constructor
{
// Initialize CRDT with pre-loaded changes
CrdtVector<Change<CrdtString, CrdtString>> changes;
CrdtNodeId node_id = 1;
CrdtString record_id = generate_uuid();
changes.emplace_back(Change<CrdtString, CrdtString>(record_id, "field1", "value1", 1, 1, node_id));
CRDT<CrdtString, CrdtString> crdt_loaded(node_id, std::move(changes));
// Attempt to merge the same changes again
crdt_loaded.merge_changes({Change<CrdtString, CrdtString>(record_id, "field1", "value1", 1, 1, node_id)});
// Verify that no duplicate changes are applied
const auto &data = crdt_loaded.get_data();
assert_true(data.at(record_id).fields.at("field1") == "value1",
"Prevent Reapplication: 'field1' value should remain 'value1'");
std::cout << "Test 'Prevent Reapplication of Changes Loaded via Constructor' passed." << std::endl;
}
// Test Case: Complex Merge Scenario with Merge DB Versions
{
// Initialize two CRDTs with pre-loaded changes
CrdtVector<Change<CrdtString, CrdtString>> changes_node1;
CrdtNodeId node1_id = 1;
CrdtString record_id = generate_uuid();
changes_node1.emplace_back(Change<CrdtString, CrdtString>(record_id, "field1", "node1_value1", 1, 1, node1_id));
CRDT<CrdtString, CrdtString> node1_crdt(node1_id, std::move(changes_node1));
CrdtVector<Change<CrdtString, CrdtString>> changes_node2;
CrdtNodeId node2_id = 2;
changes_node2.emplace_back(Change<CrdtString, CrdtString>(record_id, "field1", "node2_value1", 2, 2, node2_id));
CRDT<CrdtString, CrdtString> node2_crdt(node2_id, std::move(changes_node2));
// Merge node2 into node1
node1_crdt.merge_changes({Change<CrdtString, CrdtString>(record_id, "field1", "node2_value1", 2, 2, node2_id)});
// Merge node1 into node2
node2_crdt.merge_changes({Change<CrdtString, CrdtString>(record_id, "field1", "node1_value1", 1, 1, node1_id)});
// Verify conflict resolution based on db_version and node_id
// node2's change should prevail since it has a higher db_version
assert_true(node1_crdt.get_data().at(record_id).fields.at("field1") == "node2_value1",
"Complex Merge: node2's change should prevail in node1");
assert_true(node2_crdt.get_data().at(record_id).fields.at("field1") == "node2_value1",
"Complex Merge: node2's change should prevail in node2");
std::cout << "Test 'Complex Merge Scenario with Merge DB Versions' passed." << std::endl;
}
// Test Case: get_changes_since Considers merge_db_version Correctly
{
// Initialize CRDT and perform initial changes
CRDT<CrdtString, CrdtString> crdt(1);
CrdtString record_id = generate_uuid();
CrdtVector<Change<CrdtString, CrdtString>> changes_init;
crdt.insert_or_update(record_id, changes_init, std::make_pair("field1", "value1"));
// Apply changes and set merge_db_version via constructor
CRDT<CrdtString, CrdtString> crdt_loaded(2, std::move(changes_init));
// Make new changes after loading
CrdtVector<Change<CrdtString, CrdtString>> changes_new;
crdt_loaded.insert_or_update(record_id, changes_new, std::make_pair("field2", "value2"));
// Get changes since db_version 1
CrdtVector<Change<CrdtString, CrdtString>> retrieved_changes = crdt_loaded.get_changes_since(1);
// Should include only the new change
assert_true(retrieved_changes.size() == 1, "get_changes_since: Should retrieve one new change");
assert_true(retrieved_changes[0].col_name.has_value() && retrieved_changes[0].col_name.value() == "field2",
"get_changes_since: Retrieved change should be for 'field2'");
assert_true(retrieved_changes[0].value.has_value() && retrieved_changes[0].value.value() == "value2",
"get_changes_since: Retrieved change 'field2' value mismatch");
std::cout << "Test 'get_changes_since Considers merge_db_version Correctly' passed." << std::endl;
}
// Test Case: Multiple Loads and Merges with Merge DB Versions
{
// Simulate loading from disk multiple times
CrdtVector<Change<CrdtString, CrdtString>> changes_load1;
CrdtNodeId node_id = 1;
CrdtString record_id1 = generate_uuid();
changes_load1.emplace_back(Change<CrdtString, CrdtString>(record_id1, "field1", "value1", 1, 1, node_id));
CRDT<CrdtString, CrdtString> crdt1(node_id, std::move(changes_load1));
CrdtVector<Change<CrdtString, CrdtString>> changes_load2;
CrdtString record_id2 = generate_uuid();
changes_load2.emplace_back(Change<CrdtString, CrdtString>(record_id2, "field2", "value2", 2, 2, node_id));
CRDT<CrdtString, CrdtString> crdt2(node_id, std::move(changes_load2));
// Merge crdt2 into crdt1
crdt1.merge_changes({Change<CrdtString, CrdtString>(record_id2, "field2", "value2", 2, 2, node_id)});
// Make additional changes
CrdtVector<Change<CrdtString, CrdtString>> changes_new;
crdt1.insert_or_update(record_id1, changes_new, std::make_pair("field3", "value3"));
// Get changes since db_version 3
CrdtVector<Change<CrdtString, CrdtString>> retrieved_changes = crdt1.get_changes_since(3);
// Should include only the new change
assert_true(retrieved_changes.size() == 1, "Multiple Loads and Merges: Should retrieve one new change");
assert_true(retrieved_changes[0].col_name.has_value() && retrieved_changes[0].col_name.value() == "field3",
"Multiple Loads and Merges: Retrieved change should be for 'field3'");
assert_true(retrieved_changes[0].value.has_value() && retrieved_changes[0].value.value() == "value3",
"Multiple Loads and Merges: Retrieved change 'field3' value mismatch");
std::cout << "Test 'Multiple Loads and Merges with Merge DB Versions' passed." << std::endl;
}
// Test Case: Parent-Child Overlay Functionality
{
// Create parent CRDT
CRDT<CrdtString, CrdtString> parent_crdt(1);
CrdtString record_id_parent = generate_uuid();
CrdtVector<Change<CrdtString, CrdtString>> parent_changes;
parent_crdt.insert_or_update(record_id_parent, parent_changes, std::make_pair("id", record_id_parent),
std::make_pair("parent_field", "parent_value"));
// Create child CRDT with parent
auto parent_ptr = std::make_shared<CRDT<CrdtString, CrdtString>>(parent_crdt);
CRDT<CrdtString, CrdtString> child_crdt(2, parent_ptr);
// Child should inherit parent's record
assert_true(child_crdt.get_data_combined().find(record_id_parent) != child_crdt.get_data_combined().end(),
"Parent-Child Overlay: Child should inherit parent's record");
assert_true(child_crdt.get_data_combined().at(record_id_parent).fields.at("parent_field") == "parent_value",
"Parent-Child Overlay: Inherited field value mismatch");
// Child updates the inherited record
CrdtVector<Change<CrdtString, CrdtString>> child_changes;
child_crdt.insert_or_update(record_id_parent, child_changes, std::make_pair("child_field", "child_value"));
// Merge child's changes back to parent
parent_crdt.merge_changes(std::move(child_changes));
// Parent should now have the child's field
assert_true(parent_crdt.get_data_combined().at(record_id_parent).fields.at("child_field") == "child_value",
"Parent-Child Overlay: Parent should reflect child's update");
std::cout << "Test 'Parent-Child Overlay Functionality' passed." << std::endl;
}
// Add these tests after the existing tests in the main function
// Test Case: Parent-Child Overlay with Multiple Levels
{
// Create grandparent CRDT
CRDT<CrdtString, CrdtString> grandparent_crdt(1);
CrdtString record_id = generate_uuid();
CrdtVector<Change<CrdtString, CrdtString>> grandparent_changes;
grandparent_crdt.insert_or_update(record_id, grandparent_changes, std::make_pair("id", record_id),
std::make_pair("level", "grandparent"));
// Create parent CRDT with grandparent
auto grandparent_ptr = std::make_shared<CRDT<CrdtString, CrdtString>>(grandparent_crdt);
CRDT<CrdtString, CrdtString> parent_crdt(2, grandparent_ptr);
CrdtVector<Change<CrdtString, CrdtString>> parent_changes;
parent_crdt.insert_or_update(record_id, parent_changes, std::make_pair("level", "parent"));
// Create child CRDT with parent
auto parent_ptr = std::make_shared<CRDT<CrdtString, CrdtString>>(parent_crdt);
CRDT<CrdtString, CrdtString> child_crdt(3, parent_ptr);
CrdtVector<Change<CrdtString, CrdtString>> child_changes;
child_crdt.insert_or_update(record_id, child_changes, std::make_pair("level", "child"));
// Check that child has the most recent value
assert_true(child_crdt.get_data_combined().at(record_id).fields.at("level") == "child",
"Multi-level Overlay: Child should have its own value");
// Check that parent has its own value
assert_true(parent_crdt.get_data_combined().at(record_id).fields.at("level") == "parent",
"Multi-level Overlay: Parent should have its own value");
// Check that grandparent has its original value
assert_true(grandparent_crdt.get_data_combined().at(record_id).fields.at("level") == "grandparent",
"Multi-level Overlay: Grandparent should have its original value");
std::cout << "Test 'Parent-Child Overlay with Multiple Levels' passed." << std::endl;
}
// Test Case: Inheritance of Records from Parent
{
// Create parent CRDT
CRDT<CrdtString, CrdtString> parent_crdt(1);
CrdtString record_id1 = generate_uuid();
CrdtString record_id2 = generate_uuid();
CrdtVector<Change<CrdtString, CrdtString>> parent_changes1, parent_changes2;
parent_crdt.insert_or_update(record_id1, parent_changes1, std::make_pair("id", record_id1),
std::make_pair("data", "parent_data1"));
parent_crdt.insert_or_update(record_id2, parent_changes2, std::make_pair("id", record_id2),
std::make_pair("data", "parent_data2"));
// Create child CRDT with parent
auto parent_ptr = std::make_shared<CRDT<CrdtString, CrdtString>>(parent_crdt);
CRDT<CrdtString, CrdtString> child_crdt(2, parent_ptr);
// Check that child inherits both records from parent
assert_true(child_crdt.get_data_combined().at(record_id1).fields.at("data") == "parent_data1",
"Record Inheritance: Child should inherit record1 from parent");
assert_true(child_crdt.get_data_combined().at(record_id2).fields.at("data") == "parent_data2",
"Record Inheritance: Child should inherit record2 from parent");
std::cout << "Test 'Inheritance of Records from Parent' passed." << std::endl;
}
// Test Case: Overriding Parent Records in Child
{
// Create parent CRDT
CRDT<CrdtString, CrdtString> parent_crdt(1);
CrdtString record_id = generate_uuid();
CrdtVector<Change<CrdtString, CrdtString>> parent_changes;
parent_crdt.insert_or_update(record_id, parent_changes, std::make_pair("id", record_id),
std::make_pair("data", "parent_data"));
// Create child CRDT with parent
auto parent_ptr = std::make_shared<CRDT<CrdtString, CrdtString>>(parent_crdt);
CRDT<CrdtString, CrdtString> child_crdt(2, parent_ptr);
// Override parent's record in child
CrdtVector<Change<CrdtString, CrdtString>> child_changes;
child_crdt.insert_or_update(record_id, child_changes, std::make_pair("data", "child_data"));
// Check that child has its own value
assert_true(child_crdt.get_data_combined().at(record_id).fields.at("data") == "child_data",
"Record Override: Child should have its own value");
// Check that parent still has its original value
assert_true(parent_crdt.get_data_combined().at(record_id).fields.at("data") == "parent_data",
"Record Override: Parent should retain its original value");
std::cout << "Test 'Overriding Parent Records in Child' passed." << std::endl;
}
// Test Case: Merging Changes from Child to Parent
{
// Create parent CRDT
CRDT<CrdtString, CrdtString> parent_crdt(1);
CrdtString record_id = generate_uuid();
CrdtVector<Change<CrdtString, CrdtString>> parent_changes;
parent_crdt.insert_or_update(record_id, parent_changes, std::make_pair("id", record_id),
std::make_pair("parent_field", "parent_value"));
// Create child CRDT with parent
auto parent_ptr = std::make_shared<CRDT<CrdtString, CrdtString>>(parent_crdt);
CRDT<CrdtString, CrdtString> child_crdt(2, parent_ptr);
// Child adds a new field
CrdtVector<Change<CrdtString, CrdtString>> child_changes;
child_crdt.insert_or_update(record_id, child_changes, std::make_pair("child_field", "child_value"));
// Merge child's changes to parent
parent_crdt.merge_changes(std::move(child_changes));
// Check that parent now has the child's field
assert_true(parent_crdt.get_data_combined().at(record_id).fields.at("child_field") == "child_value",
"Child to Parent Merge: Parent should have child's new field");
// Check that parent retains its original field
assert_true(parent_crdt.get_data_combined().at(record_id).fields.at("parent_field") == "parent_value",
"Child to Parent Merge: Parent should retain its original field");
std::cout << "Test 'Merging Changes from Child to Parent' passed." << std::endl;
}
// Test Case: Get Changes Since with Parent-Child Relationship
{
// Create parent CRDT
CRDT<CrdtString, CrdtString> parent_crdt(1);
CrdtString record_id = generate_uuid();
CrdtVector<Change<CrdtString, CrdtString>> parent_changes;
parent_crdt.insert_or_update(record_id, parent_changes, std::make_pair("id", record_id),
std::make_pair("parent_field", "parent_value"));
// Create child CRDT with parent
auto parent_ptr = std::make_shared<CRDT<CrdtString, CrdtString>>(parent_crdt);
CRDT<CrdtString, CrdtString> child_crdt(2, parent_ptr);
// Child adds a new field
CrdtVector<Change<CrdtString, CrdtString>> child_changes;
child_crdt.insert_or_update(record_id, child_changes, std::make_pair("child_field", "child_value"));
// Get changes since the beginning
auto changes = child_crdt.get_changes_since(0);
// Check that changes include both parent and child fields
bool has_parent_field = false;
bool has_child_field = false;
for (const auto &change : changes) {
if (change.col_name == "parent_field" && change.value == "parent_value") {
has_parent_field = true;
}
if (change.col_name == "child_field" && change.value == "child_value") {
has_child_field = true;
}
}
assert_true(has_parent_field, "Get Changes Since: Should include parent's field");
assert_true(has_child_field, "Get Changes Since: Should include child's field");
std::cout << "Test 'Get Changes Since with Parent-Child Relationship' passed." << std::endl;
}
// Test Case: Tombstone Propagation from Parent to Child
{
// Create parent CRDT
CRDT<CrdtString, CrdtString> parent_crdt(1);
CrdtString record_id = generate_uuid();
CrdtVector<Change<CrdtString, CrdtString>> parent_changes;
parent_crdt.insert_or_update(record_id, parent_changes, std::make_pair("id", record_id), std::make_pair("field", "value"));
// Create child CRDT with parent
auto parent_ptr = std::make_shared<CRDT<CrdtString, CrdtString>>(parent_crdt);
CRDT<CrdtString, CrdtString> child_crdt(2, parent_ptr);
// Child should inherit the record
assert_true(child_crdt.get_data_combined().find(record_id) != child_crdt.get_data_combined().end(),
"Tombstone Propagation: Child should inherit the record from parent");
// Parent deletes the record
CrdtVector<Change<CrdtString, CrdtString>> parent_delete_changes;
parent_crdt.delete_record(record_id, parent_delete_changes);
// Merge deletion into child
child_crdt.merge_changes(std::move(parent_delete_changes));
// Child should now have the record tombstoned
assert_true(child_crdt.get_data_combined().at(record_id).fields.empty(),
"Tombstone Propagation: Child should have empty fields after deletion");
assert_true(child_crdt.get_data_combined().at(record_id).column_versions.find("__deleted__") !=
child_crdt.get_data().at(record_id).column_versions.end(),
"Tombstone Propagation: Child should have '__deleted__' column version");
std::cout << "Test 'Tombstone Propagation from Parent to Child' passed." << std::endl;
}
// Test Case: Conflict Resolution with Parent and Child CRDTs
{
// Create parent CRDT
CRDT<CrdtString, CrdtString> parent_crdt(1);
CrdtString record_id = generate_uuid();
CrdtVector<Change<CrdtString, CrdtString>> parent_changes;
parent_crdt.insert_or_update(record_id, parent_changes, std::make_pair("id", record_id),
std::make_pair("field", "parent_value"));
// Create child CRDT with parent
auto parent_ptr = std::make_shared<CRDT<CrdtString, CrdtString>>(parent_crdt);
CRDT<CrdtString, CrdtString> child_crdt(2, parent_ptr);
// Both parent and child update the same field concurrently
CrdtVector<Change<CrdtString, CrdtString>> parent_change_update, child_change_update;
parent_crdt.insert_or_update(record_id, parent_change_update, std::make_pair("field", "parent_updated"));
child_crdt.insert_or_update(record_id, child_change_update, std::make_pair("field", "child_updated"));
// Merge child's changes into parent
parent_crdt.merge_changes(std::move(child_change_update));
// Merge parent's changes into child
child_crdt.merge_changes(std::move(parent_change_update));
// Conflict resolution should prefer the change with the higher db_version or higher node_id
// Assuming parent and child have different db_versions, the resolution will follow the rules
// Let's verify which update prevailed
// Fetch the final value from both parent and child
std::string parent_final = parent_crdt.get_data().at(record_id).fields.at("field");
std::string child_final = child_crdt.get_data().at(record_id).fields.at("field");
// Both should be the same
assert_true(parent_final == child_final, "Conflict Resolution with Parent and Child: Data mismatch between parent and child");
// Depending on the db_version and node_id, determine the expected value
// Since child has a higher node_id, if db_versions are equal, child's update should prevail
// Otherwise, the higher db_version determines the winner
// For simplicity, let's assume child had a higher db_version
// Thus, expected value should be "child_updated"
std::string expected = "child_updated";
assert_true(parent_final == expected, "Conflict Resolution with Parent and Child: Expected 'child_updated'");
std::cout << "Test 'Conflict Resolution with Parent and Child CRDTs' passed." << std::endl;
}
// Test Case: Hierarchical Change Retrieval
{
// Create parent CRDT
CRDT<CrdtString, CrdtString> parent_crdt(1);
CrdtString record_id_parent = generate_uuid();
CrdtVector<Change<CrdtString, CrdtString>> parent_changes;
parent_crdt.insert_or_update(record_id_parent, parent_changes, std::make_pair("id", record_id_parent),
std::make_pair("parent_field", "parent_value"));
// Create child CRDT with parent
auto parent_ptr = std::make_shared<CRDT<CrdtString, CrdtString>>(parent_crdt);
CRDT<CrdtString, CrdtString> child_crdt(2, parent_ptr);
// Child adds its own record
CrdtString record_id_child = generate_uuid();
CrdtVector<Change<CrdtString, CrdtString>> child_changes;
child_crdt.insert_or_update(record_id_child, child_changes, std::make_pair("id", record_id_child),
std::make_pair("child_field", "child_value"));
// Retrieve changes since db_version 0 from child
CrdtVector<Change<CrdtString, CrdtString>> retrieved_changes = child_crdt.get_changes_since(0);
// Should include both parent and child changes
assert_true(retrieved_changes.size() == 4, "Hierarchical Change Retrieval: Should retrieve four changes");
// Verify that both changes are present
bool parent_change_found = false;
bool child_change_found = false;
for (const auto &change : retrieved_changes) {
if (change.record_id == record_id_parent && change.col_name.has_value() && change.col_name.value() == "parent_field" &&
change.value.has_value() && change.value.value() == "parent_value") {
parent_change_found = true;
}
if (change.record_id == record_id_child && change.col_name.has_value() && change.col_name.value() == "child_field" &&
change.value.has_value() && change.value.value() == "child_value") {
child_change_found = true;
}
}
assert_true(parent_change_found, "Hierarchical Change Retrieval: Parent change not found");
assert_true(child_change_found, "Hierarchical Change Retrieval: Child change not found");
std::cout << "Test 'Hierarchical Change Retrieval' passed." << std::endl;
}
// Test Case: Avoiding Duplicate Change Application via Parent
{
// Create parent CRDT
CRDT<CrdtString, CrdtString> parent_crdt(1);
CrdtString record_id = generate_uuid();
CrdtVector<Change<CrdtString, CrdtString>> parent_changes;
parent_crdt.insert_or_update(record_id, parent_changes, std::make_pair("id", record_id),
std::make_pair("field", "parent_value"));
// Create child CRDT with parent
auto parent_ptr = std::make_shared<CRDT<CrdtString, CrdtString>>(parent_crdt);
CRDT<CrdtString, CrdtString> child_crdt(2, parent_ptr);
// Parent inserts a new field
CrdtVector<Change<CrdtString, CrdtString>> parent_change_new_field;
parent_crdt.insert_or_update(record_id, parent_change_new_field, std::make_pair("new_field", "new_parent_value"));
// Merge parent's new field into child
child_crdt.merge_changes(std::move(parent_change_new_field));
// Attempt to re-merge the same change into child
child_crdt.merge_changes({Change<CrdtString, CrdtString>(record_id, "new_field", "new_parent_value", 2, 2, 1)});
// Verify that 'new_field' is correctly set without duplication
assert_true(child_crdt.get_data().at(record_id).fields.at("new_field") == "new_parent_value",
"Avoiding Duplicate Changes: 'new_field' value mismatch");
std::cout << "Test 'Avoiding Duplicate Change Application via Parent' passed." << std::endl;
}
// Test Case: Child Deletion Does Not Affect Parent
{
// Create parent CRDT
CRDT<CrdtString, CrdtString> parent_crdt(1);
CrdtString record_id = generate_uuid();
CrdtVector<Change<CrdtString, CrdtString>> parent_changes;
parent_crdt.insert_or_update(record_id, parent_changes, std::make_pair("id", record_id),
std::make_pair("field", "parent_value"));
// Create child CRDT with parent
auto parent_ptr = std::make_shared<CRDT<CrdtString, CrdtString>>(parent_crdt);
CRDT<CrdtString, CrdtString> child_crdt(2, parent_ptr);
// Child deletes the record
CrdtVector<Change<CrdtString, CrdtString>> child_delete_changes;
child_crdt.delete_record(record_id, child_delete_changes);
// Merge child's deletion into parent
parent_crdt.merge_changes(std::move(child_delete_changes));
// Parent should still have the record (since child deletion should not affect parent)
assert_true(parent_crdt.get_data().find(record_id) != parent_crdt.get_data().end(),
"Child Deletion: Parent should still have the record after child deletion");
// Child should have the record tombstoned
assert_true(child_crdt.get_data().at(record_id).fields.empty(),
"Child Deletion: Child should have empty fields after deletion");
assert_true(child_crdt.get_data().at(record_id).column_versions.find("__deleted__") !=
child_crdt.get_data().at(record_id).column_versions.end(),
"Child Deletion: Child should have '__deleted__' column version");
std::cout << "Test 'Child Deletion Does Not Affect Parent' passed." << std::endl;
}
// Test Case: Parent and Child Simultaneous Updates
{
// Create parent CRDT
CRDT<CrdtString, CrdtString> parent_crdt(1);
CrdtString record_id = generate_uuid();
CrdtVector<Change<CrdtString, CrdtString>> parent_changes;
parent_crdt.insert_or_update(record_id, parent_changes, std::make_pair("id", record_id), std::make_pair("field1", "value1"));
// Create child CRDT with parent
auto parent_ptr = std::make_shared<CRDT<CrdtString, CrdtString>>(parent_crdt);
CRDT<CrdtString, CrdtString> child_crdt(2, parent_ptr);
// Parent updates field1
CrdtVector<Change<CrdtString, CrdtString>> parent_change_update;
parent_crdt.insert_or_update(record_id, parent_change_update, std::make_pair("field1", "parent_updated"));
// Child updates field2
CrdtVector<Change<CrdtString, CrdtString>> child_change_update;
child_crdt.insert_or_update(record_id, child_change_update, std::make_pair("field2", "child_value2"));
// Merge changes
parent_crdt.merge_changes(std::move(child_change_update));
child_crdt.merge_changes(std::move(parent_change_update));
// Verify that both updates are present
assert_true(parent_crdt.get_data().at(record_id).fields.at("field1") == "parent_updated",
"Simultaneous Updates: Parent's field1 should be updated");
assert_true(parent_crdt.get_data().at(record_id).fields.at("field2") == "child_value2",
"Simultaneous Updates: Parent should have child's field2");
assert_true(child_crdt.get_data().at(record_id).fields.at("field1") == "parent_updated",
"Simultaneous Updates: Child's field1 should reflect parent's update");
assert_true(child_crdt.get_data().at(record_id).fields.at("field2") == "child_value2",
"Simultaneous Updates: Child's field2 should be updated");
std::cout << "Test 'Parent and Child Simultaneous Updates' passed." << std::endl;
}
// Test Case: Parent Deletion Prevents Child Insertions