From 8ef6635b9d95f847bea1c71928ebaaa8eff57dcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjist=C3=B3f?= Date: Mon, 24 Oct 2016 22:11:57 +0200 Subject: [PATCH] rewritten manualtest.cpp to be nicer and more readable --- manualtest.cpp | 129 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 110 insertions(+), 19 deletions(-) diff --git a/manualtest.cpp b/manualtest.cpp index a05dc2a..04db37d 100644 --- a/manualtest.cpp +++ b/manualtest.cpp @@ -1,30 +1,121 @@ #include #include #include +#include #include"String.h" +/* This is a demonstration, that the String class and it's components work + * correctly, basically a non-unit test. */ int main() { - String str1; + if (std::is_default_constructible::value) + std::cout << "class String is default constructible" << std::endl; + if (std::is_constructible::value) + std::cout << "class String is constructible from const char*" << std::endl; + if (std::is_copy_constructible::value) + std::cout << "class String is copy constructible" << std::endl; + if (std::is_copy_assignable::value) + std::cout << "class String is copy assignable" << std::endl; + if (std::is_move_constructible::value) + std::cout << "class String is move constructible" << std::endl; + if (std::is_move_assignable::value) + std::cout << "class String is move assignable" << std::endl; + + + std::cout << std::endl; + + + String str("cica"); + if (!std::strcmp(str.c_str(), "cica")) + std::cout << "String::c_str() method works" << std::endl; + + String uninitialized; + try { uninitialized.c_str(); } + catch (std::runtime_error) + { std::cout << "String::c_str() throws if String is not initialized" << std::endl; } + + + std::cout << std::endl; + + + String sajt("sajt"); + if (!std::strcmp((str+sajt).c_str(), "cicasajt")) + std::cout << "String::operator+ works with other Strings" << std::endl; + + String cica(str); + cica += sajt; + if (!std::strcmp(cica.c_str(), "cicasajt")) + std::cout << "String::operator+= works with other Strings" << std::endl; + + String cica1("cicá"); + if (!std::strcmp((cica1+'k').c_str(), "cicák")) + std::cout << "String::operator+ works with char primitives" << std::endl; + + cica1 += 'k'; + if (!std::strcmp(cica1.c_str(), "cicák")) + std::cout << "String::operator+= works with char primitives" << std::endl; + + + std::cout << std::endl; + + + String sajt1(sajt); + if (sajt == sajt1) + std::cout << "String::operator== works" << std::endl; + + try { uninitialized == sajt; } + catch (std::runtime_error) + { std::cout << "String::operator== throws if String is not initialized" << std::endl; } + + + std::cout << std::endl; + + + if (str[1] == 'i') + std::cout << "String::operator[] works" << std::endl; + + try { str[8]; } + catch (std::out_of_range) + { std::cout << "String::operator[] throws when index is out of range" << std::endl; } + + try { uninitialized[0]; } + catch (std::runtime_error) + { std::cout << "String::operator[] throws when String is not initialized" << std::endl; } + + + std::cout << std::endl; + + + String tej("tej"); + String tej1 = tej; + tej1[0] = 's'; + if (!std::strcmp(tej.c_str(), "tej") && !std::strcmp(tej1.c_str(), "sej")) + std::cout << "Copy-on-write works when indexing Strings" << std::endl; + + String lel("lel"); + String lel1("lel"); + + if (lel.c_str() == lel1.c_str()) + std::cout << "Resource sharing works on Strings" << std::endl; + + + std::cout << std::endl; + + std::stringstream stream; - stream << "sajt"; - stream >> str1; - std::cout << str1 << std::endl; - String str2(" cicasajt"); - String str3(str2); - str2 = str1; - String str4(String("sajtos cica")); - str2 = String(" tihihi"); - std::cout << str4 + str3 << std::endl; - std::cout << ((str4 + str3)+=str2) << std::endl; - str4 += 'k'; - std::cout << str4 << std::endl; - std::cout << str2+'k' << std::endl; - std::cout << str4[0] << static_cast(str2)[1] << std::endl; - String str5 = str2; - str5[0] = 'i'; - std::cout << str5 << std::endl; - std::cout << str2 << std::endl; + stream << lel; + if (!std::strcmp(stream.str().c_str(), lel.c_str())) + std::cout << "ostream operator<< works on Strings" << std::endl; + + stream = std::stringstream(); + + stream << "C++11"; + stream >> uninitialized; + if (!std::strcmp(uninitialized.c_str(), "C++11")) + std::cout << "istream operator>> works on Strings" << std::endl; + + + return EXIT_SUCCESS; } \ No newline at end of file