d01 ex04 a peu pres ok mais une erreure, et unitests ok

This commit is contained in:
Hugo LAMY
2022-02-07 18:42:28 +01:00
parent 649b6168ef
commit dc144127a9
14 changed files with 141 additions and 41 deletions

View File

@@ -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;
}
}

View File

@@ -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;

Binary file not shown.

View File

@@ -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

View 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
View 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

View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1 @@
hello

View File

@@ -0,0 +1,5 @@
ce fichier
contient
plusieurs lignes
les unes au dessus des autres
youhouuu ioieux

View File

@@ -0,0 +1,5 @@
ce fich++r
cont++nt
plus++urs lignes
les unes au dessus des autres
youhouuu io++ux

View File

@@ -0,0 +1 @@
....................................................;

View File

@@ -0,0 +1 @@
+

View File

@@ -0,0 +1,2 @@
...................................................;
. . . . . . . . . . . . . . . . . . . . . . . . . .;

View File

@@ -0,0 +1,2 @@
++++++++++++++++++++++++++++++++++++++++++++++++++.;
. . . . . . . . . . . . . . . . . . . . . . . . . .;