refcountedstring/manualtest.cpp

137 lines
4.1 KiB
C++

#include<iostream>
#include<sstream>
#include<cstring>
#include <type_traits>
#include"String.h"
/* This is a demonstration, that the String class and it's components work
* correctly, basically a non-unit test. Perfect for usage with valgrind.
* Note that these tests are only used in case GTest is not present on the system. */
int main()
{
if (std::is_default_constructible<String>::value)
std::cout << "class String is default constructible" << std::endl;
String defaultctor;
if (std::is_constructible<String, const char*>::value)
std::cout << "class String is constructible from const char*" << std::endl;
String constchar("yeeeey");
if (std::is_copy_constructible<String>::value)
std::cout << "class String is copy constructible" << std::endl;
String copy(constchar);
if (std::is_copy_assignable<String>::value)
std::cout << "class String is copy assignable" << std::endl;
String othercopy;
othercopy = constchar;
if (std::is_move_constructible<String>::value)
std::cout << "class String is move constructible" << std::endl;
String moved(std::move(othercopy));
if (std::is_move_assignable<String>::value)
std::cout << "class String is move assignable" << std::endl;
String moveassigned;
moveassigned = std::move(moved);
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;
String sajtok = "sajtok";
sajtok[5] = sajtok[0];
if (!std::strcmp(sajtok.c_str(), "sajtos"))
std::cout << "String::operator[]'s operator= works with other String::operator[]s" << 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 << 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;
}