This repository was archived by the owner on Aug 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathDiffMatchPatchTest.m
executable file
·1356 lines (1104 loc) · 72.8 KB
/
DiffMatchPatchTest.m
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
/*
* Diff Match and Patch -- Test harness
* Copyright 2018 The diff-match-patch Authors.
* https://github.com/google/diff-match-patch
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Author: [email protected] (Neil Fraser)
* ObjC port: [email protected] (Jan Weiß)
*/
#import "DiffMatchPatchTest.h"
#import "DiffMatchPatch.h"
#import "NSMutableDictionary+DMPExtensions.h"
#define stringForBOOL(A) ([((NSNumber *)A) boolValue] ? @"true" : @"false")
@interface DiffMatchPatchTest (PrivatMethods)
- (NSArray *)diff_rebuildtexts:(NSMutableArray *)diffs;
@end
@implementation DiffMatchPatchTest
- (void)test_diff_commonPrefixTest {
DiffMatchPatch *dmp = [DiffMatchPatch new];
// Detect any common suffix.
// Null case.
XCTAssertEqual((NSUInteger)0, [dmp diff_commonPrefixOfFirstString:@"abc" andSecondString:@"xyz"], @"Common suffix null case failed.");
// Non-null case.
XCTAssertEqual((NSUInteger)4, [dmp diff_commonPrefixOfFirstString:@"1234abcdef" andSecondString:@"1234xyz"], @"Common suffix non-null case failed.");
// Whole case.
XCTAssertEqual((NSUInteger)4, [dmp diff_commonPrefixOfFirstString:@"1234" andSecondString:@"1234xyz"], @"Common suffix whole case failed.");
[dmp release];
}
- (void)test_diff_commonSuffixTest {
DiffMatchPatch *dmp = [DiffMatchPatch new];
// Detect any common suffix.
// Null case.
XCTAssertEqual((NSUInteger)0, [dmp diff_commonSuffixOfFirstString:@"abc" andSecondString:@"xyz"], @"Detect any common suffix. Null case.");
// Non-null case.
XCTAssertEqual((NSUInteger)4, [dmp diff_commonSuffixOfFirstString:@"abcdef1234" andSecondString:@"xyz1234"], @"Detect any common suffix. Non-null case.");
// Whole case.
XCTAssertEqual((NSUInteger)4, [dmp diff_commonSuffixOfFirstString:@"1234" andSecondString:@"xyz1234"], @"Detect any common suffix. Whole case.");
[dmp release];
}
- (void)test_diff_commonOverlapTest {
DiffMatchPatch *dmp = [DiffMatchPatch new];
// Detect any suffix/prefix overlap.
// Null case.
XCTAssertEqual((NSUInteger)0, [dmp diff_commonOverlapOfFirstString:@"" andSecondString:@"abcd"], @"Detect any suffix/prefix overlap. Null case.");
// Whole case.
XCTAssertEqual((NSUInteger)3, [dmp diff_commonOverlapOfFirstString:@"abc" andSecondString:@"abcd"], @"Detect any suffix/prefix overlap. Whole case.");
// No overlap.
XCTAssertEqual((NSUInteger)0, [dmp diff_commonOverlapOfFirstString:@"123456" andSecondString:@"abcd"], @"Detect any suffix/prefix overlap. No overlap.");
// Overlap.
XCTAssertEqual((NSUInteger)3, [dmp diff_commonOverlapOfFirstString:@"123456xxx" andSecondString:@"xxxabcd"], @"Detect any suffix/prefix overlap. Overlap.");
// Unicode.
// Some overly clever languages (C#) may treat ligatures as equal to their
// component letters. E.g. U+FB01 == 'fi'
XCTAssertEqual((NSUInteger)0, [dmp diff_commonOverlapOfFirstString:@"fi" andSecondString:@"\U0000fb01i"], @"Detect any suffix/prefix overlap. Unicode.");
[dmp release];
}
- (void)test_diff_halfmatchTest {
DiffMatchPatch *dmp = [DiffMatchPatch new];
dmp.Diff_Timeout = 1;
NSArray *expectedResult = nil;
// No match.
XCTAssertNil([dmp diff_halfMatchOfFirstString:@"1234567890" andSecondString:@"abcdef"], @"No match #1.");
XCTAssertNil([dmp diff_halfMatchOfFirstString:@"12345" andSecondString:@"23"], @"No match #2.");
// Single Match.
expectedResult = [NSArray arrayWithObjects:@"12", @"90", @"a", @"z", @"345678", nil];
XCTAssertEqualObjects(expectedResult, [dmp diff_halfMatchOfFirstString:@"1234567890" andSecondString:@"a345678z"], @"Single Match #1.");
expectedResult = [NSArray arrayWithObjects:@"a", @"z", @"12", @"90", @"345678", nil];
XCTAssertEqualObjects(expectedResult, [dmp diff_halfMatchOfFirstString:@"a345678z" andSecondString:@"1234567890"], @"Single Match #2.");
expectedResult = [NSArray arrayWithObjects:@"abc", @"z", @"1234", @"0", @"56789", nil];
XCTAssertEqualObjects(expectedResult, [dmp diff_halfMatchOfFirstString:@"abc56789z" andSecondString:@"1234567890"], @"Single Match #3.");
expectedResult = [NSArray arrayWithObjects:@"a", @"xyz", @"1", @"7890", @"23456", nil];
XCTAssertEqualObjects(expectedResult, [dmp diff_halfMatchOfFirstString:@"a23456xyz" andSecondString:@"1234567890"], @"Single Match #4.");
// Multiple Matches.
expectedResult = [NSArray arrayWithObjects:@"12123", @"123121", @"a", @"z", @"1234123451234", nil];
XCTAssertEqualObjects(expectedResult, [dmp diff_halfMatchOfFirstString:@"121231234123451234123121" andSecondString:@"a1234123451234z"], @"Multiple Matches #1.");
expectedResult = [NSArray arrayWithObjects:@"", @"-=-=-=-=-=", @"x", @"", @"x-=-=-=-=-=-=-=", nil];
XCTAssertEqualObjects(expectedResult, [dmp diff_halfMatchOfFirstString:@"x-=-=-=-=-=-=-=-=-=-=-=-=" andSecondString:@"xx-=-=-=-=-=-=-="], @"Multiple Matches #2.");
expectedResult = [NSArray arrayWithObjects:@"-=-=-=-=-=", @"", @"", @"y", @"-=-=-=-=-=-=-=y", nil];
XCTAssertEqualObjects(expectedResult, [dmp diff_halfMatchOfFirstString:@"-=-=-=-=-=-=-=-=-=-=-=-=y" andSecondString:@"-=-=-=-=-=-=-=yy"], @"Multiple Matches #3.");
// Non-optimal halfmatch.
// Optimal diff would be -q+x=H-i+e=lloHe+Hu=llo-Hew+y not -qHillo+x=HelloHe-w+Hulloy
expectedResult = [NSArray arrayWithObjects:@"qHillo", @"w", @"x", @"Hulloy", @"HelloHe", nil];
XCTAssertEqualObjects(expectedResult, [dmp diff_halfMatchOfFirstString:@"qHilloHelloHew" andSecondString:@"xHelloHeHulloy"], @"Non-optimal halfmatch.");
// Optimal no halfmatch.
dmp.Diff_Timeout = 0;
XCTAssertNil([dmp diff_halfMatchOfFirstString:@"qHilloHelloHew" andSecondString:@"xHelloHeHulloy"], @"Optimal no halfmatch.");
[dmp release];
}
- (void)test_diff_linesToCharsTest {
DiffMatchPatch *dmp = [DiffMatchPatch new];
NSArray *result;
// Convert lines down to characters.
NSMutableArray *tmpVector = [NSMutableArray array]; // Array of NSString objects.
[tmpVector addObject:@""];
[tmpVector addObject:@"alpha\n"];
[tmpVector addObject:@"beta\n"];
result = [dmp diff_linesToCharsForFirstString:@"alpha\nbeta\nalpha\n" andSecondString:@"beta\nalpha\nbeta\n"];
XCTAssertEqualObjects(@"\001\002\001", [result objectAtIndex:0], @"Shared lines #1.");
XCTAssertEqualObjects(@"\002\001\002", [result objectAtIndex:1], @"Shared lines #2.");
XCTAssertEqualObjects(tmpVector, (NSArray *)[result objectAtIndex:2], @"Shared lines #3.");
[tmpVector removeAllObjects];
[tmpVector addObject:@""];
[tmpVector addObject:@"alpha\r\n"];
[tmpVector addObject:@"beta\r\n"];
[tmpVector addObject:@"\r\n"];
result = [dmp diff_linesToCharsForFirstString:@"" andSecondString:@"alpha\r\nbeta\r\n\r\n\r\n"];
XCTAssertEqualObjects(@"", [result objectAtIndex:0], @"Empty string and blank lines #1.");
XCTAssertEqualObjects(@"\001\002\003\003", [result objectAtIndex:1], @"Empty string and blank lines #2.");
XCTAssertEqualObjects(tmpVector, (NSArray *)[result objectAtIndex:2], @"Empty string and blank lines #3.");
[tmpVector removeAllObjects];
[tmpVector addObject:@""];
[tmpVector addObject:@"a"];
[tmpVector addObject:@"b"];
result = [dmp diff_linesToCharsForFirstString:@"a" andSecondString:@"b"];
XCTAssertEqualObjects(@"\001", [result objectAtIndex:0], @"No linebreaks #1.");
XCTAssertEqualObjects(@"\002", [result objectAtIndex:1], @"No linebreaks #2.");
XCTAssertEqualObjects(tmpVector, (NSArray *)[result objectAtIndex:2], @"No linebreaks #3.");
// More than 256 to reveal any 8-bit limitations.
unichar n = 300;
[tmpVector removeAllObjects];
NSMutableString *lines = [NSMutableString string];
NSMutableString *chars = [NSMutableString string];
NSString *currentLine;
for (unichar i = 1; i < n + 1; i++) {
currentLine = [NSString stringWithFormat:@"%d\n", (int)i];
[tmpVector addObject:currentLine];
[lines appendString:currentLine];
[chars appendString:[NSString stringWithFormat:@"%C", i]];
}
XCTAssertEqual((NSUInteger)n, tmpVector.count, @"More than 256 #1.");
XCTAssertEqual((NSUInteger)n, chars.length, @"More than 256 #2.");
[tmpVector insertObject:@"" atIndex:0];
result = [dmp diff_linesToCharsForFirstString:lines andSecondString:@""];
XCTAssertEqualObjects(chars, [result objectAtIndex:0], @"More than 256 #3.");
XCTAssertEqualObjects(@"", [result objectAtIndex:1], @"More than 256 #4.");
XCTAssertEqualObjects(tmpVector, (NSArray *)[result objectAtIndex:2], @"More than 256 #5.");
[dmp release];
}
- (void)test_diff_charsToLinesTest {
DiffMatchPatch *dmp = [DiffMatchPatch new];
// Convert chars up to lines.
NSArray *diffs = [NSArray arrayWithObjects:
[Diff diffWithOperation:DIFF_EQUAL andText:@"\001\002\001"],
[Diff diffWithOperation:DIFF_INSERT andText:@"\002\001\002"], nil];
NSMutableArray *tmpVector = [NSMutableArray array]; // Array of NSString objects.
[tmpVector addObject:@""];
[tmpVector addObject:@"alpha\n"];
[tmpVector addObject:@"beta\n"];
[dmp diff_chars:diffs toLines:tmpVector];
NSArray *expectedResult = [NSArray arrayWithObjects:
[Diff diffWithOperation:DIFF_EQUAL andText:@"alpha\nbeta\nalpha\n"],
[Diff diffWithOperation:DIFF_INSERT andText:@"beta\nalpha\nbeta\n"], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"Shared lines.");
// More than 256 to reveal any 8-bit limitations.
unichar n = 300;
[tmpVector removeAllObjects];
NSMutableString *lines = [NSMutableString string];
NSMutableString *chars = [NSMutableString string];
NSString *currentLine;
for (unichar i = 1; i < n + 1; i++) {
currentLine = [NSString stringWithFormat:@"%d\n", (int)i];
[tmpVector addObject:currentLine];
[lines appendString:currentLine];
[chars appendString:[NSString stringWithFormat:@"%C", i]];
}
XCTAssertEqual((NSUInteger)n, tmpVector.count, @"More than 256 #1.");
XCTAssertEqual((NSUInteger)n, chars.length, @"More than 256 #2.");
[tmpVector insertObject:@"" atIndex:0];
diffs = [NSArray arrayWithObject:[Diff diffWithOperation:DIFF_DELETE andText:chars]];
[dmp diff_chars:diffs toLines:tmpVector];
XCTAssertEqualObjects([NSArray arrayWithObject:[Diff diffWithOperation:DIFF_DELETE andText:lines]], diffs, @"More than 256 #3.");
// More than 65536 to verify any 16-bit limitation.
lines = [NSMutableString string];
for (int i = 1; i < 66000; i++) {
currentLine = [NSString stringWithFormat:@"%d\n", i];
[lines appendString:currentLine];
}
NSArray *result;
result = [dmp diff_linesToCharsForFirstString:lines andSecondString:@""];
diffs = [NSArray arrayWithObject:[Diff diffWithOperation:DIFF_INSERT andText:result[0]]];
[dmp diff_chars:diffs toLines:result[2]];
Diff *myDiff = diffs.firstObject;
XCTAssertEqualObjects(lines, myDiff.text, @"More than 65536.");
[dmp release];
}
- (void)test_diff_cleanupMergeTest {
DiffMatchPatch *dmp = [DiffMatchPatch new];
NSMutableArray *expectedResult = nil;
// Cleanup a messy diff.
// Null case.
NSMutableArray *diffs = [NSMutableArray array];
[dmp diff_cleanupMerge:diffs];
XCTAssertEqualObjects([NSMutableArray array], diffs, @"Null case.");
// No change case.
diffs = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_EQUAL andText:@"a"], [Diff diffWithOperation:DIFF_DELETE andText:@"b"], [Diff diffWithOperation:DIFF_INSERT andText:@"c"], nil];
[dmp diff_cleanupMerge:diffs];
expectedResult = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_EQUAL andText:@"a"], [Diff diffWithOperation:DIFF_DELETE andText:@"b"], [Diff diffWithOperation:DIFF_INSERT andText:@"c"], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"No change case.");
// Merge equalities.
diffs = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_EQUAL andText:@"a"], [Diff diffWithOperation:DIFF_EQUAL andText:@"b"], [Diff diffWithOperation:DIFF_EQUAL andText:@"c"], nil];
[dmp diff_cleanupMerge:diffs];
expectedResult = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_EQUAL andText:@"abc"], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"Merge equalities.");
// Merge deletions.
diffs = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_DELETE andText:@"a"], [Diff diffWithOperation:DIFF_DELETE andText:@"b"], [Diff diffWithOperation:DIFF_DELETE andText:@"c"], nil];
[dmp diff_cleanupMerge:diffs];
expectedResult = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_DELETE andText:@"abc"], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"Merge deletions.");
// Merge insertions.
diffs = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_INSERT andText:@"a"], [Diff diffWithOperation:DIFF_INSERT andText:@"b"], [Diff diffWithOperation:DIFF_INSERT andText:@"c"], nil];
[dmp diff_cleanupMerge:diffs];
expectedResult = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_INSERT andText:@"abc"], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"Merge insertions.");
// Merge interweave.
diffs = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_DELETE andText:@"a"], [Diff diffWithOperation:DIFF_INSERT andText:@"b"], [Diff diffWithOperation:DIFF_DELETE andText:@"c"], [Diff diffWithOperation:DIFF_INSERT andText:@"d"], [Diff diffWithOperation:DIFF_EQUAL andText:@"e"], [Diff diffWithOperation:DIFF_EQUAL andText:@"f"], nil];
[dmp diff_cleanupMerge:diffs];
expectedResult = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_DELETE andText:@"ac"], [Diff diffWithOperation:DIFF_INSERT andText:@"bd"], [Diff diffWithOperation:DIFF_EQUAL andText:@"ef"], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"Merge interweave.");
// Prefix and suffix detection.
diffs = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_DELETE andText:@"a"], [Diff diffWithOperation:DIFF_INSERT andText:@"abc"], [Diff diffWithOperation:DIFF_DELETE andText:@"dc"], nil];
[dmp diff_cleanupMerge:diffs];
expectedResult = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_EQUAL andText:@"a"], [Diff diffWithOperation:DIFF_DELETE andText:@"d"], [Diff diffWithOperation:DIFF_INSERT andText:@"b"], [Diff diffWithOperation:DIFF_EQUAL andText:@"c"], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"Prefix and suffix detection.");
// Prefix and suffix detection with equalities.
diffs = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_EQUAL andText:@"x"], [Diff diffWithOperation:DIFF_DELETE andText:@"a"], [Diff diffWithOperation:DIFF_INSERT andText:@"abc"], [Diff diffWithOperation:DIFF_DELETE andText:@"dc"], [Diff diffWithOperation:DIFF_EQUAL andText:@"y"], nil];
[dmp diff_cleanupMerge:diffs];
expectedResult = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_EQUAL andText:@"xa"], [Diff diffWithOperation:DIFF_DELETE andText:@"d"], [Diff diffWithOperation:DIFF_INSERT andText:@"b"], [Diff diffWithOperation:DIFF_EQUAL andText:@"cy"], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"Prefix and suffix detection with equalities.");
// Slide edit left.
diffs = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_EQUAL andText:@"a"], [Diff diffWithOperation:DIFF_INSERT andText:@"ba"], [Diff diffWithOperation:DIFF_EQUAL andText:@"c"], nil];
[dmp diff_cleanupMerge:diffs];
expectedResult = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_INSERT andText:@"ab"], [Diff diffWithOperation:DIFF_EQUAL andText:@"ac"], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"Slide edit left.");
// Slide edit right.
diffs = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_EQUAL andText:@"c"], [Diff diffWithOperation:DIFF_INSERT andText:@"ab"], [Diff diffWithOperation:DIFF_EQUAL andText:@"a"], nil];
[dmp diff_cleanupMerge:diffs];
expectedResult = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_EQUAL andText:@"ca"], [Diff diffWithOperation:DIFF_INSERT andText:@"ba"], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"Slide edit right.");
// Slide edit left recursive.
diffs = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_EQUAL andText:@"a"], [Diff diffWithOperation:DIFF_DELETE andText:@"b"], [Diff diffWithOperation:DIFF_EQUAL andText:@"c"], [Diff diffWithOperation:DIFF_DELETE andText:@"ac"], [Diff diffWithOperation:DIFF_EQUAL andText:@"x"], nil];
[dmp diff_cleanupMerge:diffs];
expectedResult = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_DELETE andText:@"abc"], [Diff diffWithOperation:DIFF_EQUAL andText:@"acx"], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"Slide edit left recursive.");
// Slide edit right recursive.
diffs = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_EQUAL andText:@"x"], [Diff diffWithOperation:DIFF_DELETE andText:@"ca"], [Diff diffWithOperation:DIFF_EQUAL andText:@"c"], [Diff diffWithOperation:DIFF_DELETE andText:@"b"], [Diff diffWithOperation:DIFF_EQUAL andText:@"a"], nil];
[dmp diff_cleanupMerge:diffs];
expectedResult = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_EQUAL andText:@"xca"], [Diff diffWithOperation:DIFF_DELETE andText:@"cba"], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"Slide edit right recursive.");
// Empty merge.
diffs = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_DELETE andText:@"b"], [Diff diffWithOperation:DIFF_INSERT andText:@"ab"], [Diff diffWithOperation:DIFF_EQUAL andText:@"c"], nil];
[dmp diff_cleanupMerge:diffs];
expectedResult = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_INSERT andText:@"a"], [Diff diffWithOperation:DIFF_EQUAL andText:@"bc"], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"Empty merge.");
// Empty equality.
diffs = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_EQUAL andText:@""], [Diff diffWithOperation:DIFF_INSERT andText:@"a"], [Diff diffWithOperation:DIFF_EQUAL andText:@"b"], nil];
[dmp diff_cleanupMerge:diffs];
expectedResult = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_INSERT andText:@"a"], [Diff diffWithOperation:DIFF_EQUAL andText:@"b"], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"Empty equality.");
[dmp release];
}
- (void)test_diff_cleanupSemanticLosslessTest {
DiffMatchPatch *dmp = [DiffMatchPatch new];
NSMutableArray *expectedResult = nil;
// Slide diffs to match logical boundaries.
// Null case.
NSMutableArray *diffs = [NSMutableArray array];
[dmp diff_cleanupSemanticLossless:diffs];
XCTAssertEqualObjects([NSMutableArray array], diffs, @"Null case.");
// Blank lines.
diffs = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_EQUAL andText:@"AAA\r\n\r\nBBB"],
[Diff diffWithOperation:DIFF_INSERT andText:@"\r\nDDD\r\n\r\nBBB"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"\r\nEEE"], nil];
[dmp diff_cleanupSemanticLossless:diffs];
expectedResult = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_EQUAL andText:@"AAA\r\n\r\n"],
[Diff diffWithOperation:DIFF_INSERT andText:@"BBB\r\nDDD\r\n\r\n"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"BBB\r\nEEE"], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"Blank lines.");
// Line boundaries.
diffs = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_EQUAL andText:@"AAA\r\nBBB"],
[Diff diffWithOperation:DIFF_INSERT andText:@" DDD\r\nBBB"],
[Diff diffWithOperation:DIFF_EQUAL andText:@" EEE"], nil];
[dmp diff_cleanupSemanticLossless:diffs];
expectedResult = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_EQUAL andText:@"AAA\r\n"],
[Diff diffWithOperation:DIFF_INSERT andText:@"BBB DDD\r\n"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"BBB EEE"], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"Line boundaries.");
// Word boundaries.
diffs = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_EQUAL andText:@"The c"],
[Diff diffWithOperation:DIFF_INSERT andText:@"ow and the c"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"at."], nil];
[dmp diff_cleanupSemanticLossless:diffs];
expectedResult = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_EQUAL andText:@"The "],
[Diff diffWithOperation:DIFF_INSERT andText:@"cow and the "],
[Diff diffWithOperation:DIFF_EQUAL andText:@"cat."], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"Word boundaries.");
// Alphanumeric boundaries.
diffs = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_EQUAL andText:@"The-c"],
[Diff diffWithOperation:DIFF_INSERT andText:@"ow-and-the-c"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"at."], nil];
[dmp diff_cleanupSemanticLossless:diffs];
expectedResult = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_EQUAL andText:@"The-"],
[Diff diffWithOperation:DIFF_INSERT andText:@"cow-and-the-"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"cat."], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"Alphanumeric boundaries.");
// Hitting the start.
diffs = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_EQUAL andText:@"a"],
[Diff diffWithOperation:DIFF_DELETE andText:@"a"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"ax"], nil];
[dmp diff_cleanupSemanticLossless:diffs];
expectedResult = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_DELETE andText:@"a"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"aax"], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"Hitting the start.");
// Hitting the end.
diffs = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_EQUAL andText:@"xa"],
[Diff diffWithOperation:DIFF_DELETE andText:@"a"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"a"], nil];
[dmp diff_cleanupSemanticLossless:diffs];
expectedResult = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_EQUAL andText:@"xaa"],
[Diff diffWithOperation:DIFF_DELETE andText:@"a"], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"Hitting the end.");
// Alphanumeric boundaries.
diffs = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_EQUAL andText:@"The xxx. The "],
[Diff diffWithOperation:DIFF_INSERT andText:@"zzz. The "],
[Diff diffWithOperation:DIFF_EQUAL andText:@"yyy."], nil];
[dmp diff_cleanupSemanticLossless:diffs];
expectedResult = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_EQUAL andText:@"The xxx."],
[Diff diffWithOperation:DIFF_INSERT andText:@" The zzz."],
[Diff diffWithOperation:DIFF_EQUAL andText:@" The yyy."], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"Sentence boundaries.");
[dmp release];
}
- (void)test_diff_cleanupSemanticTest {
DiffMatchPatch *dmp = [DiffMatchPatch new];
NSMutableArray *expectedResult = nil;
// Cleanup semantically trivial equalities.
// Null case.
NSMutableArray *diffs = [NSMutableArray array];
[dmp diff_cleanupSemantic:diffs];
XCTAssertEqualObjects([NSMutableArray array], diffs, @"Null case.");
// No elimination #1.
diffs = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_DELETE andText:@"ab"],
[Diff diffWithOperation:DIFF_INSERT andText:@"cd"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"12"],
[Diff diffWithOperation:DIFF_DELETE andText:@"e"], nil];
[dmp diff_cleanupSemantic:diffs];
expectedResult = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_DELETE andText:@"ab"],
[Diff diffWithOperation:DIFF_INSERT andText:@"cd"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"12"],
[Diff diffWithOperation:DIFF_DELETE andText:@"e"], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"No elimination #1.");
// No elimination #2.
diffs = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_DELETE andText:@"abc"],
[Diff diffWithOperation:DIFF_INSERT andText:@"ABC"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"1234"],
[Diff diffWithOperation:DIFF_DELETE andText:@"wxyz"], nil];
[dmp diff_cleanupSemantic:diffs];
expectedResult = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_DELETE andText:@"abc"],
[Diff diffWithOperation:DIFF_INSERT andText:@"ABC"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"1234"],
[Diff diffWithOperation:DIFF_DELETE andText:@"wxyz"], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"No elimination #2.");
// Simple elimination.
diffs = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_DELETE andText:@"a"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"b"],
[Diff diffWithOperation:DIFF_DELETE andText:@"c"], nil];
[dmp diff_cleanupSemantic:diffs];
expectedResult = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_DELETE andText:@"abc"],
[Diff diffWithOperation:DIFF_INSERT andText:@"b"], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"Simple elimination.");
// Backpass elimination.
diffs = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_DELETE andText:@"ab"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"cd"],
[Diff diffWithOperation:DIFF_DELETE andText:@"e"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"f"],
[Diff diffWithOperation:DIFF_INSERT andText:@"g"], nil];
[dmp diff_cleanupSemantic:diffs];
expectedResult = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_DELETE andText:@"abcdef"],
[Diff diffWithOperation:DIFF_INSERT andText:@"cdfg"], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"Backpass elimination.");
// Multiple eliminations.
diffs = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_INSERT andText:@"1"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"A"],
[Diff diffWithOperation:DIFF_DELETE andText:@"B"],
[Diff diffWithOperation:DIFF_INSERT andText:@"2"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"_"],
[Diff diffWithOperation:DIFF_INSERT andText:@"1"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"A"],
[Diff diffWithOperation:DIFF_DELETE andText:@"B"],
[Diff diffWithOperation:DIFF_INSERT andText:@"2"], nil];
[dmp diff_cleanupSemantic:diffs];
expectedResult = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_DELETE andText:@"AB_AB"],
[Diff diffWithOperation:DIFF_INSERT andText:@"1A2_1A2"], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"Multiple eliminations.");
// Word boundaries.
diffs = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_EQUAL andText:@"The c"],
[Diff diffWithOperation:DIFF_DELETE andText:@"ow and the c"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"at."], nil];
[dmp diff_cleanupSemantic:diffs];
expectedResult = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_EQUAL andText:@"The "],
[Diff diffWithOperation:DIFF_DELETE andText:@"cow and the "],
[Diff diffWithOperation:DIFF_EQUAL andText:@"cat."], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"Word boundaries.");
// No overlap elimination.
diffs = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_DELETE andText:@"abcxx"],
[Diff diffWithOperation:DIFF_INSERT andText:@"xxdef"], nil];
[dmp diff_cleanupSemantic:diffs];
expectedResult = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_DELETE andText:@"abcxx"],
[Diff diffWithOperation:DIFF_INSERT andText:@"xxdef"], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"No overlap elimination.");
// Overlap elimination.
diffs = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_DELETE andText:@"abcxxx"],
[Diff diffWithOperation:DIFF_INSERT andText:@"xxxdef"], nil];
[dmp diff_cleanupSemantic:diffs];
expectedResult = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_DELETE andText:@"abc"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"xxx"],
[Diff diffWithOperation:DIFF_INSERT andText:@"def"], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"Overlap elimination.");
// Reverse overlap elimination.
diffs = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_DELETE andText:@"xxxabc"],
[Diff diffWithOperation:DIFF_INSERT andText:@"defxxx"], nil];
[dmp diff_cleanupSemantic:diffs];
expectedResult = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_INSERT andText:@"def"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"xxx"],
[Diff diffWithOperation:DIFF_DELETE andText:@"abc"], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"Reverse overlap elimination.");
// Two overlap eliminations.
diffs = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_DELETE andText:@"abcd1212"],
[Diff diffWithOperation:DIFF_INSERT andText:@"1212efghi"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"----"],
[Diff diffWithOperation:DIFF_DELETE andText:@"A3"],
[Diff diffWithOperation:DIFF_INSERT andText:@"3BC"], nil];
[dmp diff_cleanupSemantic:diffs];
expectedResult = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_DELETE andText:@"abcd"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"1212"],
[Diff diffWithOperation:DIFF_INSERT andText:@"efghi"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"----"],
[Diff diffWithOperation:DIFF_DELETE andText:@"A"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"3"],
[Diff diffWithOperation:DIFF_INSERT andText:@"BC"], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"Two overlap eliminations.");
[dmp release];
}
- (void)test_diff_cleanupEfficiencyTest {
DiffMatchPatch *dmp = [DiffMatchPatch new];
NSMutableArray *expectedResult = nil;
// Cleanup operationally trivial equalities.
dmp.Diff_EditCost = 4;
// Null case.
NSMutableArray *diffs = [NSMutableArray array];
[dmp diff_cleanupEfficiency:diffs];
XCTAssertEqualObjects([NSMutableArray array], diffs, @"Null case.");
// No elimination.
diffs = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_DELETE andText:@"ab"],
[Diff diffWithOperation:DIFF_INSERT andText:@"12"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"wxyz"],
[Diff diffWithOperation:DIFF_DELETE andText:@"cd"],
[Diff diffWithOperation:DIFF_INSERT andText:@"34"], nil];
[dmp diff_cleanupEfficiency:diffs];
expectedResult = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_DELETE andText:@"ab"],
[Diff diffWithOperation:DIFF_INSERT andText:@"12"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"wxyz"],
[Diff diffWithOperation:DIFF_DELETE andText:@"cd"],
[Diff diffWithOperation:DIFF_INSERT andText:@"34"], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"No elimination.");
// Four-edit elimination.
diffs = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_DELETE andText:@"ab"],
[Diff diffWithOperation:DIFF_INSERT andText:@"12"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"xyz"],
[Diff diffWithOperation:DIFF_DELETE andText:@"cd"],
[Diff diffWithOperation:DIFF_INSERT andText:@"34"], nil];
[dmp diff_cleanupEfficiency:diffs];
expectedResult = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_DELETE andText:@"abxyzcd"],
[Diff diffWithOperation:DIFF_INSERT andText:@"12xyz34"], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"Four-edit elimination.");
// Three-edit elimination.
diffs = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_INSERT andText:@"12"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"x"],
[Diff diffWithOperation:DIFF_DELETE andText:@"cd"],
[Diff diffWithOperation:DIFF_INSERT andText:@"34"], nil];
[dmp diff_cleanupEfficiency:diffs];
expectedResult = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_DELETE andText:@"xcd"],
[Diff diffWithOperation:DIFF_INSERT andText:@"12x34"], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"Three-edit elimination.");
// Backpass elimination.
diffs = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_DELETE andText:@"ab"],
[Diff diffWithOperation:DIFF_INSERT andText:@"12"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"xy"],
[Diff diffWithOperation:DIFF_INSERT andText:@"34"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"z"],
[Diff diffWithOperation:DIFF_DELETE andText:@"cd"],
[Diff diffWithOperation:DIFF_INSERT andText:@"56"], nil];
[dmp diff_cleanupEfficiency:diffs];
expectedResult = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_DELETE andText:@"abxyzcd"],
[Diff diffWithOperation:DIFF_INSERT andText:@"12xy34z56"], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"Backpass elimination.");
// High cost elimination.
dmp.Diff_EditCost = 5;
diffs = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_DELETE andText:@"ab"],
[Diff diffWithOperation:DIFF_INSERT andText:@"12"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"wxyz"],
[Diff diffWithOperation:DIFF_DELETE andText:@"cd"],
[Diff diffWithOperation:DIFF_INSERT andText:@"34"], nil];
[dmp diff_cleanupEfficiency:diffs];
expectedResult = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_DELETE andText:@"abwxyzcd"],
[Diff diffWithOperation:DIFF_INSERT andText:@"12wxyz34"], nil];
XCTAssertEqualObjects(expectedResult, diffs, @"High cost elimination.");
dmp.Diff_EditCost = 4;
[dmp release];
}
- (void)test_diff_prettyHtmlTest {
DiffMatchPatch *dmp = [DiffMatchPatch new];
// Pretty print.
NSMutableArray *diffs = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_EQUAL andText:@"a\n"],
[Diff diffWithOperation:DIFF_DELETE andText:@"<B>b</B>"],
[Diff diffWithOperation:DIFF_INSERT andText:@"c&d"], nil];
NSString *expectedResult = @"<span>a¶<br></span><del style=\"background:#ffe6e6;\"><B>b</B></del><ins style=\"background:#e6ffe6;\">c&d</ins>";
XCTAssertEqualObjects(expectedResult, [dmp diff_prettyHtml:diffs], @"Pretty print.");
[dmp release];
}
- (void)test_diff_textTest {
DiffMatchPatch *dmp = [DiffMatchPatch new];
// Compute the source and destination texts.
NSMutableArray *diffs = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_EQUAL andText:@"jump"],
[Diff diffWithOperation:DIFF_DELETE andText:@"s"],
[Diff diffWithOperation:DIFF_INSERT andText:@"ed"],
[Diff diffWithOperation:DIFF_EQUAL andText:@" over "],
[Diff diffWithOperation:DIFF_DELETE andText:@"the"],
[Diff diffWithOperation:DIFF_INSERT andText:@"a"],
[Diff diffWithOperation:DIFF_EQUAL andText:@" lazy"], nil];
XCTAssertEqualObjects(@"jumps over the lazy", [dmp diff_text1:diffs], @"Compute the source and destination texts #1");
XCTAssertEqualObjects(@"jumped over a lazy", [dmp diff_text2:diffs], @"Compute the source and destination texts #2");
[dmp release];
}
- (void)test_diff_deltaTest {
DiffMatchPatch *dmp = [DiffMatchPatch new];
NSMutableArray *expectedResult = nil;
NSError *error = nil;
// Convert a diff into delta string.
NSMutableArray *diffs = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_EQUAL andText:@"jump"],
[Diff diffWithOperation:DIFF_DELETE andText:@"s"],
[Diff diffWithOperation:DIFF_INSERT andText:@"ed"],
[Diff diffWithOperation:DIFF_EQUAL andText:@" over "],
[Diff diffWithOperation:DIFF_DELETE andText:@"the"],
[Diff diffWithOperation:DIFF_INSERT andText:@"a"],
[Diff diffWithOperation:DIFF_EQUAL andText:@" lazy"],
[Diff diffWithOperation:DIFF_INSERT andText:@"old dog"], nil];
NSString *text1 = [dmp diff_text1:diffs];
XCTAssertEqualObjects(@"jumps over the lazy", text1, @"Convert a diff into delta string 1.");
NSString *delta = [dmp diff_toDelta:diffs];
XCTAssertEqualObjects(@"=4\t-1\t+ed\t=6\t-3\t+a\t=5\t+old dog", delta, @"Convert a diff into delta string 2.");
// Convert delta string into a diff.
XCTAssertEqualObjects(diffs, [dmp diff_fromDeltaWithText:text1 andDelta:delta error:NULL], @"Convert delta string into a diff.");
// Generates error (19 < 20).
diffs = [dmp diff_fromDeltaWithText:[text1 stringByAppendingString:@"x"] andDelta:delta error:&error];
if (diffs != nil || error == nil) {
XCTFail(@"diff_fromDelta: Too long.");
}
error = nil;
// Generates error (19 > 18).
diffs = [dmp diff_fromDeltaWithText:[text1 substringFromIndex:1] andDelta:delta error:&error];
if (diffs != nil || error == nil) {
XCTFail(@"diff_fromDelta: Too short.");
}
error = nil;
// Generates error (%c3%xy invalid Unicode).
diffs = [dmp diff_fromDeltaWithText:@"" andDelta:@"+%c3%xy" error:&error];
if (diffs != nil || error == nil) {
XCTFail(@"diff_fromDelta: Invalid character.");
}
error = nil;
// Test deltas with special characters.
unichar zero = (unichar)0;
unichar one = (unichar)1;
unichar two = (unichar)2;
diffs = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_EQUAL andText:[NSString stringWithFormat:@"\U00000680 %C \t %%", zero]],
[Diff diffWithOperation:DIFF_DELETE andText:[NSString stringWithFormat:@"\U00000681 %C \n ^", one]],
[Diff diffWithOperation:DIFF_INSERT andText:[NSString stringWithFormat:@"\U00000682 %C \\ |", two]], nil];
text1 = [dmp diff_text1:diffs];
NSString *expectedString = [NSString stringWithFormat:@"\U00000680 %C \t %%\U00000681 %C \n ^", zero, one];
XCTAssertEqualObjects(expectedString, text1, @"Test deltas with special characters.");
delta = [dmp diff_toDelta:diffs];
// Upper case, because to CFURLCreateStringByAddingPercentEscapes() uses upper.
XCTAssertEqualObjects(@"=7\t-7\t+%DA%82 %02 %5C %7C", delta, @"diff_toDelta: Unicode 1.");
XCTAssertEqualObjects(diffs, [dmp diff_fromDeltaWithText:text1 andDelta:delta error:NULL], @"diff_fromDelta: Unicode 2.");
// Verify pool of unchanged characters.
diffs = [NSMutableArray arrayWithObject:
[Diff diffWithOperation:DIFF_INSERT andText:@"A-Z a-z 0-9 - _ . ! ~ * ' ( ) ; / ? : @ & = + $ , # "]];
NSString *text2 = [dmp diff_text2:diffs];
XCTAssertEqualObjects(@"A-Z a-z 0-9 - _ . ! ~ * ' ( ) ; / ? : @ & = + $ , # ", text2, @"diff_text2: Unchanged characters 1.");
delta = [dmp diff_toDelta:diffs];
XCTAssertEqualObjects(@"+A-Z a-z 0-9 - _ . ! ~ * ' ( ) ; / ? : @ & = + $ , # ", delta, @"diff_toDelta: Unchanged characters 2.");
// Convert delta string into a diff.
expectedResult = [dmp diff_fromDeltaWithText:@"" andDelta:delta error:NULL];
XCTAssertEqualObjects(diffs, expectedResult, @"diff_fromDelta: Unchanged characters. Convert delta string into a diff.");
// 160 kb string.
NSString *a = @"abcdefghij";
NSMutableString *aMutable = [NSMutableString stringWithString:a];
for (int i = 0; i < 14; i++) {
[aMutable appendString:aMutable];
}
a = aMutable;
diffs = [NSMutableArray arrayWithObject:
[Diff diffWithOperation:DIFF_INSERT andText:a]];
delta = [dmp diff_toDelta:diffs];
XCTAssertEqualObjects([@"+" stringByAppendingString:a], delta, @"diff_toDelta: 160kb string.");
// Convert delta string into a diff.
expectedResult = [dmp diff_fromDeltaWithText:@"" andDelta:delta error:NULL];
XCTAssertEqualObjects(diffs, expectedResult, @"diff_fromDelta: 160kb string. Convert delta string into a diff.");
[dmp release];
}
- (void)test_diff_xIndexTest {
DiffMatchPatch *dmp = [DiffMatchPatch new];
// Translate a location in text1 to text2.
NSMutableArray *diffs = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_DELETE andText:@"a"],
[Diff diffWithOperation:DIFF_INSERT andText:@"1234"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"xyz"], nil] /* Diff */;
XCTAssertEqual((NSUInteger)5, [dmp diff_xIndexIn:diffs location:2], @"diff_xIndex: Translation on equality. Translate a location in text1 to text2.");
diffs = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_EQUAL andText:@"a"],
[Diff diffWithOperation:DIFF_DELETE andText:@"1234"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"xyz"], nil] /* Diff */;
XCTAssertEqual((NSUInteger)1, [dmp diff_xIndexIn:diffs location:3], @"diff_xIndex: Translation on deletion.");
[dmp release];
}
- (void)test_diff_levenshteinTest {
DiffMatchPatch *dmp = [DiffMatchPatch new];
NSMutableArray *diffs = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_DELETE andText:@"abc"],
[Diff diffWithOperation:DIFF_INSERT andText:@"1234"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"xyz"], nil] /* Diff */;
XCTAssertEqual((NSUInteger)4, [dmp diff_levenshtein:diffs], @"diff_levenshtein: Levenshtein with trailing equality.");
diffs = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_EQUAL andText:@"xyz"],
[Diff diffWithOperation:DIFF_DELETE andText:@"abc"],
[Diff diffWithOperation:DIFF_INSERT andText:@"1234"], nil] /* Diff */;
XCTAssertEqual((NSUInteger)4, [dmp diff_levenshtein:diffs], @"diff_levenshtein: Levenshtein with leading equality.");
diffs = [NSMutableArray arrayWithObjects:
[Diff diffWithOperation:DIFF_DELETE andText:@"abc"],
[Diff diffWithOperation:DIFF_EQUAL andText:@"xyz"],
[Diff diffWithOperation:DIFF_INSERT andText:@"1234"], nil] /* Diff */;
XCTAssertEqual((NSUInteger)7, [dmp diff_levenshtein:diffs], @"diff_levenshtein: Levenshtein with middle equality.");
[dmp release];
}
- (void)diff_bisectTest;
{
DiffMatchPatch *dmp = [DiffMatchPatch new];
// Normal.
NSString *a = @"cat";
NSString *b = @"map";
// Since the resulting diff hasn't been normalized, it would be ok if
// the insertion and deletion pairs are swapped.
// If the order changes, tweak this test as required.
NSMutableArray *diffs = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_DELETE andText:@"c"], [Diff diffWithOperation:DIFF_INSERT andText:@"m"], [Diff diffWithOperation:DIFF_EQUAL andText:@"a"], [Diff diffWithOperation:DIFF_DELETE andText:@"t"], [Diff diffWithOperation:DIFF_INSERT andText:@"p"], nil];
XCTAssertEqualObjects(diffs, [dmp diff_bisectOfOldString:a andNewString:b deadline:[[NSDate distantFuture] timeIntervalSinceReferenceDate]], @"Bisect test.");
// Timeout.
diffs = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_DELETE andText:@"cat"], [Diff diffWithOperation:DIFF_INSERT andText:@"map"], nil];
XCTAssertEqualObjects(diffs, [dmp diff_bisectOfOldString:a andNewString:b deadline:[[NSDate distantPast] timeIntervalSinceReferenceDate]], @"Bisect timeout.");
[dmp release];
}
- (void)test_diff_mainTest {
DiffMatchPatch *dmp = [DiffMatchPatch new];
// Perform a trivial diff.
NSMutableArray *diffs = [NSMutableArray array];
XCTAssertEqualObjects(diffs, [dmp diff_mainOfOldString:@"" andNewString:@"" checkLines:NO], @"diff_main: Null case.");
diffs = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_EQUAL andText:@"abc"], nil];
XCTAssertEqualObjects(diffs, [dmp diff_mainOfOldString:@"abc" andNewString:@"abc" checkLines:NO], @"diff_main: Equality.");
diffs = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_EQUAL andText:@"ab"], [Diff diffWithOperation:DIFF_INSERT andText:@"123"], [Diff diffWithOperation:DIFF_EQUAL andText:@"c"], nil];
XCTAssertEqualObjects(diffs, [dmp diff_mainOfOldString:@"abc" andNewString:@"ab123c" checkLines:NO], @"diff_main: Simple insertion.");
diffs = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_EQUAL andText:@"a"], [Diff diffWithOperation:DIFF_DELETE andText:@"123"], [Diff diffWithOperation:DIFF_EQUAL andText:@"bc"], nil];
XCTAssertEqualObjects(diffs, [dmp diff_mainOfOldString:@"a123bc" andNewString:@"abc" checkLines:NO], @"diff_main: Simple deletion.");
diffs = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_EQUAL andText:@"a"], [Diff diffWithOperation:DIFF_INSERT andText:@"123"], [Diff diffWithOperation:DIFF_EQUAL andText:@"b"], [Diff diffWithOperation:DIFF_INSERT andText:@"456"], [Diff diffWithOperation:DIFF_EQUAL andText:@"c"], nil];
XCTAssertEqualObjects(diffs, [dmp diff_mainOfOldString:@"abc" andNewString:@"a123b456c" checkLines:NO], @"diff_main: Two insertions.");
diffs = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_EQUAL andText:@"a"], [Diff diffWithOperation:DIFF_DELETE andText:@"123"], [Diff diffWithOperation:DIFF_EQUAL andText:@"b"], [Diff diffWithOperation:DIFF_DELETE andText:@"456"], [Diff diffWithOperation:DIFF_EQUAL andText:@"c"], nil];
XCTAssertEqualObjects(diffs, [dmp diff_mainOfOldString:@"a123b456c" andNewString:@"abc" checkLines:NO], @"diff_main: Two deletions.");
// Perform a real diff.
// Switch off the timeout.
dmp.Diff_Timeout = 0;
diffs = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_DELETE andText:@"a"], [Diff diffWithOperation:DIFF_INSERT andText:@"b"], nil];
XCTAssertEqualObjects(diffs, [dmp diff_mainOfOldString:@"a" andNewString:@"b" checkLines:NO], @"diff_main: Simple case #1.");
diffs = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_DELETE andText:@"Apple"], [Diff diffWithOperation:DIFF_INSERT andText:@"Banana"], [Diff diffWithOperation:DIFF_EQUAL andText:@"s are a"], [Diff diffWithOperation:DIFF_INSERT andText:@"lso"], [Diff diffWithOperation:DIFF_EQUAL andText:@" fruit."], nil];
XCTAssertEqualObjects(diffs, [dmp diff_mainOfOldString:@"Apples are a fruit." andNewString:@"Bananas are also fruit." checkLines:NO], @"diff_main: Simple case #2.");
diffs = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_DELETE andText:@"a"], [Diff diffWithOperation:DIFF_INSERT andText:@"\U00000680"], [Diff diffWithOperation:DIFF_EQUAL andText:@"x"], [Diff diffWithOperation:DIFF_DELETE andText:@"\t"], [Diff diffWithOperation:DIFF_INSERT andText:[NSString stringWithFormat:@"%C", 0]], nil];
NSString *aString = [NSString stringWithFormat:@"\U00000680x%C", 0];
XCTAssertEqualObjects(diffs, [dmp diff_mainOfOldString:@"ax\t" andNewString:aString checkLines:NO], @"diff_main: Simple case #3.");
diffs = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_DELETE andText:@"1"], [Diff diffWithOperation:DIFF_EQUAL andText:@"a"], [Diff diffWithOperation:DIFF_DELETE andText:@"y"], [Diff diffWithOperation:DIFF_EQUAL andText:@"b"], [Diff diffWithOperation:DIFF_DELETE andText:@"2"], [Diff diffWithOperation:DIFF_INSERT andText:@"xab"], nil];
XCTAssertEqualObjects(diffs, [dmp diff_mainOfOldString:@"1ayb2" andNewString:@"abxab" checkLines:NO], @"diff_main: Overlap #1.");
diffs = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_INSERT andText:@"xaxcx"], [Diff diffWithOperation:DIFF_EQUAL andText:@"abc"], [Diff diffWithOperation:DIFF_DELETE andText:@"y"], nil];
XCTAssertEqualObjects(diffs, [dmp diff_mainOfOldString:@"abcy" andNewString:@"xaxcxabc" checkLines:NO], @"diff_main: Overlap #2.");
diffs = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_DELETE andText:@"ABCD"], [Diff diffWithOperation:DIFF_EQUAL andText:@"a"], [Diff diffWithOperation:DIFF_DELETE andText:@"="], [Diff diffWithOperation:DIFF_INSERT andText:@"-"], [Diff diffWithOperation:DIFF_EQUAL andText:@"bcd"], [Diff diffWithOperation:DIFF_DELETE andText:@"="], [Diff diffWithOperation:DIFF_INSERT andText:@"-"], [Diff diffWithOperation:DIFF_EQUAL andText:@"efghijklmnopqrs"], [Diff diffWithOperation:DIFF_DELETE andText:@"EFGHIJKLMNOefg"], nil];
XCTAssertEqualObjects(diffs, [dmp diff_mainOfOldString:@"ABCDa=bcd=efghijklmnopqrsEFGHIJKLMNOefg" andNewString:@"a-bcd-efghijklmnopqrs" checkLines:NO], @"diff_main: Overlap #3.");
diffs = [NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_INSERT andText:@" "], [Diff diffWithOperation:DIFF_EQUAL andText:@"a"], [Diff diffWithOperation:DIFF_INSERT andText:@"nd"], [Diff diffWithOperation:DIFF_EQUAL andText:@" [[Pennsylvania]]"], [Diff diffWithOperation:DIFF_DELETE andText:@" and [[New"], nil];
XCTAssertEqualObjects(diffs, [dmp diff_mainOfOldString:@"a [[Pennsylvania]] and [[New" andNewString:@" and [[Pennsylvania]]" checkLines:NO], @"diff_main: Large equality.");
dmp.Diff_Timeout = 0.1f; // 100ms
NSString *a = @"`Twas brillig, and the slithy toves\nDid gyre and gimble in the wabe:\nAll mimsy were the borogoves,\nAnd the mome raths outgrabe.\n";
NSString *b = @"I am the very model of a modern major general,\nI've information vegetable, animal, and mineral,\nI know the kings of England, and I quote the fights historical,\nFrom Marathon to Waterloo, in order categorical.\n";
NSMutableString *aMutable = [NSMutableString stringWithString:a];
NSMutableString *bMutable = [NSMutableString stringWithString:b];
// Increase the text lengths by 1024 times to ensure a timeout.
for (int i = 0; i < 10; i++) {
[aMutable appendString:aMutable];
[bMutable appendString:bMutable];
}
a = aMutable;
b = bMutable;
NSTimeInterval startTime = [NSDate timeIntervalSinceReferenceDate];
[dmp diff_mainOfOldString:a andNewString:b];
NSTimeInterval endTime = [NSDate timeIntervalSinceReferenceDate];
// Test that we took at least the timeout period.
XCTAssertTrue((dmp.Diff_Timeout <= (endTime - startTime)), @"Test that we took at least the timeout period.");
// Test that we didn't take forever (be forgiving).
// Theoretically this test could fail very occasionally if the
// OS task swaps or locks up for a second at the wrong moment.
// This will fail when running this as PPC code thru Rosetta on Intel.
XCTAssertTrue(((dmp.Diff_Timeout * 2) > (endTime - startTime)), @"Test that we didn't take forever (be forgiving).");
dmp.Diff_Timeout = 0;
// Test the linemode speedup.
// Must be long to pass the 200 character cutoff.
a = @"1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n";
b = @"abcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\n";
XCTAssertEqualObjects([dmp diff_mainOfOldString:a andNewString:b checkLines:YES], [dmp diff_mainOfOldString:a andNewString:b checkLines:NO], @"diff_main: Simple line-mode.");
a = @"1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
b = @"abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij";
XCTAssertEqualObjects([dmp diff_mainOfOldString:a andNewString:b checkLines:YES], [dmp diff_mainOfOldString:a andNewString:b checkLines:NO], @"diff_main: Single line-mode.");
a = @"1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n";
b = @"abcdefghij\n1234567890\n1234567890\n1234567890\nabcdefghij\n1234567890\n1234567890\n1234567890\nabcdefghij\n1234567890\n1234567890\n1234567890\nabcdefghij\n";
NSArray *texts_linemode = [self diff_rebuildtexts:[dmp diff_mainOfOldString:a andNewString:b checkLines:YES]];
NSArray *texts_textmode = [self diff_rebuildtexts:[dmp diff_mainOfOldString:a andNewString:b checkLines:NO]];
XCTAssertEqualObjects(texts_textmode, texts_linemode, @"diff_main: Overlap line-mode.");
// CHANGEME: Test null inputs
[dmp release];
}
#pragma mark Match Test Functions
// MATCH TEST FUNCTIONS
- (void)test_match_alphabetTest {
DiffMatchPatch *dmp = [DiffMatchPatch new];
// Initialise the bitmasks for Bitap.
NSMutableDictionary *bitmask = [NSMutableDictionary dictionary];
[bitmask diff_setUnsignedIntegerValue:4 forUnicharKey:'a'];
[bitmask diff_setUnsignedIntegerValue:2 forUnicharKey:'b'];
[bitmask diff_setUnsignedIntegerValue:1 forUnicharKey:'c'];
XCTAssertEqualObjects(bitmask, [dmp match_alphabet:@"abc"], @"match_alphabet: Unique.");
[bitmask removeAllObjects];
[bitmask diff_setUnsignedIntegerValue:37 forUnicharKey:'a'];
[bitmask diff_setUnsignedIntegerValue:18 forUnicharKey:'b'];
[bitmask diff_setUnsignedIntegerValue:8 forUnicharKey:'c'];
XCTAssertEqualObjects(bitmask, [dmp match_alphabet:@"abcaba"], @"match_alphabet: Duplicates.");
[dmp release];
}
- (void)test_match_bitapTest {
DiffMatchPatch *dmp = [DiffMatchPatch new];
// Bitap algorithm.
dmp.Match_Distance = 100;
dmp.Match_Threshold = 0.5f;
XCTAssertEqual((NSUInteger)5, [dmp match_bitapOfText:@"abcdefghijk" andPattern:@"fgh" near:5], @"match_bitap: Exact match #1.");
XCTAssertEqual((NSUInteger)5, [dmp match_bitapOfText:@"abcdefghijk" andPattern:@"fgh" near:0], @"match_bitap: Exact match #2.");
XCTAssertEqual((NSUInteger)4, [dmp match_bitapOfText:@"abcdefghijk" andPattern:@"efxhi" near:0], @"match_bitap: Fuzzy match #1.");
XCTAssertEqual((NSUInteger)2, [dmp match_bitapOfText:@"abcdefghijk" andPattern:@"cdefxyhijk" near:5], @"match_bitap: Fuzzy match #2.");
XCTAssertEqual((NSUInteger)NSNotFound, [dmp match_bitapOfText:@"abcdefghijk" andPattern:@"bxy" near:1], @"match_bitap: Fuzzy match #3.");
XCTAssertEqual((NSUInteger)2, [dmp match_bitapOfText:@"123456789xx0" andPattern:@"3456789x0" near:2], @"match_bitap: Overflow.");
XCTAssertEqual((NSUInteger)0, [dmp match_bitapOfText:@"abcdef" andPattern:@"xxabc" near:4], @"match_bitap: Before start match.");
XCTAssertEqual((NSUInteger)3, [dmp match_bitapOfText:@"abcdef" andPattern:@"defyy" near:4], @"match_bitap: Beyond end match.");
XCTAssertEqual((NSUInteger)0, [dmp match_bitapOfText:@"abcdef" andPattern:@"xabcdefy" near:0], @"match_bitap: Oversized pattern.");
dmp.Match_Threshold = 0.4f;
XCTAssertEqual((NSUInteger)4, [dmp match_bitapOfText:@"abcdefghijk" andPattern:@"efxyhi" near:1], @"match_bitap: Threshold #1.");
dmp.Match_Threshold = 0.3f;
XCTAssertEqual((NSUInteger)NSNotFound, [dmp match_bitapOfText:@"abcdefghijk" andPattern:@"efxyhi" near:1], @"match_bitap: Threshold #2.");
dmp.Match_Threshold = 0.0f;
XCTAssertEqual((NSUInteger)1, [dmp match_bitapOfText:@"abcdefghijk" andPattern:@"bcdef" near:1], @"match_bitap: Threshold #3.");
dmp.Match_Threshold = 0.5f;