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";
}
Do'stlaringiz bilan baham: |