From 9dd7a48967f99793b818f7362ac8e95717186774 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Tue, 10 Nov 2020 09:26:04 +0100 Subject: Added more tests for cpp01/07 --- cpp01/ex07/files/nowrite.orig | 6 +++++ cpp01/ex07/files/test1.orig | 6 +++++ cpp01/ex07/files/test2.orig | 5 ++++ cpp01/ex07/files/test3.orig | 6 +++++ cpp01/ex07/files/test4.orig | 6 +++++ cpp01/ex07/main.cpp | 16 ++++++++----- cpp01/ex07/test.sh | 54 +++++++++++++++++++++++++++++++++++++++++-- cpp01/ex07/test1 | 6 ----- cpp01/ex07/test2 | 5 ---- 9 files changed, 91 insertions(+), 19 deletions(-) create mode 100644 cpp01/ex07/files/nowrite.orig create mode 100644 cpp01/ex07/files/test1.orig create mode 100644 cpp01/ex07/files/test2.orig create mode 100644 cpp01/ex07/files/test3.orig create mode 100644 cpp01/ex07/files/test4.orig delete mode 100644 cpp01/ex07/test1 delete mode 100644 cpp01/ex07/test2 (limited to 'cpp01/ex07') diff --git a/cpp01/ex07/files/nowrite.orig b/cpp01/ex07/files/nowrite.orig new file mode 100644 index 0000000..2851ba4 --- /dev/null +++ b/cpp01/ex07/files/nowrite.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/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/files/test2.orig b/cpp01/ex07/files/test2.orig new file mode 100644 index 0000000..227746e --- /dev/null +++ b/cpp01/ex07/files/test2.orig @@ -0,0 +1,5 @@ +occ occ occ hocket + +lalala occ asdfasdf occ + +occ occ occ occ 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 +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 \{\} \; diff --git a/cpp01/ex07/test1 b/cpp01/ex07/test1 deleted file mode 100644 index 2851ba4..0000000 --- a/cpp01/ex07/test1 +++ /dev/null @@ -1,6 +0,0 @@ -bonjour je suis un -charles -avec quelque bonjour -dans ce fichier -bonjour -hi im bonj diff --git a/cpp01/ex07/test2 b/cpp01/ex07/test2 deleted file mode 100644 index 227746e..0000000 --- a/cpp01/ex07/test2 +++ /dev/null @@ -1,5 +0,0 @@ -occ occ occ hocket - -lalala occ asdfasdf occ - -occ occ occ occ -- cgit