C / C++ Code Portfolio


return(thisSeparator); } } The Standard Template Library (STL)



Download 112,15 Kb.
bet24/30
Sana01.03.2022
Hajmi112,15 Kb.
#475852
1   ...   20   21   22   23   24   25   26   27   ...   30
Bog'liq
dddd

return(thisSeparator);
}
}
The Standard Template Library (STL)
// This program demonstrates the usage of unit tests on deque, string, vector, and algorithm functionality

#include


using std::cout;

#include


using std::clog;

#include


using std::string;

#include


using std::deque;
using std::back_inserter;

#include


using std::generate_n;
using std::transform;
using std::partition;
using std::sort;

#include


using std::accumulate;

#include


using std::vector;

#include


using std::ostream_iterator;

#include


using std::ostringstream;

// This function is used in dequeTest() to provide incremental numbers


int nextInt()
{
static int number = 1;
return number++;
}

// The unit test for demonstrating a deque


void dequeTest()
{
const int dequeLength = 10;
// 1+2+...+dequeLength
const int dequeAcc = 55;
// construct a deque
deque ints(dequeLength);
try
{
// populate the deque with values 1 through dequeLength
generate_n(back_inserter(ints), dequeLength, nextInt);
clog << "dequeTest generate_n() PASSED\n";
}
catch (...)
{
clog << "dequeTest FAILED; generate_n() threw an exception\n";
}
// use the accumulate() algorithm to compute the sum of the values in the deque
if (accumulate(ints.begin(), ints.end(), 0) == dequeAcc)
{
clog << "dequeTest PASSED\n";
}
else
{
clog << "dequeTest FAILED: accumulate() did not return the correct value\n";
}
}

// this function returns the upper case of a char, used in stringTest()


char letterUpper(const char letter)
{
return toupper(letter);
}

// this function provides the unit test demonstrating a string


void stringTest()
{
// initialize a string to all lower case letters
string letters = "abcdefghijklmnopqrstuvwxyz";
try
{
// use the transform() algorithm to convert all lower case letters in the string to upper case
transform(letters.begin(), letters.end(), letters.begin(), letterUpper);
clog << "stringTest transform() PASSED\n";
}
catch (...)
{
clog << "stringTest FAILED: transform() threw an exception\n";
}
// check to make sure the transform() algorithm correctly performed the uppercase conversion
if (letters == "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
{
clog << "stringTest PASSED\n";
}
else
{
clog << "stringTest FAILED: string was not converted to uppercase\n";
}
}

// this function returns if the given argument is even, used in vectorTest()


bool isEven(int i)
{
return (i % 2) == 0;
}

// this function is a unit test demonstrating a vector


void vectorTest()
{
// define an array literal containing many numbers
int arrayLiteral[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
// compute the number of elements in the array
int arrayCount = sizeof(arrayLiteral) / sizeof(*arrayLiteral);
// initialize a vector of ints containing data from the literal array
vector ints(arrayLiteral, arrayLiteral + arrayCount);
// initialize an interator for the vector
vector::iterator evens;
try
{
// partition the array into even and odd numbers and store the location of the parition
evens = partition(ints.begin(), ints.end(), isEven);
// sort the first (even) partition
sort(ints.begin(), evens);
// sort the second (odd) partition
sort(evens, ints.end());
clog << "vectorTest partition() and sort() PASSED\n";
}

Download 112,15 Kb.

Do'stlaringiz bilan baham:
1   ...   20   21   22   23   24   25   26   27   ...   30




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish