1NAME <- "atomic"
2source(file.path('_helper', 'init.R'))
3
4# - Basic Tests
5
6all.equal(as.character(diffPrint(chr.1, chr.2)), rdsf(100))
7all.equal(
8  as.character(diffPrint(chr.1, chr.2, mode="unified")), rdsf(200)
9)
10all.equal(
11  as.character(diffPrint(chr.1, chr.2, mode="context")), rdsf(400)
12)
13all.equal(
14  as.character(diffPrint(chr.1[2:3], chr.2[2], mode="sidebyside")), rdsf(500)
15)
16# Check that `extra` works
17
18all.equal(
19  as.character(
20    diffPrint(chr.1, chr.2, mode="unified", extra=list(quote=FALSE))
21  ),
22  rdsf(600)
23)
24# make sure blanks line up correctly
25
26all.equal(
27  as.character(diffPrint(chr.3, chr.4)), rdsf(700)
28)
29all.equal(
30  as.character(diffPrint(chr.3, chr.4, mode="unified")), rdsf(800)
31)
32
33# - Word wrap in atomic
34
35A <- A.1 <- B <- c(letters, LETTERS)
36B[15] <- "Alabama"
37A.1[5] <- "Ee"
38C <- A[-15]
39D <- C
40E <- B[-45]
41
42# Test simple changes to vectors; at 80 columns removing 1:8 corresponds to
43# row deletion
44
45all.equal(as.character(diffPrint(A[-(1:8)], A)), rdsf(900))
46all.equal(as.character(diffPrint(A, A[-(1:8)])), rdsf(1000))
47
48all.equal(as.character(diffPrint(A[-1], A[-2])), rdsf(1100))
49
50# Replace single word
51
52all.equal(as.character(diffPrint(A, B)), rdsf(1200))
53all.equal(as.character(diffPrint(B, A)), rdsf(1250))
54
55# Make sure turning off word.diff also turns of unwrapping, but that we can
56# turn off unwrapping without turning off word diff
57
58all.equal(
59  as.character(diffPrint(A, B, word.diff=FALSE)), rdsf(1300)
60)
61all.equal(
62  as.character(diffPrint(A, B, unwrap.atomic=FALSE)), rdsf(1400)
63)
64# Different wrap frequency and removed words that span lines
65
66all.equal(
67  as.character(diffPrint(A, A.1[-(13:18)])), rdsf(1425)
68)
69# Removing words
70
71all.equal(as.character(diffPrint(C, B)), rdsf(1450))
72
73# Two hunks
74
75all.equal(as.character(diffPrint(D, E)), rdsf(1500))
76all.equal(as.character(diffPrint(E, D)), rdsf(1600))
77
78# Vignette example
79
80state.abb.2 <- state.abb
81state.abb.2[38] <- "Pennsylvania"
82
83all.equal(
84  as.character(diffPrint(state.abb, state.abb.2)), rdsf(1700)
85)
86# Number corner cases
87
88all.equal(as.character(diffPrint(1:100, 2:101)), rdsf(1800))
89all.equal(as.character(diffPrint(2:101, 1:100)), rdsf(1900))
90all.equal(
91  as.character(diffPrint(2:101, (1:100)[-9])), rdsf(2000)
92)
93all.equal(
94  as.character(diffPrint((2:101)[-98], (1:100)[-9])), rdsf(2100)
95)
96# This is one of those that a better in-hunk align algorithm would benefit
97
98int.1 <- int.2 <- 1:100
99int.2[c(8, 20, 60)] <- 99
100int.2 <- c(50:1, int.2)
101all.equal(as.character(diffPrint(int.1, int.2)), rdsf(2200))
102
103# - with names
104rand.chrs <- do.call(paste0, expand.grid(LETTERS, LETTERS))
105F <- F1 <- F2 <- (2:105)[-98]
106G <- G2 <- G3 <- G4 <- G5 <- (1:100)[-9]
107nm.1 <- rand.chrs[seq_along(F)]
108nm.2 <- rand.chrs[seq_along(G)]
109names(F1) <- names(F2) <- nm.1
110names(G3) <- names(G2) <- names(G3) <-  names(G4) <- names(G5) <- nm.2
111names(G3)[c(5, 25, 60)] <- c("XXXXX", rand.chrs[c(300, 350)])
112names(G4)[c(5, 25, 60)] <- names(G5)[c(5, 25, 60)] <-
113  c("XX", rand.chrs[c(300, 350)])
114attr(F2, "blah") <- 1:5
115attr(G5, "blah") <- 3:8
116
117all.equal(as.character(diffPrint(F, G)), rdsf(2300))
118all.equal(as.character(diffPrint(F1, G2)), rdsf(2400))
119
120# Challenging case b/c the names wrap with values, and you have to pick one or
121# the other to match when the wrap frequencies are different
122
123all.equal(as.character(diffPrint(F1, G3)), rdsf(2500))
124all.equal(as.character(diffPrint(F1, G4)), rdsf(2520))
125
126# Attributes
127
128all.equal(as.character(diffPrint(F2, G5)), rdsf(2530))
129all.equal(as.character(diffPrint(F1, G5)), rdsf(2540))
130
131# - Original tests
132set.seed(2)
133w1 <- sample(
134  c(
135  "carrot", "cat", "cake", "eat", "rabbit", "holes", "the", "a", "pasta",
136  "boom", "noon", "sky", "hat", "blah", "paris", "dog", "snake"
137  ), 25, replace=TRUE
138)
139w4 <- w3 <- w2 <- w1
140w2[sample(seq_along(w1), 5)] <- LETTERS[1:5]
141w3 <- w1[8:15]
142w4 <- c(w1[1:5], toupper(w1[1:5]), w1[6:15], toupper(w1[1:5]))
143
144all.equal(as.character(diffPrint(w1, w2)), rdsf(2600))
145all.equal(as.character(diffPrint(w1, w3)), rdsf(2700))
146all.equal(as.character(diffPrint(w1, w4)), rdsf(2800))
147
148# - Simple word diffs
149a <- c("a", "b", "c", "d")
150b <- c("b", "c", "d", "e")
151all.equal(as.character(diffPrint(a, b)), rdsf(2900))
152
153a <- c("x", "a", "b", "c", "d", "z")
154b <- c("x", "b", "c", "d", "e", "z")
155all.equal(as.character(diffPrint(a, b)), rdsf(3000))
156
157a <- c("x", "a", "b", "c", "d", "z")
158b <- c("z", "b", "c", "d", "e", "x")
159all.equal(as.character(diffPrint(a, b)), rdsf(3100))
160
161# - Alignment edge cases
162all.equal(
163  as.character(diffPrint(20:50, 30:62)), rdsf(3200)
164)
165# below is off; should be aligning matching context line, part of the problem
166# might be that we're doing the realignment without thinking about what the
167# other hunk has.
168#
169# Possible encode each line as hunk#:diff/mix/cont
170
171# all.equal(
172#   as.character(diffPrint(20:50, 35:62)), rdsf(3300)
173# )
174
175# another interesting example where the existing algo seems to lead to a
176# reasonable outcome
177
178all.equal(
179  as.character(diffPrint(c(1:24,35:45), c(1:8, 17:45))), rdsf(3400)
180)
181