diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2020-11-10 09:26:04 +0100 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2020-11-10 09:26:04 +0100 |
| commit | 9dd7a48967f99793b818f7362ac8e95717186774 (patch) | |
| tree | d96d81af7cd6d36de7b5e01cab7688b60446f03a /cpp01/ex07 | |
| parent | 631a8d7b6c214da48f7748e78fc8c308960414fb (diff) | |
| download | piscine_cpp-9dd7a48967f99793b818f7362ac8e95717186774.tar.gz piscine_cpp-9dd7a48967f99793b818f7362ac8e95717186774.tar.bz2 piscine_cpp-9dd7a48967f99793b818f7362ac8e95717186774.zip | |
Added more tests for cpp01/07
Diffstat (limited to 'cpp01/ex07')
| -rw-r--r-- | cpp01/ex07/files/nowrite.orig (renamed from cpp01/ex07/test1) | 0 | ||||
| -rw-r--r-- | cpp01/ex07/files/test1.orig | 6 | ||||
| -rw-r--r-- | cpp01/ex07/files/test2.orig (renamed from cpp01/ex07/test2) | 0 | ||||
| -rw-r--r-- | cpp01/ex07/files/test3.orig | 6 | ||||
| -rw-r--r-- | cpp01/ex07/files/test4.orig | 6 | ||||
| -rw-r--r-- | cpp01/ex07/main.cpp | 16 | ||||
| -rwxr-xr-x | cpp01/ex07/test.sh | 54 |
7 files changed, 80 insertions, 8 deletions
diff --git a/cpp01/ex07/test1 b/cpp01/ex07/files/nowrite.orig index 2851ba4..2851ba4 100644 --- a/cpp01/ex07/test1 +++ b/cpp01/ex07/files/nowrite.orig diff --git a/cpp01/ex07/files/test1.orig b/cpp01/ex07/files/test1.orig new file mode 100644 index 0000000..2851ba4 --- /dev/null +++ b/cpp01/ex07/files/test1.orig @@ -0,0 +1,6 @@ +bonjour je suis un +charles +avec quelque bonjour +dans ce fichier +bonjour +hi im bonj diff --git a/cpp01/ex07/test2 b/cpp01/ex07/files/test2.orig index 227746e..227746e 100644 --- a/cpp01/ex07/test2 +++ b/cpp01/ex07/files/test2.orig diff --git a/cpp01/ex07/files/test3.orig b/cpp01/ex07/files/test3.orig new file mode 100644 index 0000000..8950d92 --- /dev/null +++ b/cpp01/ex07/files/test3.orig @@ -0,0 +1,6 @@ +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +abababababababababababababababababab +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +abababababababababababababababababab +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa diff --git a/cpp01/ex07/files/test4.orig b/cpp01/ex07/files/test4.orig new file mode 100644 index 0000000..0b1cda4 --- /dev/null +++ b/cpp01/ex07/files/test4.orig @@ -0,0 +1,6 @@ +aaaaaaaaaaaaaaa +abababababababa +aaaaaaaaaaaaaaa +aaaaaaaaaaaaaaa +abababababababa +aaaaaaaaaaaaaaa diff --git a/cpp01/ex07/main.cpp b/cpp01/ex07/main.cpp index d98168a..3965d3d 100644 --- a/cpp01/ex07/main.cpp +++ b/cpp01/ex07/main.cpp @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/02 17:08:28 by cacharle #+# #+# */ -/* Updated: 2020/11/09 11:22:46 by cacharle ### ########.fr */ +/* Updated: 2020/11/10 09:25:28 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,22 +25,26 @@ int main(int argc, char **argv) std::string filename(argv[1]); std::string s1(argv[2]); std::string s2(argv[3]); - if (filename.empty() || s1.empty() || s2.empty()) + if (s1.empty() || s2.empty()) { - std::cerr << "Error: filename, s1 and s2 should not be empty" << std::endl; + std::cerr << "Error: s1 and s2 should not be empty" << std::endl; return 1; } std::ifstream file(filename); - std::ofstream outfile(filename + ".replace"); + std::ofstream outfile(filename + ".replace", std::ofstream::trunc); if (!file.is_open()) { - std::cerr << "Could not open " << filename; + std::cerr << "Could not open " << filename + << ": " << std::strerror(errno) << std::endl; + outfile.close(); return 1; } if (!outfile.is_open()) { - std::cerr << "Could not create " << filename << ".replace"; + std::cerr << "Could not create " << filename << ".replace" + << ": " << std::strerror(errno) << std::endl; + file.close(); return 1; } diff --git a/cpp01/ex07/test.sh b/cpp01/ex07/test.sh index 69cdc43..8a10120 100755 --- a/cpp01/ex07/test.sh +++ b/cpp01/ex07/test.sh @@ -1,5 +1,55 @@ #!/bin/sh make all -./replace test1 bonjour aurevoir -./replace test2 occ many + +replace_test () { + echo "----------------------------------" + echo "$ ./replace $1 $2 $3" + ./replace "$1" "$2" "$3" + echo "=== ORIGIN" + cat "$1" + echo "=== REPLACED" + cat "$1.replace" +} + +replace_test_error() { + echo "----------------------------------" + echo "$ ./replace $1 $2 $3" + ./replace "$1" "$2" "$3" +} + +echo '======== BASIC =======' +replace_test files/test1.orig bonjour aurevoir +replace_test files/test2.orig occ many +replace_test files/test3.orig a A +replace_test files/test4.orig a zZz + +echo '======== ARGUMENT ERROR =======' +./replace +./replace files/test1.orig +./replace files/test1.orig bonjour aurevoir ye +./replace files/test1.orig bonjour +./replace files/test1.orig '' aurevoir +./replace files/test1.orig bonjour '' +./replace '' bonjour aurevoir + +echo '======== FILE ERROR =======' +./replace files/doesnotexist a b + +touch files/noperm +chmod 000 files/noperm +./replace files/noperm a b +rm -f files/noperm + +touch files/noread +chmod 333 files/noread +./replace files/noread a b +rm -f files/noread + +touch files/nowrite.orig.replace +chmod 555 files/nowrite.orig.replace +./replace files/nowrite.orig bonjour aurevoir +rm -f files/nowrite.orig.replace + + +find files -name '*.replace' -exec rm -f \{\} \; |
