Answer the question
In order to leave comments, you need to log in
How is the function (main) implemented, which will perform the check?
class String
{
private:
int len;
char *str;
public:
// конструктор по умолчанию
String ()
{
str = new char[1];
*str = 0; // код пуст симв
len = 0;
std::cout << "k po ymolch \n";
}
// конструктор с параметром
String (char *a)
{
std::cout<<"param \n";
int j = 0;
while (a[j] != 0)
j++;
str = new char[j + 1];
for (int i = 0; i <= j; i++)
str[i] = a[i];
}
// копирующий конструктор
String(const String & b)
{
len = b.len;
str = new char[len + 1];
for (int i = 0; i <= len; i++)
str[i] = b.str[i];
std::cout<<"kopy \n";
}
~String () // деструктор
{
delete[] str;
std::cout<<"delete \n";
}
// оператор присваивания
String & operator =(const String &b)
{
std::cout<<"prisv \n";
delete[] str;
len = b.len;
str = new char[len + 1];
for (int i = 0; i <= len; i++)
str[i] = b.str[i];
}
String operator +(const String &b) const
{
String c;
c.len = b.len + len;
c.str = new char[c.len + 1];
for (int i = 0; i < len; i++)
str[i] = str[i];
for (int i = len; i <= c.len; i++)
str[i] = b.str[i - len];
std::cout<<"concat + \n";
}
String & operator +=(const String &b)
{
*this = *this + b;
return (*this);
std::cout<<"concat += \n";
}
operator const char*() const
{
std::cout<<"priv \n";
return (str);
}
char & operator [](const int i)
{
std::cout<<"[] \n";
return(str[i]);
}
char operator [](const int i) const
{
std::cout<<"[] const \n";
return(str[i]);
}
};
Answer the question
In order to leave comments, you need to log in
We write a unit test in-house, without a framework. This means that we need to somehow simulate the work of the framework, but on our own and with a minimum of lines.
For convenience, we need one function - and a little preprocessor magic.
#include <iostream>
#include <cstring>
void doAssert(bool condition, int line)
{
if (condition) {
std::cout << "Line " << line << " ok" << std::endl;
} else {
std::cout << "Line " << line << " FAILED" << std::endl;
}
}
#define ASSERT(x) doAssert(x, __LINE__)
#define ASSERT_STREQ(x, y) doAssert((std::strcmp(x, y) == 0), __LINE__)
void testStringCreation()
{
std::string s;
ASSERT(s.length() == 0);
ASSERT(s.empty());
ASSERT_STREQ("", s.c_str());
}
void testPlusEq()
{
std::string s1 = "Alpha";
std::string s2 = "Bravo";
s1 += s2;
ASSERT_STREQ("AlphaBravo", s1.c_str());
}
int main()
{
testStringCreation();
testPlusEq();
return 0;
}
s1 += "Bravo"
, then we make sure that, for example, 16 characters are allocated. s1 = "Alpha"
, then the string is equal to "Alpha", and period. All cases possible in the string = char* operation are, of course, covered by other tests.Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question