d01 ex04 a peu pres ok mais une erreure, et unitests ok
This commit is contained in:
@@ -2,30 +2,35 @@
|
||||
|
||||
Sed::Sed(char *file, char *find, char *replacement)
|
||||
: _file( file )
|
||||
// , _new_file( file.append(".replace") )
|
||||
, _new_file( std::string(file).append(".replace") )
|
||||
, _find( find )
|
||||
, _replacement( replacement )
|
||||
{
|
||||
, _replacement( replacement ) {
|
||||
|
||||
// this->_buf =
|
||||
return;
|
||||
|
||||
}
|
||||
Sed::~Sed() {
|
||||
|
||||
// delete[] this->_buf;
|
||||
return;
|
||||
|
||||
}
|
||||
Sed::~Sed() {return;}
|
||||
|
||||
void Sed::replace() {
|
||||
|
||||
std::ifstream file(this->_file.c_str());
|
||||
char c;
|
||||
std::ofstream new_file(this->_new_file.c_str());
|
||||
char str[this->_find.length() - 1];
|
||||
int len = this->_find.length();
|
||||
char tmp;
|
||||
|
||||
while (file.get(c))
|
||||
while (file.get(str, 1 + 1, EOF))
|
||||
{
|
||||
std::cout << c;
|
||||
if (str[0] == this->_find[0])
|
||||
{
|
||||
tmp = str[0];
|
||||
file.get(str, len, EOF);
|
||||
if (this->_find.compare(1, len - 1, str) == 0)
|
||||
new_file << this->_replacement;
|
||||
else
|
||||
new_file << tmp << str;
|
||||
}
|
||||
else
|
||||
new_file << str;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
# define SED_HPP
|
||||
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
|
||||
@@ -17,6 +18,7 @@ public:
|
||||
private:
|
||||
|
||||
// std::string _buf;
|
||||
std::string _tmp;
|
||||
std::string const _file;
|
||||
std::string const _new_file;
|
||||
std::string const _find;
|
||||
|
||||
BIN
d01/ex04/sed
BIN
d01/ex04/sed
Binary file not shown.
@@ -1,27 +0,0 @@
|
||||
No 1 more tears, my heart is dry
|
||||
I don't laugh and I don't cry
|
||||
I don't think about you all the time
|
||||
But when I do - I wonder why
|
||||
|
||||
You have to go out of my door
|
||||
And leave just like you did before
|
||||
I know I said that I was sure
|
||||
But rich men can't imagine poor.
|
||||
|
||||
One day baby, we'll be old
|
||||
Oh baby, we'll be old
|
||||
And think of all the stories that we could have told
|
||||
|
||||
Little me and little you
|
||||
Kept doing all the things they do
|
||||
They never really think it through
|
||||
Like I can never think you're true
|
||||
|
||||
Here I go again - the blame
|
||||
The guilt, the pain, the hurt, the shame
|
||||
The founding fathers of our plane
|
||||
That's stuck in heavy clouds of rain.
|
||||
|
||||
One day baby, we'll be old
|
||||
Oh baby, we'll be old
|
||||
And think of all the stories that we could have told
|
||||
19
d01/ex04/unitests/colors.sh
Normal file
19
d01/ex04/unitests/colors.sh
Normal file
@@ -0,0 +1,19 @@
|
||||
# COLORS
|
||||
RED="\e[0;31m"
|
||||
GREEN="\e[0;32m"
|
||||
YELLOW="\e[0;33m"
|
||||
BLUE="\e[0;34m"
|
||||
MAGENTA="\e[0;35m"
|
||||
CYAN="\e[0;36m"
|
||||
WHITE="\e[0;37m"
|
||||
|
||||
B_RED="\e[1;31m"
|
||||
B_GREEN="\e[1;32m"
|
||||
B_YELLOW="\e[1;33m"
|
||||
B_BLUE="\e[1;34m"
|
||||
B_MAGENTA="\e[1;35m"
|
||||
B_CYAN="\e[1;36m"
|
||||
B_WHITE="\e[1;37m"
|
||||
|
||||
ENDCO="\e[0m"
|
||||
|
||||
83
d01/ex04/unitests/test.sh
Normal file
83
d01/ex04/unitests/test.sh
Normal file
@@ -0,0 +1,83 @@
|
||||
#!/bin/bash
|
||||
source ./colors.sh
|
||||
|
||||
mkdir -p test_log
|
||||
|
||||
# RUN TESTS
|
||||
function run_tests
|
||||
{
|
||||
touch $TESTNAME
|
||||
echo "$CONTENT" > $TESTNAME
|
||||
|
||||
../sed $TESTNAME "$FIND" "$REPLACEMENT"
|
||||
OUTPUT=$( cat $TESTNAME.replace )
|
||||
if [ "$OUTPUT" == "$RESULT" ]
|
||||
then
|
||||
echo -e $B_GREEN"test1 success"$ENDCO
|
||||
else
|
||||
echo -e $B_RED"test1 faillure"$ENDCO
|
||||
fi
|
||||
|
||||
mv $TESTNAME $TESTNAME.replace ./test_log
|
||||
}
|
||||
|
||||
# TEST 1 ########################################
|
||||
TESTNAME="test1"
|
||||
FIND=" "
|
||||
REPLACEMENT="hello"
|
||||
CONTENT=" "
|
||||
RESULT="hello"
|
||||
run_tests
|
||||
|
||||
# TEST 2 ########################################
|
||||
TESTNAME="test2"
|
||||
FIND="ie"
|
||||
REPLACEMENT="++"
|
||||
CONTENT=$(cat << EOF
|
||||
ce fichier
|
||||
contient
|
||||
plusieurs lignes
|
||||
les unes au dessus des autres
|
||||
youhouuu ioieux
|
||||
EOF
|
||||
)
|
||||
RESULT=$(cat << EOF
|
||||
ce fich++r
|
||||
cont++nt
|
||||
plus++urs lignes
|
||||
les unes au dessus des autres
|
||||
youhouuu io++ux
|
||||
EOF
|
||||
)
|
||||
run_tests
|
||||
|
||||
# TEST 3 ########################################
|
||||
TESTNAME="test3"
|
||||
FIND="."
|
||||
REPLACEMENT="+"
|
||||
CONTENT=$(cat << EOF
|
||||
....................................................;
|
||||
EOF
|
||||
)
|
||||
RESULT=$(cat << EOF
|
||||
++++++++++++++++++++++++++++++++++++++++++++++++++++;
|
||||
EOF
|
||||
)
|
||||
run_tests
|
||||
|
||||
# TEST 3 ########################################
|
||||
TESTNAME="test4"
|
||||
FIND=".."
|
||||
REPLACEMENT="++"
|
||||
CONTENT=$(cat << EOF
|
||||
...................................................;
|
||||
. . . . . . . . . . . . . . . . . . . . . . . . . .;
|
||||
EOF
|
||||
)
|
||||
RESULT=$(cat << EOF
|
||||
++++++++++++++++++++++++++++++++++++++++++++++++++.;
|
||||
. . . . . . . . . . . . . . . . . . . . . . . . . .;
|
||||
EOF
|
||||
)
|
||||
run_tests
|
||||
|
||||
1
d01/ex04/unitests/test_log/test1
Normal file
1
d01/ex04/unitests/test_log/test1
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
1
d01/ex04/unitests/test_log/test1.replace
Normal file
1
d01/ex04/unitests/test_log/test1.replace
Normal file
@@ -0,0 +1 @@
|
||||
hello
|
||||
5
d01/ex04/unitests/test_log/test2
Normal file
5
d01/ex04/unitests/test_log/test2
Normal file
@@ -0,0 +1,5 @@
|
||||
ce fichier
|
||||
contient
|
||||
plusieurs lignes
|
||||
les unes au dessus des autres
|
||||
youhouuu ioieux
|
||||
5
d01/ex04/unitests/test_log/test2.replace
Normal file
5
d01/ex04/unitests/test_log/test2.replace
Normal file
@@ -0,0 +1,5 @@
|
||||
ce fich++r
|
||||
cont++nt
|
||||
plus++urs lignes
|
||||
les unes au dessus des autres
|
||||
youhouuu io++ux
|
||||
1
d01/ex04/unitests/test_log/test3
Normal file
1
d01/ex04/unitests/test_log/test3
Normal file
@@ -0,0 +1 @@
|
||||
....................................................;
|
||||
1
d01/ex04/unitests/test_log/test3.replace
Normal file
1
d01/ex04/unitests/test_log/test3.replace
Normal file
@@ -0,0 +1 @@
|
||||
+
|
||||
2
d01/ex04/unitests/test_log/test4
Normal file
2
d01/ex04/unitests/test_log/test4
Normal file
@@ -0,0 +1,2 @@
|
||||
...................................................;
|
||||
. . . . . . . . . . . . . . . . . . . . . . . . . .;
|
||||
2
d01/ex04/unitests/test_log/test4.replace
Normal file
2
d01/ex04/unitests/test_log/test4.replace
Normal file
@@ -0,0 +1,2 @@
|
||||
++++++++++++++++++++++++++++++++++++++++++++++++++.;
|
||||
. . . . . . . . . . . . . . . . . . . . . . . . . .;
|
||||
Reference in New Issue
Block a user