-
Notifications
You must be signed in to change notification settings - Fork 0
/
delta-test.cpp
152 lines (133 loc) · 4.52 KB
/
delta-test.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
#include "DeltaCalculator.h"
#include "FileIO.h"
#include <assert.h>
#include <iostream>
#include <string.h>
#include <string>
#include <unistd.h>
void freeData(char *c1, char *c2)
{
free(c1);
c1 = 0;
free(c2);
c2 = 0;
}
void printVector(std::vector<uint> vec, std::string vecName)
{
std::cout << vecName << std::endl;
for (auto elem: vec)
{
std::cout << elem << std::endl;
}
std::cout << "##################" << std::endl;
}
int main(void)
{
// file operation tests
char tmp[256];
getcwd(tmp, 256);
char digitsFile[512];
sprintf(digitsFile, "%s/input/digits.txt", tmp);
char nameFile[512];
sprintf(nameFile, "%s/input/name.txt", tmp);
std::cout << digitsFile << nameFile << std::endl;
char *s1 = FileIO::readFile(digitsFile);
char *s2 = FileIO::readFile(nameFile);
std::string str1{s1};
std::string str2{s2};
assert(str1 == "1234567890");
std::cout << str2 << std::endl;
assert(str2 == "authorname");
std::cout << "File has been read succesfully" << std::endl;
// Delta Calculator constructor test
DeltaCalculator d1(s1,s2);
auto vec = d1.getChunks();
std::string str3{(char *)vec[0]};
std::string str4{(char*)vec[1]};
assert(str3 == "1234567890");
assert(str4 == "authorname");
std::cout << "DeltaCalculator with given string is constructed: success "<< std::endl;
char *data1 = 0, *data2 = 0;
// same input
data1 = strdup("helloworld");
data2 = strdup("helloworld");
DeltaCalculator d4(data1, data2);
std::vector<std::vector<uint>> delta = d4.calculateDelta();
assert(delta[0].empty());
assert(delta[1].empty());
assert(delta[2].empty());
freeData(data1, data2);
std::cout << "All equal case is success" << std::endl;
data1 = strdup("helloworlda");
data2 = strdup("helloworld");
DeltaCalculator d5(data1, data2);
std::vector<std::vector<uint>> delta2 = d5.calculateDelta();
assert(delta2[0].empty());
assert(delta2[1].empty());
assert(delta2[2].size() == 1);
freeData(data1,data2);
std::cout << "Remove 1 char case is success" << std::endl;
// removal of 7
data1 = strdup("helloworlda123457");
data2 = strdup("helloworld");
DeltaCalculator d6(data1, data2);
std::vector<std::vector<uint>> delta3 = d6.calculateDelta();
assert(delta3[0].empty());
assert(delta3[1].empty());
assert(delta3[2].size() == 7);
freeData(data1, data2);
std::cout << "Remove 7 char case is success" << std::endl;
// 1 change and 3 removal
data1 = strdup("hell@world123");
data2 = strdup("helloworld");
DeltaCalculator d7(data1, data2);
std::vector<std::vector<uint>> delta4 = d7.calculateDelta();
// printVector(delta4[1], "changes");
// printVector(delta4[2], "removals");
assert(delta4[0].empty());
std::vector<uint> expected = {1,2,3,4};
assert(delta4[1] == expected);
assert(delta4[2].size() == 3);
freeData(data1, data2);
std::cout << "1 change and 3 removal case is success" << std::endl;
// addition of one
data1 = strdup("helloworld");
data2 = strdup("helloworld1");
DeltaCalculator d8(data1, data2);
std::vector<std::vector<uint>> delta5 = d8.calculateDelta();
assert(delta5[0].size() == 1);
assert(delta5[1].empty());
assert(delta5[2].empty());
freeData(data1,data2);
std::cout << "Addition of one charactes case is success" << std::endl;
// addition of 3
data1 = strdup("helloworld");
data2 = strdup("helloworld123");
DeltaCalculator d9(data1, data2);
std::vector<std::vector<uint>> delta6 = d9.calculateDelta();
assert(delta6[0].size() == 3);
assert(delta6[1].empty());
assert(delta6[2].empty());
freeData(data1,data2);
std::cout << "Addition of 3 charactes case is success" << std::endl;
// addition of 3 change 1
data1 = strdup("helloworld");
data2 = strdup("hellow@rld123");
DeltaCalculator d10(data1, data2);
std::vector<std::vector<uint>> delta7 = d10.calculateDelta();
assert(delta7[0].size() == 3);
expected.clear();
expected = {3,4,5,6};
assert(delta7[1] == expected );
assert(delta7[2].empty());
freeData(data1,data2);
std::cout << "Addition of 3, change of one 1 case is success" << std::endl;
// Chunk count check : expecting a stderr msg
data1 = strdup("sh");
data2 = strdup("longdata");
DeltaCalculator d2(data2, data1); // toggle these 2 to see both chunk size checks
DeltaCalculator d3(data1, data2);
free(data1);
free(data2);
return 1;
}